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