blob: 290f69a7a7e7f37cc47c86f43d3df4938b5d9b98 [file] [log] [blame]
Jorim Jaggi10abe2f2017-01-03 16:44:46 +01001/*
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;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010021import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010022import 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;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010030import android.graphics.Canvas;
31import android.graphics.Color;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010032import android.graphics.GraphicBuffer;
33import android.graphics.PixelFormat;
34import android.graphics.Rect;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010035import android.os.Debug;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010036import android.platform.test.annotations.Presubmit;
37import android.support.test.filters.SmallTest;
38import android.support.test.runner.AndroidJUnit4;
39
Jorim Jaggi7361bab2017-01-16 17:17:58 +010040import org.junit.After;
41import org.junit.Before;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010042import 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
Wale Ogunwale4a73a222017-02-13 07:14:20 -080051// TODO(b/35196891): Add back to presubmit once the bug is fixed.
52//@Presubmit
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010053@RunWith(AndroidJUnit4.class)
Jorim Jaggi7361bab2017-01-16 17:17:58 +010054public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010055
Jorim Jaggi7361bab2017-01-16 17:17:58 +010056 private TaskSnapshotCache mCache;
57
58 @Before
59 public void setUp() throws Exception {
60 super.setUp();
61 mCache = new TaskSnapshotCache(sWm, mLoader);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010062 }
63
Jorim Jaggi7361bab2017-01-16 17:17:58 +010064 @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 */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010069 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +010070 mCache.onAppRemoved(window.mAppToken);
71 assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010072 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +010073 }
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 */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010080 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +010081 mCache.onAppDied(window.mAppToken);
82
83 // Should still be in the retrieval cache.
84 assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010085 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +010086
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 */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010095 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +010096 }
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 */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100103 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100104 mCache.onTaskRemoved(window.getTask().mTaskId);
105 assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100106 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 */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100124 }
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,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100132 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100133
134 // Load it from disk
135 assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100136 true /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100137
138 // Make sure it's in the cache now.
139 assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100140 false /* restoreFromDisk */, false /* reducedResolution */));
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100141 }
142}