1 /* 2 * Copyright (C) 2024 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 17 package com.android.launcher3.util 18 19 import android.content.ComponentName 20 import android.content.Intent 21 import android.os.UserHandle 22 import androidx.test.ext.junit.runners.AndroidJUnit4 23 import androidx.test.filters.SmallTest 24 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT 25 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_FOLDER 26 import com.android.launcher3.model.data.FolderInfo 27 import com.android.launcher3.model.data.ItemInfo 28 import com.android.launcher3.shortcuts.ShortcutKey 29 import com.android.launcher3.util.ItemInfoMatcher.ofShortcutKeys 30 import com.google.common.truth.Truth.assertThat 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.mockito.kotlin.spy 34 import org.mockito.kotlin.whenever 35 36 @SmallTest 37 @RunWith(AndroidJUnit4::class) 38 class ItemInfoMatcherTest { 39 40 @Test ofUser returns Predicate for ItemInfo containing given UserHandlenull41 fun `ofUser returns Predicate for ItemInfo containing given UserHandle`() { 42 val expectedItemInfo = ItemInfo().apply { user = UserHandle(11) } 43 val unexpectedItemInfo = ItemInfo().apply { user = UserHandle(0) } 44 val itemInfoStream = listOf(expectedItemInfo, unexpectedItemInfo).stream() 45 46 val predicate = ItemInfoMatcher.ofUser(UserHandle(11)) 47 val actualResults = itemInfoStream.filter(predicate).toList() 48 49 assertThat(actualResults).containsExactly(expectedItemInfo) 50 } 51 52 @Test ofComponents returns Predicate for ItemInfo containing target Component and UserHandlenull53 fun `ofComponents returns Predicate for ItemInfo containing target Component and UserHandle`() { 54 // Given 55 val expectedUserHandle = UserHandle(0) 56 val expectedComponentName = ComponentName("expectedPackage", "expectedClass") 57 val expectedItemInfo = spy(ItemInfo()) 58 expectedItemInfo.user = expectedUserHandle 59 whenever(expectedItemInfo.targetComponent).thenReturn(expectedComponentName) 60 61 val unexpectedComponentName = ComponentName("unexpectedPackage", "unexpectedClass") 62 val unexpectedItemInfo1 = spy(ItemInfo()) 63 unexpectedItemInfo1.user = expectedUserHandle 64 whenever(unexpectedItemInfo1.targetComponent).thenReturn(unexpectedComponentName) 65 66 val unexpectedItemInfo2 = spy(ItemInfo()) 67 unexpectedItemInfo2.user = UserHandle(10) 68 whenever(unexpectedItemInfo2.targetComponent).thenReturn(expectedComponentName) 69 70 val itemInfoStream = 71 listOf(expectedItemInfo, unexpectedItemInfo1, unexpectedItemInfo2).stream() 72 73 // When 74 val predicate = 75 ItemInfoMatcher.ofComponents(hashSetOf(expectedComponentName), expectedUserHandle) 76 val actualResults = itemInfoStream.filter(predicate).toList() 77 78 // Then 79 assertThat(actualResults).containsExactly(expectedItemInfo) 80 } 81 82 @Test ofPackages returns Predicate for ItemInfo containing UserHandle and target packagenull83 fun `ofPackages returns Predicate for ItemInfo containing UserHandle and target package`() { 84 // Given 85 val expectedUserHandle = UserHandle(0) 86 val expectedPackage = "expectedPackage" 87 val expectedComponentName = ComponentName(expectedPackage, "expectedClass") 88 val expectedItemInfo = spy(ItemInfo()) 89 expectedItemInfo.user = expectedUserHandle 90 whenever(expectedItemInfo.targetComponent).thenReturn(expectedComponentName) 91 92 val unexpectedPackage = "unexpectedPackage" 93 val unexpectedComponentName = ComponentName(unexpectedPackage, "unexpectedClass") 94 val unexpectedItemInfo1 = spy(ItemInfo()) 95 unexpectedItemInfo1.user = expectedUserHandle 96 whenever(unexpectedItemInfo1.targetComponent).thenReturn(unexpectedComponentName) 97 98 val unexpectedItemInfo2 = spy(ItemInfo()) 99 unexpectedItemInfo2.user = UserHandle(10) 100 whenever(unexpectedItemInfo2.targetComponent).thenReturn(expectedComponentName) 101 102 val itemInfoStream = 103 listOf(expectedItemInfo, unexpectedItemInfo1, unexpectedItemInfo2).stream() 104 105 // When 106 val predicate = ItemInfoMatcher.ofPackages(setOf(expectedPackage), expectedUserHandle) 107 val actualResults = itemInfoStream.filter(predicate).toList() 108 109 // Then 110 assertThat(actualResults).containsExactly(expectedItemInfo) 111 } 112 113 @Test ofShortcutKeys returns Predicate for Deep Shortcut Info containing given ShortcutKeynull114 fun `ofShortcutKeys returns Predicate for Deep Shortcut Info containing given ShortcutKey`() { 115 // Given 116 val expectedItemInfo = spy(ItemInfo()) 117 expectedItemInfo.itemType = ITEM_TYPE_DEEP_SHORTCUT 118 val expectedIntent = 119 Intent().apply { 120 putExtra("shortcut_id", "expectedShortcut") 121 `package` = "expectedPackage" 122 } 123 whenever(expectedItemInfo.intent).thenReturn(expectedIntent) 124 125 val unexpectedIntent = 126 Intent().apply { 127 putExtra("shortcut_id", "unexpectedShortcut") 128 `package` = "unexpectedPackage" 129 } 130 val unexpectedItemInfo = spy(ItemInfo()) 131 unexpectedItemInfo.itemType = ITEM_TYPE_DEEP_SHORTCUT 132 whenever(unexpectedItemInfo.intent).thenReturn(unexpectedIntent) 133 134 val itemInfoStream = listOf(expectedItemInfo, unexpectedItemInfo).stream() 135 val expectedShortcutKey = ShortcutKey.fromItemInfo(expectedItemInfo) 136 137 // When 138 val predicate = ItemInfoMatcher.ofShortcutKeys(setOf(expectedShortcutKey)) 139 val actualResults = itemInfoStream.filter(predicate).toList() 140 141 // Then 142 assertThat(actualResults).containsExactly(expectedItemInfo) 143 } 144 145 @Test forFolderMatch returns Predicate to match against children within Folder ItemInfonull146 fun `forFolderMatch returns Predicate to match against children within Folder ItemInfo`() { 147 // Given 148 val expectedItemInfo = spy(FolderInfo()) 149 expectedItemInfo.itemType = ITEM_TYPE_FOLDER 150 val expectedIntent = 151 Intent().apply { 152 putExtra("shortcut_id", "expectedShortcut") 153 `package` = "expectedPackage" 154 } 155 val expectedChildInfo = spy(ItemInfo()) 156 expectedChildInfo.itemType = ITEM_TYPE_DEEP_SHORTCUT 157 whenever(expectedChildInfo.intent).thenReturn(expectedIntent) 158 whenever(expectedItemInfo.getContents()).thenReturn(arrayListOf(expectedChildInfo)) 159 160 val unexpectedItemInfo = spy(FolderInfo()) 161 unexpectedItemInfo.itemType = ITEM_TYPE_FOLDER 162 163 val itemInfoStream = listOf(expectedItemInfo, unexpectedItemInfo).stream() 164 val expectedShortcutKey = ShortcutKey.fromItemInfo(expectedChildInfo) 165 166 // When 167 val predicate = ItemInfoMatcher.forFolderMatch(ofShortcutKeys(setOf(expectedShortcutKey))) 168 val actualResults = itemInfoStream.filter(predicate).toList() 169 170 // Then 171 assertThat(actualResults).containsExactly(expectedItemInfo) 172 } 173 174 @Test ofItemIds returns Predicate to match ItemInfo that contains given idsnull175 fun `ofItemIds returns Predicate to match ItemInfo that contains given ids`() { 176 // Given 177 val expectedItemInfo = spy(ItemInfo()) 178 expectedItemInfo.id = 1 179 180 val unexpectedItemInfo = spy(ItemInfo()) 181 unexpectedItemInfo.id = 2 182 183 val itemInfoStream = listOf(expectedItemInfo, unexpectedItemInfo).stream() 184 185 // When 186 val expectedIds = IntSet().apply { add(1) } 187 val predicate = ItemInfoMatcher.ofItemIds(expectedIds) 188 val actualResults = itemInfoStream.filter(predicate).toList() 189 190 // Then 191 assertThat(actualResults).containsExactly(expectedItemInfo) 192 } 193 194 @Test ofItems returns Predicate matching against provided ItemInfonull195 fun `ofItems returns Predicate matching against provided ItemInfo`() { 196 // Given 197 val expectedItemInfo = spy(ItemInfo()) 198 expectedItemInfo.id = 1 199 200 val unexpectedItemInfo = spy(ItemInfo()) 201 unexpectedItemInfo.id = 2 202 203 val itemInfoStream = listOf(expectedItemInfo, unexpectedItemInfo).stream() 204 205 // When 206 val expectedItems = setOf(expectedItemInfo) 207 val predicate = ItemInfoMatcher.ofItems(expectedItems) 208 val actualResults = itemInfoStream.filter(predicate).toList() 209 210 // Then 211 assertThat(actualResults).containsExactly(expectedItemInfo) 212 } 213 } 214