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

Commit 7a2c79a5 authored by Chris Poultney's avatar Chris Poultney
Browse files

Improve WallpaperDataParser testing

Bug: 347235611
Test: newly added tests pass
Test: manually tested setting wallpapers and rebooted
Flag: EXEMPT refactor + new testing
Change-Id: I233b4d99666c427a53e9c948ecc9de939da45823
parent d384464d
Loading
Loading
Loading
Loading
+88 −68
Original line number Diff line number Diff line
@@ -171,6 +171,64 @@ public class WallpaperDataParser {
            stream = new FileInputStream(file);
            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;
        do {
            type = parser.next();
@@ -228,51 +286,8 @@ public class WallpaperDataParser {
                }
            }
        } 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);

        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);
        return lockWallpaper;
    }

    private void ensureSaneWallpaperData(WallpaperData wallpaper) {
@@ -449,6 +464,20 @@ public class WallpaperDataParser {
        try {
            fstream = new FileOutputStream(journal.chooseForWrite(), false);
            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);

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

        out.endDocument();

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

    @VisibleForTesting
+81 −9
Original line number 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.server.wallpaper.WallpaperUtils.WALLPAPER;

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

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

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

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

    private static ComponentName sImageWallpaperComponentName;
    private static ComponentName sDefaultWallpaperComponent;

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

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

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

    @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);
        systemWallpaperData.setComponent(sDefaultWallpaperComponent);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        try {
            TypedXmlSerializer serializer = Xml.newBinarySerializer();
            serializer.setOutput(new ByteArrayOutputStream(), StandardCharsets.UTF_8.name());
            serializer.startDocument(StandardCharsets.UTF_8.name(), true);
            mService.mWallpaperDataParser.writeWallpaperAttributes(
                    serializer, "wp", systemWallpaperData);
            serializer.setOutput(ostream, StandardCharsets.UTF_8.name());
            mService.mWallpaperDataParser.saveSettingsToSerializer(serializer, systemWallpaperData,
                    null);
            ostream.close();
        } catch (IOException e) {
            fail("exception occurred while writing system wallpaper attributes");
        }

        WallpaperData shouldMatchSystem = new WallpaperData(0, FLAG_SYSTEM);
        try {
            ByteArrayInputStream istream = new ByteArrayInputStream(ostream.toByteArray());
            TypedXmlPullParser parser = Xml.newBinaryPullParser();
            mService.mWallpaperDataParser.parseWallpaperAttributes(parser, shouldMatchSystem, true);
        } catch (XmlPullParserException e) {
            parser.setInput(istream, StandardCharsets.UTF_8.name());
            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");
        }
        assertEquals(systemWallpaperData.primaryColors, shouldMatchSystem.primaryColors);

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

    @Test