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 |
| 51 | @Presubmit |
| 52 | @RunWith(AndroidJUnit4.class) |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 53 | public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 54 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 55 | private TaskSnapshotCache mCache; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() throws Exception { |
| 59 | super.setUp(); |
| 60 | mCache = new TaskSnapshotCache(sWm, mLoader); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 */)); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 123 | } |
| 124 | } |