Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/webapps/ActivityAssignerTest.java

Issue 1989283002: Upstream: Launch WebApkActivity from WebAPK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update ActivityAssigner. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_TYPE_COUNT];
35 mContext.addSharedPreferences(ActivityAssigner.PREF_PACKAGE, mPreference s); 36 for (int i = 0; i < ActivityAssigner.ACTIVITY_TYPE_COUNT; ++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 String webappId = BASE_WEBAPP_ID;
47 ActivityAssigner assigner = ActivityAssigner.instance(mContext, webappId );
43 48
44 // Make sure that no webapps have been assigned to any Activities for a fresh install. 49 // Make sure that no webapps have been assigned to any Activities for a fresh install.
50 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries();
gone 2016/05/26 18:20:36 This ordering change seems unnecessary.
Xi Han 2016/05/26 20:54:58 Done.
45 checkState(assigner); 51 checkState(assigner);
46 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries();
47 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); 52 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size());
48 for (ActivityAssigner.ActivityEntry entry : entries) { 53 for (ActivityAssigner.ActivityEntry entry : entries) {
49 assertEquals(null, entry.mWebappId); 54 assertEquals(null, entry.mWebappId);
50 } 55 }
51 } 56 }
52 57
53 /** 58 /**
54 * Make sure invalid entries get culled & that we still have the correct num ber of unique 59 * Make sure invalid entries get culled & that we still have the correct num ber of unique
55 * Activity indices available. 60 * Activity indices available.
56 */ 61 */
57 @UiThreadTest 62 @UiThreadTest
58 @SmallTest 63 @SmallTest
59 @Feature({"Webapps"}) 64 @Feature({"Webapps"})
60 public void testEntriesDownsized() { 65 public void testEntriesDownsized() {
61 // Store preferences indicating that more Activities existed previously than there are now. 66 // Store preferences indicating that more Activities existed previously than there are now.
62 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1; 67 int numSavedEntries = ActivityAssigner.NUM_WEBAPP_ACTIVITIES + 1;
63 createPreferences(numSavedEntries); 68 String webappId = BASE_WEBAPP_ID;
69 int index = ActivityAssigner.getIndex(webappId);
70 createPreferences(numSavedEntries, index);
64 71
65 ActivityAssigner assigner = ActivityAssigner.instance(mContext); 72 ActivityAssigner assigner = ActivityAssigner.instance(mContext, webappId );
73 assertEquals(index, assigner.getActivityTypeIndex());
66 checkState(assigner); 74 checkState(assigner);
67 } 75 }
68 76
69 /** 77 /**
70 * Make sure we recover from corrupted stored preferences. 78 * Make sure we recover from corrupted stored preferences.
71 */ 79 */
72 @UiThreadTest 80 @UiThreadTest
73 @SmallTest 81 @SmallTest
74 @Feature({"Webapps"}) 82 @Feature({"Webapps"})
75 public void testCorruptedPreferences() { 83 public void testCorruptedPreferences() {
76 String wrongVariableType = "omgwtfbbq"; 84 String wrongVariableType = "omgwtfbbq";
77 mPreferences.clear(); 85 String webappId = BASE_WEBAPP_ID;
78 mPreferences.put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES, wrongVariableT ype); 86 int index = ActivityAssigner.getIndex(BASE_WEBAPP_ID);
87 mPreferences[index].clear();
88 mPreferences[index].put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES[index], wrongVariableType);
79 89
80 ActivityAssigner assigner = ActivityAssigner.instance(mContext); 90 ActivityAssigner assigner = ActivityAssigner.instance(mContext, webappId );
91 assertEquals(index, assigner.getActivityTypeIndex());
81 checkState(assigner); 92 checkState(assigner);
82 } 93 }
83 94
84 @UiThreadTest 95 @UiThreadTest
85 @SmallTest 96 @SmallTest
86 @Feature({"Webapps"}) 97 @Feature({"Webapps"})
87 public void testAssignment() { 98 public void testAssignment() {
88 ActivityAssigner assigner = ActivityAssigner.instance(mContext); 99 String webappId = BASE_WEBAPP_ID;
100 ActivityAssigner assigner = ActivityAssigner.instance(mContext, webappId );
101 int index = assigner.getActivityTypeIndex();
102
89 checkState(assigner); 103 checkState(assigner);
90 104
91 // Assign all of the Activities to webapps. 105 // Assign all of the Activities to webapps.
92 // Go backwards to make sure ordering doesn't matter. 106 // Go backwards to make sure ordering doesn't matter.
93 Map<String, Integer> testMap = new HashMap<String, Integer>(); 107 Map<String, Integer> testMap = new HashMap<String, Integer>();
94 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) { 108 for (int i = ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; i >= 0; --i) {
95 String currentWebappId = BASE_WEBAPP_ID + i; 109 String currentWebappId = BASE_WEBAPP_ID + i;
96 int activityIndex = assigner.assign(currentWebappId); 110 int activityIndex = assigner.assign(currentWebappId);
97 testMap.put(currentWebappId, activityIndex); 111 testMap.put(currentWebappId, activityIndex);
98 } 112 }
99 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size()); 113 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, testMap.size());
100 114
101 // Make sure that passing in the same webapp ID gives back the same Acti vity. 115 // 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) { 116 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) {
103 String currentWebappId = BASE_WEBAPP_ID + i; 117 String currentWebappId = BASE_WEBAPP_ID + i;
104 int actualIndex = assigner.assign(currentWebappId); 118 int actualIndex = assigner.assign(currentWebappId);
105 int expectedIndex = testMap.get(currentWebappId); 119 int expectedIndex = testMap.get(currentWebappId);
106 assertEquals(expectedIndex, actualIndex); 120 assertEquals(expectedIndex, actualIndex);
107 } 121 }
108 122
109 // Access all but the last one to ensure that the last Activity is recyc led. 123 // 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) { 124 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES - 1; ++i) {
111 String currentWebappId = BASE_WEBAPP_ID + i; 125 String currentWebappId = BASE_WEBAPP_ID + i;
112 int activityIndex = testMap.get(currentWebappId); 126 int activityIndex = testMap.get(currentWebappId);
113 assigner.markActivityUsed(activityIndex, currentWebappId); 127 assigner.markActivityUsed(activityIndex, currentWebappId);
114 } 128 }
115 129
116 // Make sure that the least recently used Activity is repurposed when we run out. 130 // Make sure that the least recently used Activity is repurposed when we run out.
117 String overflowWebappId = "OVERFLOW_ID"; 131 String overflowWebappId = "OVERFLOW_ID";
132 assertEquals(index, ActivityAssigner.getIndex(overflowWebappId));
118 int overflowActivityIndex = assigner.assign(overflowWebappId); 133 int overflowActivityIndex = assigner.assign(overflowWebappId);
119 134
120 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB APP_ACTIVITIES - 1); 135 String lastAssignedWebappId = BASE_WEBAPP_ID + (ActivityAssigner.NUM_WEB APP_ACTIVITIES - 1);
121 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap pId); 136 int lastAssignedCurrentActivityIndex = assigner.assign(lastAssignedWebap pId);
122 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId ); 137 int lastAssignedPreviousActivityIndex = testMap.get(lastAssignedWebappId );
123 138
124 assertEquals("Overflow webapp did not steal the Activity from the other webapp", 139 assertEquals("Overflow webapp did not steal the Activity from the other webapp",
125 lastAssignedPreviousActivityIndex, overflowActivityIndex); 140 lastAssignedPreviousActivityIndex, overflowActivityIndex);
126 assertNotSame("Webapp did not get reassigned to a new Activity.", 141 assertNotSame("Webapp did not get reassigned to a new Activity.",
127 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn dex); 142 lastAssignedPreviousActivityIndex, lastAssignedCurrentActivityIn dex);
128 143
129 checkState(assigner); 144 checkState(assigner);
130 } 145 }
131 146
147 @UiThreadTest
148 @SmallTest
149 @Feature({"WebApk"})
150 public void testGetIndex() {
151 String webappId = BASE_WEBAPP_ID;
152 assertEquals(ActivityAssigner.WEBAPP_ACTIVITY_INDEX, ActivityAssigner.ge tIndex(webappId));
153
154 String webApkId = WebApkConstants.WEBAPK_ID_PREFIX + "id";
155 assertEquals(ActivityAssigner.WEBAPK_ACTIVITY_INDEX, ActivityAssigner.ge tIndex(webApkId));
156 }
157
132 /** Saves state indicating that a number of WebappActivities have already be en saved out. */ 158 /** Saves state indicating that a number of WebappActivities have already be en saved out. */
133 private void createPreferences(int numSavedEntries) { 159 private void createPreferences(int numSavedEntries, int activityTypeIndex) {
134 mPreferences.clear(); 160 mPreferences[activityTypeIndex].clear();
135 mPreferences.put(ActivityAssigner.PREF_NUM_SAVED_ENTRIES, numSavedEntrie s); 161 mPreferences[activityTypeIndex].put(
162 ActivityAssigner.PREF_NUM_SAVED_ENTRIES[activityTypeIndex], numS avedEntries);
136 for (int i = 0; i < numSavedEntries; ++i) { 163 for (int i = 0; i < numSavedEntries; ++i) {
137 String activityIndexKey = ActivityAssigner.PREF_ACTIVITY_INDEX + i; 164 String activityIndexKey =
138 mPreferences.put(activityIndexKey, i); 165 ActivityAssigner.PREF_ACTIVITY_INDEX[activityTypeIndex] + i;
166 mPreferences[activityTypeIndex].put(activityIndexKey, i);
139 167
140 String webappIdKey = ActivityAssigner.PREF_WEBAPP_ID + i; 168 String webappIdKey = ActivityAssigner.PREF_WEBAPP_ID[activityTypeInd ex] + i;
141 String webappIdValue = BASE_WEBAPP_ID + i; 169 String webappIdValue = BASE_WEBAPP_ID + i;
142 mPreferences.put(webappIdKey, webappIdValue); 170 mPreferences[activityTypeIndex].put(webappIdKey, webappIdValue);
143 } 171 }
144 } 172 }
145 173
146 /** Checks the saved state to make sure it makes sense. */ 174 /** Checks the saved state to make sure it makes sense. */
147 private void checkState(ActivityAssigner assigner) { 175 private void checkState(ActivityAssigner assigner) {
148 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries(); 176 List<ActivityAssigner.ActivityEntry> entries = assigner.getEntries();
177 int activityTypeIndex = assigner.getActivityTypeIndex();
149 178
150 // Confirm that the right number of entries in memory and in the prefere nces. 179 // Confirm that the right number of entries in memory and in the prefere nces.
151 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size()); 180 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, entries.size());
152 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES, 181 assertEquals(ActivityAssigner.NUM_WEBAPP_ACTIVITIES,
153 (int) (Integer) mPreferences.get(ActivityAssigner.PREF_NUM_SAVED _ENTRIES)); 182 (int) (Integer) mPreferences[activityTypeIndex].get(
183 ActivityAssigner.PREF_NUM_SAVED_ENTRIES[activityTypeInde x]));
154 184
155 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES - 1. 185 // Confirm that the Activity indices go from 0 to NUM_WEBAPP_ACTIVITIES - 1.
156 HashSet<Integer> assignedActivities = new HashSet<Integer>(); 186 HashSet<Integer> assignedActivities = new HashSet<Integer>();
157 for (ActivityAssigner.ActivityEntry entry : entries) { 187 for (ActivityAssigner.ActivityEntry entry : entries) {
158 assignedActivities.add(entry.mActivityIndex); 188 assignedActivities.add(entry.mActivityIndex);
159 } 189 }
160 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) { 190 for (int i = 0; i < ActivityAssigner.NUM_WEBAPP_ACTIVITIES; ++i) {
161 assertTrue(assignedActivities.contains(i)); 191 assertTrue(assignedActivities.contains(i));
162 } 192 }
163 } 193 }
164 } 194 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698