1 /* 2 * Copyright (C) 2022 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 17 package android.server.wm; 18 19 import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED; 20 import static android.app.ActivityManager.LOCK_TASK_MODE_NONE; 21 import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED; 22 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 23 import static android.server.wm.app.Components.TEST_ACTIVITY; 24 import static android.view.Display.DEFAULT_DISPLAY; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 import android.app.Activity; 29 import android.app.ActivityOptions; 30 import android.content.Intent; 31 import android.platform.test.annotations.Presubmit; 32 import android.server.wm.WindowManagerState.Task; 33 34 import com.android.compatibility.common.util.ApiTest; 35 36 import org.junit.Test; 37 38 /** 39 * Build/Install/Run: 40 * atest CtsWindowManagerDeviceTestCases:LockTaskModeTests 41 */ 42 @Presubmit 43 public class LockTaskModeTests extends ActivityManagerTestBase { 44 private static final String[] LOCK_TASK_PACKAGES_ALLOWLIST = 45 new String[] {"android.server.wm.app"}; 46 47 @Test 48 @ApiTest(apis = {"android.app.ActivityTaskManager#updateLockTaskPackages", 49 "android.app.ActivityTaskManager#startSystemLockTaskMode"}) 50 public void startAllowedPackageIntoLockTaskMode_anotherAppPinned_exitsPinningEntersLockTaskMode()51 startAllowedPackageIntoLockTaskMode_anotherAppPinned_exitsPinningEntersLockTaskMode() { 52 // clear lock task 53 runWithShellPermission(() -> mAtm.updateLockTaskPackages(mContext, 54 new String[0])); 55 assertThat(mAm.getLockTaskModeState()).isEqualTo(LOCK_TASK_MODE_NONE); 56 57 try (TestActivitySession<TestActivity> session = createManagedTestActivitySession()) { 58 // pin app 59 session.mFinishAfterClose = true; 60 session.launchTestActivityOnDisplaySync(TestActivity.class, DEFAULT_DISPLAY); 61 Task task = mWmState.getRootTaskByActivity(session.getActivity().getComponentName()); 62 runWithShellPermission(() -> mAtm.startSystemLockTaskMode(task.mTaskId)); 63 waitForOrFail("Task in app pinning mode", () -> { 64 return mAm.getLockTaskModeState() == LOCK_TASK_MODE_PINNED; 65 }); 66 67 // setup lock task mode allowlist 68 runWithShellPermission(() -> mAtm.updateLockTaskPackages(mContext, 69 LOCK_TASK_PACKAGES_ALLOWLIST)); 70 71 // start allowed package into lock task mode 72 mContext.startActivity(new Intent() 73 .addFlags(FLAG_ACTIVITY_NEW_TASK) 74 .setComponent(TEST_ACTIVITY), 75 ActivityOptions.makeBasic().setLockTaskEnabled(true).toBundle()); 76 waitAndAssertResumedActivity(TEST_ACTIVITY, "Activity must be started and resumed"); 77 waitForOrFail("Task in lock task mode", () -> { 78 return mAm.getLockTaskModeState() == LOCK_TASK_MODE_LOCKED; 79 }); 80 } finally { 81 // cleanup 82 runWithShellPermission(() -> { 83 mAtm.stopSystemLockTaskMode(); 84 mAtm.updateLockTaskPackages(mContext, new String[0]); 85 }); 86 waitForOrFail("Task should not in app pinning mode", () -> { 87 return mAm.getLockTaskModeState() == LOCK_TASK_MODE_NONE; 88 }); 89 } 90 } 91 92 /* An empty Activity which will be used in app pinning. */ 93 public static final class TestActivity extends Activity {} 94 } 95