• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Context
20 import android.content.pm.ApplicationInfo
21 import android.content.pm.ApplicationInfo.FLAG_EXTERNAL_STORAGE
22 import android.content.pm.ApplicationInfo.FLAG_INSTALLED
23 import android.content.pm.ApplicationInfo.FLAG_SUSPENDED
24 import android.content.pm.ApplicationInfo.FLAG_SYSTEM
25 import android.content.pm.LauncherApps
26 import android.os.UserHandle
27 import android.platform.test.annotations.EnableFlags
28 import android.platform.test.flag.junit.SetFlagsRule
29 import androidx.test.ext.junit.runners.AndroidJUnit4
30 import androidx.test.filters.SmallTest
31 import com.android.launcher3.Flags.FLAG_ENABLE_SUPPORT_FOR_ARCHIVING
32 import org.junit.Assert.assertFalse
33 import org.junit.Assert.assertNotNull
34 import org.junit.Assert.assertNull
35 import org.junit.Assert.assertTrue
36 import org.junit.Before
37 import org.junit.Rule
38 import org.junit.Test
39 import org.junit.runner.RunWith
40 import org.mockito.Mockito
41 import org.mockito.kotlin.any
42 import org.mockito.kotlin.eq
43 import org.mockito.kotlin.whenever
44 
45 /** Unit tests for {@link ApplicationInfoWrapper}. */
46 @SmallTest
47 @RunWith(AndroidJUnit4::class)
48 class ApplicationInfoWrapperTest {
49 
50     @get:Rule val setFlagsRule = SetFlagsRule(SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT)
51 
52     private lateinit var context: Context
53     private lateinit var launcherApps: LauncherApps
54 
55     @Before
setupnull56     fun setup() {
57         context = Mockito.mock(Context::class.java)
58         launcherApps = Mockito.mock(LauncherApps::class.java)
59         whenever(context.getSystemService(eq(LauncherApps::class.java))).thenReturn(launcherApps)
60     }
61 
62     @Test
63     @EnableFlags(FLAG_ENABLE_SUPPORT_FOR_ARCHIVING)
archivedApp_appInfoIsNotNullnull64     fun archivedApp_appInfoIsNotNull() {
65         val applicationInfo = ApplicationInfo()
66         applicationInfo.isArchived = true
67         whenever(launcherApps.getApplicationInfo(eq(TEST_PACKAGE), any(), eq(TEST_USER)))
68             .thenReturn(applicationInfo)
69 
70         val wrapper = ApplicationInfoWrapper(context, TEST_PACKAGE, TEST_USER)
71         assertNotNull(wrapper.getInfo())
72         assertTrue(wrapper.isArchived())
73         assertFalse(wrapper.isInstalled())
74     }
75 
76     @Test
notInstalledApp_nullAppInfonull77     fun notInstalledApp_nullAppInfo() {
78         val applicationInfo = ApplicationInfo()
79         whenever(launcherApps.getApplicationInfo(eq(TEST_PACKAGE), any(), eq(TEST_USER)))
80             .thenReturn(applicationInfo)
81 
82         val wrapper = ApplicationInfoWrapper(context, TEST_PACKAGE, TEST_USER)
83         assertNull(wrapper.getInfo())
84         assertFalse(wrapper.isInstalled())
85     }
86 
87     @Test
appInfo_suspendednull88     fun appInfo_suspended() {
89         val wrapper =
90             ApplicationInfoWrapper(
91                 ApplicationInfo().apply { flags = FLAG_INSTALLED.or(FLAG_SUSPENDED) }
92             )
93         assertTrue(wrapper.isSuspended())
94     }
95 
96     @Test
appInfo_notSuspendednull97     fun appInfo_notSuspended() {
98         val wrapper = ApplicationInfoWrapper(ApplicationInfo())
99         assertFalse(wrapper.isSuspended())
100     }
101 
102     @Test
appInfo_systemnull103     fun appInfo_system() {
104         val wrapper = ApplicationInfoWrapper(ApplicationInfo().apply { flags = FLAG_SYSTEM })
105         assertTrue(wrapper.isSystem())
106     }
107 
108     @Test
appInfo_notSystemnull109     fun appInfo_notSystem() {
110         val wrapper = ApplicationInfoWrapper(ApplicationInfo())
111         assertFalse(wrapper.isSystem())
112     }
113 
114     @Test
appInfo_onSDCardnull115     fun appInfo_onSDCard() {
116         val wrapper =
117             ApplicationInfoWrapper(ApplicationInfo().apply { flags = FLAG_EXTERNAL_STORAGE })
118         assertTrue(wrapper.isOnSdCard())
119     }
120 
121     @Test
appInfo_notOnSDCardnull122     fun appInfo_notOnSDCard() {
123         val wrapper = ApplicationInfoWrapper(ApplicationInfo())
124         assertFalse(wrapper.isOnSdCard())
125     }
126 
127     companion object {
128         const val TEST_PACKAGE = "com.android.test.package"
129         private val TEST_USER = UserHandle.of(3)
130     }
131 }
132