1 /* 2 * Copyright (C) 2023 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.folder 18 19 import android.R 20 import android.content.Context 21 import android.os.Process 22 import androidx.test.core.app.ApplicationProvider 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import androidx.test.filters.SmallTest 25 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation 26 import com.android.launcher3.LauncherPrefs 27 import com.android.launcher3.LauncherPrefs.Companion.get 28 import com.android.launcher3.icons.BaseIconFactory 29 import com.android.launcher3.icons.FastBitmapDrawable 30 import com.android.launcher3.icons.UserBadgeDrawable 31 import com.android.launcher3.model.data.FolderInfo 32 import com.android.launcher3.model.data.ItemInfo 33 import com.android.launcher3.util.ActivityContextWrapper 34 import com.android.launcher3.util.FlagOp 35 import com.android.launcher3.util.LauncherLayoutBuilder 36 import com.android.launcher3.util.LauncherModelHelper 37 import com.android.launcher3.util.UserIconInfo 38 import org.junit.After 39 import org.junit.Before 40 import org.junit.Test 41 import org.junit.runner.RunWith 42 43 /** Tests for [PreviewItemManager] */ 44 @SmallTest 45 @RunWith(AndroidJUnit4::class) 46 class PreviewItemManagerTest { 47 48 private lateinit var previewItemManager: PreviewItemManager 49 private lateinit var context: Context 50 private lateinit var folderItems: ArrayList<ItemInfo> 51 private lateinit var modelHelper: LauncherModelHelper 52 private lateinit var folderIcon: FolderIcon 53 54 @Before setupnull55 fun setup() { 56 getInstrumentation().runOnMainSync { 57 folderIcon = 58 FolderIcon(ActivityContextWrapper(ApplicationProvider.getApplicationContext())) 59 } 60 context = getInstrumentation().targetContext 61 previewItemManager = PreviewItemManager(folderIcon) 62 modelHelper = LauncherModelHelper() 63 modelHelper 64 .setupDefaultLayoutProvider( 65 LauncherLayoutBuilder() 66 .atWorkspace(0, 0, 1) 67 .putFolder(R.string.copy) 68 .addApp(LauncherModelHelper.TEST_PACKAGE, LauncherModelHelper.TEST_ACTIVITY) 69 .addApp(LauncherModelHelper.TEST_PACKAGE, LauncherModelHelper.TEST_ACTIVITY2) 70 .addApp(LauncherModelHelper.TEST_PACKAGE, LauncherModelHelper.TEST_ACTIVITY3) 71 .addApp(LauncherModelHelper.TEST_PACKAGE, LauncherModelHelper.TEST_ACTIVITY4) 72 .build() 73 ) 74 .loadModelSync() 75 folderItems = modelHelper.bgDataModel.collections.valueAt(0).getContents() 76 folderIcon.mInfo = modelHelper.bgDataModel.collections.valueAt(0) as FolderInfo 77 folderIcon.mInfo.getContents().addAll(folderItems) 78 79 // Use getAppContents() to "cast" contents to WorkspaceItemInfo so we can set bitmaps 80 val folderApps = modelHelper.bgDataModel.collections.valueAt(0).getAppContents() 81 // Set first icon to be themed. 82 folderApps[0] 83 .bitmap 84 .setMonoIcon( 85 folderApps[0].bitmap.icon, 86 BaseIconFactory( 87 context, 88 context.resources.configuration.densityDpi, 89 previewItemManager.mIconSize 90 ) 91 ) 92 93 // Set second icon to be non-themed. 94 folderApps[1] 95 .bitmap 96 .setMonoIcon( 97 null, 98 BaseIconFactory( 99 context, 100 context.resources.configuration.densityDpi, 101 previewItemManager.mIconSize 102 ) 103 ) 104 105 // Set third icon to be themed with badge. 106 folderApps[2] 107 .bitmap 108 .setMonoIcon( 109 folderApps[2].bitmap.icon, 110 BaseIconFactory( 111 context, 112 context.resources.configuration.densityDpi, 113 previewItemManager.mIconSize 114 ) 115 ) 116 folderApps[2].bitmap = folderApps[2].bitmap.withFlags(profileFlagOp(UserIconInfo.TYPE_WORK)) 117 118 // Set fourth icon to be non-themed with badge. 119 folderApps[3].bitmap = folderApps[3].bitmap.withFlags(profileFlagOp(UserIconInfo.TYPE_WORK)) 120 folderApps[3] 121 .bitmap 122 .setMonoIcon( 123 null, 124 BaseIconFactory( 125 context, 126 context.resources.configuration.densityDpi, 127 previewItemManager.mIconSize 128 ) 129 ) 130 } 131 @After 132 @Throws(Exception::class) tearDownnull133 fun tearDown() { 134 modelHelper.destroy() 135 } 136 137 @Test checkThemedIconWithThemingOn_iconShouldBeThemednull138 fun checkThemedIconWithThemingOn_iconShouldBeThemed() { 139 get(context).put(LauncherPrefs.THEMED_ICONS, true) 140 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 141 142 previewItemManager.setDrawable(drawingParams, folderItems[0]) 143 144 assert((drawingParams.drawable as FastBitmapDrawable).isThemed) 145 } 146 147 @Test checkThemedIconWithThemingOff_iconShouldNotBeThemednull148 fun checkThemedIconWithThemingOff_iconShouldNotBeThemed() { 149 get(context).put(LauncherPrefs.THEMED_ICONS, false) 150 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 151 152 previewItemManager.setDrawable(drawingParams, folderItems[0]) 153 154 assert(!(drawingParams.drawable as FastBitmapDrawable).isThemed) 155 } 156 157 @Test checkUnthemedIconWithThemingOn_iconShouldNotBeThemednull158 fun checkUnthemedIconWithThemingOn_iconShouldNotBeThemed() { 159 get(context).put(LauncherPrefs.THEMED_ICONS, true) 160 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 161 162 previewItemManager.setDrawable(drawingParams, folderItems[1]) 163 164 assert(!(drawingParams.drawable as FastBitmapDrawable).isThemed) 165 } 166 167 @Test checkUnthemedIconWithThemingOff_iconShouldNotBeThemednull168 fun checkUnthemedIconWithThemingOff_iconShouldNotBeThemed() { 169 get(context).put(LauncherPrefs.THEMED_ICONS, false) 170 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 171 172 previewItemManager.setDrawable(drawingParams, folderItems[1]) 173 174 assert(!(drawingParams.drawable as FastBitmapDrawable).isThemed) 175 } 176 177 @Test checkThemedIconWithBadgeWithThemingOn_iconAndBadgeShouldBeThemednull178 fun checkThemedIconWithBadgeWithThemingOn_iconAndBadgeShouldBeThemed() { 179 get(context).put(LauncherPrefs.THEMED_ICONS, true) 180 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 181 182 previewItemManager.setDrawable(drawingParams, folderItems[2]) 183 184 assert((drawingParams.drawable as FastBitmapDrawable).isThemed) 185 assert( 186 ((drawingParams.drawable as FastBitmapDrawable).badge as UserBadgeDrawable).mIsThemed 187 ) 188 } 189 190 @Test checkUnthemedIconWithBadgeWithThemingOn_badgeShouldBeThemednull191 fun checkUnthemedIconWithBadgeWithThemingOn_badgeShouldBeThemed() { 192 get(context).put(LauncherPrefs.THEMED_ICONS, true) 193 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 194 195 previewItemManager.setDrawable(drawingParams, folderItems[3]) 196 197 assert(!(drawingParams.drawable as FastBitmapDrawable).isThemed) 198 assert( 199 ((drawingParams.drawable as FastBitmapDrawable).badge as UserBadgeDrawable).mIsThemed 200 ) 201 } 202 203 @Test checkUnthemedIconWithBadgeWithThemingOff_iconAndBadgeShouldNotBeThemednull204 fun checkUnthemedIconWithBadgeWithThemingOff_iconAndBadgeShouldNotBeThemed() { 205 get(context).put(LauncherPrefs.THEMED_ICONS, false) 206 val drawingParams = PreviewItemDrawingParams(0f, 0f, 0f) 207 208 previewItemManager.setDrawable(drawingParams, folderItems[3]) 209 210 assert(!(drawingParams.drawable as FastBitmapDrawable).isThemed) 211 assert( 212 !((drawingParams.drawable as FastBitmapDrawable).badge as UserBadgeDrawable).mIsThemed 213 ) 214 } 215 profileFlagOpnull216 private fun profileFlagOp(type: Int) = 217 UserIconInfo(Process.myUserHandle(), type).applyBitmapInfoFlags(FlagOp.NO_OP) 218 } 219