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