blob: dc964806b7a9d3a73035445ce7a638d2b7b9c340 [file] [log] [blame]
Winson Chung61c9e5a2017-10-11 10:39:32 -07001/*
Wale Ogunwale59507092018-10-29 09:00:30 -07002 * Copyright (C) 2018 The Android Open Source Project
Winson Chung61c9e5a2017-10-11 10:39:32 -07003 *
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
Wale Ogunwale59507092018-10-29 09:00:30 -070014 * limitations under the License
Winson Chung61c9e5a2017-10-11 10:39:32 -070015 */
16
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Winson Chung61c9e5a2017-10-11 10:39:32 -070018
Winson Chung61c9e5a2017-10-11 10:39:32 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Winson Chung61c9e5a2017-10-11 10:39:32 -070020import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
21import static android.view.Display.DEFAULT_DISPLAY;
22
Wale Ogunwale59507092018-10-29 09:00:30 -070023import static com.android.server.wm.ActivityDisplay.POSITION_BOTTOM;
Winson Chung61c9e5a2017-10-11 10:39:32 -070024
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090025import static com.google.common.truth.Truth.assertThat;
26
27import static org.junit.Assert.assertEquals;
Winson Chung61c9e5a2017-10-11 10:39:32 -070028
29import android.app.ActivityManager.RunningTaskInfo;
30import android.content.ComponentName;
Winson Chung61c9e5a2017-10-11 10:39:32 -070031import android.platform.test.annotations.Presubmit;
Winson Chung61c9e5a2017-10-11 10:39:32 -070032
Brett Chabota26eda92018-07-23 13:08:30 -070033import androidx.test.filters.MediumTest;
Brett Chabota26eda92018-07-23 13:08:30 -070034
Winson Chung61c9e5a2017-10-11 10:39:32 -070035import org.junit.Before;
36import org.junit.Test;
Winson Chung61c9e5a2017-10-11 10:39:32 -070037
38import java.util.ArrayList;
39
40/**
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090041 * Build/Install/Run:
42 * atest WmTests:RunningTasksTest
Winson Chung61c9e5a2017-10-11 10:39:32 -070043 */
44@MediumTest
45@Presubmit
Winson Chung61c9e5a2017-10-11 10:39:32 -070046public class RunningTasksTest extends ActivityTestsBase {
47
Winson Chung61c9e5a2017-10-11 10:39:32 -070048 private RunningTasks mRunningTasks;
49
50 @Before
Winson Chung61c9e5a2017-10-11 10:39:32 -070051 public void setUp() throws Exception {
Winson Chung61c9e5a2017-10-11 10:39:32 -070052 mRunningTasks = new RunningTasks();
53 }
54
55 @Test
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090056 public void testCollectTasksByLastActiveTime() {
Winson Chung61c9e5a2017-10-11 10:39:32 -070057 // Create a number of stacks with tasks (of incrementing active time)
Riddle Hsu86cb7de2018-08-13 23:29:58 +080058 final ArrayList<ActivityDisplay> displays = new ArrayList<>();
Riddle Hsub70b36d2018-09-11 21:20:02 +080059 final ActivityDisplay display = TestActivityDisplay.create(mSupervisor, DEFAULT_DISPLAY);
Riddle Hsu86cb7de2018-08-13 23:29:58 +080060 displays.add(display);
Winson Chung61c9e5a2017-10-11 10:39:32 -070061
62 final int numStacks = 2;
63 for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) {
Louis Changf2835df2018-10-17 15:14:45 +080064 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -080065 new StackBuilder(mRootActivityContainer).setCreateActivity(false).build();
Winson Chung61c9e5a2017-10-11 10:39:32 -070066 display.addChild(stack, POSITION_BOTTOM);
67 }
68
69 final int numTasks = 10;
70 int activeTime = 0;
71 for (int i = 0; i < numTasks; i++) {
72 createTask(display.getChildAt(i % numStacks), ".Task" + i, i, activeTime++);
73 }
74
75 // Ensure that the latest tasks were returned in order of decreasing last active time,
76 // collected from all tasks across all the stacks
77 final int numFetchTasks = 5;
78 ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
79 mRunningTasks.getTasks(5, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
80 displays, -1 /* callingUid */, true /* allowed */);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090081 assertThat(tasks).hasSize(numFetchTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070082 for (int i = 0; i < numFetchTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090083 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -070084 }
85
86 // Ensure that requesting more than the total number of tasks only returns the subset
87 // and does not crash
88 tasks.clear();
89 mRunningTasks.getTasks(100, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
90 displays, -1 /* callingUid */, true /* allowed */);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090091 assertThat(tasks).hasSize(numTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070092 for (int i = 0; i < numTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090093 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -070094 }
95 }
96
97 /**
98 * Create a task with a single activity in it, with the given last active time.
99 */
100 private TaskRecord createTask(ActivityStack stack, String className, int taskId,
101 int lastActiveTime) {
102 final TaskRecord task = new TaskBuilder(mService.mStackSupervisor)
103 .setComponent(new ComponentName(mContext.getPackageName(), className))
104 .setTaskId(taskId)
105 .setStack(stack)
106 .build();
107 task.lastActiveTime = lastActiveTime;
108 final ActivityRecord activity = new ActivityBuilder(mService)
109 .setTask(task)
110 .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity"))
111 .build();
112 return task;
113 }
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900114}