Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.keyguard; |
| 18 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 19 | import static org.mockito.Matchers.any; |
| 20 | import static org.mockito.Matchers.anyInt; |
| 21 | import static org.mockito.Matchers.argThat; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.eq; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.verify; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 26 | |
| 27 | import android.app.Activity; |
| 28 | import android.app.ActivityManager; |
| 29 | import android.app.ActivityOptions; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 30 | import android.app.IActivityTaskManager; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 31 | import android.app.IApplicationThread; |
| 32 | import android.app.ProfilerInfo; |
| 33 | import android.content.ComponentName; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.os.Bundle; |
| 37 | import android.os.IBinder; |
| 38 | import android.os.UserHandle; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 39 | |
| 40 | import androidx.test.filters.SmallTest; |
| 41 | import androidx.test.runner.AndroidJUnit4; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 42 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 43 | import com.android.systemui.SysuiTestCase; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 44 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 45 | import com.android.systemui.shared.system.TaskStackChangeListener; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 46 | |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
| 50 | import org.mockito.ArgumentCaptor; |
| 51 | import org.mockito.ArgumentMatcher; |
| 52 | import org.mockito.Mock; |
| 53 | import org.mockito.MockitoAnnotations; |
| 54 | |
| 55 | /** |
| 56 | * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityControllerTest |
| 57 | */ |
| 58 | @SmallTest |
| 59 | @RunWith(AndroidJUnit4.class) |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 60 | public class WorkLockActivityControllerTest extends SysuiTestCase { |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 61 | private static final int USER_ID = 333; |
| 62 | private static final int TASK_ID = 444; |
| 63 | |
| 64 | private @Mock Context mContext; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 65 | private @Mock ActivityManagerWrapper mActivityManager; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 66 | private @Mock IActivityTaskManager mIActivityTaskManager; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 67 | |
| 68 | private WorkLockActivityController mController; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 69 | private TaskStackChangeListener mTaskStackListener; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 70 | |
| 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 Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 79 | final ArgumentCaptor<TaskStackChangeListener> listenerCaptor = |
| 80 | ArgumentCaptor.forClass(TaskStackChangeListener.class); |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 81 | mController = new WorkLockActivityController(mContext, mActivityManager, |
| 82 | mIActivityTaskManager); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 83 | |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 84 | verify(mActivityManager).registerTaskStackListener(listenerCaptor.capture()); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 85 | 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 Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 98 | verify(mIActivityTaskManager, never()).removeTask(anyInt() /*taskId*/); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 99 | } |
| 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 Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 112 | verify(mIActivityTaskManager).removeTask(TASK_ID); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // End of tests, start of helpers |
| 116 | // ------------------------------ |
| 117 | |
| 118 | private void setActivityStartCode(int taskId, boolean taskOverlay, int code) throws Exception { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 119 | doReturn(code).when(mIActivityTaskManager).startActivityAsUser( |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 120 | 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 Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 134 | verify(mIActivityTaskManager).startActivityAsUser( |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 135 | eq((IApplicationThread) null), |
| 136 | eq((String) null), |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 137 | 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 | } |