2021-10-06 15:50:46 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2021 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.ui;
|
|
|
|
|
|
|
|
|
|
import static com.android.launcher3.LauncherState.ALL_APPS;
|
|
|
|
|
import static com.android.launcher3.LauncherState.NORMAL;
|
|
|
|
|
import static com.android.launcher3.allapps.AllAppsStore.DEFER_UPDATES_TEST;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2022-08-02 18:32:12 +00:00
|
|
|
import static org.junit.Assume.assumeTrue;
|
2022-08-28 21:54:04 -07:00
|
|
|
|
2021-10-25 18:54:01 -07:00
|
|
|
import android.util.Log;
|
2021-10-06 15:50:46 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
|
2022-05-01 12:18:14 -07:00
|
|
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
|
|
|
|
|
2021-10-06 15:50:46 -07:00
|
|
|
import com.android.launcher3.R;
|
2022-01-14 23:15:47 -05:00
|
|
|
import com.android.launcher3.allapps.ActivityAllAppsContainerView;
|
2021-10-06 15:50:46 -07:00
|
|
|
import com.android.launcher3.allapps.AllAppsPagedView;
|
|
|
|
|
import com.android.launcher3.allapps.WorkEduCard;
|
2022-05-01 12:18:14 -07:00
|
|
|
import com.android.launcher3.allapps.WorkPausedCard;
|
2021-10-06 15:50:46 -07:00
|
|
|
import com.android.launcher3.allapps.WorkProfileManager;
|
|
|
|
|
import com.android.launcher3.tapl.LauncherInstrumentation;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
2023-01-03 12:46:43 -08:00
|
|
|
import org.junit.Ignore;
|
2021-10-06 15:50:46 -07:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.Objects;
|
2022-05-01 12:18:14 -07:00
|
|
|
import java.util.function.Predicate;
|
2021-10-06 15:50:46 -07:00
|
|
|
|
|
|
|
|
public class WorkProfileTest extends AbstractLauncherUiTest {
|
|
|
|
|
|
2022-01-14 23:15:47 -05:00
|
|
|
private static final int WORK_PAGE = ActivityAllAppsContainerView.AdapterHolder.WORK;
|
2021-10-06 15:50:46 -07:00
|
|
|
|
|
|
|
|
private int mProfileUserId;
|
2022-08-02 18:32:12 +00:00
|
|
|
private boolean mWorkProfileSetupSuccessful;
|
2022-08-31 15:12:08 -07:00
|
|
|
private final String TAG = "WorkProfileTest";
|
2021-10-06 15:50:46 -07:00
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
@Override
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
super.setUp();
|
|
|
|
|
String output =
|
|
|
|
|
mDevice.executeShellCommand(
|
|
|
|
|
"pm create-user --profileOf 0 --managed TestProfile");
|
2022-08-31 15:12:08 -07:00
|
|
|
// b/203817455
|
|
|
|
|
updateWorkProfileSetupSuccessful("pm create-user", output);
|
2021-10-06 15:50:46 -07:00
|
|
|
|
|
|
|
|
String[] tokens = output.split("\\s+");
|
|
|
|
|
mProfileUserId = Integer.parseInt(tokens[tokens.length - 1]);
|
2022-08-31 15:12:08 -07:00
|
|
|
output = mDevice.executeShellCommand("am start-user " + mProfileUserId);
|
|
|
|
|
updateWorkProfileSetupSuccessful("am start-user", output);
|
|
|
|
|
|
|
|
|
|
if (!mWorkProfileSetupSuccessful) {
|
|
|
|
|
return; // no need to setup launcher since all tests will skip.
|
|
|
|
|
}
|
2021-11-09 09:33:24 -08:00
|
|
|
|
|
|
|
|
mDevice.pressHome();
|
|
|
|
|
waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
|
2021-11-09 17:02:31 -08:00
|
|
|
waitForStateTransitionToEnd("Launcher internal state didn't switch to Normal",
|
|
|
|
|
() -> NORMAL);
|
2021-11-09 09:33:24 -08:00
|
|
|
waitForResumed("Launcher internal state is still Background");
|
|
|
|
|
executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
|
2021-11-09 17:02:31 -08:00
|
|
|
waitForStateTransitionToEnd("Launcher internal state didn't switch to All Apps",
|
|
|
|
|
() -> ALL_APPS);
|
2021-10-06 15:50:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void removeWorkProfile() throws Exception {
|
|
|
|
|
mDevice.executeShellCommand("pm remove-user " + mProfileUserId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void resumeAppStoreUpdate() {
|
|
|
|
|
executeOnLauncher(launcher -> {
|
|
|
|
|
if (launcher == null || launcher.getAppsView() == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
launcher.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void waitForWorkTabSetup() {
|
|
|
|
|
waitForLauncherCondition("Work tab not setup", launcher -> {
|
|
|
|
|
if (launcher.getAppsView().getContentView() instanceof AllAppsPagedView) {
|
|
|
|
|
launcher.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}, LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void workTabExists() {
|
2022-08-02 18:32:12 +00:00
|
|
|
assumeTrue(mWorkProfileSetupSuccessful);
|
2022-08-31 15:12:08 -07:00
|
|
|
waitForWorkTabSetup();
|
2021-10-06 15:50:46 -07:00
|
|
|
waitForLauncherCondition("Personal tab is missing",
|
|
|
|
|
launcher -> launcher.getAppsView().isPersonalTabVisible(),
|
|
|
|
|
LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
waitForLauncherCondition("Work tab is missing",
|
|
|
|
|
launcher -> launcher.getAppsView().isWorkTabVisible(),
|
|
|
|
|
LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-03 12:46:43 -08:00
|
|
|
@Ignore("b/243855320")
|
2021-10-06 15:50:46 -07:00
|
|
|
public void toggleWorks() {
|
2022-08-02 18:32:12 +00:00
|
|
|
assumeTrue(mWorkProfileSetupSuccessful);
|
2021-10-06 15:50:46 -07:00
|
|
|
waitForWorkTabSetup();
|
|
|
|
|
executeOnLauncher(launcher -> {
|
|
|
|
|
AllAppsPagedView pagedView = (AllAppsPagedView) launcher.getAppsView().getContentView();
|
|
|
|
|
pagedView.setCurrentPage(WORK_PAGE);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
WorkProfileManager manager = getFromLauncher(l -> l.getAppsView().getWorkManager());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
waitForLauncherCondition("work profile initial state check failed", launcher ->
|
|
|
|
|
manager.getWorkModeSwitch() != null
|
|
|
|
|
&& manager.getCurrentState() == WorkProfileManager.STATE_ENABLED
|
|
|
|
|
&& manager.getWorkModeSwitch().isEnabled(),
|
|
|
|
|
LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
|
|
|
|
|
//start work profile toggle OFF test
|
2022-08-28 21:54:04 -07:00
|
|
|
executeOnLauncher(l -> {
|
|
|
|
|
// Ensure updates are not deferred so notification happens when apps pause.
|
|
|
|
|
l.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST);
|
|
|
|
|
l.getAppsView().getWorkManager().getWorkModeSwitch().performClick();
|
|
|
|
|
});
|
2021-10-06 15:50:46 -07:00
|
|
|
|
|
|
|
|
waitForLauncherCondition("Work profile toggle OFF failed", launcher -> {
|
|
|
|
|
manager.reset(); // pulls current state from system
|
|
|
|
|
return manager.getCurrentState() == WorkProfileManager.STATE_DISABLED;
|
|
|
|
|
}, LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
|
2022-05-01 12:18:14 -07:00
|
|
|
waitForWorkCard("Work paused card not shown", view -> view instanceof WorkPausedCard);
|
|
|
|
|
|
2021-10-06 15:50:46 -07:00
|
|
|
// start work profile toggle ON test
|
|
|
|
|
executeOnLauncher(l -> {
|
2022-01-14 23:15:47 -05:00
|
|
|
ActivityAllAppsContainerView<?> allApps = l.getAppsView();
|
2021-10-06 15:50:46 -07:00
|
|
|
assertEquals("Work tab is not focused", allApps.getCurrentPage(), WORK_PAGE);
|
2022-05-17 00:01:54 +00:00
|
|
|
View workPausedCard = allApps.getActiveRecyclerView()
|
|
|
|
|
.findViewHolderForAdapterPosition(0).itemView;
|
2021-10-06 15:50:46 -07:00
|
|
|
workPausedCard.findViewById(R.id.enable_work_apps).performClick();
|
|
|
|
|
});
|
|
|
|
|
waitForLauncherCondition("Work profile toggle ON failed", launcher -> {
|
|
|
|
|
manager.reset(); // pulls current state from system
|
|
|
|
|
return manager.getCurrentState() == WorkProfileManager.STATE_ENABLED;
|
|
|
|
|
}, LauncherInstrumentation.WAIT_TIME_MS);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testEdu() {
|
2022-08-02 18:32:12 +00:00
|
|
|
assumeTrue(mWorkProfileSetupSuccessful);
|
2021-10-06 15:50:46 -07:00
|
|
|
waitForWorkTabSetup();
|
|
|
|
|
executeOnLauncher(l -> {
|
2022-07-15 16:23:02 -07:00
|
|
|
l.getSharedPrefs().edit().putInt(WorkProfileManager.KEY_WORK_EDU_STEP, 0).commit();
|
2021-10-06 15:50:46 -07:00
|
|
|
((AllAppsPagedView) l.getAppsView().getContentView()).setCurrentPage(WORK_PAGE);
|
|
|
|
|
l.getAppsView().getWorkManager().reset();
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-01 12:18:14 -07:00
|
|
|
waitForWorkCard("Work profile education not shown", view -> view instanceof WorkEduCard);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void waitForWorkCard(String message, Predicate<View> workCardCheck) {
|
|
|
|
|
waitForLauncherCondition(message, l -> {
|
|
|
|
|
l.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST);
|
|
|
|
|
ViewHolder holder = l.getAppsView().getActiveRecyclerView()
|
|
|
|
|
.findViewHolderForAdapterPosition(0);
|
|
|
|
|
try {
|
|
|
|
|
return holder != null && workCardCheck.test(holder.itemView);
|
|
|
|
|
} finally {
|
|
|
|
|
l.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
|
|
|
|
|
}
|
|
|
|
|
}, LauncherInstrumentation.WAIT_TIME_MS);
|
2021-10-06 15:50:46 -07:00
|
|
|
}
|
2022-08-31 15:12:08 -07:00
|
|
|
|
|
|
|
|
private void updateWorkProfileSetupSuccessful(String cli, String output) {
|
|
|
|
|
Log.d(TAG, "updateWorkProfileSetupSuccessful, cli=" + cli + " " + "output=" + output);
|
|
|
|
|
if (output.startsWith("Success")) {
|
|
|
|
|
assertTrue(output, output.startsWith("Success"));
|
|
|
|
|
mWorkProfileSetupSuccessful = true;
|
|
|
|
|
} else {
|
|
|
|
|
mWorkProfileSetupSuccessful = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 15:50:46 -07:00
|
|
|
}
|