Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [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.server.am; |
| 18 | |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 19 | import static android.view.WindowManagerPolicy.NAV_BAR_BOTTOM; |
| 20 | import static android.view.WindowManagerPolicy.NAV_BAR_LEFT; |
| 21 | import static android.view.WindowManagerPolicy.NAV_BAR_RIGHT; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertNull; |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.when; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 25 | |
| 26 | import android.content.ComponentName; |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 28 | import android.platform.test.annotations.Presubmit; |
| 29 | import android.support.test.filters.MediumTest; |
| 30 | import android.support.test.runner.AndroidJUnit4; |
| 31 | |
| 32 | import org.junit.runner.RunWith; |
| 33 | import org.junit.Test; |
| 34 | |
| 35 | /** |
| 36 | * Tests for the {@link ActivityRecord} class. |
| 37 | * |
| 38 | * Build/Install/Run: |
| 39 | * bit FrameworksServicesTests:com.android.server.am.ActivityRecordTests |
| 40 | */ |
| 41 | @MediumTest |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 42 | @Presubmit |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 43 | @RunWith(AndroidJUnit4.class) |
| 44 | public class ActivityRecordTests extends ActivityTestsBase { |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 45 | private static final int TEST_STACK_ID = 100; |
| 46 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 47 | private final ComponentName testActivityComponent = |
| 48 | ComponentName.unflattenFromString("com.foo/.BarActivity"); |
| 49 | @Test |
| 50 | public void testStackCleanupOnClearingTask() throws Exception { |
| 51 | final ActivityManagerService service = createActivityManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 52 | final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 53 | final ActivityRecord record = createActivity(service, testActivityComponent, task); |
| 54 | |
| 55 | record.setTask(null); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 56 | assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 1); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testStackCleanupOnActivityRemoval() throws Exception { |
| 61 | final ActivityManagerService service = createActivityManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 62 | final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 63 | final ActivityRecord record = createActivity(service, testActivityComponent, task); |
| 64 | |
| 65 | task.removeActivity(record); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 66 | assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 1); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testStackCleanupOnTaskRemoval() throws Exception { |
| 71 | final ActivityManagerService service = createActivityManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 72 | final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 73 | final ActivityRecord record = createActivity(service, testActivityComponent, task); |
| 74 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 75 | service.mStackSupervisor.getStack(TEST_STACK_ID) |
| 76 | .removeTask(task, null /*reason*/, ActivityStack.REMOVE_TASK_MODE_MOVING); |
| 77 | |
| 78 | // Stack should be gone on task removal. |
| 79 | assertNull(service.mStackSupervisor.getStack(TEST_STACK_ID)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testNoCleanupMovingActivityInSameStack() throws Exception { |
| 84 | final ActivityManagerService service = createActivityManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 85 | final TaskRecord oldTask = createTask(service, testActivityComponent, TEST_STACK_ID); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 86 | final ActivityRecord record = createActivity(service, testActivityComponent, oldTask); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 87 | final TaskRecord newTask = createTask(service, testActivityComponent, TEST_STACK_ID); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 88 | |
| 89 | record.reparent(newTask, 0, null /*reason*/); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 90 | assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 0); |
| 91 | } |
| 92 | |
| 93 | private static int getActivityRemovedFromStackCount(ActivityManagerService service, |
| 94 | int stackId) { |
| 95 | final ActivityStack stack = service.mStackSupervisor.getStack(stackId); |
| 96 | if (stack instanceof ActivityStackReporter) { |
| 97 | return ((ActivityStackReporter) stack).onActivityRemovedFromStackInvocationCount(); |
| 98 | } |
| 99 | |
| 100 | return -1; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 101 | } |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 102 | |
| 103 | @Test |
| 104 | public void testPositionLimitedAspectRatioNavBarBottom() throws Exception { |
| 105 | verifyPositionWithLimitedAspectRatio(NAV_BAR_BOTTOM, new Rect(0, 0, 1000, 2000), 1.5f, |
| 106 | new Rect(0, 0, 1000, 1500)); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testPositionLimitedAspectRatioNavBarLeft() throws Exception { |
| 111 | verifyPositionWithLimitedAspectRatio(NAV_BAR_LEFT, new Rect(0, 0, 2000, 1000), 1.5f, |
| 112 | new Rect(500, 0, 2000, 1000)); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testPositionLimitedAspectRatioNavBarRight() throws Exception { |
| 117 | verifyPositionWithLimitedAspectRatio(NAV_BAR_RIGHT, new Rect(0, 0, 2000, 1000), 1.5f, |
| 118 | new Rect(0, 0, 1500, 1000)); |
| 119 | } |
| 120 | |
| 121 | private void verifyPositionWithLimitedAspectRatio(int navBarPosition, Rect taskBounds, |
| 122 | float aspectRatio, Rect expectedActivityBounds) { |
| 123 | final ActivityManagerService service = createActivityManagerService(); |
| 124 | final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID); |
| 125 | final ActivityRecord record = createActivity(service, testActivityComponent, task); |
| 126 | |
| 127 | // Verify with nav bar on the right. |
| 128 | when(service.mWindowManager.getNavBarPosition()).thenReturn(navBarPosition); |
| 129 | task.getConfiguration().setAppBounds(taskBounds); |
| 130 | record.info.maxAspectRatio = aspectRatio; |
| 131 | record.ensureActivityConfigurationLocked(0 /* globalChanges */, false /* preserveWindow */); |
| 132 | assertEquals(expectedActivityBounds, record.getBounds()); |
| 133 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 134 | } |