blob: 3e316f674dbfd3cacabbff4e029f5b8ff1bddde7 [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;
Nicholas Sauerd6b44522019-09-10 20:23:41 -070032import android.util.ArraySet;
Winson Chung61c9e5a2017-10-11 10:39:32 -070033
Brett Chabota26eda92018-07-23 13:08:30 -070034import androidx.test.filters.MediumTest;
Brett Chabota26eda92018-07-23 13:08:30 -070035
Winson Chung61c9e5a2017-10-11 10:39:32 -070036import org.junit.Before;
37import org.junit.Test;
Winson Chung61c9e5a2017-10-11 10:39:32 -070038
39import java.util.ArrayList;
40
41/**
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090042 * Build/Install/Run:
43 * atest WmTests:RunningTasksTest
Winson Chung61c9e5a2017-10-11 10:39:32 -070044 */
45@MediumTest
46@Presubmit
Winson Chung61c9e5a2017-10-11 10:39:32 -070047public class RunningTasksTest extends ActivityTestsBase {
48
Nicholas Sauerd6b44522019-09-10 20:23:41 -070049 private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>();
50
Winson Chung61c9e5a2017-10-11 10:39:32 -070051 private RunningTasks mRunningTasks;
52
53 @Before
Winson Chung61c9e5a2017-10-11 10:39:32 -070054 public void setUp() throws Exception {
Winson Chung61c9e5a2017-10-11 10:39:32 -070055 mRunningTasks = new RunningTasks();
56 }
57
58 @Test
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090059 public void testCollectTasksByLastActiveTime() {
Winson Chung61c9e5a2017-10-11 10:39:32 -070060 // Create a number of stacks with tasks (of incrementing active time)
Riddle Hsu86cb7de2018-08-13 23:29:58 +080061 final ArrayList<ActivityDisplay> displays = new ArrayList<>();
Riddle Hsub70b36d2018-09-11 21:20:02 +080062 final ActivityDisplay display = TestActivityDisplay.create(mSupervisor, DEFAULT_DISPLAY);
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++) {
Louis Changf2835df2018-10-17 15:14:45 +080067 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -080068 new StackBuilder(mRootActivityContainer).setCreateActivity(false).build();
Winson Chung61c9e5a2017-10-11 10:39:32 -070069 display.addChild(stack, POSITION_BOTTOM);
70 }
71
72 final int numTasks = 10;
73 int activeTime = 0;
74 for (int i = 0; i < numTasks; i++) {
75 createTask(display.getChildAt(i % numStacks), ".Task" + i, i, activeTime++);
76 }
77
78 // Ensure that the latest tasks were returned in order of decreasing last active time,
79 // collected from all tasks across all the stacks
80 final int numFetchTasks = 5;
81 ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
82 mRunningTasks.getTasks(5, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
Nicholas Sauerd6b44522019-09-10 20:23:41 -070083 displays, -1 /* callingUid */, true /* allowed */, true /*crossUser */,
84 PROFILE_IDS);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090085 assertThat(tasks).hasSize(numFetchTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070086 for (int i = 0; i < numFetchTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090087 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -070088 }
89
90 // Ensure that requesting more than the total number of tasks only returns the subset
91 // and does not crash
92 tasks.clear();
93 mRunningTasks.getTasks(100, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED,
Nicholas Sauerd6b44522019-09-10 20:23:41 -070094 displays, -1 /* callingUid */, true /* allowed */, true /* crossUser */,
95 PROFILE_IDS);
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090096 assertThat(tasks).hasSize(numTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070097 for (int i = 0; i < numTasks; i++) {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090098 assertEquals(numTasks - i - 1, tasks.get(i).id);
Winson Chung61c9e5a2017-10-11 10:39:32 -070099 }
100 }
101
102 /**
103 * Create a task with a single activity in it, with the given last active time.
104 */
105 private TaskRecord createTask(ActivityStack stack, String className, int taskId,
106 int lastActiveTime) {
107 final TaskRecord task = new TaskBuilder(mService.mStackSupervisor)
108 .setComponent(new ComponentName(mContext.getPackageName(), className))
109 .setTaskId(taskId)
110 .setStack(stack)
111 .build();
112 task.lastActiveTime = lastActiveTime;
113 final ActivityRecord activity = new ActivityBuilder(mService)
114 .setTask(task)
115 .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity"))
116 .build();
117 return task;
118 }
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900119}