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

Commit 953f2656 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Improve WallpaperDataParser testing" into main

parents 43b8f8a8 7a2c79a5
Loading
Loading
Loading
Loading
+88 −68
Original line number Original line Diff line number Diff line
@@ -171,6 +171,64 @@ public class WallpaperDataParser {
            stream = new FileInputStream(file);
            stream = new FileInputStream(file);
            TypedXmlPullParser parser = Xml.resolvePullParser(stream);
            TypedXmlPullParser parser = Xml.resolvePullParser(stream);


            lockWallpaper = loadSettingsFromSerializer(parser, wallpaper, userId, loadSystem,
                    loadLock, keepDimensionHints, wpdData);

            success = true;
        } catch (FileNotFoundException e) {
            Slog.w(TAG, "no current wallpaper -- first boot?");
        } catch (NullPointerException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (NumberFormatException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (XmlPullParserException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (IOException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (IndexOutOfBoundsException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        }
        IoUtils.closeQuietly(stream);

        mWallpaperDisplayHelper.ensureSaneWallpaperDisplaySize(wpdData, DEFAULT_DISPLAY);

        if (loadSystem) {
            if (!success) {
                wallpaper.cropHint.set(0, 0, 0, 0);
                wpdData.mPadding.set(0, 0, 0, 0);
                wallpaper.name = "";
            } else {
                if (wallpaper.wallpaperId <= 0) {
                    wallpaper.wallpaperId = makeWallpaperIdLocked();
                    if (DEBUG) {
                        Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId
                                + "); now " + wallpaper.wallpaperId);
                    }
                }
            }
            ensureSaneWallpaperData(wallpaper);
            wallpaper.mWhich = lockWallpaper != null ? FLAG_SYSTEM : FLAG_SYSTEM | FLAG_LOCK;
        }

        if (loadLock) {
            if (!success) lockWallpaper = null;
            if (lockWallpaper != null) {
                ensureSaneWallpaperData(lockWallpaper);
                lockWallpaper.mWhich = FLAG_LOCK;
            }
        }

        return new WallpaperLoadingResult(wallpaper, lockWallpaper, success);
    }

    // This method updates `wallpaper` in place, but returns `lockWallpaper`. This is because
    // `wallpaper` already exists if it's being read per `loadSystem`, but `lockWallpaper` is
    // created conditionally if there is lock screen wallpaper data to read.
    @VisibleForTesting
    WallpaperData loadSettingsFromSerializer(TypedXmlPullParser parser, WallpaperData wallpaper,
            int userId, boolean loadSystem, boolean loadLock, boolean keepDimensionHints,
            DisplayData wpdData) throws IOException, XmlPullParserException {
        WallpaperData lockWallpaper = null;
        int type;
        int type;
        do {
        do {
            type = parser.next();
            type = parser.next();
@@ -228,51 +286,8 @@ public class WallpaperDataParser {
                }
                }
            }
            }
        } while (type != XmlPullParser.END_DOCUMENT);
        } while (type != XmlPullParser.END_DOCUMENT);
            success = true;
        } catch (FileNotFoundException e) {
            Slog.w(TAG, "no current wallpaper -- first boot?");
        } catch (NullPointerException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (NumberFormatException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (XmlPullParserException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (IOException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        } catch (IndexOutOfBoundsException e) {
            Slog.w(TAG, "failed parsing " + file + " " + e);
        }
        IoUtils.closeQuietly(stream);


        mWallpaperDisplayHelper.ensureSaneWallpaperDisplaySize(wpdData, DEFAULT_DISPLAY);
        return lockWallpaper;

        if (loadSystem) {
            if (!success) {
                wallpaper.cropHint.set(0, 0, 0, 0);
                wpdData.mPadding.set(0, 0, 0, 0);
                wallpaper.name = "";
            } else {
                if (wallpaper.wallpaperId <= 0) {
                    wallpaper.wallpaperId = makeWallpaperIdLocked();
                    if (DEBUG) {
                        Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId
                                + "); now " + wallpaper.wallpaperId);
                    }
                }
            }
            ensureSaneWallpaperData(wallpaper);
            wallpaper.mWhich = lockWallpaper != null ? FLAG_SYSTEM : FLAG_SYSTEM | FLAG_LOCK;
        }

        if (loadLock) {
            if (!success) lockWallpaper = null;
            if (lockWallpaper != null) {
                ensureSaneWallpaperData(lockWallpaper);
                lockWallpaper.mWhich = FLAG_LOCK;
            }
        }

        return new WallpaperLoadingResult(wallpaper, lockWallpaper, success);
    }
    }


    private void ensureSaneWallpaperData(WallpaperData wallpaper) {
    private void ensureSaneWallpaperData(WallpaperData wallpaper) {
@@ -449,6 +464,20 @@ public class WallpaperDataParser {
        try {
        try {
            fstream = new FileOutputStream(journal.chooseForWrite(), false);
            fstream = new FileOutputStream(journal.chooseForWrite(), false);
            TypedXmlSerializer out = Xml.resolveSerializer(fstream);
            TypedXmlSerializer out = Xml.resolveSerializer(fstream);
            saveSettingsToSerializer(out, wallpaper, lockWallpaper);
            fstream.flush();
            FileUtils.sync(fstream);
            fstream.close();
            journal.commit();
        } catch (IOException e) {
            IoUtils.closeQuietly(fstream);
            journal.rollback();
        }
    }

    @VisibleForTesting
    void saveSettingsToSerializer(TypedXmlSerializer out, WallpaperData wallpaper,
            WallpaperData lockWallpaper) throws IOException {
        out.startDocument(null, true);
        out.startDocument(null, true);


        if (wallpaper != null) {
        if (wallpaper != null) {
@@ -460,15 +489,6 @@ public class WallpaperDataParser {
        }
        }


        out.endDocument();
        out.endDocument();

            fstream.flush();
            FileUtils.sync(fstream);
            fstream.close();
            journal.commit();
        } catch (IOException e) {
            IoUtils.closeQuietly(fstream);
            journal.rollback();
        }
    }
    }


    @VisibleForTesting
    @VisibleForTesting
+81 −9
Original line number Original line Diff line number Diff line
@@ -32,6 +32,8 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSess
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER;
import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER;


import static com.google.common.truth.Truth.assertThat;

import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotEquals;
@@ -50,6 +52,7 @@ import static org.mockito.Mockito.verify;


import android.app.AppGlobals;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.AppOpsManager;
import android.app.Flags;
import android.app.WallpaperColors;
import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.ComponentName;
@@ -64,7 +67,10 @@ import android.hardware.display.DisplayManager;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemClock;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.wallpaper.IWallpaperConnection;
import android.service.wallpaper.IWallpaperConnection;
import android.service.wallpaper.IWallpaperEngine;
import android.service.wallpaper.IWallpaperEngine;
import android.service.wallpaper.WallpaperService;
import android.service.wallpaper.WallpaperService;
@@ -91,8 +97,10 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mock;
@@ -100,6 +108,7 @@ import org.mockito.MockitoAnnotations;
import org.mockito.quality.Strictness;
import org.mockito.quality.Strictness;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserException;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileInputStream;
@@ -125,6 +134,7 @@ public class WallpaperManagerServiceTests {
    @ClassRule
    @ClassRule
    public static final TestableContext sContext = new TestableContext(
    public static final TestableContext sContext = new TestableContext(
            InstrumentationRegistry.getInstrumentation().getTargetContext(), null);
            InstrumentationRegistry.getInstrumentation().getTargetContext(), null);

    private static ComponentName sImageWallpaperComponentName;
    private static ComponentName sImageWallpaperComponentName;
    private static ComponentName sDefaultWallpaperComponent;
    private static ComponentName sDefaultWallpaperComponent;


@@ -133,8 +143,11 @@ public class WallpaperManagerServiceTests {
    @Mock
    @Mock
    private DisplayManager mDisplayManager;
    private DisplayManager mDisplayManager;


    private final TemporaryFolder mFolder = new TemporaryFolder();
    private final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    @Rule
    @Rule
    public final TemporaryFolder mFolder = new TemporaryFolder();
    public RuleChain rules = RuleChain.outerRule(mSetFlagsRule).around(mFolder);

    private final SparseArray<File> mTempDirs = new SparseArray<>();
    private final SparseArray<File> mTempDirs = new SparseArray<>();
    private WallpaperManagerService mService;
    private WallpaperManagerService mService;
    private static IWallpaperConnection.Stub sWallpaperService;
    private static IWallpaperConnection.Stub sWallpaperService;
@@ -325,6 +338,7 @@ public class WallpaperManagerServiceTests {
     * is issued to the wallpaper.
     * is issued to the wallpaper.
     */
     */
    @Test
    @Test
    @Ignore("b/368345733")
    public void testSetCurrentComponent() throws Exception {
    public void testSetCurrentComponent() throws Exception {
        final int testUserId = USER_SYSTEM;
        final int testUserId = USER_SYSTEM;
        mService.switchUser(testUserId, null);
        mService.switchUser(testUserId, null);
@@ -411,26 +425,84 @@ public class WallpaperManagerServiceTests {
    }
    }


    @Test
    @Test
    public void testXmlSerializationRoundtrip() {
    @EnableFlags(Flags.FLAG_REMOVE_NEXT_WALLPAPER_COMPONENT)
    public void testSaveLoadSettings() {
        WallpaperData expectedData = mService.getCurrentWallpaperData(FLAG_SYSTEM, 0);
        expectedData.setComponent(sDefaultWallpaperComponent);
        expectedData.primaryColors = new WallpaperColors(Color.valueOf(Color.RED),
                Color.valueOf(Color.BLUE), null);
        expectedData.mWallpaperDimAmount = 0.5f;
        expectedData.mUidToDimAmount.put(0, 0.5f);
        expectedData.mUidToDimAmount.put(1, 0.4f);

        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        try {
            TypedXmlSerializer serializer = Xml.newBinarySerializer();
            serializer.setOutput(ostream, StandardCharsets.UTF_8.name());
            mService.mWallpaperDataParser.saveSettingsToSerializer(serializer, expectedData, null);
            ostream.close();
        } catch (IOException e) {
            fail("exception occurred while writing system wallpaper attributes");
        }

        WallpaperData actualData = new WallpaperData(0, FLAG_SYSTEM);
        try {
            ByteArrayInputStream istream = new ByteArrayInputStream(ostream.toByteArray());
            TypedXmlPullParser parser = Xml.newBinaryPullParser();
            parser.setInput(istream, StandardCharsets.UTF_8.name());
            mService.mWallpaperDataParser.loadSettingsFromSerializer(parser,
                    actualData, /* userId= */0, /* loadSystem= */ true, /* loadLock= */
                    false, /* keepDimensionHints= */ true,
                    new WallpaperDisplayHelper.DisplayData(0));
        } catch (IOException | XmlPullParserException e) {
            fail("exception occurred while parsing wallpaper");
        }

        assertThat(actualData.getComponent()).isEqualTo(expectedData.getComponent());
        assertThat(actualData.primaryColors).isEqualTo(expectedData.primaryColors);
        assertThat(actualData.mWallpaperDimAmount).isEqualTo(expectedData.mWallpaperDimAmount);
        assertThat(actualData.mUidToDimAmount).isNotNull();
        assertThat(actualData.mUidToDimAmount.size()).isEqualTo(
                expectedData.mUidToDimAmount.size());
        for (int i = 0; i < actualData.mUidToDimAmount.size(); i++) {
            int key = actualData.mUidToDimAmount.keyAt(0);
            assertThat(actualData.mUidToDimAmount.get(key)).isEqualTo(
                    expectedData.mUidToDimAmount.get(key));
        }
    }

    @Test
    @DisableFlags(Flags.FLAG_REMOVE_NEXT_WALLPAPER_COMPONENT)
    public void testSaveLoadSettings_legacyNextComponent() {
        WallpaperData systemWallpaperData = mService.getCurrentWallpaperData(FLAG_SYSTEM, 0);
        WallpaperData systemWallpaperData = mService.getCurrentWallpaperData(FLAG_SYSTEM, 0);
        systemWallpaperData.setComponent(sDefaultWallpaperComponent);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        try {
        try {
            TypedXmlSerializer serializer = Xml.newBinarySerializer();
            TypedXmlSerializer serializer = Xml.newBinarySerializer();
            serializer.setOutput(new ByteArrayOutputStream(), StandardCharsets.UTF_8.name());
            serializer.setOutput(ostream, StandardCharsets.UTF_8.name());
            serializer.startDocument(StandardCharsets.UTF_8.name(), true);
            mService.mWallpaperDataParser.saveSettingsToSerializer(serializer, systemWallpaperData,
            mService.mWallpaperDataParser.writeWallpaperAttributes(
                    null);
                    serializer, "wp", systemWallpaperData);
            ostream.close();
        } catch (IOException e) {
        } catch (IOException e) {
            fail("exception occurred while writing system wallpaper attributes");
            fail("exception occurred while writing system wallpaper attributes");
        }
        }


        WallpaperData shouldMatchSystem = new WallpaperData(0, FLAG_SYSTEM);
        WallpaperData shouldMatchSystem = new WallpaperData(0, FLAG_SYSTEM);
        try {
        try {
            ByteArrayInputStream istream = new ByteArrayInputStream(ostream.toByteArray());
            TypedXmlPullParser parser = Xml.newBinaryPullParser();
            TypedXmlPullParser parser = Xml.newBinaryPullParser();
            mService.mWallpaperDataParser.parseWallpaperAttributes(parser, shouldMatchSystem, true);
            parser.setInput(istream, StandardCharsets.UTF_8.name());
        } catch (XmlPullParserException e) {
            mService.mWallpaperDataParser.loadSettingsFromSerializer(parser,
                    shouldMatchSystem, /* userId= */0, /* loadSystem= */ true, /* loadLock= */
                    false, /* keepDimensionHints= */ true,
                    new WallpaperDisplayHelper.DisplayData(0));
        } catch (IOException | XmlPullParserException e) {
            fail("exception occurred while parsing wallpaper");
            fail("exception occurred while parsing wallpaper");
        }
        }
        assertEquals(systemWallpaperData.primaryColors, shouldMatchSystem.primaryColors);

        assertThat(shouldMatchSystem.nextWallpaperComponent).isEqualTo(
                systemWallpaperData.getComponent());
        assertThat(shouldMatchSystem.primaryColors).isEqualTo(systemWallpaperData.primaryColors);
    }
    }


    @Test
    @Test