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