Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.server.wm; |
| 18 | |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 19 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 20 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 25 | |
Evan Rosky | ddedfd4 | 2019-10-04 13:38:38 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertNull; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 30 | import static org.mockito.ArgumentMatchers.anyString; |
| 31 | import static org.mockito.Mockito.times; |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 32 | import static org.mockito.Mockito.when; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 33 | |
| 34 | import android.app.Activity; |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 35 | import android.app.PictureInPictureParams; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 36 | import android.app.servertransaction.ClientTransaction; |
| 37 | import android.app.servertransaction.EnterPipRequestedItem; |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 38 | import android.content.res.Configuration; |
Evan Rosky | ddedfd4 | 2019-10-04 13:38:38 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 40 | import android.os.IBinder; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 42 | import android.view.IDisplayWindowListener; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 43 | |
| 44 | import androidx.test.filters.MediumTest; |
| 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 48 | import org.junit.runner.RunWith; |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 49 | import org.mockito.ArgumentCaptor; |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 50 | import org.mockito.MockitoSession; |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 51 | |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Tests for the {@link ActivityTaskManagerService} class. |
| 56 | * |
| 57 | * Build/Install/Run: |
| 58 | * atest WmTests:ActivityTaskManagerServiceTests |
| 59 | */ |
| 60 | @MediumTest |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 61 | @RunWith(WindowTestRunner.class) |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 62 | public class ActivityTaskManagerServiceTests extends ActivityTestsBase { |
| 63 | |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 64 | private final ArgumentCaptor<ClientTransaction> mClientTransactionCaptor = |
| 65 | ArgumentCaptor.forClass(ClientTransaction.class); |
| 66 | |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 67 | @Before |
| 68 | public void setUp() throws Exception { |
| 69 | doReturn(false).when(mService).isBooting(); |
| 70 | doReturn(true).when(mService).isBooted(); |
| 71 | } |
| 72 | |
| 73 | /** Verify that activity is finished correctly upon request. */ |
| 74 | @Test |
| 75 | public void testActivityFinish() { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 76 | final ActivityStack stack = new StackBuilder(mRootWindowContainer).build(); |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 77 | final ActivityRecord activity = stack.getBottomMostTask().getTopNonFinishingActivity(); |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 78 | assertTrue("Activity must be finished", mService.finishActivity(activity.appToken, |
| 79 | 0 /* resultCode */, null /* resultData */, |
| 80 | Activity.DONT_FINISH_TASK_WITH_ACTIVITY)); |
| 81 | assertTrue(activity.finishing); |
| 82 | |
| 83 | assertTrue("Duplicate activity finish request must also return 'true'", |
| 84 | mService.finishActivity(activity.appToken, 0 /* resultCode */, |
| 85 | null /* resultData */, Activity.DONT_FINISH_TASK_WITH_ACTIVITY)); |
| 86 | } |
Evan Rosky | ddedfd4 | 2019-10-04 13:38:38 -0700 | [diff] [blame] | 87 | |
| 88 | @Test |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 89 | public void testOnPictureInPictureRequested() throws RemoteException { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 90 | final ActivityStack stack = new StackBuilder(mRootWindowContainer).build(); |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 91 | final ActivityRecord activity = stack.getBottomMostTask().getTopNonFinishingActivity(); |
| 92 | ClientLifecycleManager lifecycleManager = mService.getLifecycleManager(); |
| 93 | doNothing().when(lifecycleManager).scheduleTransaction(any()); |
| 94 | doReturn(true).when(activity).checkEnterPictureInPictureState(anyString(), anyBoolean()); |
| 95 | |
| 96 | mService.requestPictureInPictureMode(activity.token); |
| 97 | |
| 98 | verify(lifecycleManager).scheduleTransaction(mClientTransactionCaptor.capture()); |
| 99 | final ClientTransaction transaction = mClientTransactionCaptor.getValue(); |
| 100 | // Check that only an enter pip request item callback was scheduled. |
| 101 | assertEquals(1, transaction.getCallbacks().size()); |
| 102 | assertTrue(transaction.getCallbacks().get(0) instanceof EnterPipRequestedItem); |
| 103 | // Check the activity lifecycle state remains unchanged. |
| 104 | assertNull(transaction.getLifecycleStateRequest()); |
| 105 | } |
| 106 | |
| 107 | @Test(expected = IllegalStateException.class) |
| 108 | public void testOnPictureInPictureRequested_cannotEnterPip() throws RemoteException { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 109 | final ActivityStack stack = new StackBuilder(mRootWindowContainer).build(); |
jorgegil@google.com | 06bc323 | 2019-10-31 14:51:22 -0700 | [diff] [blame] | 110 | final ActivityRecord activity = stack.getBottomMostTask().getTopNonFinishingActivity(); |
| 111 | ClientLifecycleManager lifecycleManager = mService.getLifecycleManager(); |
| 112 | doNothing().when(lifecycleManager).scheduleTransaction(any()); |
| 113 | doReturn(false).when(activity).checkEnterPictureInPictureState(anyString(), anyBoolean()); |
| 114 | |
| 115 | mService.requestPictureInPictureMode(activity.token); |
| 116 | |
| 117 | // Check enter no transactions with enter pip requests are made. |
| 118 | verify(lifecycleManager, times(0)).scheduleTransaction(any()); |
| 119 | } |
| 120 | |
| 121 | @Test |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 122 | public void testDisplayWindowListener() { |
| 123 | final ArrayList<Integer> added = new ArrayList<>(); |
| 124 | final ArrayList<Integer> changed = new ArrayList<>(); |
| 125 | final ArrayList<Integer> removed = new ArrayList<>(); |
| 126 | IDisplayWindowListener listener = new IDisplayWindowListener.Stub() { |
| 127 | @Override |
| 128 | public void onDisplayAdded(int displayId) { |
| 129 | added.add(displayId); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 134 | changed.add(displayId); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onDisplayRemoved(int displayId) { |
| 139 | removed.add(displayId); |
| 140 | } |
| 141 | }; |
| 142 | mService.mWindowManager.registerDisplayWindowListener(listener); |
| 143 | // Check that existing displays call added |
| 144 | assertEquals(1, added.size()); |
| 145 | assertEquals(0, changed.size()); |
| 146 | assertEquals(0, removed.size()); |
| 147 | added.clear(); |
| 148 | // Check adding a display |
Louis Chang | 677921f | 2019-12-06 16:44:24 +0800 | [diff] [blame] | 149 | DisplayContent newDisp1 = new TestDisplayContent.Builder(mService, 600, 800).build(); |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 150 | assertEquals(1, added.size()); |
| 151 | assertEquals(0, changed.size()); |
| 152 | assertEquals(0, removed.size()); |
| 153 | added.clear(); |
| 154 | // Check that changes are reported |
| 155 | Configuration c = new Configuration(newDisp1.getRequestedOverrideConfiguration()); |
| 156 | c.windowConfiguration.setBounds(new Rect(0, 0, 1000, 1300)); |
| 157 | newDisp1.onRequestedOverrideConfigurationChanged(c); |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 158 | mService.mRootWindowContainer.ensureVisibilityAndConfig(null /* starting */, |
Evan Rosky | 8220708 | 2019-10-08 17:28:29 -0700 | [diff] [blame] | 159 | newDisp1.mDisplayId, false /* markFrozenIfConfigChanged */, |
| 160 | false /* deferResume */); |
| 161 | assertEquals(0, added.size()); |
| 162 | assertEquals(1, changed.size()); |
| 163 | assertEquals(0, removed.size()); |
| 164 | changed.clear(); |
| 165 | // Check that removal is reported |
| 166 | newDisp1.remove(); |
| 167 | assertEquals(0, added.size()); |
| 168 | assertEquals(0, changed.size()); |
| 169 | assertEquals(1, removed.size()); |
| 170 | } |
Hyangseok Chae | ed0624e | 2019-11-07 10:15:46 +0900 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | a test to verify b/144045134 - ignore PIP mode request for destroyed activity. |
| 174 | mocks r.getParent() to return null to cause NPE inside enterPipRunnable#run() in |
| 175 | ActivityTaskMangerservice#enterPictureInPictureMode(), which rebooted the device. |
| 176 | It doesn't fully simulate the issue's reproduce steps, but this should suffice. |
| 177 | */ |
| 178 | @Test |
| 179 | public void testEnterPipModeWhenRecordParentChangesToNull() { |
| 180 | MockitoSession mockSession = mockitoSession() |
| 181 | .initMocks(this) |
| 182 | .mockStatic(ActivityRecord.class) |
| 183 | .startMocking(); |
| 184 | |
| 185 | ActivityRecord record = mock(ActivityRecord.class); |
| 186 | IBinder token = mock(IBinder.class); |
| 187 | PictureInPictureParams params = mock(PictureInPictureParams.class); |
| 188 | record.pictureInPictureArgs = params; |
| 189 | |
| 190 | //mock operations in private method ensureValidPictureInPictureActivityParamsLocked() |
| 191 | when(ActivityRecord.forTokenLocked(token)).thenReturn(record); |
| 192 | doReturn(true).when(record).supportsPictureInPicture(); |
| 193 | doReturn(false).when(params).hasSetAspectRatio(); |
| 194 | |
| 195 | //mock other operations |
| 196 | doReturn(true).when(record) |
| 197 | .checkEnterPictureInPictureState("enterPictureInPictureMode", false); |
| 198 | doReturn(false).when(mService).isInPictureInPictureMode(any()); |
| 199 | doReturn(false).when(mService).isKeyguardLocked(); |
| 200 | |
| 201 | //to simulate NPE |
| 202 | doReturn(null).when(record).getParent(); |
| 203 | |
| 204 | mService.enterPictureInPictureMode(token, params); |
| 205 | //if record's null parent is not handled gracefully, test will fail with NPE |
| 206 | |
| 207 | mockSession.finishMocking(); |
| 208 | } |
Andrii Kulian | 40eda67 | 2019-07-30 15:05:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |