TaskSnapshotCacheTest.java revision 35e3f53a30588b79e0309fdbeef29a8c18eef65d
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// TODO(b/35196891): Add back to presubmit once the bug is fixed.
52//@Presubmit
53@RunWith(AndroidJUnit4.class)
54public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase {
55
56    private TaskSnapshotCache mCache;
57
58    @Before
59    public void setUp() throws Exception {
60        super.setUp();
61        mCache = new TaskSnapshotCache(sWm, mLoader);
62    }
63
64    @Test
65    public void testAppRemoved() throws Exception {
66        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
67        mCache.putSnapshot(window.getTask(), createSnapshot());
68        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
69                false /* restoreFromDisk */, false /* reducedResolution */));
70        mCache.onAppRemoved(window.mAppToken);
71        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
72                false /* restoreFromDisk */, false /* reducedResolution */));
73    }
74
75    @Test
76    public void testAppDied() throws Exception {
77        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
78        mCache.putSnapshot(window.getTask(), createSnapshot());
79        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
80                false /* restoreFromDisk */, false /* reducedResolution */));
81        mCache.onAppDied(window.mAppToken);
82
83        // Should still be in the retrieval cache.
84        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
85                false /* restoreFromDisk */, false /* reducedResolution */));
86
87        // Trash retrieval cache.
88        for (int i = 0; i < 20; i++) {
89            mCache.putSnapshot(createWindow(null, FIRST_APPLICATION_WINDOW, "window").getTask(),
90                    createSnapshot());
91        }
92
93        // Should not be in cache anymore
94        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
95                false /* restoreFromDisk */, false /* reducedResolution */));
96    }
97
98    @Test
99    public void testTaskRemoved() throws Exception {
100        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
101        mCache.putSnapshot(window.getTask(), createSnapshot());
102        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
103                false /* restoreFromDisk */, false /* reducedResolution */));
104        mCache.onTaskRemoved(window.getTask().mTaskId);
105        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
106                false /* restoreFromDisk */, false /* reducedResolution */));
107    }
108
109    @Test
110    public void testReduced_notCached() throws Exception {
111        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
112        mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot());
113        mPersister.waitForQueueEmpty();
114        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
115                false /* restoreFromDisk */, false /* reducedResolution */));
116
117        // Load it from disk
118        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
119                true /* restoreFromDisk */, true /* reducedResolution */));
120
121        // Make sure it's not in the cache now.
122        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
123                false /* restoreFromDisk */, false /* reducedResolution */));
124    }
125
126    @Test
127    public void testRestoreFromDisk() throws Exception {
128        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
129        mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot());
130        mPersister.waitForQueueEmpty();
131        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
132                false /* restoreFromDisk */, false /* reducedResolution */));
133
134        // Load it from disk
135        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
136                true /* restoreFromDisk */, false /* reducedResolution */));
137
138        // Make sure it's in the cache now.
139        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
140                false /* restoreFromDisk */, false /* reducedResolution */));
141    }
142}
143