TaskSnapshotCacheTest.java revision 7361babf94baa985eaa8bd2e94fcb16f00670998
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.wm;
18
19import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
20import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER;
21import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY;
22import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER;
23import static android.graphics.GraphicBuffer.create;
24import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static junit.framework.Assert.assertNotNull;
26import static junit.framework.Assert.assertNull;
27
28import android.app.ActivityManager.TaskSnapshot;
29import android.content.res.Configuration;
30import android.graphics.Canvas;
31import android.graphics.Color;
32import android.graphics.GraphicBuffer;
33import android.graphics.PixelFormat;
34import android.graphics.Rect;
35import android.os.Debug;
36import android.platform.test.annotations.Presubmit;
37import android.support.test.filters.SmallTest;
38import android.support.test.runner.AndroidJUnit4;
39
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
45/**
46 * Test class for {@link TaskSnapshotCache}.
47 *
48 * runtest frameworks-services -c com.android.server.wm.TaskSnapshotCacheTest
49 */
50@SmallTest
51@Presubmit
52@RunWith(AndroidJUnit4.class)
53public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase {
54
55    private TaskSnapshotCache mCache;
56
57    @Before
58    public void setUp() throws Exception {
59        super.setUp();
60        mCache = new TaskSnapshotCache(sWm, mLoader);
61    }
62
63    @Test
64    public void testAppRemoved() throws Exception {
65        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
66        mCache.putSnapshot(window.getTask(), createSnapshot());
67        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
68                false /* restoreFromDisk */));
69        mCache.onAppRemoved(window.mAppToken);
70        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
71                false /* restoreFromDisk */));
72    }
73
74    @Test
75    public void testAppDied() throws Exception {
76        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
77        mCache.putSnapshot(window.getTask(), createSnapshot());
78        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
79                false /* restoreFromDisk */));
80        mCache.onAppDied(window.mAppToken);
81
82        // Should still be in the retrieval cache.
83        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
84                false /* restoreFromDisk */));
85
86        // Trash retrieval cache.
87        for (int i = 0; i < 20; i++) {
88            mCache.putSnapshot(createWindow(null, FIRST_APPLICATION_WINDOW, "window").getTask(),
89                    createSnapshot());
90        }
91
92        // Should not be in cache anymore
93        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
94                false /* restoreFromDisk */));
95    }
96
97    @Test
98    public void testTaskRemoved() throws Exception {
99        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
100        mCache.putSnapshot(window.getTask(), createSnapshot());
101        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
102                false /* restoreFromDisk */));
103        mCache.onTaskRemoved(window.getTask().mTaskId);
104        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
105                false /* restoreFromDisk */));
106    }
107
108    @Test
109    public void testRestoreFromDisk() throws Exception {
110        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
111        mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot());
112        mPersister.waitForQueueEmpty();
113        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
114                false /* restoreFromDisk */));
115
116        // Load it from disk
117        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
118                true /* restoreFromDisk */));
119
120        // Make sure it's in the cache now.
121        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
122                false /* restoreFromDisk */));
123    }
124}
125