1 /*
2  * Copyright (C) 2020 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.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 
25 import android.content.res.Configuration;
26 import android.graphics.Rect;
27 import android.platform.test.annotations.Presubmit;
28 import android.util.ArraySet;
29 import android.window.TaskSnapshot;
30 
31 import androidx.test.filters.MediumTest;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.MockitoAnnotations;
37 
38 import java.io.File;
39 
40 /**
41  * Test class for {@link TaskSnapshotPersister} and {@link AppSnapshotLoader}
42  *
43  * Build/Install/Run:
44  * atest TaskSnapshotPersisterLoaderTest
45  */
46 @MediumTest
47 @Presubmit
48 @RunWith(WindowTestRunner.class)
49 public class TaskSnapshotLowResDisabledTest extends TaskSnapshotPersisterTestBase {
50 
51     private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40);
52 
53     private TaskSnapshotCache mCache;
54 
TaskSnapshotLowResDisabledTest()55     public TaskSnapshotLowResDisabledTest() {
56         super(0.8f, 0.0f);
57     }
58 
59     @Override
60     @Before
setUp()61     public void setUp() {
62         super.setUp();
63         MockitoAnnotations.initMocks(this);
64         mCache = new TaskSnapshotCache(mWm, mLoader);
65     }
66 
67     @Test
testPersistAndLoadSnapshot()68     public void testPersistAndLoadSnapshot() {
69         mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
70         mSnapshotPersistQueue.waitForQueueEmpty();
71         final File[] files = new File[]{
72                 new File(FILES_DIR.getPath() + "/snapshots/1.proto"),
73                 new File(FILES_DIR.getPath() + "/snapshots/1.jpg")};
74         final File[] nonExistsFiles = new File[]{
75                 new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg")};
76         assertTrueForFiles(files, File::exists, " must exist");
77         assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist");
78         final TaskSnapshot snapshot = mLoader.loadTask(1, mTestUserId, false /* isLowResolution */);
79         assertNotNull(snapshot);
80         assertEquals(MOCK_SNAPSHOT_ID, snapshot.getId());
81         assertEquals(TEST_INSETS, snapshot.getContentInsets());
82         assertNotNull(snapshot.getSnapshot());
83         assertEquals(Configuration.ORIENTATION_PORTRAIT, snapshot.getOrientation());
84         assertNull(mLoader.loadTask(1, mTestUserId, true /* isLowResolution */));
85     }
86 
87     @Test
testRemoveObsoleteFiles()88     public void testRemoveObsoleteFiles() {
89         mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
90         mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
91         final ArraySet<Integer> taskIds = new ArraySet<>();
92         taskIds.add(1);
93         mPersister.removeObsoleteFiles(taskIds, new int[]{mTestUserId});
94         mSnapshotPersistQueue.waitForQueueEmpty();
95         final File[] existsFiles = new File[]{
96                 new File(FILES_DIR.getPath() + "/snapshots/1.proto"),
97                 new File(FILES_DIR.getPath() + "/snapshots/1.jpg")};
98         final File[] nonExistsFiles = new File[]{
99                 new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg"),
100                 new File(FILES_DIR.getPath() + "/snapshots/2.proto"),
101                 new File(FILES_DIR.getPath() + "/snapshots/2.jpg"),
102                 new File(FILES_DIR.getPath() + "/snapshots/2_reduced.jpg")};
103         assertTrueForFiles(existsFiles, File::exists, " must exist");
104         assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist");
105     }
106 
107     @Test
testRemoveObsoleteFiles_addedOneInTheMeantime()108     public void testRemoveObsoleteFiles_addedOneInTheMeantime() {
109         mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
110         final ArraySet<Integer> taskIds = new ArraySet<>();
111         taskIds.add(1);
112         mPersister.removeObsoleteFiles(taskIds, new int[]{mTestUserId});
113         mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
114         mSnapshotPersistQueue.waitForQueueEmpty();
115         final File[] existsFiles = new File[]{
116                 new File(FILES_DIR.getPath() + "/snapshots/1.proto"),
117                 new File(FILES_DIR.getPath() + "/snapshots/1.jpg"),
118                 new File(FILES_DIR.getPath() + "/snapshots/2.proto"),
119                 new File(FILES_DIR.getPath() + "/snapshots/2.jpg")};
120         final File[] nonExistsFiles = new File[]{
121                 new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg"),
122                 new File(FILES_DIR.getPath() + "/snapshots/2_reduced.jpg")};
123         assertTrueForFiles(existsFiles, File::exists, " must exist");
124         assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist");
125     }
126 
127     @Test
testReduced_notCached()128     public void testReduced_notCached() {
129         final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
130         mPersister.persistSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId, createSnapshot());
131         mSnapshotPersistQueue.waitForQueueEmpty();
132         assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId,
133                 false /* restoreFromDisk */, false /* isLowResolution */));
134 
135         // Attempt to load the low-res snapshot from the disk
136         assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId,
137                 true /* restoreFromDisk */, true /* isLowResolution */));
138 
139         // Load the high-res (default) snapshot from disk
140         assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId,
141                 true /* restoreFromDisk */, false /* isLowResolution */));
142 
143         // Make sure it's not in the cache now.
144         assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId,
145                 false /* restoreFromDisk */, true /* isLowResolution */));
146 
147         // Make sure it's not in the cache now.
148         assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId,
149                 false /* restoreFromDisk */, false /* isLowResolution */));
150     }
151 }
152