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