1package com.android.launcher3.model; 2 3import android.support.test.runner.AndroidJUnit4; 4 5import com.android.launcher3.AppInfo; 6import com.android.launcher3.ItemInfo; 7import com.android.launcher3.ShortcutInfo; 8 9import org.junit.Before; 10import org.junit.Test; 11import org.junit.runner.RunWith; 12 13import java.util.Arrays; 14import java.util.HashSet; 15 16import static org.junit.Assert.assertEquals; 17import static org.junit.Assert.assertFalse; 18import static org.junit.Assert.assertNotNull; 19import static org.junit.Assert.assertNotSame; 20import static org.junit.Assert.assertNull; 21 22/** 23 * Tests for {@link CacheDataUpdatedTask} 24 */ 25@RunWith(AndroidJUnit4.class) 26public class CacheDataUpdatedTaskTest extends BaseModelUpdateTaskTestCase { 27 28 private static final String NEW_LABEL_PREFIX = "new-label-"; 29 30 @Before 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 39 private CacheDataUpdatedTask newTask(int op, String... pkg) { 40 return new CacheDataUpdatedTask(op, myUser, new HashSet<>(Arrays.asList(pkg))); 41 } 42 43 @Test 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 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 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 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