blob: 4e5fbd8f8279e596d78814dc2c7b6fd202c1d9c8 [file] [log] [blame]
Winson Chung61c9e5a2017-10-11 10:39:32 -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
19import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
20import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
21import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
22import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
23import static android.view.Display.DEFAULT_DISPLAY;
24
25import static com.android.server.am.ActivityDisplay.POSITION_BOTTOM;
26
27import static org.junit.Assert.assertTrue;
28
29import android.app.ActivityManager.RunningTaskInfo;
30import android.content.ComponentName;
31import android.content.Context;
32import android.platform.test.annotations.Presubmit;
33import android.support.test.InstrumentationRegistry;
34import android.support.test.filters.MediumTest;
35import android.support.test.runner.AndroidJUnit4;
36import android.util.SparseArray;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41
42import java.util.ArrayList;
43
Tadashi G. Takaokadee5a4d2018-09-19 11:59:40 +090044 /**
45 * Tests for {@link RunningTasks}.
46 *
47 * Build/Install/Run:
48 * atest WmTests:RunningTasksTest
Winson Chung61c9e5a2017-10-11 10:39:32 -070049 */
50@MediumTest
51@Presubmit
52@RunWith(AndroidJUnit4.class)
53public class RunningTasksTest extends ActivityTestsBase {
54
55 private Context mContext = InstrumentationRegistry.getContext();
56 private ActivityManagerService mService;
57
58 private RunningTasks mRunningTasks;
59
60 @Before
61 @Override
62 public void setUp() throws Exception {
63 super.setUp();
64
65 mService = createActivityManagerService();
66 mRunningTasks = new RunningTasks();
67 }
68
69 @Test
70 public void testCollectTasksByLastActiveTime() throws Exception {
71 // Create a number of stacks with tasks (of incrementing active time)
72 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
73 final SparseArray<ActivityDisplay> displays = new SparseArray<>();
Winson Chung59a47ded2018-01-25 17:46:06 +000074 final ActivityDisplay display = new TestActivityDisplay(supervisor, DEFAULT_DISPLAY);
Winson Chung61c9e5a2017-10-11 10:39:32 -070075 displays.put(DEFAULT_DISPLAY, display);
76
77 final int numStacks = 2;
78 for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) {
79 final ActivityStack stack = new TestActivityStack(display, stackIndex, supervisor,
80 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true);
81 display.addChild(stack, POSITION_BOTTOM);
82 }
83
84 final int numTasks = 10;
85 int activeTime = 0;
86 for (int i = 0; i < numTasks; i++) {
87 createTask(display.getChildAt(i % numStacks), ".Task" + i, i, activeTime++);
88 }
89
90 // Ensure that the latest tasks were returned in order of decreasing last active time,
91 // collected from all tasks across all the stacks
92 final int numFetchTasks = 5;
93 ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
94 mRunningTasks.getTasks(5, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
95 displays, -1 /* callingUid */, true /* allowed */);
96 assertTrue(tasks.size() == numFetchTasks);
97 for (int i = 0; i < numFetchTasks; i++) {
98 assertTrue(tasks.get(i).id == (numTasks - i - 1));
99 }
100
101 // Ensure that requesting more than the total number of tasks only returns the subset
102 // and does not crash
103 tasks.clear();
104 mRunningTasks.getTasks(100, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
105 displays, -1 /* callingUid */, true /* allowed */);
106 assertTrue(tasks.size() == numTasks);
107 for (int i = 0; i < numTasks; i++) {
108 assertTrue(tasks.get(i).id == (numTasks - i - 1));
109 }
110 }
111
112 /**
113 * Create a task with a single activity in it, with the given last active time.
114 */
115 private TaskRecord createTask(ActivityStack stack, String className, int taskId,
116 int lastActiveTime) {
117 final TaskRecord task = new TaskBuilder(mService.mStackSupervisor)
118 .setComponent(new ComponentName(mContext.getPackageName(), className))
119 .setTaskId(taskId)
120 .setStack(stack)
121 .build();
122 task.lastActiveTime = lastActiveTime;
123 final ActivityRecord activity = new ActivityBuilder(mService)
124 .setTask(task)
125 .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity"))
126 .build();
127 return task;
128 }
Tadashi G. Takaokadee5a4d2018-09-19 11:59:40 +0900129}