blob: 47a3a7242a449e45f175fd5b6e86eb6bcc36b25a [file] [log] [blame]
Bryce Leeaf691c02017-03-20 14:20:22 -07001/*
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
17package com.android.server.am;
18
Andrii Kulian3a1619d2017-07-07 14:38:09 -070019import static android.view.WindowManagerPolicy.NAV_BAR_BOTTOM;
20import static android.view.WindowManagerPolicy.NAV_BAR_LEFT;
21import static android.view.WindowManagerPolicy.NAV_BAR_RIGHT;
Bryce Lee04ab3462017-04-10 15:06:33 -070022import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNull;
Andrii Kulian3a1619d2017-07-07 14:38:09 -070024import static org.mockito.Mockito.when;
Bryce Leeaf691c02017-03-20 14:20:22 -070025
26import android.content.ComponentName;
Andrii Kulian3a1619d2017-07-07 14:38:09 -070027import android.graphics.Rect;
Bryce Leeaf691c02017-03-20 14:20:22 -070028import android.platform.test.annotations.Presubmit;
29import android.support.test.filters.MediumTest;
30import android.support.test.runner.AndroidJUnit4;
31
32import org.junit.runner.RunWith;
33import 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 Lee3115bdf2017-04-05 08:39:40 -070042@Presubmit
Bryce Leeaf691c02017-03-20 14:20:22 -070043@RunWith(AndroidJUnit4.class)
44public class ActivityRecordTests extends ActivityTestsBase {
Bryce Lee04ab3462017-04-10 15:06:33 -070045 private static final int TEST_STACK_ID = 100;
46
Bryce Leeaf691c02017-03-20 14:20:22 -070047 private final ComponentName testActivityComponent =
48 ComponentName.unflattenFromString("com.foo/.BarActivity");
49 @Test
50 public void testStackCleanupOnClearingTask() throws Exception {
51 final ActivityManagerService service = createActivityManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -070052 final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
Bryce Leeaf691c02017-03-20 14:20:22 -070053 final ActivityRecord record = createActivity(service, testActivityComponent, task);
54
55 record.setTask(null);
Bryce Lee04ab3462017-04-10 15:06:33 -070056 assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 1);
Bryce Leeaf691c02017-03-20 14:20:22 -070057 }
58
59 @Test
60 public void testStackCleanupOnActivityRemoval() throws Exception {
61 final ActivityManagerService service = createActivityManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -070062 final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
Bryce Leeaf691c02017-03-20 14:20:22 -070063 final ActivityRecord record = createActivity(service, testActivityComponent, task);
64
65 task.removeActivity(record);
Bryce Lee04ab3462017-04-10 15:06:33 -070066 assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 1);
Bryce Leeaf691c02017-03-20 14:20:22 -070067 }
68
69 @Test
70 public void testStackCleanupOnTaskRemoval() throws Exception {
71 final ActivityManagerService service = createActivityManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -070072 final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
Bryce Leeaf691c02017-03-20 14:20:22 -070073 final ActivityRecord record = createActivity(service, testActivityComponent, task);
74
Bryce Lee04ab3462017-04-10 15:06:33 -070075 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 Leeaf691c02017-03-20 14:20:22 -070080 }
81
82 @Test
83 public void testNoCleanupMovingActivityInSameStack() throws Exception {
84 final ActivityManagerService service = createActivityManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -070085 final TaskRecord oldTask = createTask(service, testActivityComponent, TEST_STACK_ID);
Bryce Leeaf691c02017-03-20 14:20:22 -070086 final ActivityRecord record = createActivity(service, testActivityComponent, oldTask);
Bryce Lee04ab3462017-04-10 15:06:33 -070087 final TaskRecord newTask = createTask(service, testActivityComponent, TEST_STACK_ID);
Bryce Leeaf691c02017-03-20 14:20:22 -070088
89 record.reparent(newTask, 0, null /*reason*/);
Bryce Lee04ab3462017-04-10 15:06:33 -070090 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 Leeaf691c02017-03-20 14:20:22 -0700101 }
Andrii Kulian3a1619d2017-07-07 14:38:09 -0700102
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);
Wale Ogunwale822e5122017-07-26 06:02:24 -0700129 task.getConfiguration().windowConfiguration.setAppBounds(taskBounds);
Andrii Kulian3a1619d2017-07-07 14:38:09 -0700130 record.info.maxAspectRatio = aspectRatio;
131 record.ensureActivityConfigurationLocked(0 /* globalChanges */, false /* preserveWindow */);
132 assertEquals(expectedActivityBounds, record.getBounds());
133 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700134}