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.pm 18 19 import android.os.Process.myUserHandle 20 import android.os.UserHandle 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import androidx.test.platform.app.InstrumentationRegistry 23 import com.android.launcher3.util.Executors.MODEL_EXECUTOR 24 import com.android.launcher3.util.LauncherModelHelper 25 import com.android.launcher3.util.TestUtil 26 import com.android.launcher3.util.UserIconInfo 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.After 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 @RunWith(AndroidJUnit4::class) 34 class UserCacheTest { 35 36 private val launcherModelHelper = LauncherModelHelper() 37 private val sandboxContext = launcherModelHelper.sandboxContext 38 private lateinit var userCache: UserCache 39 40 @Before setupnull41 fun setup() { 42 userCache = UserCache.getInstance(sandboxContext) 43 } 44 45 @After teardownnull46 fun teardown() { 47 launcherModelHelper.destroy() 48 } 49 50 @Test getBadgeDrawable only returns a UserBadgeDrawable given a user in the cachenull51 fun `getBadgeDrawable only returns a UserBadgeDrawable given a user in the cache`() { 52 // Given 53 val expectedIconInfo = UserIconInfo(myUserHandle(), UserIconInfo.TYPE_WORK) 54 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 55 userCache.putToCache(myUserHandle(), expectedIconInfo) 56 } 57 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 58 // When 59 val actualDrawable = UserCache.getBadgeDrawable(sandboxContext, myUserHandle()) 60 val unexpectedDrawable = UserCache.getBadgeDrawable(sandboxContext, UserHandle(66)) 61 // Then 62 assertThat(actualDrawable).isNotNull() 63 assertThat(unexpectedDrawable).isNull() 64 } 65 66 @Test getPreInstallApps returns list of pre installed apps given a usernull67 fun `getPreInstallApps returns list of pre installed apps given a user`() { 68 // Given 69 val expectedApps = listOf("Google") 70 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 71 userCache.putToPreInstallCache(myUserHandle(), expectedApps) 72 } 73 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 74 // When 75 val actualApps = userCache.getPreInstallApps(myUserHandle()) 76 // Then 77 assertThat(actualApps).isEqualTo(expectedApps) 78 } 79 80 @Test getUserProfiles returns copy of UserCache profilesnull81 fun `getUserProfiles returns copy of UserCache profiles`() { 82 // Given 83 val expectedProfiles = listOf(myUserHandle()) 84 val expectedIconInfo = UserIconInfo(myUserHandle(), UserIconInfo.TYPE_MAIN) 85 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 86 userCache.putToCache(myUserHandle(), expectedIconInfo) 87 } 88 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 89 // When 90 val actualProfiles = userCache.userProfiles 91 // Then 92 assertThat(actualProfiles).isEqualTo(expectedProfiles) 93 } 94 95 @Test getUserForSerialNumber returns user key matching given entry serial numbernull96 fun `getUserForSerialNumber returns user key matching given entry serial number`() { 97 // Given 98 val expectedSerial = 42L 99 val expectedProfile = UserHandle(42) 100 val expectedIconInfo = UserIconInfo(myUserHandle(), UserIconInfo.TYPE_MAIN, expectedSerial) 101 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 102 userCache.putToCache(expectedProfile, expectedIconInfo) 103 } 104 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 105 // When 106 val actualProfile = userCache.getUserForSerialNumber(expectedSerial) 107 // Then 108 assertThat(actualProfile).isEqualTo(expectedProfile) 109 } 110 111 @Test getUserInfo returns cached UserIconInfo given user keynull112 fun `getUserInfo returns cached UserIconInfo given user key`() { 113 // Given 114 val expectedProfile = UserHandle(1) 115 val expectedIconInfo = UserIconInfo(myUserHandle(), UserIconInfo.TYPE_WORK) 116 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 117 userCache.putToCache(expectedProfile, expectedIconInfo) 118 } 119 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 120 // When 121 val actualIconInfo = userCache.getUserInfo(expectedProfile) 122 // Then 123 assertThat(actualIconInfo).isEqualTo(expectedIconInfo) 124 } 125 126 @Test getSerialNumberForUser returns cached UserIconInfo serial number given user keynull127 fun `getSerialNumberForUser returns cached UserIconInfo serial number given user key`() { 128 // Given 129 val expectedSerial = 42L 130 val expectedProfile = UserHandle(1) 131 val expectedIconInfo = UserIconInfo(myUserHandle(), UserIconInfo.TYPE_WORK, expectedSerial) 132 TestUtil.runOnExecutorSync(MODEL_EXECUTOR) { 133 userCache.putToCache(expectedProfile, expectedIconInfo) 134 } 135 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 136 // When 137 val actualSerial = userCache.getSerialNumberForUser(expectedProfile) 138 // Then 139 assertThat(actualSerial).isEqualTo(expectedSerial) 140 } 141 } 142