1/*
2 * Copyright (C) 2016 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.wm;
18
19import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
20import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
21import static android.view.WindowManager.TRANSIT_UNSET;
22import static com.android.server.wm.TaskSnapshotController.*;
23import static junit.framework.Assert.assertEquals;
24import static junit.framework.Assert.assertFalse;
25import static junit.framework.Assert.assertTrue;
26
27import android.platform.test.annotations.Presubmit;
28import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
30import android.util.ArraySet;
31
32import com.google.android.collect.Sets;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36/**
37 * Test class for {@link TaskSnapshotController}.
38 *
39 * runtest frameworks-services -c com.android.server.wm.TaskSnapshotControllerTest
40 */
41@SmallTest
42@Presubmit
43@RunWith(AndroidJUnit4.class)
44public class TaskSnapshotControllerTest extends WindowTestsBase {
45
46    @Test
47    public void testGetClosingApps_closing() throws Exception {
48        final WindowState closingWindow = createWindow(null, FIRST_APPLICATION_WINDOW,
49                "closingWindow");
50        closingWindow.mAppToken.setVisibility(null, false /* visible */, TRANSIT_UNSET,
51                true /* performLayout */, false /* isVoiceInteraction */);
52        final ArraySet<AppWindowToken> closingApps = new ArraySet<>();
53        closingApps.add(closingWindow.mAppToken);
54        final ArraySet<Task> closingTasks = new ArraySet<>();
55        sWm.mTaskSnapshotController.getClosingTasks(closingApps, closingTasks);
56        assertEquals(1, closingTasks.size());
57        assertEquals(closingWindow.mAppToken.getTask(), closingTasks.valueAt(0));
58    }
59
60    @Test
61    public void testGetClosingApps_notClosing() throws Exception {
62        final WindowState closingWindow = createWindow(null, FIRST_APPLICATION_WINDOW,
63                "closingWindow");
64        final WindowState openingWindow = createAppWindow(closingWindow.getTask(),
65                FIRST_APPLICATION_WINDOW, "openingWindow");
66        closingWindow.mAppToken.setVisibility(null, false /* visible */, TRANSIT_UNSET,
67                true /* performLayout */, false /* isVoiceInteraction */);
68        openingWindow.mAppToken.setVisibility(null, true /* visible */, TRANSIT_UNSET,
69                true /* performLayout */, false /* isVoiceInteraction */);
70        final ArraySet<AppWindowToken> closingApps = new ArraySet<>();
71        closingApps.add(closingWindow.mAppToken);
72        final ArraySet<Task> closingTasks = new ArraySet<>();
73        sWm.mTaskSnapshotController.getClosingTasks(closingApps, closingTasks);
74        assertEquals(0, closingTasks.size());
75    }
76
77    @Test
78    public void testGetClosingApps_skipClosingAppsSnapshotTasks() throws Exception {
79        final WindowState closingWindow = createWindow(null, FIRST_APPLICATION_WINDOW,
80                "closingWindow");
81        closingWindow.mAppToken.setVisibility(null, false /* visible */, TRANSIT_UNSET,
82                true /* performLayout */, false /* isVoiceInteraction */);
83        final ArraySet<AppWindowToken> closingApps = new ArraySet<>();
84        closingApps.add(closingWindow.mAppToken);
85        final ArraySet<Task> closingTasks = new ArraySet<>();
86        sWm.mTaskSnapshotController.addSkipClosingAppSnapshotTasks(
87                Sets.newArraySet(closingWindow.mAppToken.getTask()));
88        sWm.mTaskSnapshotController.getClosingTasks(closingApps, closingTasks);
89        assertEquals(0, closingTasks.size());
90    }
91
92    @Test
93    public void testGetSnapshotMode() throws Exception {
94        final WindowState disabledWindow = createWindow(null,
95                FIRST_APPLICATION_WINDOW, mDisplayContent, "disabledWindow");
96        disabledWindow.mAppToken.setDisablePreviewScreenshots(true);
97        assertEquals(SNAPSHOT_MODE_APP_THEME,
98                sWm.mTaskSnapshotController.getSnapshotMode(disabledWindow.getTask()));
99
100        final WindowState normalWindow = createWindow(null,
101                FIRST_APPLICATION_WINDOW, mDisplayContent, "normalWindow");
102        assertEquals(SNAPSHOT_MODE_REAL,
103                sWm.mTaskSnapshotController.getSnapshotMode(normalWindow.getTask()));
104
105        final WindowState secureWindow = createWindow(null,
106                FIRST_APPLICATION_WINDOW, mDisplayContent, "secureWindow");
107        secureWindow.mAttrs.flags |= FLAG_SECURE;
108        assertEquals(SNAPSHOT_MODE_APP_THEME,
109                sWm.mTaskSnapshotController.getSnapshotMode(secureWindow.getTask()));
110    }
111}
112