blob: 5c9ccae8b7d1ba3a3fe08fd9459a5cbf728510fc [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;
Winson Chung61c9e5a2017-10-11 10:39:32 -070021
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090022import static com.google.common.truth.Truth.assertThat;
23
24import static org.junit.Assert.assertEquals;
Winson Chung61c9e5a2017-10-11 10:39:32 -070025
26import android.app.ActivityManager.RunningTaskInfo;
27import android.content.ComponentName;
Winson Chung61c9e5a2017-10-11 10:39:32 -070028import android.platform.test.annotations.Presubmit;
Nicholas Sauer3f9249f2019-09-10 20:23:41 -070029import android.util.ArraySet;
Winson Chung61c9e5a2017-10-11 10:39:32 -070030
Brett Chabota26eda92018-07-23 13:08:30 -070031import androidx.test.filters.MediumTest;
Brett Chabota26eda92018-07-23 13:08:30 -070032
Winson Chung61c9e5a2017-10-11 10:39:32 -070033import org.junit.Before;
34import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080035import org.junit.runner.RunWith;
Winson Chung61c9e5a2017-10-11 10:39:32 -070036
37import java.util.ArrayList;
38
39/**
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090040 * Build/Install/Run:
41 * atest WmTests:RunningTasksTest
Winson Chung61c9e5a2017-10-11 10:39:32 -070042 */
43@MediumTest
44@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080045@RunWith(WindowTestRunner.class)
Winson Chung61c9e5a2017-10-11 10:39:32 -070046public class RunningTasksTest extends ActivityTestsBase {
47
Nicholas Sauer3f9249f2019-09-10 20:23:41 -070048 private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>();
49
Winson Chung61c9e5a2017-10-11 10:39:32 -070050 private RunningTasks mRunningTasks;
51
52 @Before
Winson Chung61c9e5a2017-10-11 10:39:32 -070053 public void setUp() throws Exception {
Winson Chung61c9e5a2017-10-11 10:39:32 -070054 mRunningTasks = new RunningTasks();
55 }
56
57 @Test
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090058 public void testCollectTasksByLastActiveTime() {
Winson Chung61c9e5a2017-10-11 10:39:32 -070059 // Create a number of stacks with tasks (of incrementing active time)
Riddle Hsu86cb7de2018-08-13 23:29:58 +080060 final ArrayList<ActivityDisplay> displays = new ArrayList<>();
Evan Rosky4a51dbc02019-09-11 17:28:07 -070061 final ActivityDisplay display =
62 new TestActivityDisplay.Builder(mService, 1000, 2500).build();
Riddle Hsu86cb7de2018-08-13 23:29:58 +080063 displays.add(display);
Winson Chung61c9e5a2017-10-11 10:39:32 -070064
65 final int numStacks = 2;
66 for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070067 final ActivityStack stack = new StackBuilder(mRootActivityContainer)
68 .setCreateActivity(false)
Evan Rosky4a51dbc02019-09-11 17:28:07 -070069 .setDisplay(display)
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070070 .setOnTop(false)
71 .build();
Winson Chung61c9e5a2017-10-11 10:39:32 -070072 }
73
74 final int numTasks = 10;
75 int activeTime = 0;
76 for (int i = 0; i < numTasks; i++) {
Louis Chang2453d062019-11-19 22:30:48 +080077 createTask(display.getStackAt(i % numStacks), ".Task" + i, i, activeTime++);
Winson Chung61c9e5a2017-10-11 10:39:32 -070078 }
79
80 // Ensure that the latest tasks were returned in order of decreasing last active time,
81 // collected from all tasks across all the stacks
82 final int numFetchTasks = 5;
83 ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
84 mRunningTasks.getTasks(5, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
Nicholas Sauer3f9249f2019-09-10 20:23:41 -070085 displays, -1 /* callingUid */, true /* allowed */, true /*crossUser */,
86 PROFILE_IDS);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090087 assertThat(tasks).hasSize(numFetchTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070088 for (int i = 0; i < numFetchTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090089 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -070090 }
91
92 // Ensure that requesting more than the total number of tasks only returns the subset
93 // and does not crash
94 tasks.clear();
95 mRunningTasks.getTasks(100, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
Nicholas Sauer3f9249f2019-09-10 20:23:41 -070096 displays, -1 /* callingUid */, true /* allowed */, true /* crossUser */,
97 PROFILE_IDS);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090098 assertThat(tasks).hasSize(numTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070099 for (int i = 0; i < numTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900100 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -0700101 }
102 }
103
104 /**
105 * Create a task with a single activity in it, with the given last active time.
106 */
Louis Changcdec0802019-11-11 11:45:07 +0800107 private Task createTask(ActivityStack stack, String className, int taskId,
Winson Chung61c9e5a2017-10-11 10:39:32 -0700108 int lastActiveTime) {
Louis Changcdec0802019-11-11 11:45:07 +0800109 final Task task = new TaskBuilder(mService.mStackSupervisor)
Winson Chung61c9e5a2017-10-11 10:39:32 -0700110 .setComponent(new ComponentName(mContext.getPackageName(), className))
111 .setTaskId(taskId)
112 .setStack(stack)
113 .build();
114 task.lastActiveTime = lastActiveTime;
115 final ActivityRecord activity = new ActivityBuilder(mService)
116 .setTask(task)
117 .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity"))
118 .build();
119 return task;
120 }
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900121}