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.support.test.annotation.UiThreadTest; |
7 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
8 import android.test.InstrumentationTestCase; | 9 import android.support.test.rule.UiThreadTestRule; |
9 import android.test.UiThreadTest; | 10 |
| 11 import org.junit.After; |
| 12 import org.junit.Assert; |
| 13 import org.junit.Before; |
| 14 import org.junit.Rule; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
10 | 17 |
11 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
12 import org.chromium.base.metrics.RecordHistogram; | 19 import org.chromium.base.metrics.RecordHistogram; |
13 import org.chromium.base.test.util.AdvancedMockContext; | 20 import org.chromium.base.test.util.AdvancedMockContext; |
14 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
15 import org.chromium.base.test.util.RetryOnFailure; | 22 import org.chromium.base.test.util.RetryOnFailure; |
| 23 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
16 | 24 |
17 import java.util.HashMap; | 25 import java.util.HashMap; |
18 import java.util.HashSet; | 26 import java.util.HashSet; |
19 import java.util.List; | 27 import java.util.List; |
20 import java.util.Map; | 28 import java.util.Map; |
21 | 29 |
22 /** | 30 /** |
23 * Tests for the ActivityAssigner class. | 31 * Tests for the ActivityAssigner class. |
24 */ | 32 */ |
25 public class ActivityAssignerTest extends InstrumentationTestCase { | 33 @RunWith(ChromeJUnit4ClassRunner.class) |
| 34 public class ActivityAssignerTest { |
26 private static final String BASE_WEBAPP_ID = "BASE_WEBAPP_ID_"; | 35 private static final String BASE_WEBAPP_ID = "BASE_WEBAPP_ID_"; |
27 | 36 |
28 private AdvancedMockContext mContext; | 37 private AdvancedMockContext mContext; |
29 private HashMap<String, Object>[] mPreferences; | 38 private HashMap<String, Object>[] mPreferences; |
30 | 39 |
| 40 @Rule |
| 41 public UiThreadTestRule mRule = new UiThreadTestRule(); |
| 42 |
| 43 @Before |
31 @SuppressWarnings("unchecked") | 44 @SuppressWarnings("unchecked") |
32 @Override | 45 public void setUp() throws Exception { |
33 protected void setUp() throws Exception { | |
34 super.setUp(); | |
35 RecordHistogram.setDisabledForTests(true); | 46 RecordHistogram.setDisabledForTests(true); |
36 mContext = new AdvancedMockContext(ContextUtils.getApplicationContext())
; | 47 mContext = new AdvancedMockContext(ContextUtils.getApplicationContext())
; |
37 mPreferences = new HashMap[ActivityAssigner.NAMESPACE_COUNT]; | 48 mPreferences = new HashMap[ActivityAssigner.NAMESPACE_COUNT]; |
38 for (int i = 0; i < ActivityAssigner.NAMESPACE_COUNT; ++i) { | 49 for (int i = 0; i < ActivityAssigner.NAMESPACE_COUNT; ++i) { |
39 mPreferences[i] = new HashMap<String, Object>(); | 50 mPreferences[i] = new HashMap<String, Object>(); |
40 mContext.addSharedPreferences(ActivityAssigner.PREF_PACKAGE[i], mPre
ferences[i]); | 51 mContext.addSharedPreferences(ActivityAssigner.PREF_PACKAGE[i], mPre
ferences[i]); |
41 } | 52 } |
42 ContextUtils.initApplicationContextForTests(mContext); | 53 ContextUtils.initApplicationContextForTests(mContext); |
43 } | 54 } |
44 | 55 |
45 @Override | 56 @After |
46 public void tearDown() throws Exception { | 57 public void tearDown() throws Exception { |
47 super.tearDown(); | |
48 RecordHistogram.setDisabledForTests(false); | 58 RecordHistogram.setDisabledForTests(false); |
49 } | 59 } |
50 | 60 |
| 61 @Test |
51 @UiThreadTest | 62 @UiThreadTest |
52 @SmallTest | 63 @SmallTest |
53 @Feature({"Webapps"}) | 64 @Feature({"Webapps"}) |
54 @RetryOnFailure | 65 @RetryOnFailure |
55 public void testEntriesCreated() { | 66 public void testEntriesCreated() { |
56 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); | 67 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); |
57 | 68 |
58 // Make sure that no webapps have been assigned to any Activities for a
fresh install. | 69 // Make sure that no webapps have been assigned to any Activities for a
fresh install. |
59 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); | 70 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); |
60 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); | 71 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); |
61 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); | 72 Assert.assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size
()); |
62 for (ActivityAssigner.ActivityEntry entry : entries) { | 73 for (ActivityAssigner.ActivityEntry entry : entries) { |
63 assertEquals(null, entry.mWebappId); | 74 Assert.assertEquals(null, entry.mWebappId); |
64 } | 75 } |
65 } | 76 } |
66 | 77 |
67 /** | 78 /** |
68 * Make sure invalid entries get culled & that we still have the correct num
ber of unique | 79 * Make sure invalid entries get culled & that we still have the correct num
ber of unique |
69 * Activity indices available. | 80 * Activity indices available. |
70 */ | 81 */ |
| 82 @Test |
71 @UiThreadTest | 83 @UiThreadTest |
72 @SmallTest | 84 @SmallTest |
73 @Feature({"Webapps"}) | 85 @Feature({"Webapps"}) |
74 public void testEntriesDownsized() { | 86 public void testEntriesDownsized() { |
75 // Store preferences indicating that more Activities existed previously
than there are now. | 87 // Store preferences indicating that more Activities existed previously
than there are now. |
76 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1; | 88 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1; |
77 createPreferences(numSavedEntries, ActivityAssigner.WEBAPP_NAMESPACE); | 89 createPreferences(numSavedEntries, ActivityAssigner.WEBAPP_NAMESPACE); |
78 | 90 |
79 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); | 91 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); |
80 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); | 92 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); |
81 } | 93 } |
82 | 94 |
83 /** | 95 /** |
84 * Make sure we recover from corrupted stored preferences. | 96 * Make sure we recover from corrupted stored preferences. |
85 */ | 97 */ |
| 98 @Test |
86 @UiThreadTest | 99 @UiThreadTest |
87 @SmallTest | 100 @SmallTest |
88 @Feature({"Webapps"}) | 101 @Feature({"Webapps"}) |
89 @RetryOnFailure | 102 @RetryOnFailure |
90 public void testCorruptedPreferences() { | 103 public void testCorruptedPreferences() { |
91 String wrongVariableType = "omgwtfbbq"; | 104 String wrongVariableType = "omgwtfbbq"; |
92 int index = ActivityAssigner.WEBAPP_NAMESPACE; | 105 int index = ActivityAssigner.WEBAPP_NAMESPACE; |
93 mPreferences[index].clear(); | 106 mPreferences[index].clear(); |
94 mPreferences[index].put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES[index],
wrongVariableType); | 107 mPreferences[index].put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES[index],
wrongVariableType); |
95 | 108 |
96 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); | 109 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); |
97 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); | 110 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); |
98 } | 111 } |
99 | 112 |
| 113 @Test |
100 @UiThreadTest | 114 @UiThreadTest |
101 @SmallTest | 115 @SmallTest |
102 @Feature({"Webapps"}) | 116 @Feature({"Webapps"}) |
103 public void testAssignment() { | 117 public void testAssignment() { |
104 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); | 118 ActivityAssigner assigner = ActivityAssigner.instance(ActivityAssigner.W
EBAPP_NAMESPACE); |
105 | 119 |
106 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); | 120 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); |
107 | 121 |
108 // Assign all of the Activities to webapps. | 122 // Assign all of the Activities to webapps. |
109 // Go backwards to make sure ordering doesn't matter. | 123 // Go backwards to make sure ordering doesn't matter. |
110 Map<String, Integer> testMap = new HashMap<String, Integer>(); | 124 Map<String, Integer> testMap = new HashMap<String, Integer>(); |
111 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) { | 125 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) { |
112 String currentWebappId = BASE_WEBAPP_ID + i; | 126 String currentWebappId = BASE_WEBAPP_ID + i; |
113 int activityIndex = assigner.assign(currentWebappId); | 127 int activityIndex = assigner.assign(currentWebappId); |
114 testMap.put(currentWebappId, activityIndex); | 128 testMap.put(currentWebappId, activityIndex); |
115 } | 129 } |
116 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size()); | 130 Assert.assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size
()); |
117 | 131 |
118 // Make sure that passing in the same webapp ID gives back the same Acti
vity. | 132 // Make sure that passing in the same webapp ID gives back the same Acti
vity. |
119 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { | 133 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { |
120 String currentWebappId = BASE_WEBAPP_ID + i; | 134 String currentWebappId = BASE_WEBAPP_ID + i; |
121 int actualIndex = assigner.assign(currentWebappId); | 135 int actualIndex = assigner.assign(currentWebappId); |
122 int expectedIndex = testMap.get(currentWebappId); | 136 int expectedIndex = testMap.get(currentWebappId); |
123 assertEquals(expectedIndex, actualIndex); | 137 Assert.assertEquals(expectedIndex, actualIndex); |
124 } | 138 } |
125 | 139 |
126 // Access all but the last one to ensure that the last Activity is recyc
led. | 140 // Access all but the last one to ensure that the last Activity is recyc
led. |
127 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; ++i) { | 141 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; ++i) { |
128 String currentWebappId = BASE_WEBAPP_ID + i; | 142 String currentWebappId = BASE_WEBAPP_ID + i; |
129 int activityIndex = testMap.get(currentWebappId); | 143 int activityIndex = testMap.get(currentWebappId); |
130 assigner.markActivityUsed(activityIndex, currentWebappId); | 144 assigner.markActivityUsed(activityIndex, currentWebappId); |
131 } | 145 } |
132 | 146 |
133 // Make sure that the least recently used Activity is repurposed when we
run out. | 147 // Make sure that the least recently used Activity is repurposed when we
run out. |
134 String overflowWebappId = "OVERFLOW_ID"; | 148 String overflowWebappId = "OVERFLOW_ID"; |
135 int overflowActivityIndex = assigner.assign(overflowWebappId); | 149 int overflowActivityIndex = assigner.assign(overflowWebappId); |
136 | 150 |
137 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB
APP_ACTIVITIES - 1); | 151 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB
APP_ACTIVITIES - 1); |
138 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap
pId); | 152 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap
pId); |
139 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId
); | 153 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId
); |
140 | 154 |
141 assertEquals("Overflow webapp did not steal the Activity from the other
webapp", | 155 Assert.assertEquals("Overflow webapp did not steal the Activity from the
other webapp", |
142 lastAssignedPreviousActivityIndex, overflowActivityIndex); | 156 lastAssignedPreviousActivityIndex, overflowActivityIndex); |
143 assertNotSame("Webapp did not get reassigned to a new Activity.", | 157 Assert.assertNotSame("Webapp did not get reassigned to a new Activity.", |
144 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn
dex); | 158 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn
dex); |
145 | 159 |
146 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); | 160 checkState(assigner, ActivityAssigner.WEBAPP_NAMESPACE); |
147 } | 161 } |
148 | 162 |
| 163 @Test |
149 @UiThreadTest | 164 @UiThreadTest |
150 @SmallTest | 165 @SmallTest |
151 @Feature({"WebApk"}) | 166 @Feature({"WebApk"}) |
152 public void testInstance() { | 167 public void testInstance() { |
153 assertNotSame( | 168 Assert.assertNotSame(ActivityAssigner.instance(ActivityAssigner.WEBAPP_N
AMESPACE), |
154 ActivityAssigner.instance(ActivityAssigner.WEBAPP_NAMESPACE), | |
155 ActivityAssigner.instance(ActivityAssigner.WEBAPK_NAMESPACE)); | 169 ActivityAssigner.instance(ActivityAssigner.WEBAPK_NAMESPACE)); |
156 } | 170 } |
157 | 171 |
158 /** Saves state indicating that a number of WebappActivities have already be
en saved out. */ | 172 /** Saves state indicating that a number of WebappActivities have already be
en saved out. */ |
159 private void createPreferences(int numSavedEntries, int activityTypeIndex) { | 173 private void createPreferences(int numSavedEntries, int activityTypeIndex) { |
160 mPreferences[activityTypeIndex].clear(); | 174 mPreferences[activityTypeIndex].clear(); |
161 mPreferences[activityTypeIndex].put( | 175 mPreferences[activityTypeIndex].put( |
162 ActivityAssigner.PREF_NUM_SAVED_ENTRIES[activityTypeIndex], numS
avedEntries); | 176 ActivityAssigner.PREF_NUM_SAVED_ENTRIES[activityTypeIndex], numS
avedEntries); |
163 for (int i = 0; i < numSavedEntries; ++i) { | 177 for (int i = 0; i < numSavedEntries; ++i) { |
164 String activityIndexKey = | 178 String activityIndexKey = |
165 ActivityAssigner.PREF_ACTIVITY_INDEX[activityTypeIndex] + i; | 179 ActivityAssigner.PREF_ACTIVITY_INDEX[activityTypeIndex] + i; |
166 mPreferences[activityTypeIndex].put(activityIndexKey, i); | 180 mPreferences[activityTypeIndex].put(activityIndexKey, i); |
167 | 181 |
168 String webappIdKey = ActivityAssigner.PREF_ACTIVITY_ID[activityTypeI
ndex] + i; | 182 String webappIdKey = ActivityAssigner.PREF_ACTIVITY_ID[activityTypeI
ndex] + i; |
169 String webappIdValue = BASE_WEBAPP_ID + i; | 183 String webappIdValue = BASE_WEBAPP_ID + i; |
170 mPreferences[activityTypeIndex].put(webappIdKey, webappIdValue); | 184 mPreferences[activityTypeIndex].put(webappIdKey, webappIdValue); |
171 } | 185 } |
172 } | 186 } |
173 | 187 |
174 /** Checks the saved state to make sure it makes sense. */ | 188 /** Checks the saved state to make sure it makes sense. */ |
175 private void checkState(ActivityAssigner assigner, int namespace) { | 189 private void checkState(ActivityAssigner assigner, int namespace) { |
176 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); | 190 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); |
177 | 191 |
178 // Confirm that the right number of entries in memory and in the prefere
nces. | 192 // Confirm that the right number of entries in memory and in the prefere
nces. |
179 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); | 193 Assert.assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size
()); |
180 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, | 194 Assert.assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, |
181 (int) (Integer) mPreferences[namespace].get( | 195 (int) (Integer) mPreferences[namespace].get( |
182 assigner.getPreferenceNumberOfSavedEntries())); | 196 assigner.getPreferenceNumberOfSavedEntries())); |
183 | 197 |
184 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES
- 1. | 198 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES
- 1. |
185 HashSet<Integer> assignedActivities = new HashSet<Integer>(); | 199 HashSet<Integer> assignedActivities = new HashSet<Integer>(); |
186 for (ActivityAssigner.ActivityEntry entry : entries) { | 200 for (ActivityAssigner.ActivityEntry entry : entries) { |
187 assignedActivities.add(entry.mActivityIndex); | 201 assignedActivities.add(entry.mActivityIndex); |
188 } | 202 } |
189 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { | 203 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { |
190 assertTrue(assignedActivities.contains(i)); | 204 Assert.assertTrue(assignedActivities.contains(i)); |
191 } | 205 } |
192 } | 206 } |
193 } | 207 } |
OLD | NEW |