Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab18eed1 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Fix TaskSnapshot tests when low res is disabled" into rvc-dev am: baed20e4 am: da354323

Change-Id: I355252c83591f8c45937daa8f9ec3af60c2831ac
parents d5747887 da354323
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -49,6 +49,10 @@ public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase {
    @Mock
    TaskSnapshot mSnapshot;

    public TaskSnapshotCacheTest() {
        super(0.8f, 0.5f);
    }

    @Override
    @Before
    public void setUp() {
+143 −0
Original line number Diff line number Diff line
/*
 * 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<Integer> 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<Integer> 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 */));
    }
}
+4 −8
Original line number Diff line number Diff line
@@ -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<File> predicate,
            String message) {
        for (File file : files) {
            assertTrue(file.getName() + message, predicate.test(file));
        }
    }

    @Test
    public void testTaskRemovedFromRecents() {
        mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
+35 −0
Original line number Diff line number Diff line
@@ -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<File> predicate,
            String message) {
        for (File file : files) {
            assertTrue(file.getName() + message, predicate.test(file));
        }
    }

    /**
     * Builds a TaskSnapshot.
     */