1 /*
2  * Copyright (C) 2021 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 com.android.launcher3.LauncherState.ALL_APPS;
19 import static com.android.launcher3.LauncherState.NORMAL;
20 import static com.android.launcher3.allapps.AllAppsStore.DEFER_UPDATES_TEST;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.util.Log;
26 import android.view.View;
27 
28 import com.android.launcher3.R;
29 import com.android.launcher3.allapps.AllAppsContainerView;
30 import com.android.launcher3.allapps.AllAppsPagedView;
31 import com.android.launcher3.allapps.WorkAdapterProvider;
32 import com.android.launcher3.allapps.WorkEduCard;
33 import com.android.launcher3.allapps.WorkProfileManager;
34 import com.android.launcher3.tapl.LauncherInstrumentation;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 
40 import java.util.Objects;
41 
42 public class WorkProfileTest extends AbstractLauncherUiTest {
43 
44     private static final int WORK_PAGE = AllAppsContainerView.AdapterHolder.WORK;
45 
46     private int mProfileUserId;
47 
48     @Before
49     @Override
setUp()50     public void setUp() throws Exception {
51         super.setUp();
52         String output =
53                 mDevice.executeShellCommand(
54                         "pm create-user --profileOf 0 --managed TestProfile");
55         Log.d("b/203817455", "pm create-user; output: " + output);
56         assertTrue("Failed to create work profile", output.startsWith("Success"));
57 
58         String[] tokens = output.split("\\s+");
59         mProfileUserId = Integer.parseInt(tokens[tokens.length - 1]);
60         mDevice.executeShellCommand("am start-user " + mProfileUserId);
61 
62         mDevice.pressHome();
63         waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
64         waitForStateTransitionToEnd("Launcher internal state didn't switch to Normal",
65                 () -> NORMAL);
66         waitForResumed("Launcher internal state is still Background");
67         executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
68         waitForStateTransitionToEnd("Launcher internal state didn't switch to All Apps",
69                 () -> ALL_APPS);
70     }
71 
72     @After
removeWorkProfile()73     public void removeWorkProfile() throws Exception {
74         mDevice.executeShellCommand("pm remove-user " + mProfileUserId);
75     }
76 
77     @After
resumeAppStoreUpdate()78     public void resumeAppStoreUpdate() {
79         executeOnLauncher(launcher -> {
80             if (launcher == null || launcher.getAppsView() == null) {
81                 return;
82             }
83             launcher.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST);
84         });
85     }
86 
waitForWorkTabSetup()87     private void waitForWorkTabSetup() {
88         waitForLauncherCondition("Work tab not setup", launcher -> {
89             if (launcher.getAppsView().getContentView() instanceof AllAppsPagedView) {
90                 launcher.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
91                 return true;
92             }
93             return false;
94         }, LauncherInstrumentation.WAIT_TIME_MS);
95     }
96 
97     @Test
workTabExists()98     public void workTabExists() {
99         waitForLauncherCondition("Personal tab is missing",
100                 launcher -> launcher.getAppsView().isPersonalTabVisible(),
101                 LauncherInstrumentation.WAIT_TIME_MS);
102         waitForLauncherCondition("Work tab is missing",
103                 launcher -> launcher.getAppsView().isWorkTabVisible(),
104                 LauncherInstrumentation.WAIT_TIME_MS);
105     }
106 
107     @Test
toggleWorks()108     public void toggleWorks() {
109         waitForWorkTabSetup();
110 
111         executeOnLauncher(launcher -> {
112             AllAppsPagedView pagedView = (AllAppsPagedView) launcher.getAppsView().getContentView();
113             pagedView.setCurrentPage(WORK_PAGE);
114         });
115 
116         WorkProfileManager manager = getFromLauncher(l -> l.getAppsView().getWorkManager());
117 
118 
119         waitForLauncherCondition("work profile initial state check failed", launcher ->
120                         manager.getWorkModeSwitch() != null
121                                 && manager.getCurrentState() == WorkProfileManager.STATE_ENABLED
122                                 && manager.getWorkModeSwitch().isEnabled(),
123                 LauncherInstrumentation.WAIT_TIME_MS);
124 
125         //start work profile toggle OFF test
126         executeOnLauncher(l -> l.getAppsView().getWorkManager().getWorkModeSwitch().performClick());
127 
128         waitForLauncherCondition("Work profile toggle OFF failed", launcher -> {
129             manager.reset(); // pulls current state from system
130             return manager.getCurrentState() == WorkProfileManager.STATE_DISABLED;
131         }, LauncherInstrumentation.WAIT_TIME_MS);
132 
133         // start work profile toggle ON test
134         executeOnLauncher(l -> {
135             AllAppsContainerView allApps = l.getAppsView();
136             assertEquals("Work tab is not focused", allApps.getCurrentPage(), WORK_PAGE);
137             View workPausedCard = allApps.getActiveRecyclerView().findViewHolderForAdapterPosition(
138                     0).itemView;
139             workPausedCard.findViewById(R.id.enable_work_apps).performClick();
140         });
141         waitForLauncherCondition("Work profile toggle ON failed", launcher -> {
142             manager.reset(); // pulls current state from system
143             return manager.getCurrentState() == WorkProfileManager.STATE_ENABLED;
144         }, LauncherInstrumentation.WAIT_TIME_MS);
145 
146     }
147 
148     @Test
testEdu()149     public void testEdu() {
150         waitForWorkTabSetup();
151         executeOnLauncher(l -> {
152             l.getSharedPrefs().edit().putInt(WorkAdapterProvider.KEY_WORK_EDU_STEP, 0).commit();
153             ((AllAppsPagedView) l.getAppsView().getContentView()).setCurrentPage(WORK_PAGE);
154             l.getAppsView().getWorkManager().reset();
155         });
156 
157         waitForLauncherCondition("Work profile education not shown",
158                 l -> l.getAppsView().getActiveRecyclerView()
159                         .findViewHolderForAdapterPosition(0).itemView instanceof WorkEduCard,
160                 LauncherInstrumentation.WAIT_TIME_MS);
161     }
162 }
163