ActivityRecordTests.java revision 4cb9b96d596ca10e298a6398893b773dab1a16c4
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// TODO(b/36916522): Currently failing in CI.
37// @Presubmit
38@RunWith(AndroidJUnit4.class)
39public class ActivityRecordTests extends ActivityTestsBase {
40    private final ComponentName testActivityComponent =
41            ComponentName.unflattenFromString("com.foo/.BarActivity");
42    @Test
43    public void testStackCleanupOnClearingTask() throws Exception {
44        final ActivityManagerService service = createActivityManagerService();
45        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
46        final TaskRecord task = createTask(service, testActivityComponent, testStack);
47        final ActivityRecord record = createActivity(service, testActivityComponent, task);
48
49        record.setTask(null);
50        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
51    }
52
53    @Test
54    public void testStackCleanupOnActivityRemoval() throws Exception {
55        final ActivityManagerService service = createActivityManagerService();
56        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
57        final TaskRecord task = createTask(service, testActivityComponent, testStack);
58        final ActivityRecord record = createActivity(service, testActivityComponent, task);
59
60        task.removeActivity(record);
61        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
62    }
63
64    @Test
65    public void testStackCleanupOnTaskRemoval() throws Exception {
66        final ActivityManagerService service = createActivityManagerService();
67        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
68        final TaskRecord task = createTask(service, testActivityComponent, testStack);
69        final ActivityRecord record = createActivity(service, testActivityComponent, task);
70
71        testStack.removeTask(task, null /*reason*/, ActivityStack.REMOVE_TASK_MODE_MOVING);
72        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 1);
73    }
74
75    @Test
76    public void testNoCleanupMovingActivityInSameStack() throws Exception {
77        final ActivityManagerService service = createActivityManagerService();
78        final TestActivityStack testStack = new ActivityStackBuilder(service).build();
79        final TaskRecord oldTask = createTask(service, testActivityComponent, testStack);
80        final ActivityRecord record = createActivity(service, testActivityComponent, oldTask);
81        final TaskRecord newTask = createTask(service, testActivityComponent, testStack);
82
83        record.reparent(newTask, 0, null /*reason*/);
84        assertTrue(testStack.onActivityRemovedFromStackInvocationCount() == 0);
85    }
86}
87