Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE; |
| 20 | import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 21 | import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 22 | import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER; |
| 23 | import static android.graphics.GraphicBuffer.create; |
| 24 | import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; |
| 25 | import static junit.framework.Assert.assertNotNull; |
| 26 | import static junit.framework.Assert.assertNull; |
| 27 | |
| 28 | import android.app.ActivityManager.TaskSnapshot; |
| 29 | import android.content.res.Configuration; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 30 | import android.graphics.Canvas; |
| 31 | import android.graphics.Color; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 32 | import android.graphics.GraphicBuffer; |
| 33 | import android.graphics.PixelFormat; |
| 34 | import android.graphics.Rect; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 35 | import android.os.Debug; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 36 | import android.platform.test.annotations.Presubmit; |
| 37 | import android.support.test.filters.SmallTest; |
| 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 42 | import org.junit.Test; |
| 43 | import 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 Ogunwale | 4a73a22 | 2017-02-13 07:14:20 -0800 | [diff] [blame] | 51 | // TODO(b/35196891): Add back to presubmit once the bug is fixed. |
| 52 | //@Presubmit |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 53 | @RunWith(AndroidJUnit4.class) |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 54 | public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 55 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 56 | private TaskSnapshotCache mCache; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | super.setUp(); |
| 61 | mCache = new TaskSnapshotCache(sWm, mLoader); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 69 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 70 | mCache.onAppRemoved(window.mAppToken); |
| 71 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 72 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 80 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 81 | mCache.onAppDied(window.mAppToken); |
| 82 | |
| 83 | // Should still be in the retrieval cache. |
| 84 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 85 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 95 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 103 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 104 | mCache.onTaskRemoved(window.getTask().mTaskId); |
| 105 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 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 */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 132 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 133 | |
| 134 | // Load it from disk |
| 135 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 136 | true /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 137 | |
| 138 | // Make sure it's in the cache now. |
| 139 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame^] | 140 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 141 | } |
| 142 | } |