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 package com.android.launcher3.ui;
17 
18 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
19 
20 import static com.android.launcher3.model.data.AppInfo.EMPTY_ARRAY;
21 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.when;
26 
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.Process;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.platform.test.flag.junit.SetFlagsRule;
34 
35 import androidx.test.filters.SmallTest;
36 import androidx.test.runner.AndroidJUnit4;
37 
38 import com.android.launcher3.allapps.ActivityAllAppsContainerView;
39 import com.android.launcher3.allapps.WorkProfileManager;
40 import com.android.launcher3.logging.StatsLogManager;
41 import com.android.launcher3.model.data.AppInfo;
42 import com.android.launcher3.pm.UserCache;
43 import com.android.launcher3.util.ActivityContextWrapper;
44 import com.android.launcher3.util.UserIconInfo;
45 
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 
54 import java.util.Arrays;
55 
56 @SmallTest
57 @RunWith(AndroidJUnit4.class)
58 public class ActivityAllAppsContainerViewTest {
59 
60     private static final UserHandle WORK_HANDLE = new UserHandle(13);
61     @Mock
62     private StatsLogManager mStatsLogManager;
63     @Mock
64     private UserCache mUserCache;
65     @Mock
66     private UserManager mUserManager;
67     private AppInfo[] mWorkAppInfo;
68     private ActivityAllAppsContainerView<?> mActivityAllAppsContainerView;
69     private WorkProfileManager mWorkManager;
70     private Context mContext;
71 
72     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
73 
74     @Before
setUp()75     public void setUp() {
76         MockitoAnnotations.initMocks(this);
77         mContext = new ActivityContextWrapper(getApplicationContext());
78         mActivityAllAppsContainerView = new ActivityAllAppsContainerView(mContext);
79         when(mUserCache.getUserProfiles())
80                 .thenReturn(Arrays.asList(Process.myUserHandle(), WORK_HANDLE));
81         when(mUserCache.getUserInfo(Process.myUserHandle()))
82                 .thenReturn(new UserIconInfo(Process.myUserHandle(), 0));
83         when(mUserCache.getUserInfo(WORK_HANDLE))
84                 .thenReturn(new UserIconInfo(WORK_HANDLE, 1));
85         mWorkManager = new WorkProfileManager(mUserManager, mActivityAllAppsContainerView,
86                 mStatsLogManager, mUserCache);
87         mActivityAllAppsContainerView.setWorkManager(mWorkManager);
88         ComponentName componentName = new ComponentName(mContext,
89                 "com.android.launcher3.tests.Activity" + "Gmail");
90         AppInfo gmailWorkAppInfo = new AppInfo(componentName, "Gmail", WORK_HANDLE, new Intent());
91         mWorkAppInfo = new AppInfo[]{gmailWorkAppInfo};
92     }
93 
94     @Test
testOnAppsUpdatedWithoutWorkApps_shouldShowTabsIsFalse()95     public void testOnAppsUpdatedWithoutWorkApps_shouldShowTabsIsFalse() {
96         mActivityAllAppsContainerView.getAppsStore().setApps(EMPTY_ARRAY, 0, null);
97 
98         mActivityAllAppsContainerView.onAppsUpdated();
99 
100         assertThat(mActivityAllAppsContainerView.shouldShowTabs()).isEqualTo(false);
101     }
102 
103     @Test
testOnAppsUpdatedWithWorkApps_shouldShowTabsIsTrue()104     public void testOnAppsUpdatedWithWorkApps_shouldShowTabsIsTrue() {
105         mActivityAllAppsContainerView.getAppsStore().setApps(mWorkAppInfo, 0, null);
106 
107         mActivityAllAppsContainerView.onAppsUpdated();
108 
109         assertThat(mActivityAllAppsContainerView.shouldShowTabs()).isEqualTo(true);
110     }
111 
112     @Test
testWorkProfileEnabled_requestQuietModeCalledCorrectly()113     public void testWorkProfileEnabled_requestQuietModeCalledCorrectly() throws Exception {
114         /* Setup */
115         when(mUserManager.requestQuietModeEnabled(false, WORK_HANDLE))
116                 .thenReturn(true);
117 
118         /* Execution */
119         mWorkManager.setWorkProfileEnabled(true);
120 
121         /* Assertion */
122         awaitTasksCompleted();
123         Mockito.verify(mUserManager).requestQuietModeEnabled(false, WORK_HANDLE);
124     }
125 
awaitTasksCompleted()126     private static void awaitTasksCompleted() throws Exception {
127         UI_HELPER_EXECUTOR.submit(() -> null).get();
128     }
129 }
130