Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 20 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
| 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 23 | import static android.view.Display.DEFAULT_DISPLAY; |
| 24 | |
| 25 | import static com.android.server.am.ActivityDisplay.POSITION_BOTTOM; |
| 26 | |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | import android.app.ActivityManager.RunningTaskInfo; |
| 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.platform.test.annotations.Presubmit; |
| 33 | import android.support.test.InstrumentationRegistry; |
| 34 | import android.support.test.filters.MediumTest; |
| 35 | import android.support.test.runner.AndroidJUnit4; |
| 36 | import android.util.SparseArray; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 44 | /** |
| 45 | * Tests for {@link RunningTasks}. |
| 46 | * |
| 47 | * Build/Install/Run: |
| 48 | * atest WmTests:RunningTasksTest |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 49 | */ |
| 50 | @MediumTest |
| 51 | @Presubmit |
| 52 | @RunWith(AndroidJUnit4.class) |
| 53 | public class RunningTasksTest extends ActivityTestsBase { |
| 54 | |
| 55 | private Context mContext = InstrumentationRegistry.getContext(); |
| 56 | private ActivityManagerService mService; |
| 57 | |
| 58 | private RunningTasks mRunningTasks; |
| 59 | |
| 60 | @Before |
| 61 | @Override |
| 62 | public void setUp() throws Exception { |
| 63 | super.setUp(); |
| 64 | |
| 65 | mService = createActivityManagerService(); |
| 66 | mRunningTasks = new RunningTasks(); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testCollectTasksByLastActiveTime() throws Exception { |
| 71 | // Create a number of stacks with tasks (of incrementing active time) |
| 72 | final ActivityStackSupervisor supervisor = mService.mStackSupervisor; |
| 73 | final SparseArray<ActivityDisplay> displays = new SparseArray<>(); |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 74 | final ActivityDisplay display = new TestActivityDisplay(supervisor, DEFAULT_DISPLAY); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 75 | displays.put(DEFAULT_DISPLAY, display); |
| 76 | |
| 77 | final int numStacks = 2; |
| 78 | for (int stackIndex = 0; stackIndex < numStacks; stackIndex++) { |
| 79 | final ActivityStack stack = new TestActivityStack(display, stackIndex, supervisor, |
| 80 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true); |
| 81 | display.addChild(stack, POSITION_BOTTOM); |
| 82 | } |
| 83 | |
| 84 | final int numTasks = 10; |
| 85 | int activeTime = 0; |
| 86 | for (int i = 0; i < numTasks; i++) { |
| 87 | createTask(display.getChildAt(i % numStacks), ".Task" + i, i, activeTime++); |
| 88 | } |
| 89 | |
| 90 | // Ensure that the latest tasks were returned in order of decreasing last active time, |
| 91 | // collected from all tasks across all the stacks |
| 92 | final int numFetchTasks = 5; |
| 93 | ArrayList<RunningTaskInfo> tasks = new ArrayList<>(); |
| 94 | mRunningTasks.getTasks(5, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED, |
| 95 | displays, -1 /* callingUid */, true /* allowed */); |
| 96 | assertTrue(tasks.size() == numFetchTasks); |
| 97 | for (int i = 0; i < numFetchTasks; i++) { |
| 98 | assertTrue(tasks.get(i).id == (numTasks - i - 1)); |
| 99 | } |
| 100 | |
| 101 | // Ensure that requesting more than the total number of tasks only returns the subset |
| 102 | // and does not crash |
| 103 | tasks.clear(); |
| 104 | mRunningTasks.getTasks(100, tasks, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED, |
| 105 | displays, -1 /* callingUid */, true /* allowed */); |
| 106 | assertTrue(tasks.size() == numTasks); |
| 107 | for (int i = 0; i < numTasks; i++) { |
| 108 | assertTrue(tasks.get(i).id == (numTasks - i - 1)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Create a task with a single activity in it, with the given last active time. |
| 114 | */ |
| 115 | private TaskRecord createTask(ActivityStack stack, String className, int taskId, |
| 116 | int lastActiveTime) { |
| 117 | final TaskRecord task = new TaskBuilder(mService.mStackSupervisor) |
| 118 | .setComponent(new ComponentName(mContext.getPackageName(), className)) |
| 119 | .setTaskId(taskId) |
| 120 | .setStack(stack) |
| 121 | .build(); |
| 122 | task.lastActiveTime = lastActiveTime; |
| 123 | final ActivityRecord activity = new ActivityBuilder(mService) |
| 124 | .setTask(task) |
| 125 | .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity")) |
| 126 | .build(); |
| 127 | return task; |
| 128 | } |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 129 | } |