blob: 199c4c283452a4a2097fd054c9fcc6a38708d955 [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
19import static org.mockito.Mockito.doReturn;
20import static org.mockito.Mockito.eq;
21import static org.mockito.Mockito.never;
22import static org.mockito.Mockito.verify;
Robin Lee1d996bd2017-01-31 23:01:59 +000023import static org.mockito.Matchers.any;
24import static org.mockito.Matchers.anyInt;
25import static org.mockito.Matchers.argThat;
26
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;
39import android.support.test.filters.SmallTest;
40import android.support.test.runner.AndroidJUnit4;
41
Jason Monkfba8faf2017-05-23 10:42:59 -040042import com.android.systemui.SysuiTestCase;
Winson Chung2cf6ad82017-11-09 17:36:59 -080043import com.android.systemui.shared.system.ActivityManagerWrapper;
Robin Lee1d996bd2017-01-31 23:01:59 +000044
Winson Chung67f5c8b2018-09-24 12:09:19 -070045import com.android.systemui.shared.system.TaskStackChangeListener;
Robin Lee1d996bd2017-01-31 23:01:59 +000046import org.junit.Before;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.mockito.ArgumentCaptor;
50import org.mockito.ArgumentMatcher;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
54/**
55 * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityControllerTest
56 */
57@SmallTest
58@RunWith(AndroidJUnit4.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040059public class WorkLockActivityControllerTest extends SysuiTestCase {
Robin Lee1d996bd2017-01-31 23:01:59 +000060 private static final int USER_ID = 333;
61 private static final int TASK_ID = 444;
62
63 private @Mock Context mContext;
Winson Chung2cf6ad82017-11-09 17:36:59 -080064 private @Mock ActivityManagerWrapper mActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070065 private @Mock IActivityTaskManager mIActivityTaskManager;
Robin Lee1d996bd2017-01-31 23:01:59 +000066
67 private WorkLockActivityController mController;
Winson Chung67f5c8b2018-09-24 12:09:19 -070068 private TaskStackChangeListener mTaskStackListener;
Robin Lee1d996bd2017-01-31 23:01:59 +000069
70 @Before
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
73
74 // Set a package name to use for checking ComponentName well-formedness in tests.
75 doReturn("com.example.test").when(mContext).getPackageName();
76
77 // Construct controller. Save the TaskStackListener for injecting events.
Winson Chung67f5c8b2018-09-24 12:09:19 -070078 final ArgumentCaptor<TaskStackChangeListener> listenerCaptor =
79 ArgumentCaptor.forClass(TaskStackChangeListener.class);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070080 mController = new WorkLockActivityController(mContext, mActivityManager,
81 mIActivityTaskManager);
Robin Lee1d996bd2017-01-31 23:01:59 +000082
Winson Chung2cf6ad82017-11-09 17:36:59 -080083 verify(mActivityManager).registerTaskStackListener(listenerCaptor.capture());
Robin Lee1d996bd2017-01-31 23:01:59 +000084 mTaskStackListener = listenerCaptor.getValue();
85 }
86
87 @Test
88 public void testOverlayStartedWhenLocked() throws Exception {
89 // When starting an activity succeeds,
90 setActivityStartCode(TASK_ID, true /*taskOverlay*/, ActivityManager.START_SUCCESS);
91
92 // And the controller receives a message saying the profile is locked,
93 mTaskStackListener.onTaskProfileLocked(TASK_ID, USER_ID);
94
95 // The overlay should start and the task the activity started in should not be removed.
96 verifyStartActivity(TASK_ID, true /*taskOverlay*/);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070097 verify(mIActivityTaskManager, never()).removeTask(anyInt() /*taskId*/);
Robin Lee1d996bd2017-01-31 23:01:59 +000098 }
99
100 @Test
101 public void testRemoveTaskOnFailureToStartOverlay() throws Exception {
102 // When starting an activity fails,
103 setActivityStartCode(TASK_ID, true /*taskOverlay*/, ActivityManager.START_CLASS_NOT_FOUND);
104
105 // And the controller receives a message saying the profile is locked,
106 mTaskStackListener.onTaskProfileLocked(TASK_ID, USER_ID);
107
108 // The task the activity started in should be removed to prevent the locked task from
109 // being shown.
110 verifyStartActivity(TASK_ID, true /*taskOverlay*/);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700111 verify(mIActivityTaskManager).removeTask(TASK_ID);
Robin Lee1d996bd2017-01-31 23:01:59 +0000112 }
113
114 // End of tests, start of helpers
115 // ------------------------------
116
117 private void setActivityStartCode(int taskId, boolean taskOverlay, int code) throws Exception {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700118 doReturn(code).when(mIActivityTaskManager).startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +0000119 eq((IApplicationThread) null),
120 eq((String) null),
121 any(Intent.class),
122 eq((String) null),
123 eq((IBinder) null),
124 eq((String) null),
125 anyInt(),
126 anyInt(),
127 eq((ProfilerInfo) null),
128 argThat(hasOptions(taskId, taskOverlay)),
129 eq(UserHandle.USER_CURRENT));
130 }
131
132 private void verifyStartActivity(int taskId, boolean taskOverlay) throws Exception {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700133 verify(mIActivityTaskManager).startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +0000134 eq((IApplicationThread) null),
135 eq((String) null),
136 any(Intent.class),
137 eq((String) null),
138 eq((IBinder) null),
139 eq((String) null),
140 anyInt(),
141 anyInt(),
142 eq((ProfilerInfo) null),
143 argThat(hasOptions(taskId, taskOverlay)),
144 eq(UserHandle.USER_CURRENT));
145 }
146
147 private static ArgumentMatcher<Intent> hasComponent(final Context context,
148 final Class<? extends Activity> activityClass) {
149 return new ArgumentMatcher<Intent>() {
150 @Override
151 public boolean matches(Intent intent) {
152 return new ComponentName(context, activityClass).equals(intent.getComponent());
153 }
154 };
155 }
156
157 private static ArgumentMatcher<Bundle> hasOptions(final int taskId, final boolean overlay) {
158 return new ArgumentMatcher<Bundle>() {
159 @Override
160 public boolean matches(Bundle item) {
161 final ActivityOptions options = ActivityOptions.fromBundle(item);
162 return (options.getLaunchTaskId() == taskId)
163 && (options.getTaskOverlay() == overlay);
164 }
165 };
166 }
167}