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