Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 1 | /* |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 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 |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 14 | * limitations under the License |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 18 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 19 | import static android.view.Display.DEFAULT_DISPLAY; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 20 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
| 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
| 24 | import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_BOTTOM; |
| 25 | import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_LEFT; |
| 26 | import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_RIGHT; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 27 | import static com.android.server.wm.ActivityStack.ActivityState.INITIALIZING; |
| 28 | import static com.android.server.wm.ActivityStack.ActivityState.PAUSING; |
| 29 | import static com.android.server.wm.ActivityStack.ActivityState.STOPPED; |
| 30 | import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 31 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 32 | import static org.junit.Assert.assertEquals; |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertFalse; |
Yunfan Chen | 1ee84ea | 2018-11-13 16:03:37 -0800 | [diff] [blame] | 34 | import static org.junit.Assert.assertNotNull; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 35 | import static org.junit.Assert.assertNull; |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertTrue; |
chaviw | 82a0ba8 | 2018-03-15 14:26:29 -0700 | [diff] [blame] | 37 | |
| 38 | import android.app.ActivityOptions; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 39 | import android.app.servertransaction.ClientTransaction; |
| 40 | import android.app.servertransaction.PauseActivityItem; |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 41 | import android.graphics.Rect; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 42 | import android.platform.test.annotations.Presubmit; |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 43 | import android.util.MutableBoolean; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 44 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 45 | import androidx.test.filters.MediumTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 46 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 47 | import org.junit.Before; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 48 | import org.junit.Test; |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 49 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 50 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Tests for the {@link ActivityRecord} class. |
| 53 | * |
| 54 | * Build/Install/Run: |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 55 | * atest WmTests:ActivityRecordTests |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 56 | */ |
| 57 | @MediumTest |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 58 | @Presubmit |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 59 | public class ActivityRecordTests extends ActivityTestsBase { |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 60 | private TestActivityStack mStack; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 61 | private TaskRecord mTask; |
| 62 | private ActivityRecord mActivity; |
| 63 | |
| 64 | @Before |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 65 | public void setUp() throws Exception { |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 66 | setupActivityTaskManagerService(); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 67 | mStack = new StackBuilder(mRootActivityContainer).build(); |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 68 | mTask = mStack.getChildAt(0); |
| 69 | mActivity = mTask.getTopActivity(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 72 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 73 | public void testStackCleanupOnClearingTask() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 74 | mActivity.setTask(null); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 75 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 1); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 79 | public void testStackCleanupOnActivityRemoval() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 80 | mTask.removeActivity(mActivity); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 81 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 1); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 85 | public void testStackCleanupOnTaskRemoval() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 86 | mStack.removeTask(mTask, null /*reason*/, REMOVE_TASK_MODE_MOVING); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 87 | // Stack should be gone on task removal. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 88 | assertNull(mService.mRootActivityContainer.getStack(mStack.mStackId)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 92 | public void testNoCleanupMovingActivityInSameStack() { |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 93 | final TaskRecord newTask = new TaskBuilder(mService.mStackSupervisor).setStack(mStack) |
| 94 | .build(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 95 | mActivity.reparent(newTask, 0, null /*reason*/); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 96 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 0); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 97 | } |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | cea9e6d | 2018-02-22 18:06:44 -0800 | [diff] [blame] | 99 | @Test |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 100 | public void testPausingWhenVisibleFromStopped() throws Exception { |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 101 | final MutableBoolean pauseFound = new MutableBoolean(false); |
| 102 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 103 | final ClientTransaction transaction = invocationOnMock.getArgument(0); |
| 104 | if (transaction.getLifecycleStateRequest() instanceof PauseActivityItem) { |
| 105 | pauseFound.value = true; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 106 | } |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 107 | return null; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 108 | }).when(mActivity.app.getThread()).scheduleTransaction(any()); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 109 | |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 110 | mActivity.setState(STOPPED, "testPausingWhenVisibleFromStopped"); |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 111 | |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 112 | // The activity is in the focused stack so it should not move to paused. |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 113 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 114 | assertTrue(mActivity.isState(STOPPED)); |
| 115 | assertFalse(pauseFound.value); |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 116 | |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 117 | // Clear focused stack |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 118 | final ActivityDisplay display = mRootActivityContainer.getDefaultDisplay(); |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 119 | when(display.getFocusedStack()).thenReturn(null); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 120 | |
| 121 | // In the unfocused stack, the activity should move to paused. |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 122 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 123 | assertTrue(mActivity.isState(PAUSING)); |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 124 | assertTrue(pauseFound.value); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 125 | |
| 126 | // Make sure that the state does not change for current non-stopping states. |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 127 | mActivity.setState(INITIALIZING, "testPausingWhenVisibleFromStopped"); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 128 | |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 129 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 130 | |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 131 | assertTrue(mActivity.isState(INITIALIZING)); |
Bryce Lee | a0fb8e0 | 2018-02-28 14:21:07 -0800 | [diff] [blame] | 132 | |
| 133 | // Make sure the state does not change if we are not the current top activity. |
| 134 | mActivity.setState(STOPPED, "testPausingWhenVisibleFromStopped behind"); |
| 135 | |
| 136 | // Make sure that the state does not change when we have an activity becoming translucent |
| 137 | final ActivityRecord topActivity = new ActivityBuilder(mService).setTask(mTask).build(); |
| 138 | mStack.mTranslucentActivityWaiting = topActivity; |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 139 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | a0fb8e0 | 2018-02-28 14:21:07 -0800 | [diff] [blame] | 140 | |
| 141 | assertTrue(mActivity.isState(STOPPED)); |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 145 | public void testPositionLimitedAspectRatioNavBarBottom() { |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 146 | verifyPositionWithLimitedAspectRatio(NAV_BAR_BOTTOM, new Rect(0, 0, 1000, 2000), 1.5f, |
| 147 | new Rect(0, 0, 1000, 1500)); |
| 148 | } |
| 149 | |
| 150 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 151 | public void testPositionLimitedAspectRatioNavBarLeft() { |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 152 | verifyPositionWithLimitedAspectRatio(NAV_BAR_LEFT, new Rect(0, 0, 2000, 1000), 1.5f, |
| 153 | new Rect(500, 0, 2000, 1000)); |
| 154 | } |
| 155 | |
| 156 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 157 | public void testPositionLimitedAspectRatioNavBarRight() { |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 158 | verifyPositionWithLimitedAspectRatio(NAV_BAR_RIGHT, new Rect(0, 0, 2000, 1000), 1.5f, |
| 159 | new Rect(0, 0, 1500, 1000)); |
| 160 | } |
| 161 | |
| 162 | private void verifyPositionWithLimitedAspectRatio(int navBarPosition, Rect taskBounds, |
| 163 | float aspectRatio, Rect expectedActivityBounds) { |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 164 | // Verify with nav bar on the right. |
Tiger Huang | 7c610aa | 2018-10-27 00:01:01 +0800 | [diff] [blame] | 165 | when(mService.mWindowManager.getNavBarPosition(mActivity.getDisplayId())) |
| 166 | .thenReturn(navBarPosition); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 167 | mTask.getConfiguration().windowConfiguration.setAppBounds(taskBounds); |
| 168 | mActivity.info.maxAspectRatio = aspectRatio; |
Wale Ogunwale | b6d75f3 | 2018-02-22 20:44:56 -0800 | [diff] [blame] | 169 | mActivity.ensureActivityConfiguration( |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 170 | 0 /* globalChanges */, false /* preserveWindow */); |
| 171 | assertEquals(expectedActivityBounds, mActivity.getBounds()); |
| 172 | } |
| 173 | |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 174 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 175 | public void testCanBeLaunchedOnDisplay() { |
Riddle Hsu | 1656713 | 2018-08-16 21:37:47 +0800 | [diff] [blame] | 176 | mService.mSupportsMultiWindow = true; |
| 177 | final ActivityRecord activity = new ActivityBuilder(mService).build(); |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 178 | |
Riddle Hsu | 1656713 | 2018-08-16 21:37:47 +0800 | [diff] [blame] | 179 | // An activity can be launched on default display. |
| 180 | assertTrue(activity.canBeLaunchedOnDisplay(DEFAULT_DISPLAY)); |
| 181 | // An activity cannot be launched on a non-existent display. |
| 182 | assertFalse(activity.canBeLaunchedOnDisplay(DEFAULT_DISPLAY + 1)); |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 183 | } |
| 184 | |
chaviw | 82a0ba8 | 2018-03-15 14:26:29 -0700 | [diff] [blame] | 185 | @Test |
| 186 | public void testsApplyOptionsLocked() { |
| 187 | ActivityOptions activityOptions = ActivityOptions.makeBasic(); |
| 188 | |
| 189 | // Set and apply options for ActivityRecord. Pending options should be cleared |
| 190 | mActivity.updateOptionsLocked(activityOptions); |
| 191 | mActivity.applyOptionsLocked(); |
| 192 | assertNull(mActivity.pendingOptions); |
| 193 | |
| 194 | // Set options for two ActivityRecords in same Task. Apply one ActivityRecord options. |
| 195 | // Pending options should be cleared for both ActivityRecords |
| 196 | ActivityRecord activity2 = new ActivityBuilder(mService).setTask(mTask).build(); |
| 197 | activity2.updateOptionsLocked(activityOptions); |
| 198 | mActivity.updateOptionsLocked(activityOptions); |
| 199 | mActivity.applyOptionsLocked(); |
| 200 | assertNull(mActivity.pendingOptions); |
| 201 | assertNull(activity2.pendingOptions); |
| 202 | |
| 203 | // Set options for two ActivityRecords in separate Tasks. Apply one ActivityRecord options. |
| 204 | // Pending options should be cleared for only ActivityRecord that was applied |
| 205 | TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor).setStack(mStack).build(); |
| 206 | activity2 = new ActivityBuilder(mService).setTask(task2).build(); |
| 207 | activity2.updateOptionsLocked(activityOptions); |
| 208 | mActivity.updateOptionsLocked(activityOptions); |
| 209 | mActivity.applyOptionsLocked(); |
| 210 | assertNull(mActivity.pendingOptions); |
| 211 | assertNotNull(activity2.pendingOptions); |
| 212 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 213 | } |