ActivityRecordTests.java revision af691c0be7bbfea63e880dd717c51a38a0bc874a
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
17package com.android.server.am;
18
19import static org.junit.Assert.assertTrue;
20
21import android.content.ComponentName;
22import android.platform.test.annotations.Presubmit;
23import android.support.test.filters.MediumTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.runner.RunWith;
27import org.junit.Test;
28
29/**
30 * Tests for the {@link ActivityRecord} class.
31 *
32 * Build/Install/Run:
33 *  bit FrameworksServicesTests:com.android.server.am.ActivityRecordTests
34 */
35@MediumTest
36@Presubmit
37@RunWith(AndroidJUnit4.class)
38public class ActivityRecordTests extends ActivityTestsBase {
39    private final ComponentName testActivityComponent =
40            ComponentName.unflattenFromString("com.foo/.BarActivity");
41    @Test
42    public void testStackCleanupOnClearingTask() throws Exception {
43        final ActivityManagerService service = createActivityManagerService();
44        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
45        final TaskRecord task = createTask(service, testActivityComponent, testStack);
46        final ActivityRecord record = createActivity(service, testActivityComponent, task);
47
48        record.setTask(null);
49        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
50    }
51
52    @Test
53    public void testStackCleanupOnActivityRemoval() throws Exception {
54        final ActivityManagerService service = createActivityManagerService();
55        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
56        final TaskRecord task = createTask(service, testActivityComponent, testStack);
57        final ActivityRecord record = createActivity(service, testActivityComponent, task);
58
59        task.removeActivity(record);
60        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
61    }
62
63    @Test
64    public void testStackCleanupOnTaskRemoval() throws Exception {
65        final ActivityManagerService service = createActivityManagerService();
66        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
67        final TaskRecord task = createTask(service, testActivityComponent, testStack);
68        final ActivityRecord record = createActivity(service, testActivityComponent, task);
69
70        testStack.removeTask(task, null /*reason*/, ActivityStack.REMOVE_TASK_MODE_MOVING);
71        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
72    }
73
74    @Test
75    public void testNoCleanupMovingActivityInSameStack() throws Exception {
76        final ActivityManagerService service = createActivityManagerService();
77        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
78        final TaskRecord oldTask = createTask(service, testActivityComponent, testStack);
79        final ActivityRecord record = createActivity(service, testActivityComponent, oldTask);
80        final TaskRecord newTask = createTask(service, testActivityComponent, testStack);
81
82        record.reparent(newTask, 0, null /*reason*/);
83        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 0);
84    }
85}
86