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; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 32 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 33 | import androidx.test.filters.MediumTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 34 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 37 | |
| 38 | import java.util.ArrayList; |
| 39 | |
| 40 | /** |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 41 | * Build/Install/Run: |
| 42 | * atest WmTests:RunningTasksTest |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 43 | */ |
| 44 | @MediumTest |
| 45 | @Presubmit |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 46 | public class RunningTasksTest extends ActivityTestsBase { |
| 47 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 48 | private RunningTasks mRunningTasks; |
| 49 | |
| 50 | @Before |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 51 | public void setUp() throws Exception { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 52 | mRunningTasks = new RunningTasks(); |
| 53 | } |
| 54 | |
| 55 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 56 | public void testCollectTasksByLastActiveTime() { |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 57 | // Create a number of stacks with tasks (of incrementing active time) |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 58 | final ArrayList<ActivityDisplay> displays = new ArrayList<>(); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 59 | final ActivityDisplay display = TestActivityDisplay.create(mSupervisor, DEFAULT_DISPLAY); |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 60 | displays.add(display); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 61 | |
| 62 | final int numStacks = 2; |
| 63 | for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) { |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 64 | final ActivityStack stack = |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 65 | new StackBuilder(mRootActivityContainer).setCreateActivity(false).build(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 66 | 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. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 81 | assertThat(tasks).hasSize(numFetchTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 82 | for (int i = 0; i < numFetchTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 83 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 84 | } |
| 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. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 91 | assertThat(tasks).hasSize(numTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 92 | for (int i = 0; i < numTasks; i++) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 93 | assertEquals(numTasks - i - 1, tasks.get(i).id); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 94 | } |
| 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. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 114 | } |