blob: d26ae6aaad79853b5289c464bec81072916b830d [file] [log] [blame]
Robin Lee1d996bd2017-01-31 23:01:59 +00001/*
2 * Copyright (C) 2017 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
17package com.android.systemui.keyguard;
18
Brett Chabot84151d92019-02-27 15:37:59 -080019import static org.mockito.Matchers.any;
20import static org.mockito.Matchers.anyInt;
21import static org.mockito.Matchers.argThat;
Robin Lee1d996bd2017-01-31 23:01:59 +000022import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.eq;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.verify;
Robin Lee1d996bd2017-01-31 23:01:59 +000026
27import android.app.Activity;
28import android.app.ActivityManager;
29import android.app.ActivityOptions;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070030import android.app.IActivityTaskManager;
Robin Lee1d996bd2017-01-31 23:01:59 +000031import android.app.IApplicationThread;
32import android.app.ProfilerInfo;
33import android.content.ComponentName;
34import android.content.Context;
35import android.content.Intent;
36import android.os.Bundle;
37import android.os.IBinder;
38import android.os.UserHandle;
Brett Chabot84151d92019-02-27 15:37:59 -080039
40import androidx.test.filters.SmallTest;
41import androidx.test.runner.AndroidJUnit4;
Robin Lee1d996bd2017-01-31 23:01:59 +000042
Jason Monkfba8faf2017-05-23 10:42:59 -040043import com.android.systemui.SysuiTestCase;
Winson Chung2cf6ad82017-11-09 17:36:59 -080044import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung67f5c8b2018-09-24 12:09:19 -070045import com.android.systemui.shared.system.TaskStackChangeListener;
Brett Chabot84151d92019-02-27 15:37:59 -080046
Robin Lee1d996bd2017-01-31 23:01:59 +000047import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.ArgumentCaptor;
51import org.mockito.ArgumentMatcher;
52import org.mockito.Mock;
53import org.mockito.MockitoAnnotations;
54
55/**
56 * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityControllerTest
57 */
58@SmallTest
59@RunWith(AndroidJUnit4.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040060public class WorkLockActivityControllerTest extends SysuiTestCase {
Robin Lee1d996bd2017-01-31 23:01:59 +000061 private static final int USER_ID = 333;
62 private static final int TASK_ID = 444;
63
64 private @Mock Context mContext;
Winson Chung2cf6ad82017-11-09 17:36:59 -080065 private @Mock ActivityManagerWrapper mActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070066 private @Mock IActivityTaskManager mIActivityTaskManager;
Robin Lee1d996bd2017-01-31 23:01:59 +000067
68 private WorkLockActivityController mController;
Winson Chung67f5c8b2018-09-24 12:09:19 -070069 private TaskStackChangeListener mTaskStackListener;
Robin Lee1d996bd2017-01-31 23:01:59 +000070
71 @Before
72 public void setUp() throws Exception {
73 MockitoAnnotations.initMocks(this);
74
75 // Set a package name to use for checking ComponentName well-formedness in tests.
76 doReturn("com.example.test").when(mContext).getPackageName();
77
78 // Construct controller. Save the TaskStackListener for injecting events.
Winson Chung67f5c8b2018-09-24 12:09:19 -070079 final ArgumentCaptor<TaskStackChangeListener> listenerCaptor =
80 ArgumentCaptor.forClass(TaskStackChangeListener.class);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070081 mController = new WorkLockActivityController(mContext, mActivityManager,
82 mIActivityTaskManager);
Robin Lee1d996bd2017-01-31 23:01:59 +000083
Winson Chung2cf6ad82017-11-09 17:36:59 -080084 verify(mActivityManager).registerTaskStackListener(listenerCaptor.capture());
Robin Lee1d996bd2017-01-31 23:01:59 +000085 mTaskStackListener = listenerCaptor.getValue();
86 }
87
88 @Test
89 public void testOverlayStartedWhenLocked() throws Exception {
90 // When starting an activity succeeds,
91 setActivityStartCode(TASK_ID, true /*taskOverlay*/, ActivityManager.START_SUCCESS);
92
93 // And the controller receives a message saying the profile is locked,
94 mTaskStackListener.onTaskProfileLocked(TASK_ID, USER_ID);
95
96 // The overlay should start and the task the activity started in should not be removed.
97 verifyStartActivity(TASK_ID, true /*taskOverlay*/);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070098 verify(mIActivityTaskManager, never()).removeTask(anyInt() /*taskId*/);
Robin Lee1d996bd2017-01-31 23:01:59 +000099 }
100
101 @Test
102 public void testRemoveTaskOnFailureToStartOverlay() throws Exception {
103 // When starting an activity fails,
104 setActivityStartCode(TASK_ID, true /*taskOverlay*/, ActivityManager.START_CLASS_NOT_FOUND);
105
106 // And the controller receives a message saying the profile is locked,
107 mTaskStackListener.onTaskProfileLocked(TASK_ID, USER_ID);
108
109 // The task the activity started in should be removed to prevent the locked task from
110 // being shown.
111 verifyStartActivity(TASK_ID, true /*taskOverlay*/);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700112 verify(mIActivityTaskManager).removeTask(TASK_ID);
Robin Lee1d996bd2017-01-31 23:01:59 +0000113 }
114
115 // End of tests, start of helpers
116 // ------------------------------
117
118 private void setActivityStartCode(int taskId, boolean taskOverlay, int code) throws Exception {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700119 doReturn(code).when(mIActivityTaskManager).startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +0000120 eq((IApplicationThread) null),
121 eq((String) null),
122 any(Intent.class),
123 eq((String) null),
124 eq((IBinder) null),
125 eq((String) null),
126 anyInt(),
127 anyInt(),
128 eq((ProfilerInfo) null),
129 argThat(hasOptions(taskId, taskOverlay)),
130 eq(UserHandle.USER_CURRENT));
131 }
132
133 private void verifyStartActivity(int taskId, boolean taskOverlay) throws Exception {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700134 verify(mIActivityTaskManager).startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +0000135 eq((IApplicationThread) null),
136 eq((String) null),
Robin Lee1d996bd2017-01-31 23:01:59 +0000137 any(Intent.class),
138 eq((String) null),
139 eq((IBinder) null),
140 eq((String) null),
141 anyInt(),
142 anyInt(),
143 eq((ProfilerInfo) null),
144 argThat(hasOptions(taskId, taskOverlay)),
145 eq(UserHandle.USER_CURRENT));
146 }
147
148 private static ArgumentMatcher<Intent> hasComponent(final Context context,
149 final Class<? extends Activity> activityClass) {
150 return new ArgumentMatcher<Intent>() {
151 @Override
152 public boolean matches(Intent intent) {
153 return new ComponentName(context, activityClass).equals(intent.getComponent());
154 }
155 };
156 }
157
158 private static ArgumentMatcher<Bundle> hasOptions(final int taskId, final boolean overlay) {
159 return new ArgumentMatcher<Bundle>() {
160 @Override
161 public boolean matches(Bundle item) {
162 final ActivityOptions options = ActivityOptions.fromBundle(item);
163 return (options.getLaunchTaskId() == taskId)
164 && (options.getTaskOverlay() == overlay);
165 }
166 };
167 }
168}