1 /* 2 * Copyright 2018, 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.os.Process; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.Log; 29 import android.widget.TextView; 30 31 import androidx.test.filters.LargeTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.launcher3.R; 35 import com.android.launcher3.allapps.AllAppsContainerView; 36 import com.android.launcher3.allapps.AllAppsPagedView; 37 import com.android.launcher3.allapps.WorkModeSwitch; 38 import com.android.launcher3.dragndrop.DragLayer; 39 import com.android.launcher3.tapl.LauncherInstrumentation; 40 import com.android.launcher3.testing.TestProtocol; 41 import com.android.launcher3.views.WorkEduView; 42 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Ignore; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 49 import java.util.List; 50 import java.util.Objects; 51 import java.util.concurrent.atomic.AtomicInteger; 52 53 @LargeTest 54 @RunWith(AndroidJUnit4.class) 55 public class WorkTabTest extends AbstractLauncherUiTest { 56 57 private int mProfileUserId; 58 59 private static final int WORK_PAGE = AllAppsContainerView.AdapterHolder.WORK; 60 61 @Before 62 @Override setUp()63 public void setUp() throws Exception { 64 super.setUp(); 65 String output = 66 mDevice.executeShellCommand( 67 "pm create-user --profileOf 0 --managed TestProfile"); 68 assertTrue("Failed to create work profile", output.startsWith("Success")); 69 70 String[] tokens = output.split("\\s+"); 71 mProfileUserId = Integer.parseInt(tokens[tokens.length - 1]); 72 Log.d(TestProtocol.WORK_PROFILE_REMOVED, "Created new user uid" + mProfileUserId); 73 mDevice.executeShellCommand("am start-user " + mProfileUserId); 74 } 75 76 @After removeWorkProfile()77 public void removeWorkProfile() throws Exception { 78 Log.d(TestProtocol.WORK_PROFILE_REMOVED, "(teardown) removing uid" + mProfileUserId, 79 new Exception()); 80 mDevice.executeShellCommand("pm remove-user " + mProfileUserId); 81 } 82 83 @After resumeAppStoreUpdate()84 public void resumeAppStoreUpdate() { 85 executeOnLauncher(launcher -> { 86 if (launcher == null || launcher.getAppsView() == null) { 87 return; 88 } 89 launcher.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST); 90 Log.d(TestProtocol.WORK_PROFILE_REMOVED, "resuming AppStore updates"); 91 }); 92 } 93 94 @Ignore("b/182844465") 95 @Test workTabExists()96 public void workTabExists() { 97 mDevice.pressHome(); 98 waitForLauncherCondition("Launcher didn't start", Objects::nonNull); 99 executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS)); 100 waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS); 101 waitForLauncherCondition("Personal tab is missing", 102 launcher -> launcher.getAppsView().isPersonalTabVisible(), 60000); 103 waitForLauncherCondition("Work tab is missing", 104 launcher -> launcher.getAppsView().isWorkTabVisible(), 60000); 105 } 106 107 @Ignore("b/182844465") 108 @Test toggleWorks()109 public void toggleWorks() { 110 mDevice.pressHome(); 111 waitForLauncherCondition("Launcher didn't start", Objects::nonNull); 112 executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS)); 113 waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS); 114 getOnceNotNull("Apps view did not bind", 115 launcher -> launcher.getAppsView().getWorkModeSwitch(), 60000); 116 117 UserManager userManager = getFromLauncher(l -> l.getSystemService(UserManager.class)); 118 assertEquals(2, userManager.getUserProfiles().size()); 119 UserHandle workProfile = getFromLauncher(l -> { 120 UserHandle myHandle = Process.myUserHandle(); 121 List<UserHandle> userProfiles = userManager.getUserProfiles(); 122 return userProfiles.get(0) == myHandle ? userProfiles.get(1) : userProfiles.get(0); 123 }); 124 125 waitForLauncherCondition("work profile can't be turned off", 126 l -> userManager.requestQuietModeEnabled(true, workProfile)); 127 128 assertTrue(userManager.isQuietModeEnabled(workProfile)); 129 executeOnLauncher(launcher -> { 130 WorkModeSwitch wf = launcher.getAppsView().getWorkModeSwitch(); 131 ((AllAppsPagedView) launcher.getAppsView().getContentView()).snapToPageImmediately( 132 AllAppsContainerView.AdapterHolder.WORK); 133 wf.toggle(); 134 }); 135 waitForLauncherCondition("Work toggle did not work", 136 l -> l.getSystemService(UserManager.class).isQuietModeEnabled(workProfile)); 137 } 138 139 @Ignore("b/182844465") 140 @Test testWorkEduFlow()141 public void testWorkEduFlow() { 142 mDevice.pressHome(); 143 waitForLauncherCondition("Launcher didn't start", Objects::nonNull); 144 executeOnLauncher(launcher -> launcher.getSharedPrefs().edit().remove( 145 WorkEduView.KEY_WORK_EDU_STEP).remove( 146 WorkEduView.KEY_LEGACY_WORK_EDU_SEEN).commit()); 147 148 waitForLauncherCondition("Work tab not setup", launcher -> { 149 if (launcher.getAppsView().getContentView() instanceof AllAppsPagedView) { 150 launcher.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST); 151 return true; 152 } 153 return false; 154 }, LauncherInstrumentation.WAIT_TIME_MS); 155 156 executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS)); 157 WorkEduView workEduView = getEduView(); 158 // verify personal app edu is seen first and click "next" 159 executeOnLauncher(l -> { 160 assertEquals(((TextView) workEduView.findViewById(R.id.content_text)).getText(), 161 l.getResources().getString(R.string.work_profile_edu_personal_apps)); 162 workEduView.findViewById(R.id.proceed).callOnClick(); 163 }); 164 165 AtomicInteger attempt = new AtomicInteger(0); 166 // verify work edu is seen next 167 waitForLauncherCondition("Launcher did not show the next edu screen", l -> { 168 Log.d(TestProtocol.WORK_PROFILE_REMOVED, 169 "running test attempt" + attempt.getAndIncrement()); 170 if (!(l.getAppsView().getContentView() instanceof AllAppsPagedView)) { 171 Log.d(TestProtocol.WORK_PROFILE_REMOVED, "Work tab not setup. Skipping test"); 172 return false; 173 } 174 if (((AllAppsPagedView) l.getAppsView().getContentView()).getCurrentPage() 175 != WORK_PAGE) { 176 Log.d(TestProtocol.WORK_PROFILE_REMOVED, "Work page not highlighted"); 177 } 178 return ((TextView) workEduView.findViewById(R.id.content_text)).getText().equals( 179 l.getResources().getString(R.string.work_profile_edu_work_apps)); 180 }); 181 } 182 183 @Ignore("b/182844465") 184 @Test testWorkEduIntermittent()185 public void testWorkEduIntermittent() { 186 mDevice.pressHome(); 187 waitForLauncherCondition("Launcher didn't start", Objects::nonNull); 188 executeOnLauncher(launcher -> launcher.getSharedPrefs().edit().remove( 189 WorkEduView.KEY_WORK_EDU_STEP).remove( 190 WorkEduView.KEY_LEGACY_WORK_EDU_SEEN).commit()); 191 192 193 waitForLauncherCondition("Work tab not setup", 194 launcher -> launcher.getAppsView().getContentView() instanceof AllAppsPagedView, 195 60000); 196 executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS)); 197 198 // verify personal app edu is seen 199 getEduView(); 200 201 // dismiss personal edu 202 mDevice.pressHome(); 203 waitForState("Launcher did not go home", () -> NORMAL); 204 205 // open work tab 206 executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS)); 207 waitForState("Launcher did not switch to all apps", () -> ALL_APPS); 208 waitForLauncherCondition("Work tab not setup", 209 launcher -> launcher.getAppsView().getContentView() instanceof AllAppsPagedView, 210 60000); 211 212 executeOnLauncher(launcher -> { 213 AllAppsPagedView pagedView = (AllAppsPagedView) launcher.getAppsView().getContentView(); 214 pagedView.setCurrentPage(WORK_PAGE); 215 }); 216 217 WorkEduView workEduView = getEduView(); 218 219 // verify work tab edu is shown 220 waitForLauncherCondition("Launcher did not show the next edu screen", 221 l -> ((TextView) workEduView.findViewById(R.id.content_text)).getText().equals( 222 l.getResources().getString(R.string.work_profile_edu_work_apps))); 223 } 224 225 getEduView()226 private WorkEduView getEduView() { 227 waitForLauncherCondition("Edu did not show", l -> { 228 DragLayer dragLayer = l.getDragLayer(); 229 return dragLayer.getChildCount() > 0 && dragLayer.getChildAt( 230 dragLayer.getChildCount() - 1) instanceof WorkEduView; 231 }, 6000); 232 return getFromLauncher(launcher -> (WorkEduView) launcher.getDragLayer().getChildAt( 233 launcher.getDragLayer().getChildCount() - 1)); 234 } 235 236 }