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 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 19 | import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 20 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 21 | import static junit.framework.Assert.assertNotNull; |
| 22 | import static junit.framework.Assert.assertNull; |
| 23 | |
Wale Ogunwale | 68e882c | 2017-05-03 07:54:23 -0700 | [diff] [blame] | 24 | import android.platform.test.annotations.Presubmit; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 25 | |
| 26 | import androidx.test.filters.SmallTest; |
| 27 | import androidx.test.runner.AndroidJUnit4; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 28 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 29 | import org.junit.Before; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
| 33 | /** |
| 34 | * Test class for {@link TaskSnapshotCache}. |
| 35 | * |
John Reck | 3d294e7 | 2018-09-21 20:26:48 +0000 | [diff] [blame] | 36 | * runtest frameworks-services -c com.android.server.wm.TaskSnapshotCacheTest |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 37 | */ |
| 38 | @SmallTest |
Wale Ogunwale | 68e882c | 2017-05-03 07:54:23 -0700 | [diff] [blame] | 39 | @Presubmit |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 40 | @RunWith(AndroidJUnit4.class) |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 41 | public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 42 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 43 | private TaskSnapshotCache mCache; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() throws Exception { |
| 47 | super.setUp(); |
| 48 | mCache = new TaskSnapshotCache(sWm, mLoader); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 51 | @Test |
| 52 | public void testAppRemoved() throws Exception { |
| 53 | final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); |
| 54 | mCache.putSnapshot(window.getTask(), createSnapshot()); |
| 55 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 56 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 57 | mCache.onAppRemoved(window.mAppToken); |
| 58 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 59 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testAppDied() throws Exception { |
| 64 | final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); |
| 65 | mCache.putSnapshot(window.getTask(), createSnapshot()); |
| 66 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 67 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 68 | mCache.onAppDied(window.mAppToken); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 69 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 70 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testTaskRemoved() throws Exception { |
| 75 | final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); |
| 76 | mCache.putSnapshot(window.getTask(), createSnapshot()); |
| 77 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 78 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 79 | mCache.onTaskRemoved(window.getTask().mTaskId); |
| 80 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 81 | false /* restoreFromDisk */, false /* reducedResolution */)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testReduced_notCached() throws Exception { |
| 86 | final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); |
| 87 | mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot()); |
| 88 | mPersister.waitForQueueEmpty(); |
| 89 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
| 90 | false /* restoreFromDisk */, false /* reducedResolution */)); |
| 91 | |
| 92 | // Load it from disk |
| 93 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
| 94 | true /* restoreFromDisk */, true /* reducedResolution */)); |
| 95 | |
| 96 | // Make sure it's not in the cache now. |
| 97 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
| 98 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testRestoreFromDisk() throws Exception { |
| 103 | final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); |
| 104 | mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot()); |
| 105 | mPersister.waitForQueueEmpty(); |
| 106 | assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 107 | false /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 108 | |
| 109 | // Load it from disk |
| 110 | assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 111 | true /* restoreFromDisk */, false /* reducedResolution */)); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 112 | } |
| 113 | } |