WorkTabTest.java 9.8 KB

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