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; |
| 21 | import static android.view.Display.DEFAULT_DISPLAY; |
| 22 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 23 | import static com.android.server.wm.ActivityDisplay.POSITION_BOTTOM; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 24 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 25 | import static com.google.common.truth.Truth.assertThat; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 28 | |
| 29 | import android.app.ActivityManager.RunningTaskInfo; |
| 30 | import android.content.ComponentName; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 31 | import android.platform.test.annotations.Presubmit; |
Nicholas Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 32 | import android.util.ArraySet; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 33 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 34 | import androidx.test.filters.MediumTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 35 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | |
| 41 | /** |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 42 | * Build/Install/Run: |
| 43 | * atest WmTests:RunningTasksTest |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 44 | */ |
| 45 | @MediumTest |
| 46 | @Presubmit |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 47 | public class RunningTasksTest extends ActivityTestsBase { |
| 48 | |
Nicholas Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 49 | private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>(); |
| 50 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 51 | private RunningTasks mRunningTasks; |
| 52 | |
| 53 | @Before |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 54 | public void setUp() throws Exception { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 55 | mRunningTasks = new RunningTasks(); |
| 56 | } |
| 57 | |
| 58 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 59 | public void testCollectTasksByLastActiveTime() { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 60 | // Create a number of stacks with tasks (of incrementing active time) |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 61 | final ArrayList<ActivityDisplay> displays = new ArrayList<>(); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 62 | final ActivityDisplay display = TestActivityDisplay.create(mSupervisor, DEFAULT_DISPLAY); |
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++) { |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 67 | final ActivityStack stack = |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 68 | new StackBuilder(mRootActivityContainer).setCreateActivity(false).build(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 69 | 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 Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 83 | displays, -1 /* callingUid */, true /* allowed */, true /*crossUser */, |
| 84 | PROFILE_IDS); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 85 | assertThat(tasks).hasSize(numFetchTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 86 | for (int i = 0; i < numFetchTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 87 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 88 | } |
| 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 Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 94 | displays, -1 /* callingUid */, true /* allowed */, true /* crossUser */, |
| 95 | PROFILE_IDS); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 96 | assertThat(tasks).hasSize(numTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 97 | for (int i = 0; i < numTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 98 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 99 | } |
| 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. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 119 | } |