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 package com.android.launcher3.util;
17 
18 import static com.android.launcher3.Flags.FLAG_ENABLE_SUPPORT_FOR_ARCHIVING;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.LauncherApps;
29 import android.content.pm.PackageManager;
30 import android.os.UserHandle;
31 import android.platform.test.annotations.RequiresFlagsEnabled;
32 import android.platform.test.flag.junit.CheckFlagsRule;
33 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 import androidx.test.filters.SmallTest;
37 import androidx.test.platform.app.InstrumentationRegistry;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 
45 /** Unit tests for {@link PackageManagerHelper}. */
46 @SmallTest
47 @RunWith(AndroidJUnit4.class)
48 public final class PackageManagerHelperTest {
49     @Rule
50     public ExpectedException exception = ExpectedException.none();
51 
52     @Rule
53     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
54 
55     private static final String TEST_PACKAGE = "com.android.test.package";
56     private static final int TEST_USER = 2;
57 
58     private Context mContext;
59     private LauncherApps mLauncherApps;
60     private PackageManagerHelper mPackageManagerHelper;
61 
62     @Before
setup()63     public void setup() {
64         mContext = mock(Context.class);
65         mLauncherApps = mock(LauncherApps.class);
66         when(mContext.getSystemService(eq(LauncherApps.class))).thenReturn(mLauncherApps);
67         when(mContext.getResources()).thenReturn(
68                 InstrumentationRegistry.getInstrumentation().getTargetContext().getResources());
69         mPackageManagerHelper = new PackageManagerHelper(mContext);
70     }
71 
72     @Test
73     @RequiresFlagsEnabled(FLAG_ENABLE_SUPPORT_FOR_ARCHIVING)
getApplicationInfo_archivedApp_appInfoIsNotNull()74     public void getApplicationInfo_archivedApp_appInfoIsNotNull()
75             throws PackageManager.NameNotFoundException {
76         ApplicationInfo applicationInfo = new ApplicationInfo();
77         applicationInfo.isArchived = true;
78         when(mLauncherApps.getApplicationInfo(TEST_PACKAGE, 0 /* flags */,
79                 UserHandle.of(TEST_USER)))
80                 .thenReturn(applicationInfo);
81 
82         assertThat(mPackageManagerHelper.getApplicationInfo(TEST_PACKAGE, UserHandle.of(TEST_USER),
83                 0 /* flags */))
84                 .isNotNull();
85     }
86 }
87