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.
5 package org
.chromium
.chrome
.browser
.webapps
;
7 import android
.test
.InstrumentationTestCase
;
8 import android
.test
.UiThreadTest
;
9 import android
.test
.suitebuilder
.annotation
.SmallTest
;
11 import org
.chromium
.base
.test
.util
.AdvancedMockContext
;
12 import org
.chromium
.base
.test
.util
.Feature
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
20 * Tests for the ActivityAssigner class.
22 public class ActivityAssignerTest
extends InstrumentationTestCase
{
23 private static final String BASE_WEBAPP_ID
= "BASE_WEBAPP_ID_";
25 private AdvancedMockContext mContext
;
26 private HashMap
<String
, Object
> mPreferences
;
29 protected void setUp() throws Exception
{
31 mContext
= new AdvancedMockContext();
32 mPreferences
= new HashMap
<String
, Object
>();
33 mContext
.addSharedPreferences(ActivityAssigner
.PREF_PACKAGE
, mPreferences
);
39 public void testEntriesCreated() {
40 ActivityAssigner assigner
= ActivityAssigner
.instance(mContext
);
42 // Make sure that no webapps have been assigned to any Activities for a fresh install.
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
);
52 * Make sure invalid entries get culled & that we still have the correct number of unique
53 * Activity indices available.
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
);
63 ActivityAssigner assigner
= ActivityAssigner
.instance(mContext
);
68 * Make sure we recover from corrupted stored preferences.
73 public void testCorruptedPreferences() {
74 String wrongVariableType
= "omgwtfbbq";
76 mPreferences
.put(ActivityAssigner
.PREF_NUM_SAVED_ENTRIES
, wrongVariableType
);
78 ActivityAssigner assigner
= ActivityAssigner
.instance(mContext
);
85 public void testAssignment() {
86 ActivityAssigner assigner
= ActivityAssigner
.instance(mContext
);
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
);
97 assertEquals(ActivityAssigner
.NUM_WEBAPP_ACTIVITIES
, testMap
.size());
99 // Make sure that passing in the same webapp ID gives back the same Activity.
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
);
107 // Access all but the last one to ensure that the last Activity is recycled.
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
);
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
);
118 String lastAssignedWebappId
= BASE_WEBAPP_ID
+ (ActivityAssigner
.NUM_WEBAPP_ACTIVITIES
- 1);
119 int lastAssignedCurrentActivityIndex
= assigner
.assign(lastAssignedWebappId
);
120 int lastAssignedPreviousActivityIndex
= testMap
.get(lastAssignedWebappId
);
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
, lastAssignedCurrentActivityIndex
);
127 checkState(assigner
);
130 /** Saves state indicating that a number of WebappActivities have already been saved out. */
131 private void createPreferences(int numSavedEntries
) {
132 mPreferences
.clear();
133 mPreferences
.put(ActivityAssigner
.PREF_NUM_SAVED_ENTRIES
, numSavedEntries
);
134 for (int i
= 0; i
< numSavedEntries
; ++i
) {
135 String activityIndexKey
= ActivityAssigner
.PREF_ACTIVITY_INDEX
+ i
;
136 mPreferences
.put(activityIndexKey
, i
);
138 String webappIdKey
= ActivityAssigner
.PREF_WEBAPP_ID
+ i
;
139 String webappIdValue
= BASE_WEBAPP_ID
+ i
;
140 mPreferences
.put(webappIdKey
, webappIdValue
);
144 /** Checks the saved state to make sure it makes sense. */
145 private void checkState(ActivityAssigner assigner
) {
146 List
<ActivityAssigner
.ActivityEntry
> entries
= assigner
.getEntries();
148 // Confirm that the right number of entries in memory and in the preferences.
149 assertEquals(ActivityAssigner
.NUM_WEBAPP_ACTIVITIES
, entries
.size());
150 assertEquals(ActivityAssigner
.NUM_WEBAPP_ACTIVITIES
,
151 (int) (Integer
) mPreferences
.get(ActivityAssigner
.PREF_NUM_SAVED_ENTRIES
));
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
);
158 for (int i
= 0; i
< ActivityAssigner
.NUM_WEBAPP_ACTIVITIES
; ++i
) {
159 assertTrue(assignedActivities
.contains(i
));