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; |
Garfield Tan | 36a69ad | 2019-01-16 17:08:23 -0800 | [diff] [blame] | 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyInt; |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; |
Garfield Tan | 0443b37 | 2019-01-04 15:00:13 -0800 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
Garfield Tan | 36a69ad | 2019-01-16 17:08:23 -0800 | [diff] [blame] | 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq; |
Garfield Tan | 0443b37 | 2019-01-04 15:00:13 -0800 | [diff] [blame] | 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
Andrii Kulian | 0c869cc | 2019-02-06 19:50:32 -0800 | [diff] [blame] | 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
Garfield Tan | 0443b37 | 2019-01-04 15:00:13 -0800 | [diff] [blame] | 28 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 29 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 30 | import static com.android.server.wm.ActivityStack.ActivityState.INITIALIZING; |
| 31 | import static com.android.server.wm.ActivityStack.ActivityState.PAUSING; |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 32 | import static com.android.server.wm.ActivityStack.ActivityState.RESUMED; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 33 | import static com.android.server.wm.ActivityStack.ActivityState.STOPPED; |
| 34 | import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING; |
Andrii Kulian | 0c869cc | 2019-02-06 19:50:32 -0800 | [diff] [blame] | 35 | import static com.android.server.wm.ActivityStack.STACK_VISIBILITY_INVISIBLE; |
| 36 | import static com.android.server.wm.ActivityStack.STACK_VISIBILITY_VISIBLE; |
| 37 | import static com.android.server.wm.ActivityStack.STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 38 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 39 | import static org.junit.Assert.assertEquals; |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 40 | import static org.junit.Assert.assertFalse; |
Yunfan Chen | 1ee84ea | 2018-11-13 16:03:37 -0800 | [diff] [blame] | 41 | import static org.junit.Assert.assertNotNull; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 42 | import static org.junit.Assert.assertNull; |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 43 | import static org.junit.Assert.assertTrue; |
chaviw | 82a0ba8 | 2018-03-15 14:26:29 -0700 | [diff] [blame] | 44 | |
| 45 | import android.app.ActivityOptions; |
Garfield Tan | 36a69ad | 2019-01-16 17:08:23 -0800 | [diff] [blame] | 46 | import android.app.servertransaction.ActivityConfigurationChangeItem; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 47 | import android.app.servertransaction.ClientTransaction; |
| 48 | import android.app.servertransaction.PauseActivityItem; |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 49 | import android.content.pm.ActivityInfo; |
Garfield Tan | 0443b37 | 2019-01-04 15:00:13 -0800 | [diff] [blame] | 50 | import android.content.res.Configuration; |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 51 | import android.graphics.Rect; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 52 | import android.platform.test.annotations.Presubmit; |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 53 | import android.util.MergedConfiguration; |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 54 | import android.util.MutableBoolean; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 55 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 56 | import androidx.test.filters.MediumTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 57 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 58 | import org.junit.Before; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 59 | import org.junit.Test; |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 60 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 61 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Tests for the {@link ActivityRecord} class. |
| 64 | * |
| 65 | * Build/Install/Run: |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 66 | * atest WmTests:ActivityRecordTests |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 67 | */ |
| 68 | @MediumTest |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 69 | @Presubmit |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 70 | public class ActivityRecordTests extends ActivityTestsBase { |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 71 | private TestActivityStack mStack; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 72 | private TaskRecord mTask; |
| 73 | private ActivityRecord mActivity; |
| 74 | |
| 75 | @Before |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 76 | public void setUp() throws Exception { |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 77 | mStack = (TestActivityStack) new StackBuilder(mRootActivityContainer).build(); |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 78 | mTask = mStack.getChildAt(0); |
| 79 | mActivity = mTask.getTopActivity(); |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 80 | |
| 81 | doReturn(false).when(mService).isBooting(); |
| 82 | doReturn(true).when(mService).isBooted(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 85 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 86 | public void testStackCleanupOnClearingTask() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 87 | mActivity.setTask(null); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 88 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 1); |
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 testStackCleanupOnActivityRemoval() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 93 | mTask.removeActivity(mActivity); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 94 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 1); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 98 | public void testStackCleanupOnTaskRemoval() { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 99 | mStack.removeTask(mTask, null /*reason*/, REMOVE_TASK_MODE_MOVING); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 100 | // Stack should be gone on task removal. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 101 | assertNull(mService.mRootActivityContainer.getStack(mStack.mStackId)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 105 | public void testNoCleanupMovingActivityInSameStack() { |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 106 | final TaskRecord newTask = new TaskBuilder(mService.mStackSupervisor).setStack(mStack) |
| 107 | .build(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 108 | mActivity.reparent(newTask, 0, null /*reason*/); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 109 | assertEquals(mStack.onActivityRemovedFromStackInvocationCount(), 0); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 110 | } |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 111 | |
Andreas Gampe | cea9e6d | 2018-02-22 18:06:44 -0800 | [diff] [blame] | 112 | @Test |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 113 | public void testPausingWhenVisibleFromStopped() throws Exception { |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 114 | final MutableBoolean pauseFound = new MutableBoolean(false); |
| 115 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 116 | final ClientTransaction transaction = invocationOnMock.getArgument(0); |
| 117 | if (transaction.getLifecycleStateRequest() instanceof PauseActivityItem) { |
| 118 | pauseFound.value = true; |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 119 | } |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 120 | return null; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 121 | }).when(mActivity.app.getThread()).scheduleTransaction(any()); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 122 | |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 123 | mActivity.setState(STOPPED, "testPausingWhenVisibleFromStopped"); |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 124 | |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 125 | // The activity is in the focused stack so it should be resumed. |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 126 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 127 | assertTrue(mActivity.isState(RESUMED)); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 128 | assertFalse(pauseFound.value); |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 129 | |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 130 | // Make the activity non focusable |
| 131 | mActivity.setState(STOPPED, "testPausingWhenVisibleFromStopped"); |
| 132 | doReturn(false).when(mActivity).isFocusable(); |
Bryce Lee | d939cf0 | 2018-03-12 09:04:44 -0700 | [diff] [blame] | 133 | |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 134 | // If the activity is not focusable, it should move to paused. |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 135 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 136 | assertTrue(mActivity.isState(PAUSING)); |
Andrii Kulian | 0447068 | 2018-01-10 15:32:31 -0800 | [diff] [blame] | 137 | assertTrue(pauseFound.value); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 138 | |
| 139 | // 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] | 140 | mActivity.setState(INITIALIZING, "testPausingWhenVisibleFromStopped"); |
Andrii Kulian | 6b32151 | 2019-01-23 06:37:00 +0000 | [diff] [blame] | 141 | doReturn(true).when(mActivity).isFocusable(); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 142 | |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 143 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | 052957b | 2018-01-16 11:13:30 -0800 | [diff] [blame] | 144 | |
Bryce Lee | 7ace395 | 2018-02-16 14:34:32 -0800 | [diff] [blame] | 145 | assertTrue(mActivity.isState(INITIALIZING)); |
Bryce Lee | a0fb8e0 | 2018-02-28 14:21:07 -0800 | [diff] [blame] | 146 | |
| 147 | // Make sure the state does not change if we are not the current top activity. |
| 148 | mActivity.setState(STOPPED, "testPausingWhenVisibleFromStopped behind"); |
| 149 | |
| 150 | // Make sure that the state does not change when we have an activity becoming translucent |
| 151 | final ActivityRecord topActivity = new ActivityBuilder(mService).setTask(mTask).build(); |
| 152 | mStack.mTranslucentActivityWaiting = topActivity; |
Andrii Kulian | a39ae3e | 2018-05-31 12:43:54 -0700 | [diff] [blame] | 153 | mActivity.makeVisibleIfNeeded(null /* starting */, true /* reportToClient */); |
Bryce Lee | a0fb8e0 | 2018-02-28 14:21:07 -0800 | [diff] [blame] | 154 | |
| 155 | assertTrue(mActivity.isState(STOPPED)); |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Riddle Hsu | 0a343c3 | 2018-12-21 00:40:48 +0800 | [diff] [blame] | 158 | private void ensureActivityConfiguration() { |
| 159 | mActivity.ensureActivityConfiguration(0 /* globalChanges */, false /* preserveWindow */); |
| 160 | } |
| 161 | |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 162 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 163 | public void testCanBeLaunchedOnDisplay() { |
Riddle Hsu | 1656713 | 2018-08-16 21:37:47 +0800 | [diff] [blame] | 164 | mService.mSupportsMultiWindow = true; |
| 165 | final ActivityRecord activity = new ActivityBuilder(mService).build(); |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 166 | |
Riddle Hsu | 1656713 | 2018-08-16 21:37:47 +0800 | [diff] [blame] | 167 | // An activity can be launched on default display. |
| 168 | assertTrue(activity.canBeLaunchedOnDisplay(DEFAULT_DISPLAY)); |
| 169 | // An activity cannot be launched on a non-existent display. |
| 170 | assertFalse(activity.canBeLaunchedOnDisplay(DEFAULT_DISPLAY + 1)); |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 171 | } |
| 172 | |
chaviw | 82a0ba8 | 2018-03-15 14:26:29 -0700 | [diff] [blame] | 173 | @Test |
| 174 | public void testsApplyOptionsLocked() { |
| 175 | ActivityOptions activityOptions = ActivityOptions.makeBasic(); |
| 176 | |
| 177 | // Set and apply options for ActivityRecord. Pending options should be cleared |
| 178 | mActivity.updateOptionsLocked(activityOptions); |
| 179 | mActivity.applyOptionsLocked(); |
| 180 | assertNull(mActivity.pendingOptions); |
| 181 | |
| 182 | // Set options for two ActivityRecords in same Task. Apply one ActivityRecord options. |
| 183 | // Pending options should be cleared for both ActivityRecords |
| 184 | ActivityRecord activity2 = new ActivityBuilder(mService).setTask(mTask).build(); |
| 185 | activity2.updateOptionsLocked(activityOptions); |
| 186 | mActivity.updateOptionsLocked(activityOptions); |
| 187 | mActivity.applyOptionsLocked(); |
| 188 | assertNull(mActivity.pendingOptions); |
| 189 | assertNull(activity2.pendingOptions); |
| 190 | |
| 191 | // Set options for two ActivityRecords in separate Tasks. Apply one ActivityRecord options. |
| 192 | // Pending options should be cleared for only ActivityRecord that was applied |
| 193 | TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor).setStack(mStack).build(); |
| 194 | activity2 = new ActivityBuilder(mService).setTask(task2).build(); |
| 195 | activity2.updateOptionsLocked(activityOptions); |
| 196 | mActivity.updateOptionsLocked(activityOptions); |
| 197 | mActivity.applyOptionsLocked(); |
| 198 | assertNull(mActivity.pendingOptions); |
| 199 | assertNotNull(activity2.pendingOptions); |
| 200 | } |
Garfield Tan | 0443b37 | 2019-01-04 15:00:13 -0800 | [diff] [blame] | 201 | |
| 202 | @Test |
| 203 | public void testNewOverrideConfigurationIncrementsSeq() { |
| 204 | final Configuration newConfig = new Configuration(); |
| 205 | |
| 206 | final int prevSeq = mActivity.getMergedOverrideConfiguration().seq; |
| 207 | mActivity.onRequestedOverrideConfigurationChanged(newConfig); |
| 208 | assertEquals(prevSeq + 1, mActivity.getMergedOverrideConfiguration().seq); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void testNewParentConfigurationIncrementsSeq() { |
| 213 | final Configuration newConfig = new Configuration( |
| 214 | mTask.getRequestedOverrideConfiguration()); |
| 215 | newConfig.orientation = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT |
| 216 | ? Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT; |
| 217 | |
| 218 | final int prevSeq = mActivity.getMergedOverrideConfiguration().seq; |
| 219 | mTask.onRequestedOverrideConfigurationChanged(newConfig); |
| 220 | assertEquals(prevSeq + 1, mActivity.getMergedOverrideConfiguration().seq); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void testNotifiesSeqIncrementToAppToken() { |
| 225 | final Configuration appWindowTokenRequestedOrientation = mock(Configuration.class); |
| 226 | mActivity.mAppWindowToken = mock(AppWindowToken.class); |
| 227 | doReturn(appWindowTokenRequestedOrientation).when(mActivity.mAppWindowToken) |
| 228 | .getRequestedOverrideConfiguration(); |
| 229 | |
| 230 | final Configuration newConfig = new Configuration(); |
| 231 | newConfig.orientation = Configuration.ORIENTATION_PORTRAIT; |
| 232 | |
| 233 | final int prevSeq = mActivity.getMergedOverrideConfiguration().seq; |
| 234 | mActivity.onRequestedOverrideConfigurationChanged(newConfig); |
| 235 | assertEquals(prevSeq + 1, appWindowTokenRequestedOrientation.seq); |
| 236 | verify(mActivity.mAppWindowToken).onMergedOverrideConfigurationChanged(); |
| 237 | } |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 238 | |
| 239 | @Test |
| 240 | public void testSetsRelaunchReason_NotDragResizing() { |
| 241 | mActivity.setState(ActivityStack.ActivityState.RESUMED, "Testing"); |
| 242 | |
| 243 | mTask.onRequestedOverrideConfigurationChanged(mTask.getConfiguration()); |
| 244 | mActivity.setLastReportedConfiguration(new MergedConfiguration(new Configuration(), |
| 245 | mActivity.getConfiguration())); |
| 246 | |
| 247 | mActivity.info.configChanges &= ~ActivityInfo.CONFIG_ORIENTATION; |
| 248 | final Configuration newConfig = new Configuration(mTask.getConfiguration()); |
| 249 | newConfig.orientation = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT |
| 250 | ? Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT; |
| 251 | mTask.onRequestedOverrideConfigurationChanged(newConfig); |
| 252 | |
| 253 | mActivity.mRelaunchReason = ActivityTaskManagerService.RELAUNCH_REASON_NONE; |
| 254 | |
Riddle Hsu | 0a343c3 | 2018-12-21 00:40:48 +0800 | [diff] [blame] | 255 | ensureActivityConfiguration(); |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 256 | |
| 257 | assertEquals(ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE, |
| 258 | mActivity.mRelaunchReason); |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void testSetsRelaunchReason_DragResizing() { |
| 263 | mActivity.setState(ActivityStack.ActivityState.RESUMED, "Testing"); |
| 264 | |
| 265 | mTask.onRequestedOverrideConfigurationChanged(mTask.getConfiguration()); |
| 266 | mActivity.setLastReportedConfiguration(new MergedConfiguration(new Configuration(), |
| 267 | mActivity.getConfiguration())); |
| 268 | |
| 269 | mActivity.info.configChanges &= ~ActivityInfo.CONFIG_ORIENTATION; |
| 270 | final Configuration newConfig = new Configuration(mTask.getConfiguration()); |
| 271 | newConfig.orientation = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT |
| 272 | ? Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT; |
| 273 | mTask.onRequestedOverrideConfigurationChanged(newConfig); |
| 274 | |
| 275 | doReturn(true).when(mTask.getTask()).isDragResizing(); |
| 276 | |
| 277 | mActivity.mRelaunchReason = ActivityTaskManagerService.RELAUNCH_REASON_NONE; |
| 278 | |
Riddle Hsu | 0a343c3 | 2018-12-21 00:40:48 +0800 | [diff] [blame] | 279 | ensureActivityConfiguration(); |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 280 | |
| 281 | assertEquals(ActivityTaskManagerService.RELAUNCH_REASON_FREE_RESIZE, |
| 282 | mActivity.mRelaunchReason); |
| 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void testSetsRelaunchReason_NonResizeConfigChanges() { |
| 287 | mActivity.setState(ActivityStack.ActivityState.RESUMED, "Testing"); |
| 288 | |
| 289 | mTask.onRequestedOverrideConfigurationChanged(mTask.getConfiguration()); |
| 290 | mActivity.setLastReportedConfiguration(new MergedConfiguration(new Configuration(), |
| 291 | mActivity.getConfiguration())); |
| 292 | |
| 293 | mActivity.info.configChanges &= ~ActivityInfo.CONFIG_FONT_SCALE; |
| 294 | final Configuration newConfig = new Configuration(mTask.getConfiguration()); |
| 295 | newConfig.fontScale = 5; |
| 296 | mTask.onRequestedOverrideConfigurationChanged(newConfig); |
| 297 | |
| 298 | mActivity.mRelaunchReason = |
| 299 | ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE; |
| 300 | |
Riddle Hsu | 0a343c3 | 2018-12-21 00:40:48 +0800 | [diff] [blame] | 301 | ensureActivityConfiguration(); |
Garfield Tan | f6cc5c9 | 2019-01-15 13:54:12 -0800 | [diff] [blame] | 302 | |
| 303 | assertEquals(ActivityTaskManagerService.RELAUNCH_REASON_NONE, |
| 304 | mActivity.mRelaunchReason); |
| 305 | } |
Garfield Tan | 36a69ad | 2019-01-16 17:08:23 -0800 | [diff] [blame] | 306 | |
| 307 | @Test |
| 308 | public void testSetRequestedOrientationUpdatesConfiguration() throws Exception { |
| 309 | mActivity.setState(ActivityStack.ActivityState.RESUMED, "Testing"); |
| 310 | |
| 311 | mTask.onRequestedOverrideConfigurationChanged(mTask.getConfiguration()); |
| 312 | mActivity.setLastReportedConfiguration(new MergedConfiguration(new Configuration(), |
| 313 | mActivity.getConfiguration())); |
| 314 | |
| 315 | mActivity.info.configChanges |= ActivityInfo.CONFIG_ORIENTATION; |
| 316 | final Configuration newConfig = new Configuration(mActivity.getConfiguration()); |
| 317 | newConfig.orientation = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT |
| 318 | ? Configuration.ORIENTATION_LANDSCAPE |
| 319 | : Configuration.ORIENTATION_PORTRAIT; |
| 320 | |
| 321 | // Mimic the behavior that display doesn't handle app's requested orientation. |
| 322 | doAnswer(invocation -> { |
| 323 | mTask.onConfigurationChanged(newConfig); |
| 324 | return null; |
| 325 | }).when(mActivity.mAppWindowToken).setOrientation(anyInt(), any(), any()); |
| 326 | |
| 327 | final int requestedOrientation; |
| 328 | switch (newConfig.orientation) { |
| 329 | case Configuration.ORIENTATION_LANDSCAPE: |
| 330 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
| 331 | break; |
| 332 | case Configuration.ORIENTATION_PORTRAIT: |
| 333 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
| 334 | break; |
| 335 | default: |
| 336 | throw new IllegalStateException("Orientation in new config should be either" |
| 337 | + "landscape or portrait."); |
| 338 | } |
| 339 | mActivity.setRequestedOrientation(requestedOrientation); |
| 340 | |
| 341 | final ActivityConfigurationChangeItem expected = |
| 342 | ActivityConfigurationChangeItem.obtain(newConfig); |
| 343 | verify(mService.getLifecycleManager()).scheduleTransaction(eq(mActivity.app.getThread()), |
| 344 | eq(mActivity.appToken), eq(expected)); |
| 345 | } |
Andrii Kulian | f219536 | 2019-01-31 18:20:11 -0800 | [diff] [blame] | 346 | |
| 347 | @Test |
| 348 | public void testShouldMakeActive_deferredResume() { |
| 349 | mActivity.setState(ActivityStack.ActivityState.STOPPED, "Testing"); |
| 350 | |
| 351 | mSupervisor.beginDeferResume(); |
| 352 | assertEquals(false, mActivity.shouldMakeActive(null /* activeActivity */)); |
| 353 | |
| 354 | mSupervisor.endDeferResume(); |
| 355 | assertEquals(true, mActivity.shouldMakeActive(null /* activeActivity */)); |
| 356 | } |
Garfield Tan | 4026330 | 2019-02-01 15:27:35 -0800 | [diff] [blame] | 357 | |
| 358 | @Test |
Andrii Kulian | 0c869cc | 2019-02-06 19:50:32 -0800 | [diff] [blame] | 359 | public void testShouldResume_stackVisibility() { |
| 360 | mActivity.setState(ActivityStack.ActivityState.STOPPED, "Testing"); |
| 361 | spyOn(mStack); |
| 362 | |
| 363 | doReturn(STACK_VISIBILITY_VISIBLE).when(mStack).getVisibility(null); |
| 364 | assertEquals(true, mActivity.shouldResumeActivity(null /* activeActivity */)); |
| 365 | |
| 366 | doReturn(STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT).when(mStack).getVisibility(null); |
| 367 | assertEquals(false, mActivity.shouldResumeActivity(null /* activeActivity */)); |
| 368 | |
| 369 | doReturn(STACK_VISIBILITY_INVISIBLE).when(mStack).getVisibility(null); |
| 370 | assertEquals(false, mActivity.shouldResumeActivity(null /* activeActivity */)); |
| 371 | } |
| 372 | |
| 373 | @Test |
Garfield Tan | 4026330 | 2019-02-01 15:27:35 -0800 | [diff] [blame] | 374 | public void testPushConfigurationWhenLaunchTaskBehind() throws Exception { |
| 375 | mActivity.setState(ActivityStack.ActivityState.STOPPED, "Testing"); |
| 376 | |
| 377 | final TestActivityStack stack = (TestActivityStack) new StackBuilder(mRootActivityContainer) |
| 378 | .build(); |
| 379 | try { |
| 380 | stack.setIsTranslucent(false); |
| 381 | assertFalse(mStack.shouldBeVisible(null /* starting */)); |
| 382 | |
| 383 | mTask.onRequestedOverrideConfigurationChanged(mTask.getConfiguration()); |
| 384 | mActivity.setLastReportedConfiguration(new MergedConfiguration(new Configuration(), |
| 385 | mActivity.getConfiguration())); |
| 386 | |
| 387 | mActivity.mLaunchTaskBehind = true; |
| 388 | mActivity.info.configChanges |= ActivityInfo.CONFIG_ORIENTATION; |
| 389 | final Configuration newConfig = new Configuration(mActivity.getConfiguration()); |
| 390 | newConfig.orientation = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT |
| 391 | ? Configuration.ORIENTATION_LANDSCAPE |
| 392 | : Configuration.ORIENTATION_PORTRAIT; |
| 393 | |
| 394 | mTask.onConfigurationChanged(newConfig); |
| 395 | |
| 396 | mActivity.ensureActivityConfiguration(0 /* globalChanges */, |
| 397 | false /* preserveWindow */, true /* ignoreStopState */); |
| 398 | |
| 399 | final ActivityConfigurationChangeItem expected = |
| 400 | ActivityConfigurationChangeItem.obtain(newConfig); |
| 401 | verify(mService.getLifecycleManager()).scheduleTransaction( |
| 402 | eq(mActivity.app.getThread()), eq(mActivity.appToken), eq(expected)); |
| 403 | } finally { |
| 404 | stack.getDisplay().removeChild(stack); |
| 405 | } |
| 406 | } |
Riddle Hsu | 0a343c3 | 2018-12-21 00:40:48 +0800 | [diff] [blame] | 407 | |
| 408 | @Test |
| 409 | public void testFixedScreenConfigurationWhenMovingToDisplay() { |
| 410 | // Initialize different bounds on a new display. |
| 411 | final ActivityDisplay newDisplay = addNewActivityDisplayAt(ActivityDisplay.POSITION_TOP); |
| 412 | newDisplay.setBounds(0, 0, 1000, 2000); |
| 413 | newDisplay.getConfiguration().densityDpi = 300; |
| 414 | |
| 415 | mTask.getWindowConfiguration().setAppBounds(mStack.getDisplay().getBounds()); |
| 416 | mTask.getConfiguration().densityDpi = 200; |
| 417 | when(mActivity.mAppWindowToken.getOrientationIgnoreVisibility()).thenReturn( |
| 418 | ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 419 | mActivity.info.resizeMode = ActivityInfo.RESIZE_MODE_UNRESIZEABLE; |
| 420 | mActivity.info.maxAspectRatio = 1.5f; |
| 421 | ensureActivityConfiguration(); |
| 422 | final Rect originalBounds = new Rect(mActivity.getBounds()); |
| 423 | final int originalDpi = mActivity.getConfiguration().densityDpi; |
| 424 | |
| 425 | // Move the non-resizable activity to the new display. |
| 426 | mStack.reparent(newDisplay, true /* onTop */, false /* displayRemoved */); |
| 427 | ensureActivityConfiguration(); |
| 428 | |
| 429 | assertEquals(originalBounds, mActivity.getBounds()); |
| 430 | assertEquals(originalDpi, mActivity.getConfiguration().densityDpi); |
| 431 | } |
| 432 | |
| 433 | @Test |
| 434 | public void testFixedScreenBoundsWhenDisplaySizeChanged() { |
| 435 | when(mActivity.mAppWindowToken.getOrientationIgnoreVisibility()).thenReturn( |
| 436 | ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 437 | mTask.getWindowConfiguration().setAppBounds(mStack.getDisplay().getBounds()); |
| 438 | mActivity.info.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
| 439 | mActivity.info.resizeMode = ActivityInfo.RESIZE_MODE_UNRESIZEABLE; |
| 440 | ensureActivityConfiguration(); |
| 441 | final Rect originalBounds = new Rect(mActivity.getBounds()); |
| 442 | |
| 443 | // Change the size of current display. |
| 444 | mStack.getDisplay().setBounds(0, 0, 1000, 2000); |
| 445 | ensureActivityConfiguration(); |
| 446 | |
| 447 | assertEquals(originalBounds, mActivity.getBounds()); |
| 448 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 449 | } |