diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotCacheTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotCacheTest.java index 056836b7e5105..f9f161ac952d6 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotCacheTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotCacheTest.java @@ -49,6 +49,10 @@ public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase { @Mock TaskSnapshot mSnapshot; + public TaskSnapshotCacheTest() { + super(0.8f, 0.5f); + } + @Override @Before public void setUp() { diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotLowResDisabledTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotLowResDisabledTest.java new file mode 100644 index 0000000000000..2fb0b4c24ba2c --- /dev/null +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotLowResDisabledTest.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wm; + +import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import android.app.ActivityManager.TaskSnapshot; +import android.content.res.Configuration; +import android.graphics.Rect; +import android.platform.test.annotations.Presubmit; +import android.util.ArraySet; + +import androidx.test.filters.MediumTest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; + +import java.io.File; + +/** + * Test class for {@link TaskSnapshotPersister} and {@link TaskSnapshotLoader} + * + * Build/Install/Run: + * atest TaskSnapshotPersisterLoaderTest + */ +@MediumTest +@Presubmit +@RunWith(WindowTestRunner.class) +public class TaskSnapshotLowResDisabledTest extends TaskSnapshotPersisterTestBase { + + private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); + + private TaskSnapshotCache mCache; + + public TaskSnapshotLowResDisabledTest() { + super(0.8f, 0.0f); + } + + @Override + @Before + public void setUp() { + super.setUp(); + MockitoAnnotations.initMocks(this); + mCache = new TaskSnapshotCache(mWm, mLoader); + } + + @Test + public void testPersistAndLoadSnapshot() { + mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); + mPersister.waitForQueueEmpty(); + final File[] files = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1.proto"), + new File(FILES_DIR.getPath() + "/snapshots/1.jpg")}; + final File[] nonExistsFiles = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg")}; + assertTrueForFiles(files, File::exists, " must exist"); + assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist"); + final TaskSnapshot snapshot = mLoader.loadTask(1, mTestUserId, false /* isLowResolution */); + assertNotNull(snapshot); + assertEquals(MOCK_SNAPSHOT_ID, snapshot.getId()); + assertEquals(TEST_INSETS, snapshot.getContentInsets()); + assertNotNull(snapshot.getSnapshot()); + assertEquals(Configuration.ORIENTATION_PORTRAIT, snapshot.getOrientation()); + } + + + @Test + public void testRemoveObsoleteFiles() { + mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); + mPersister.persistSnapshot(2, mTestUserId, createSnapshot()); + final ArraySet taskIds = new ArraySet<>(); + taskIds.add(1); + mPersister.removeObsoleteFiles(taskIds, new int[]{mTestUserId}); + mPersister.waitForQueueEmpty(); + final File[] existsFiles = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1.proto"), + new File(FILES_DIR.getPath() + "/snapshots/1.jpg")}; + final File[] nonExistsFiles = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg"), + new File(FILES_DIR.getPath() + "/snapshots/2.proto"), + new File(FILES_DIR.getPath() + "/snapshots/2.jpg"), + new File(FILES_DIR.getPath() + "/snapshots/2_reduced.jpg")}; + assertTrueForFiles(existsFiles, File::exists, " must exist"); + assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist"); + } + + @Test + public void testRemoveObsoleteFiles_addedOneInTheMeantime() { + mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); + final ArraySet taskIds = new ArraySet<>(); + taskIds.add(1); + mPersister.removeObsoleteFiles(taskIds, new int[]{mTestUserId}); + mPersister.persistSnapshot(2, mTestUserId, createSnapshot()); + mPersister.waitForQueueEmpty(); + final File[] existsFiles = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1.proto"), + new File(FILES_DIR.getPath() + "/snapshots/1.jpg"), + new File(FILES_DIR.getPath() + "/snapshots/2.proto"), + new File(FILES_DIR.getPath() + "/snapshots/2.jpg")}; + final File[] nonExistsFiles = new File[]{ + new File(FILES_DIR.getPath() + "/snapshots/1_reduced.jpg"), + new File(FILES_DIR.getPath() + "/snapshots/2_reduced.jpg")}; + assertTrueForFiles(existsFiles, File::exists, " must exist"); + assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist"); + } + + @Test + public void testReduced_notCached() { + final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window"); + mPersister.persistSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId, createSnapshot()); + mPersister.waitForQueueEmpty(); + assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId, + false /* restoreFromDisk */, false /* isLowResolution */)); + + // Load it from disk + assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId, + true /* restoreFromDisk */, true /* isLowResolution */)); + + // Make sure it's not in the cache now. + assertNull(mCache.getSnapshot(window.getTask().mTaskId, mWm.mCurrentUserId, + false /* restoreFromDisk */, false /* isLowResolution */)); + } +} diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterLoaderTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterLoaderTest.java index 40f15b7e9d39b..9a9abbac1fd1c 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterLoaderTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterLoaderTest.java @@ -48,7 +48,6 @@ import org.junit.runner.RunWith; import org.mockito.MockitoSession; import java.io.File; -import java.util.function.Predicate; /** * Test class for {@link TaskSnapshotPersister} and {@link TaskSnapshotLoader} @@ -65,6 +64,10 @@ public class TaskSnapshotPersisterLoaderTest extends TaskSnapshotPersisterTestBa private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); + public TaskSnapshotPersisterLoaderTest() { + super(0.8f, 0.5f); + } + @Test public void testPersistAndLoadSnapshot() { mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); @@ -81,13 +84,6 @@ public class TaskSnapshotPersisterLoaderTest extends TaskSnapshotPersisterTestBa assertEquals(Configuration.ORIENTATION_PORTRAIT, snapshot.getOrientation()); } - private static void assertTrueForFiles(File[] files, Predicate predicate, - String message) { - for (File file : files) { - assertTrue(file.getName() + message, predicate.test(file)); - } - } - @Test public void testTaskRemovedFromRecents() { mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java index fa6663c063714..6d78658a05504 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java @@ -23,8 +23,14 @@ import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY; import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + import android.app.ActivityManager.TaskSnapshot; import android.content.ComponentName; +import android.content.ContextWrapper; +import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorSpace; @@ -39,6 +45,7 @@ import org.junit.After; import org.junit.Before; import java.io.File; +import java.util.function.Predicate; /** * Base class for tests that use a {@link TaskSnapshotPersister}. @@ -49,14 +56,35 @@ class TaskSnapshotPersisterTestBase extends WindowTestsBase { static final File FILES_DIR = getInstrumentation().getTargetContext().getFilesDir(); static final long MOCK_SNAPSHOT_ID = 12345678; + private ContextWrapper mContextSpy; + private Resources mResourcesSpy; TaskSnapshotPersister mPersister; TaskSnapshotLoader mLoader; int mTestUserId; + float mHighResScale; + float mLowResScale; + + TaskSnapshotPersisterTestBase(float highResScale, float lowResScale) { + super(); + mHighResScale = highResScale; + mLowResScale = lowResScale; + } @Before public void setUp() { final UserManager um = UserManager.get(getInstrumentation().getTargetContext()); mTestUserId = um.getUserHandle(); + + mContextSpy = spy(new ContextWrapper(mWm.mContext)); + mResourcesSpy = spy(mContextSpy.getResources()); + when(mContextSpy.getResources()).thenReturn(mResourcesSpy); + when(mResourcesSpy.getFloat( + com.android.internal.R.dimen.config_highResTaskSnapshotScale)) + .thenReturn(mHighResScale); + when(mResourcesSpy.getFloat( + com.android.internal.R.dimen.config_lowResTaskSnapshotScale)) + .thenReturn(mLowResScale); + mPersister = new TaskSnapshotPersister(mWm, userId -> FILES_DIR); mLoader = new TaskSnapshotLoader(mPersister); mPersister.start(); @@ -84,6 +112,13 @@ class TaskSnapshotPersisterTestBase extends WindowTestsBase { .build(); } + protected static void assertTrueForFiles(File[] files, Predicate predicate, + String message) { + for (File file : files) { + assertTrue(file.getName() + message, predicate.test(file)); + } + } + /** * Builds a TaskSnapshot. */