blob: 4efcb7ed3cde5a6f5b3f42fe309c5e8afffb2f15 [file] [log] [blame]
Fan Zhang914afbf2016-11-01 15:36:25 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settingslib.drawer;
18
James Lemieux5c50dc12018-02-12 01:30:32 -080019import static com.google.common.truth.Truth.assertThat;
20
Fan Zhangfc76ab32016-11-14 09:48:06 -080021import android.content.ComponentName;
Fan Zhang914afbf2016-11-01 15:36:25 -070022import android.content.Context;
Fan Zhangfc76ab32016-11-14 09:48:06 -080023import android.content.Intent;
Fan Zhang914afbf2016-11-01 15:36:25 -070024import android.util.Pair;
25
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070026import com.android.settingslib.SettingsLibRobolectricTestRunner;
Fan Zhang914afbf2016-11-01 15:36:25 -070027
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
Fan Zhang914afbf2016-11-01 15:36:25 -070031import org.robolectric.shadows.ShadowApplication;
32
33import java.util.HashMap;
34import java.util.Map;
35
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070036@RunWith(SettingsLibRobolectricTestRunner.class)
Fan Zhang914afbf2016-11-01 15:36:25 -070037public class CategoryManagerTest {
38
39 private Context mContext;
40 private CategoryManager mCategoryManager;
41 private Map<Pair<String, String>, Tile> mTileByComponentCache;
42 private Map<String, DashboardCategory> mCategoryByKeyMap;
43
44 @Before
45 public void setUp() {
46 mContext = ShadowApplication.getInstance().getApplicationContext();
47 mTileByComponentCache = new HashMap<>();
48 mCategoryByKeyMap = new HashMap<>();
49 mCategoryManager = CategoryManager.get(mContext);
50 }
51
52 @Test
53 public void getInstance_shouldBeSingleton() {
54 assertThat(mCategoryManager).isSameAs(CategoryManager.get(mContext));
55 }
56
57 @Test
58 public void backwardCompatCleanupForCategory_shouldNotChangeCategoryForNewKeys() {
59 final Tile tile1 = new Tile();
60 final Tile tile2 = new Tile();
61 tile1.category = CategoryKey.CATEGORY_ACCOUNT;
62 tile2.category = CategoryKey.CATEGORY_ACCOUNT;
63 final DashboardCategory category = new DashboardCategory();
64 category.addTile(tile1);
65 category.addTile(tile2);
66 mCategoryByKeyMap.put(CategoryKey.CATEGORY_ACCOUNT, category);
67 mTileByComponentCache.put(new Pair<>("PACKAGE", "1"), tile1);
68 mTileByComponentCache.put(new Pair<>("PACKAGE", "2"), tile2);
69
70 mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap);
71
72 assertThat(mCategoryByKeyMap.size()).isEqualTo(1);
73 assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_ACCOUNT)).isNotNull();
74 }
75
76 @Test
77 public void backwardCompatCleanupForCategory_shouldNotChangeCategoryForMixedKeys() {
78 final Tile tile1 = new Tile();
79 final Tile tile2 = new Tile();
80 final String oldCategory = "com.android.settings.category.wireless";
81 tile1.category = CategoryKey.CATEGORY_ACCOUNT;
82 tile2.category = oldCategory;
83 final DashboardCategory category1 = new DashboardCategory();
84 category1.addTile(tile1);
85 final DashboardCategory category2 = new DashboardCategory();
86 category2.addTile(tile2);
87 mCategoryByKeyMap.put(CategoryKey.CATEGORY_ACCOUNT, category1);
88 mCategoryByKeyMap.put(oldCategory, category2);
89 mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tile1);
90 mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS2"), tile2);
91
92 mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap);
93
94 assertThat(mCategoryByKeyMap.size()).isEqualTo(2);
Doris Lingb8d2cd42017-11-27 12:24:09 -080095 assertThat(
96 mCategoryByKeyMap.get(CategoryKey.CATEGORY_ACCOUNT).getTilesCount()).isEqualTo(1);
97 assertThat(mCategoryByKeyMap.get(oldCategory).getTilesCount()).isEqualTo(1);
Fan Zhang914afbf2016-11-01 15:36:25 -070098 }
99
100 @Test
101 public void backwardCompatCleanupForCategory_shouldChangeCategoryForOldKeys() {
102 final Tile tile1 = new Tile();
103 final String oldCategory = "com.android.settings.category.wireless";
104 tile1.category = oldCategory;
105 final DashboardCategory category1 = new DashboardCategory();
106 category1.addTile(tile1);
107 mCategoryByKeyMap.put(oldCategory, category1);
108 mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tile1);
109
110 mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap);
111
112 // Added 1 more category to category map.
113 assertThat(mCategoryByKeyMap.size()).isEqualTo(2);
114 // The new category map has CATEGORY_NETWORK type now, which contains 1 tile.
Doris Lingb8d2cd42017-11-27 12:24:09 -0800115 assertThat(
116 mCategoryByKeyMap.get(CategoryKey.CATEGORY_NETWORK).getTilesCount()).isEqualTo(1);
Fan Zhang914afbf2016-11-01 15:36:25 -0700117 // Old category still exists.
Doris Lingb8d2cd42017-11-27 12:24:09 -0800118 assertThat(mCategoryByKeyMap.get(oldCategory).getTilesCount()).isEqualTo(1);
Fan Zhang914afbf2016-11-01 15:36:25 -0700119 }
Fan Zhangfc76ab32016-11-14 09:48:06 -0800120
121 @Test
Doris Lingce311592017-03-13 12:56:10 -0700122 public void sortCategories_singlePackage_shouldReorderBasedOnPriority() {
Fan Zhangfc76ab32016-11-14 09:48:06 -0800123 // Create some fake tiles that are not sorted.
124 final String testPackage = "com.android.test";
125 final DashboardCategory category = new DashboardCategory();
126 final Tile tile1 = new Tile();
127 tile1.intent =
128 new Intent().setComponent(new ComponentName(testPackage, "class1"));
129 tile1.priority = 100;
130 final Tile tile2 = new Tile();
131 tile2.intent =
132 new Intent().setComponent(new ComponentName(testPackage, "class2"));
133 tile2.priority = 50;
134 final Tile tile3 = new Tile();
135 tile3.intent =
136 new Intent().setComponent(new ComponentName(testPackage, "class3"));
137 tile3.priority = 200;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800138 category.addTile(tile1);
139 category.addTile(tile2);
140 category.addTile(tile3);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800141 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
142
Doris Lingce311592017-03-13 12:56:10 -0700143 // Sort their priorities
144 mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
Fan Zhangfc76ab32016-11-14 09:48:06 -0800145 mCategoryByKeyMap);
146
147 // Verify they are now sorted.
Doris Lingb8d2cd42017-11-27 12:24:09 -0800148 assertThat(category.getTile(0)).isSameAs(tile3);
149 assertThat(category.getTile(1)).isSameAs(tile1);
150 assertThat(category.getTile(2)).isSameAs(tile2);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800151 }
152
153 @Test
Doris Lingce311592017-03-13 12:56:10 -0700154 public void sortCategories_multiPackage_shouldReorderBasedOnPackageAndPriority() {
Fan Zhangfc76ab32016-11-14 09:48:06 -0800155 // Create some fake tiles that are not sorted.
156 final String testPackage1 = "com.android.test1";
157 final String testPackage2 = "com.android.test2";
158 final DashboardCategory category = new DashboardCategory();
159 final Tile tile1 = new Tile();
160 tile1.intent =
161 new Intent().setComponent(new ComponentName(testPackage2, "class1"));
162 tile1.priority = 100;
163 final Tile tile2 = new Tile();
164 tile2.intent =
165 new Intent().setComponent(new ComponentName(testPackage1, "class2"));
166 tile2.priority = 100;
167 final Tile tile3 = new Tile();
168 tile3.intent =
169 new Intent().setComponent(new ComponentName(testPackage1, "class3"));
170 tile3.priority = 50;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800171 category.addTile(tile1);
172 category.addTile(tile2);
173 category.addTile(tile3);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800174 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
175
Doris Lingce311592017-03-13 12:56:10 -0700176 // Sort their priorities
177 mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
Fan Zhangfc76ab32016-11-14 09:48:06 -0800178 mCategoryByKeyMap);
179
180 // Verify they are now sorted.
Doris Lingb8d2cd42017-11-27 12:24:09 -0800181 assertThat(category.getTile(0)).isSameAs(tile2);
182 assertThat(category.getTile(1)).isSameAs(tile1);
183 assertThat(category.getTile(2)).isSameAs(tile3);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800184 }
185
186 @Test
Doris Lingce311592017-03-13 12:56:10 -0700187 public void sortCategories_internalPackageTiles_shouldSkipTileForInternalPackage() {
Fan Zhangfc76ab32016-11-14 09:48:06 -0800188 // Create some fake tiles that are not sorted.
189 final String testPackage =
190 ShadowApplication.getInstance().getApplicationContext().getPackageName();
191 final DashboardCategory category = new DashboardCategory();
192 final Tile tile1 = new Tile();
193 tile1.intent =
194 new Intent().setComponent(new ComponentName(testPackage, "class1"));
195 tile1.priority = 100;
196 final Tile tile2 = new Tile();
197 tile2.intent =
198 new Intent().setComponent(new ComponentName(testPackage, "class2"));
199 tile2.priority = 100;
200 final Tile tile3 = new Tile();
201 tile3.intent =
202 new Intent().setComponent(new ComponentName(testPackage, "class3"));
203 tile3.priority = 50;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800204 category.addTile(tile1);
205 category.addTile(tile2);
206 category.addTile(tile3);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800207 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
208
Doris Lingce311592017-03-13 12:56:10 -0700209 // Sort their priorities
210 mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
Fan Zhangfc76ab32016-11-14 09:48:06 -0800211 mCategoryByKeyMap);
212
213 // Verify the sorting order is not changed
Doris Lingb8d2cd42017-11-27 12:24:09 -0800214 assertThat(category.getTile(0)).isSameAs(tile1);
215 assertThat(category.getTile(1)).isSameAs(tile2);
216 assertThat(category.getTile(2)).isSameAs(tile3);
Doris Lingce311592017-03-13 12:56:10 -0700217 }
218
219 @Test
220 public void sortCategories_internalAndExternalPackageTiles_shouldRetainPriorityOrdering() {
221 // Inject one external tile among internal tiles.
222 final String testPackage =
223 ShadowApplication.getInstance().getApplicationContext().getPackageName();
224 final String testPackage2 = "com.google.test2";
225 final DashboardCategory category = new DashboardCategory();
226 final Tile tile1 = new Tile();
227 tile1.intent = new Intent().setComponent(new ComponentName(testPackage, "class1"));
228 tile1.priority = 2;
229 final Tile tile2 = new Tile();
230 tile2.intent = new Intent().setComponent(new ComponentName(testPackage, "class2"));
231 tile2.priority = 1;
232 final Tile tile3 = new Tile();
233 tile3.intent = new Intent().setComponent(new ComponentName(testPackage2, "class0"));
234 tile3.priority = 0;
235 final Tile tile4 = new Tile();
236 tile4.intent = new Intent().setComponent(new ComponentName(testPackage, "class3"));
237 tile4.priority = -1;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800238 category.addTile(tile1);
239 category.addTile(tile2);
240 category.addTile(tile3);
241 category.addTile(tile4);
Doris Lingce311592017-03-13 12:56:10 -0700242 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
243
244 // Sort their priorities
245 mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
246 mCategoryByKeyMap);
247
248 // Verify the sorting order is not changed
Doris Lingb8d2cd42017-11-27 12:24:09 -0800249 assertThat(category.getTile(0)).isSameAs(tile1);
250 assertThat(category.getTile(1)).isSameAs(tile2);
251 assertThat(category.getTile(2)).isSameAs(tile3);
252 assertThat(category.getTile(3)).isSameAs(tile4);
Doris Lingce311592017-03-13 12:56:10 -0700253 }
254
255 @Test
256 public void sortCategories_samePriority_internalPackageTileShouldTakePrecedence() {
257 // Inject one external tile among internal tiles with same priority.
258 final String testPackage =
259 ShadowApplication.getInstance().getApplicationContext().getPackageName();
260 final String testPackage2 = "com.google.test2";
261 final String testPackage3 = "com.abcde.test3";
262 final DashboardCategory category = new DashboardCategory();
263 final Tile tile1 = new Tile();
264 tile1.intent = new Intent().setComponent(new ComponentName(testPackage2, "class1"));
265 tile1.priority = 1;
266 final Tile tile2 = new Tile();
267 tile2.intent = new Intent().setComponent(new ComponentName(testPackage, "class2"));
268 tile2.priority = 1;
269 final Tile tile3 = new Tile();
270 tile3.intent = new Intent().setComponent(new ComponentName(testPackage3, "class3"));
271 tile3.priority = 1;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800272 category.addTile(tile1);
273 category.addTile(tile2);
274 category.addTile(tile3);
Doris Lingce311592017-03-13 12:56:10 -0700275 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
276
277 // Sort their priorities
278 mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
279 mCategoryByKeyMap);
280
281 // Verify the sorting order is internal first, follow by package name ordering
Doris Lingb8d2cd42017-11-27 12:24:09 -0800282 assertThat(category.getTile(0)).isSameAs(tile2);
283 assertThat(category.getTile(1)).isSameAs(tile3);
284 assertThat(category.getTile(2)).isSameAs(tile1);
Fan Zhangfc76ab32016-11-14 09:48:06 -0800285 }
Fan Zhang5fa4af02016-11-16 12:40:35 -0800286
287 @Test
288 public void filterTiles_noDuplicate_noChange() {
289 // Create some unique tiles
290 final String testPackage =
291 ShadowApplication.getInstance().getApplicationContext().getPackageName();
292 final DashboardCategory category = new DashboardCategory();
293 final Tile tile1 = new Tile();
294 tile1.intent =
295 new Intent().setComponent(new ComponentName(testPackage, "class1"));
296 tile1.priority = 100;
297 final Tile tile2 = new Tile();
298 tile2.intent =
299 new Intent().setComponent(new ComponentName(testPackage, "class2"));
300 tile2.priority = 100;
301 final Tile tile3 = new Tile();
302 tile3.intent =
303 new Intent().setComponent(new ComponentName(testPackage, "class3"));
304 tile3.priority = 50;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800305 category.addTile(tile1);
306 category.addTile(tile2);
307 category.addTile(tile3);
Fan Zhang5fa4af02016-11-16 12:40:35 -0800308 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
309
310 mCategoryManager.filterDuplicateTiles(mCategoryByKeyMap);
311
Doris Lingb8d2cd42017-11-27 12:24:09 -0800312 assertThat(category.getTilesCount()).isEqualTo(3);
Fan Zhang5fa4af02016-11-16 12:40:35 -0800313 }
314
315 @Test
316 public void filterTiles_hasDuplicate_shouldOnlyKeepUniqueTiles() {
317 // Create tiles pointing to same intent.
318 final String testPackage =
319 ShadowApplication.getInstance().getApplicationContext().getPackageName();
320 final DashboardCategory category = new DashboardCategory();
321 final Tile tile1 = new Tile();
322 tile1.intent =
323 new Intent().setComponent(new ComponentName(testPackage, "class1"));
324 tile1.priority = 100;
325 final Tile tile2 = new Tile();
326 tile2.intent =
327 new Intent().setComponent(new ComponentName(testPackage, "class1"));
328 tile2.priority = 100;
329 final Tile tile3 = new Tile();
330 tile3.intent =
331 new Intent().setComponent(new ComponentName(testPackage, "class1"));
332 tile3.priority = 50;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800333 category.addTile(tile1);
334 category.addTile(tile2);
335 category.addTile(tile3);
Fan Zhang5fa4af02016-11-16 12:40:35 -0800336 mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);
337
338 mCategoryManager.filterDuplicateTiles(mCategoryByKeyMap);
339
Doris Lingb8d2cd42017-11-27 12:24:09 -0800340 assertThat(category.getTilesCount()).isEqualTo(1);
Fan Zhang5fa4af02016-11-16 12:40:35 -0800341 }
Fan Zhang914afbf2016-11-01 15:36:25 -0700342}