Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 1 | /* |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 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 |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 14 | * limitations under the License |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 18 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 21 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 25 | |
| 26 | import android.app.ActivityManager.RunningTaskInfo; |
| 27 | import android.content.ComponentName; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 28 | import android.platform.test.annotations.Presubmit; |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 29 | import android.util.ArraySet; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 30 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 31 | import androidx.test.filters.MediumTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 35 | import org.junit.runner.RunWith; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
| 38 | |
| 39 | /** |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 40 | * Build/Install/Run: |
| 41 | * atest WmTests:RunningTasksTest |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 42 | */ |
| 43 | @MediumTest |
| 44 | @Presubmit |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 45 | @RunWith(WindowTestRunner.class) |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 46 | public class RunningTasksTest extends ActivityTestsBase { |
| 47 | |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 48 | private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>(); |
| 49 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 50 | private RunningTasks mRunningTasks; |
| 51 | |
| 52 | @Before |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 53 | public void setUp() throws Exception { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 54 | mRunningTasks = new RunningTasks(); |
| 55 | } |
| 56 | |
| 57 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 58 | public void testCollectTasksByLastActiveTime() { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 59 | // Create a number of stacks with tasks (of incrementing active time) |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 60 | final ArrayList<ActivityDisplay> displays = new ArrayList<>(); |
Evan Rosky | 4a51dbc0 | 2019-09-11 17:28:07 -0700 | [diff] [blame] | 61 | final ActivityDisplay display = |
| 62 | new TestActivityDisplay.Builder(mService, 1000, 2500).build(); |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 63 | displays.add(display); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 64 | |
| 65 | final int numStacks = 2; |
| 66 | for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 67 | final ActivityStack stack = new StackBuilder(mRootActivityContainer) |
| 68 | .setCreateActivity(false) |
Evan Rosky | 4a51dbc0 | 2019-09-11 17:28:07 -0700 | [diff] [blame] | 69 | .setDisplay(display) |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 70 | .setOnTop(false) |
| 71 | .build(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | final int numTasks = 10; |
| 75 | int activeTime = 0; |
| 76 | for (int i = 0; i < numTasks; i++) { |
Louis Chang | 2453d06 | 2019-11-19 22:30:48 +0800 | [diff] [blame] | 77 | createTask(display.getStackAt(i % numStacks), ".Task" + i, i, activeTime++); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 78 | } |
| 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 Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 85 | displays, -1 /* callingUid */, true /* allowed */, true /*crossUser */, |
| 86 | PROFILE_IDS); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 87 | assertThat(tasks).hasSize(numFetchTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 88 | for (int i = 0; i < numFetchTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 89 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 90 | } |
| 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 Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 96 | displays, -1 /* callingUid */, true /* allowed */, true /* crossUser */, |
| 97 | PROFILE_IDS); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 98 | assertThat(tasks).hasSize(numTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 99 | for (int i = 0; i < numTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 100 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create a task with a single activity in it, with the given last active time. |
| 106 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 107 | private Task createTask(ActivityStack stack, String className, int taskId, |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 108 | int lastActiveTime) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 109 | final Task task = new TaskBuilder(mService.mStackSupervisor) |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 110 | .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. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 121 | } |