OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.test.InstrumentationTestCase; | 7 import android.test.InstrumentationTestCase; |
8 import android.test.UiThreadTest; | 8 import android.test.UiThreadTest; |
9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
10 | 10 |
11 import org.chromium.base.metrics.RecordHistogram; | 11 import org.chromium.base.metrics.RecordHistogram; |
12 import org.chromium.base.test.util.AdvancedMockContext; | 12 import org.chromium.base.test.util.AdvancedMockContext; |
13 import org.chromium.base.test.util.Feature; | 13 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.webapk.lib.common.WebApkConstants; |
14 | 15 |
15 import java.util.HashMap; | 16 import java.util.HashMap; |
16 import java.util.HashSet; | 17 import java.util.HashSet; |
17 import java.util.List; | 18 import java.util.List; |
18 import java.util.Map; | 19 import java.util.Map; |
19 | 20 |
20 /** | 21 /** |
21 * Tests for the ActivityAssigner class. | 22 * Tests for the ActivityAssigner class. |
22 */ | 23 */ |
23 public class ActivityAssignerTest extends InstrumentationTestCase { | 24 public class ActivityAssignerTest extends InstrumentationTestCase { |
24 private static final String BASE_WEBAPP_ID = "BASE_WEBAPP_ID_"; | 25 private static final String BASE_WEBAPP_ID = "BASE_WEBAPP_ID_"; |
25 | 26 |
26 private AdvancedMockContext mContext; | 27 private AdvancedMockContext mContext; |
27 private HashMap<String, Object> mPreferences; | 28 private HashMap<String, Object>[] mPreferences; |
28 | 29 |
29 @Override | 30 @Override |
30 protected void setUp() throws Exception { | 31 protected void setUp() throws Exception { |
31 super.setUp(); | 32 super.setUp(); |
32 RecordHistogram.disableForTests(); | 33 RecordHistogram.disableForTests(); |
33 mContext = new AdvancedMockContext(); | 34 mContext = new AdvancedMockContext(); |
34 mPreferences = new HashMap<String, Object>(); | 35 mPreferences = new HashMap[ActivityAssigner.ACTIVITY_TYPES]; |
35 mContext.addSharedPreferences(ActivityAssigner.PREF_PACKAGE, mPreference
s); | 36 for (int i = 0; i < ActivityAssigner.ACTIVITY_TYPES; ++i) { |
| 37 mPreferences[i] = new HashMap<String, Object>(); |
| 38 mContext.addSharedPreferences(ActivityAssigner.PREF_PACKAGE[i], mPre
ferences[i]); |
| 39 } |
36 } | 40 } |
37 | 41 |
38 @UiThreadTest | 42 @UiThreadTest |
39 @SmallTest | 43 @SmallTest |
40 @Feature({"Webapps"}) | 44 @Feature({"Webapps"}) |
41 public void testEntriesCreated() { | 45 public void testEntriesCreated() { |
42 ActivityAssigner assigner = ActivityAssigner.instance(mContext); | 46 ActivityAssigner assigner = ActivityAssigner.instance(mContext); |
43 | 47 |
44 // Make sure that no webapps have been assigned to any Activities for a
fresh install. | 48 // Make sure that no webapps have been assigned to any Activities for a
fresh install. |
45 checkState(assigner); | 49 List<ActivityAssigner.ActivityEntry>[] entries = assigner.getEntries(); |
46 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); | 50 for (int i = 0; i < entries.length; ++i) { |
47 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); | 51 checkState(assigner, i); |
48 for (ActivityAssigner.ActivityEntry entry : entries) { | 52 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries[i].size
()); |
49 assertEquals(null, entry.mWebappId); | 53 for (ActivityAssigner.ActivityEntry entry : entries[i]) { |
| 54 assertEquals(null, entry.mWebappId); |
| 55 } |
50 } | 56 } |
51 } | 57 } |
52 | 58 |
53 /** | 59 /** |
54 * Make sure invalid entries get culled & that we still have the correct num
ber of unique | 60 * Make sure invalid entries get culled & that we still have the correct num
ber of unique |
55 * Activity indices available. | 61 * Activity indices available. |
56 */ | 62 */ |
57 @UiThreadTest | 63 @UiThreadTest |
58 @SmallTest | 64 @SmallTest |
59 @Feature({"Webapps"}) | 65 @Feature({"Webapps"}) |
60 public void testEntriesDownsized() { | 66 public void testEntriesDownsized() { |
61 // Store preferences indicating that more Activities existed previously
than there are now. | 67 // Store preferences indicating that more Activities existed previously
than there are now. |
62 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1; | 68 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1; |
63 createPreferences(numSavedEntries); | 69 int index = ActivityAssigner.WEBAPP_ACTIVITY_INDEX; |
| 70 createPreferences(numSavedEntries, index); |
64 | 71 |
65 ActivityAssigner assigner = ActivityAssigner.instance(mContext); | 72 ActivityAssigner assigner = ActivityAssigner.instance(mContext); |
66 checkState(assigner); | 73 checkState(assigner, index); |
67 } | 74 } |
68 | 75 |
69 /** | 76 /** |
70 * Make sure we recover from corrupted stored preferences. | 77 * Make sure we recover from corrupted stored preferences. |
71 */ | 78 */ |
72 @UiThreadTest | 79 @UiThreadTest |
73 @SmallTest | 80 @SmallTest |
74 @Feature({"Webapps"}) | 81 @Feature({"Webapps"}) |
75 public void testCorruptedPreferences() { | 82 public void testCorruptedPreferences() { |
76 String wrongVariableType = "omgwtfbbq"; | 83 String wrongVariableType = "omgwtfbbq"; |
77 mPreferences.clear(); | 84 int index = ActivityAssigner.WEBAPP_ACTIVITY_INDEX; |
78 mPreferences.put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES, wrongVariableT
ype); | 85 mPreferences[index].clear(); |
| 86 mPreferences[index].put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES[index],
wrongVariableType); |
79 | 87 |
80 ActivityAssigner assigner = ActivityAssigner.instance(mContext); | 88 ActivityAssigner assigner = ActivityAssigner.instance(mContext); |
81 checkState(assigner); | 89 checkState(assigner, index); |
82 } | 90 } |
83 | 91 |
84 @UiThreadTest | 92 @UiThreadTest |
85 @SmallTest | 93 @SmallTest |
86 @Feature({"Webapps"}) | 94 @Feature({"Webapps"}) |
87 public void testAssignment() { | 95 public void testAssignment() { |
88 ActivityAssigner assigner = ActivityAssigner.instance(mContext); | 96 ActivityAssigner assigner = ActivityAssigner.instance(mContext); |
89 checkState(assigner); | 97 int index = assigner.getIndex(BASE_WEBAPP_ID); |
| 98 checkState(assigner, index); |
90 | 99 |
91 // Assign all of the Activities to webapps. | 100 // Assign all of the Activities to webapps. |
92 // Go backwards to make sure ordering doesn't matter. | 101 // Go backwards to make sure ordering doesn't matter. |
93 Map<String, Integer> testMap = new HashMap<String, Integer>(); | 102 Map<String, Integer> testMap = new HashMap<String, Integer>(); |
94 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) { | 103 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) { |
95 String currentWebappId = BASE_WEBAPP_ID + i; | 104 String currentWebappId = BASE_WEBAPP_ID + i; |
96 int activityIndex = assigner.assign(currentWebappId); | 105 int activityIndex = assigner.assign(currentWebappId); |
97 testMap.put(currentWebappId, activityIndex); | 106 testMap.put(currentWebappId, activityIndex); |
98 } | 107 } |
99 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size()); | 108 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size()); |
100 | 109 |
101 // Make sure that passing in the same webapp ID gives back the same Acti
vity. | 110 // Make sure that passing in the same webapp ID gives back the same Acti
vity. |
102 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { | 111 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { |
103 String currentWebappId = BASE_WEBAPP_ID + i; | 112 String currentWebappId = BASE_WEBAPP_ID + i; |
104 int actualIndex = assigner.assign(currentWebappId); | 113 int actualIndex = assigner.assign(currentWebappId); |
105 int expectedIndex = testMap.get(currentWebappId); | 114 int expectedIndex = testMap.get(currentWebappId); |
106 assertEquals(expectedIndex, actualIndex); | 115 assertEquals(expectedIndex, actualIndex); |
107 } | 116 } |
108 | 117 |
109 // Access all but the last one to ensure that the last Activity is recyc
led. | 118 // Access all but the last one to ensure that the last Activity is recyc
led. |
110 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; ++i) { | 119 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; ++i) { |
111 String currentWebappId = BASE_WEBAPP_ID + i; | 120 String currentWebappId = BASE_WEBAPP_ID + i; |
112 int activityIndex = testMap.get(currentWebappId); | 121 int activityIndex = testMap.get(currentWebappId); |
113 assigner.markActivityUsed(activityIndex, currentWebappId); | 122 assigner.markActivityUsed(activityIndex, currentWebappId); |
114 } | 123 } |
115 | 124 |
116 // Make sure that the least recently used Activity is repurposed when we
run out. | 125 // Make sure that the least recently used Activity is repurposed when we
run out. |
117 String overflowWebappId = "OVERFLOW_ID"; | 126 String overflowWebappId = "OVERFLOW_ID"; |
| 127 assertEquals(index, assigner.getIndex(overflowWebappId)); |
118 int overflowActivityIndex = assigner.assign(overflowWebappId); | 128 int overflowActivityIndex = assigner.assign(overflowWebappId); |
119 | 129 |
120 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB
APP_ACTIVITIES - 1); | 130 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB
APP_ACTIVITIES - 1); |
121 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap
pId); | 131 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap
pId); |
122 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId
); | 132 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId
); |
123 | 133 |
124 assertEquals("Overflow webapp did not steal the Activity from the other
webapp", | 134 assertEquals("Overflow webapp did not steal the Activity from the other
webapp", |
125 lastAssignedPreviousActivityIndex, overflowActivityIndex); | 135 lastAssignedPreviousActivityIndex, overflowActivityIndex); |
126 assertNotSame("Webapp did not get reassigned to a new Activity.", | 136 assertNotSame("Webapp did not get reassigned to a new Activity.", |
127 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn
dex); | 137 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn
dex); |
128 | 138 |
129 checkState(assigner); | 139 checkState(assigner, index); |
| 140 } |
| 141 |
| 142 @UiThreadTest |
| 143 @SmallTest |
| 144 @Feature({"WebApk"}) |
| 145 public void testGetIndex() { |
| 146 String webappId = BASE_WEBAPP_ID; |
| 147 ActivityAssigner assigner = ActivityAssigner.instance(mContext); |
| 148 assertEquals(ActivityAssigner.WEBAPP_ACTIVITY_INDEX, assigner.getIndex(w
ebappId)); |
| 149 |
| 150 String webApkId = WebApkConstants.WEBAPK_ID_PREFIX + "id"; |
| 151 assertEquals(ActivityAssigner.WEBAPK_ACTIVITY_INDEX, assigner.getIndex(w
ebApkId)); |
130 } | 152 } |
131 | 153 |
132 /** Saves state indicating that a number of WebappActivities have already be
en saved out. */ | 154 /** Saves state indicating that a number of WebappActivities have already be
en saved out. */ |
133 private void createPreferences(int numSavedEntries) { | 155 private void createPreferences(int numSavedEntries, int activityType) { |
134 mPreferences.clear(); | 156 mPreferences[activityType].clear(); |
135 mPreferences.put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES, numSavedEntrie
s); | 157 mPreferences[activityType].put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES[a
ctivityType], |
| 158 numSavedEntries); |
136 for (int i = 0; i < numSavedEntries; ++i) { | 159 for (int i = 0; i < numSavedEntries; ++i) { |
137 String activityIndexKey = ActivityAssigner.PREF_ACTIVITY_INDEX + i; | 160 String activityIndexKey = |
138 mPreferences.put(activityIndexKey, i); | 161 ActivityAssigner.PREF_ACTIVITY_INDEX[activityType] + i; |
| 162 mPreferences[activityType].put(activityIndexKey, i); |
139 | 163 |
140 String webappIdKey = ActivityAssigner.PREF_WEBAPP_ID + i; | 164 String webappIdKey = ActivityAssigner.PREF_WEBAPP_ID[activityType] +
i; |
141 String webappIdValue = BASE_WEBAPP_ID + i; | 165 String webappIdValue = BASE_WEBAPP_ID + i; |
142 mPreferences.put(webappIdKey, webappIdValue); | 166 mPreferences[activityType].put(webappIdKey, webappIdValue); |
143 } | 167 } |
144 } | 168 } |
145 | 169 |
146 /** Checks the saved state to make sure it makes sense. */ | 170 /** Checks the saved state to make sure it makes sense. */ |
147 private void checkState(ActivityAssigner assigner) { | 171 private void checkState(ActivityAssigner assigner, int index) { |
148 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); | 172 List<ActivityAssigner.ActivityEntry>[] entries = assigner.getEntries(); |
149 | 173 |
150 // Confirm that the right number of entries in memory and in the prefere
nces. | 174 // Confirm that the right number of entries in memory and in the prefere
nces. |
151 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); | 175 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries[index].size
()); |
152 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, | 176 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, |
153 (int) (Integer) mPreferences.get(ActivityAssigner.PREF_NUM_SAVED
_ENTRIES)); | 177 (int) (Integer) mPreferences[index].get( |
| 178 ActivityAssigner.PREF_NUM_SAVED_ENTRIES[index])); |
154 | 179 |
155 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES
- 1. | 180 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES
- 1. |
156 HashSet<Integer> assignedActivities = new HashSet<Integer>(); | 181 HashSet<Integer> assignedActivities = new HashSet<Integer>(); |
157 for (ActivityAssigner.ActivityEntry entry : entries) { | 182 for (ActivityAssigner.ActivityEntry entry : entries[index]) { |
158 assignedActivities.add(entry.mActivityIndex); | 183 assignedActivities.add(entry.mActivityIndex); |
159 } | 184 } |
160 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { | 185 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { |
161 assertTrue(assignedActivities.contains(i)); | 186 assertTrue(assignedActivities.contains(i)); |
162 } | 187 } |
163 } | 188 } |
164 } | 189 } |
OLD | NEW |