• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.model;
2 
3 import android.support.test.runner.AndroidJUnit4;
4 
5 import com.android.launcher3.AppInfo;
6 import com.android.launcher3.ItemInfo;
7 import com.android.launcher3.ShortcutInfo;
8 
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import java.util.Arrays;
14 import java.util.HashSet;
15 
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNotSame;
20 import static org.junit.Assert.assertNull;
21 
22 /**
23  * Tests for {@link CacheDataUpdatedTask}
24  */
25 @RunWith(AndroidJUnit4.class)
26 public class CacheDataUpdatedTaskTest extends BaseModelUpdateTaskTestCase {
27 
28     private static final String NEW_LABEL_PREFIX = "new-label-";
29 
30     @Before
initData()31     public void initData() throws Exception {
32         initializeData("cache_data_updated_task_data");
33         // Add dummy entries in the cache to simulate update
34         for (ItemInfo info : bgDataModel.itemsIdMap) {
35             iconCache.addCache(info.getTargetComponent(), NEW_LABEL_PREFIX + info.id);
36         }
37     }
38 
newTask(int op, String... pkg)39     private CacheDataUpdatedTask newTask(int op, String... pkg) {
40         return new CacheDataUpdatedTask(op, myUser, new HashSet<>(Arrays.asList(pkg)));
41     }
42 
43     @Test
testCacheUpdate_update_apps()44     public void testCacheUpdate_update_apps() throws Exception {
45         // Clear all icons from apps list so that its easy to check what was updated
46         for (AppInfo info : allAppsList.data) {
47             info.iconBitmap = null;
48         }
49 
50         executeTaskForTest(newTask(CacheDataUpdatedTask.OP_CACHE_UPDATE, "app1"));
51 
52         // Verify that only the app icons of app1 (id 1 & 2) are updated. Custom shortcut (id 7)
53         // is not updated
54         verifyUpdate(1L, 2L);
55 
56         // Verify that only app1 var updated in allAppsList
57         assertFalse(allAppsList.data.isEmpty());
58         for (AppInfo info : allAppsList.data) {
59             if (info.componentName.getPackageName().equals("app1")) {
60                 assertNotNull(info.iconBitmap);
61             } else {
62                 assertNull(info.iconBitmap);
63             }
64         }
65     }
66 
67     @Test
testSessionUpdate_ignores_normal_apps()68     public void testSessionUpdate_ignores_normal_apps() throws Exception {
69         executeTaskForTest(newTask(CacheDataUpdatedTask.OP_SESSION_UPDATE, "app1"));
70 
71         // app1 has no restored shortcuts. Verify that nothing was updated.
72         verifyUpdate();
73     }
74 
75     @Test
testSessionUpdate_updates_pending_apps()76     public void testSessionUpdate_updates_pending_apps() throws Exception {
77         executeTaskForTest(newTask(CacheDataUpdatedTask.OP_SESSION_UPDATE, "app3"));
78 
79         // app3 has only restored apps (id 5, 6) and shortcuts (id 9). Verify that only apps were
80         // were updated
81         verifyUpdate(5L, 6L);
82     }
83 
verifyUpdate(Long... idsUpdated)84     private void verifyUpdate(Long... idsUpdated) {
85         HashSet<Long> updates = new HashSet<>(Arrays.asList(idsUpdated));
86         for (ItemInfo info : bgDataModel.itemsIdMap) {
87             if (updates.contains(info.id)) {
88                 assertEquals(NEW_LABEL_PREFIX + info.id, info.title);
89                 assertNotNull(((ShortcutInfo) info).iconBitmap);
90             } else {
91                 assertNotSame(NEW_LABEL_PREFIX + info.id, info.title);
92                 assertNull(((ShortcutInfo) info).iconBitmap);
93             }
94         }
95     }
96 }
97