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

Commit f16f6e26 authored by Aurélien Pomini's avatar Aurélien Pomini Committed by Android (Google) Code Review
Browse files

Merge "Add WallpaperBackupAgent cropHint test" into main

parents ed0b7794 6b8505ad
Loading
Loading
Loading
Loading
+75 −0
Original line number Diff line number Diff line
@@ -36,9 +36,11 @@ import static com.android.window.flags.Flags.multiCrop;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -57,6 +59,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Rect;
import android.os.FileUtils;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
@@ -78,6 +81,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -87,6 +91,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RunWith(AndroidJUnit4.class)
@@ -815,6 +820,48 @@ public class WallpaperBackupAgentTest {
                WallpaperEventLogger.ERROR_LIVE_PACKAGE_NOT_INSTALLED);
    }

    @Test
    public void testOnRestore_noCropHints() throws Exception {
        testParseCropHints(Map.of());
    }

    @Test
    public void testOnRestore_singleCropHint() throws Exception {
        Map<Integer, Rect> testMap = Map.of(WallpaperManager.PORTRAIT, new Rect(1, 2, 3, 4));
        testParseCropHints(testMap);
    }

    @Test
    public void testOnRestore_multipleCropHints() throws Exception {
        Map<Integer, Rect> testMap = Map.of(
                WallpaperManager.PORTRAIT, new Rect(1, 2, 3, 4),
                WallpaperManager.SQUARE_PORTRAIT, new Rect(5, 6, 7, 8),
                WallpaperManager.SQUARE_LANDSCAPE, new Rect(9, 10, 11, 12));
        testParseCropHints(testMap);
    }

    private void testParseCropHints(Map<Integer, Rect> testMap) throws Exception {
        assumeTrue(multiCrop());
        mockRestoredStaticWallpaperFile(testMap);
        mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
        mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
                BackupAnnotations.OperationType.RESTORE);

        mWallpaperBackupAgent.onRestoreFinished();

        ArgumentMatcher<SparseArray<Rect>> matcher = array -> {
            boolean result = testMap.entrySet().stream().allMatch(entry -> {
                int key = entry.getKey();
                return (array.contains(key) && array.get(key).equals(testMap.get(key)));
            });
            for (int i = 0; i < array.size(); i++) {
                if (!testMap.containsKey(array.keyAt(i))) result = false;
            }
            return result;
        };
        verify(mWallpaperManager).setStreamWithCrops(any(), argThat(matcher), eq(true), anyInt());
    }

    private void mockCurrentWallpaperIds(int systemWallpaperId, int lockWallpaperId) {
        when(mWallpaperManager.getWallpaperId(eq(FLAG_SYSTEM))).thenReturn(systemWallpaperId);
        when(mWallpaperManager.getWallpaperId(eq(FLAG_LOCK))).thenReturn(lockWallpaperId);
@@ -880,6 +927,34 @@ public class WallpaperBackupAgentTest {
        fstream.close();
    }

    private void mockRestoredStaticWallpaperFile(Map<Integer, Rect> crops) throws Exception {
        File wallpaperFile = new File(mContext.getFilesDir(), WALLPAPER_INFO_STAGE);
        wallpaperFile.createNewFile();
        FileOutputStream fstream = new FileOutputStream(wallpaperFile, false);
        TypedXmlSerializer out = Xml.resolveSerializer(fstream);
        out.startDocument(null, true);
        out.startTag(null, "wp");
        for (Map.Entry<Integer, Rect> entry: crops.entrySet()) {
            String orientation = switch (entry.getKey()) {
                case WallpaperManager.PORTRAIT -> "Portrait";
                case WallpaperManager.LANDSCAPE -> "Landscape";
                case WallpaperManager.SQUARE_PORTRAIT -> "SquarePortrait";
                case WallpaperManager.SQUARE_LANDSCAPE -> "SquareLandscape";
                default -> throw new IllegalArgumentException("Invalid orientation");
            };
            Rect rect = entry.getValue();
            out.attributeInt(null, "cropLeft" + orientation, rect.left);
            out.attributeInt(null, "cropTop" + orientation, rect.top);
            out.attributeInt(null, "cropRight" + orientation, rect.right);
            out.attributeInt(null, "cropBottom" + orientation, rect.bottom);
        }
        out.endTag(null, "wp");
        out.endDocument();
        fstream.flush();
        FileUtils.sync(fstream);
        fstream.close();
    }

    private WallpaperInfo getFakeWallpaperInfo() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();
        Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);