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

Commit 60f91228 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

OMS: Assert which packages are updated

When the overlay manager is initialized and when packages are
installed, upgraded, or removed, the overlay manager must report any
updates to the currently enabled overlays for each package to PMS.

Test that when a package event happens, all packages that may have
their overlays affected are returned as updated packages from the
OMSImpl event methods.

Bug: 180016597
Test: atest
Change-Id: I9906f8805a2c2728e8c8b57ab9990b169ef06a2d
parent d145f376
Loading
Loading
Loading
Loading
+84 −75
Original line number Diff line number Diff line
@@ -23,15 +23,14 @@ import static org.junit.Assert.assertTrue;

import android.content.om.OverlayIdentifier;
import android.content.om.OverlayInfo;
import android.util.ArraySet;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.function.BiConsumer;
import java.util.Set;
import java.util.function.Consumer;

@RunWith(AndroidJUnit4.class)
public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceImplTestsBase {
@@ -45,51 +44,47 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
    private static final OverlayIdentifier IDENTIFIER2 = new OverlayIdentifier(OVERLAY2);

    @Test
    public void testUpdateOverlaysForUser() {
    public void alwaysInitializeAllPackages() {
        final OverlayManagerServiceImpl impl = getImpl();
        final String otherTarget = "some.other.target";
        addPackage(target(TARGET), USER);
        addPackage(target(otherTarget), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);

        // do nothing, expect no change
        final ArraySet<PackageAndUser> a = impl.updateOverlaysForUser(USER);
        assertEquals(3, a.size());
        assertTrue(a.containsAll(Arrays.asList(
                new PackageAndUser(TARGET, USER),
        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER),
                        new PackageAndUser(otherTarget, USER),
                new PackageAndUser(OVERLAY, USER))));
                        new PackageAndUser(OVERLAY, USER));

        final ArraySet<PackageAndUser> b = impl.updateOverlaysForUser(USER);
        assertEquals(3, b.size());
        assertTrue(b.containsAll(Arrays.asList(
                new PackageAndUser(TARGET, USER),
                new PackageAndUser(otherTarget, USER),
                new PackageAndUser(OVERLAY, USER))));
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
    }

    @Test
    public void testImmutableEnabledChange() throws Exception {
    public void testImmutableEnabledChange() {
        final OverlayManagerServiceImpl impl = getImpl();
        installPackage(target(TARGET), USER);
        installPackage(overlay(OVERLAY, TARGET), USER);
        addPackage(target(TARGET), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);

        configureSystemOverlay(OVERLAY, false /* mutable */, false /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);
        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));

        configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o1);
        assertFalse(o1.isEnabled());
        assertFalse(o1.isMutable);

        configureSystemOverlay(OVERLAY, false /* mutable */, true /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);
        configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o2);
        assertTrue(o2.isEnabled());
        assertFalse(o2.isMutable);

        configureSystemOverlay(OVERLAY, false /* mutable */, false /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);
        configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o3);
        assertFalse(o3.isEnabled());
@@ -97,27 +92,30 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
    }

    @Test
    public void testMutableEnabledChangeHasNoEffect() throws Exception {
    public void testMutableEnabledChangeHasNoEffect() {
        final OverlayManagerServiceImpl impl = getImpl();
        installPackage(target(TARGET), USER);
        installPackage(overlay(OVERLAY, TARGET), USER);
        configureSystemOverlay(OVERLAY, true /* mutable */, false /* enabled */, 0 /* priority */);
        addPackage(target(TARGET), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);
        configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);

        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));

        impl.updateOverlaysForUser(USER);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o1);
        assertFalse(o1.isEnabled());
        assertTrue(o1.isMutable);

        configureSystemOverlay(OVERLAY, true /* mutable */, true /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);
        configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_ENABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o2);
        assertFalse(o2.isEnabled());
        assertTrue(o2.isMutable);

        configureSystemOverlay(OVERLAY, true /* mutable */, false /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);
        configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o3);
        assertFalse(o3.isEnabled());
@@ -125,59 +123,68 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
    }

    @Test
    public void testMutableEnabledToImmutableEnabled() throws Exception {
    public void testMutableEnabledToImmutableEnabled() {
        final OverlayManagerServiceImpl impl = getImpl();
        installPackage(target(TARGET), USER);
        installPackage(overlay(OVERLAY, TARGET), USER);
        addPackage(target(TARGET), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);

        final BiConsumer<Boolean, Boolean> setOverlay = (mutable, enabled) -> {
            configureSystemOverlay(OVERLAY, mutable, enabled, 0 /* priority */);
            impl.updateOverlaysForUser(USER);
        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));

        final Consumer<ConfigState> setOverlay = (state -> {
            configureSystemOverlay(OVERLAY, state, 0 /* priority */);
            assertEquals(allPackages, impl.updateOverlaysForUser(USER));
            final OverlayInfo o = impl.getOverlayInfo(IDENTIFIER, USER);
            assertNotNull(o);
            assertEquals(enabled, o.isEnabled());
            assertEquals(mutable, o.isMutable);
        };
            assertEquals(o.isEnabled(), state == ConfigState.IMMUTABLE_ENABLED
                    || state == ConfigState.MUTABLE_ENABLED);
            assertEquals(o.isMutable, state == ConfigState.MUTABLE_DISABLED
                    || state == ConfigState.MUTABLE_ENABLED);
        });

        // Immutable/enabled -> mutable/enabled
        setOverlay.accept(false /* mutable */, true /* enabled */);
        setOverlay.accept(true /* mutable */, true /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);
        setOverlay.accept(ConfigState.MUTABLE_ENABLED);

        // Mutable/enabled -> immutable/enabled
        setOverlay.accept(false /* mutable */, true /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);

        // Immutable/enabled -> mutable/disabled
        setOverlay.accept(true /* mutable */, false /* enabled */);
        setOverlay.accept(ConfigState.MUTABLE_DISABLED);

        // Mutable/disabled -> immutable/enabled
        setOverlay.accept(false /* mutable */, true /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);

        // Immutable/enabled -> immutable/disabled
        setOverlay.accept(false /* mutable */, false /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);

        // Immutable/disabled -> mutable/enabled
        setOverlay.accept(true /* mutable */, true /* enabled */);
        setOverlay.accept(ConfigState.MUTABLE_ENABLED);

        // Mutable/enabled -> immutable/disabled
        setOverlay.accept(false /* mutable */, false /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);

        // Immutable/disabled -> mutable/disabled
        setOverlay.accept(true /* mutable */, false /* enabled */);
        setOverlay.accept(ConfigState.MUTABLE_DISABLED);

        // Mutable/disabled -> immutable/disabled
        setOverlay.accept(false /* mutable */, false /* enabled */);
        setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);
    }

    @Test
    public void testMutablePriorityChange() throws Exception {
        final OverlayManagerServiceImpl impl = getImpl();
        installPackage(target(TARGET), USER);
        installPackage(overlay(OVERLAY, TARGET), USER);
        installPackage(overlay(OVERLAY2, TARGET), USER);
        configureSystemOverlay(OVERLAY, true /* mutable */, false /* enabled */, 0 /* priority */);
        configureSystemOverlay(OVERLAY2, true /* mutable */, false /* enabled */, 1 /* priority */);
        impl.updateOverlaysForUser(USER);
        addPackage(target(TARGET), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);
        addPackage(overlay(OVERLAY2, TARGET), USER);
        configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
        configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 1 /* priority */);

        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER),
                        new PackageAndUser(OVERLAY2, USER));

        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o1);
        assertEquals(0, o1.priority);
@@ -193,10 +200,9 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
        impl.setEnabled(IDENTIFIER, true, USER);

        // Reorder the overlays
        configureSystemOverlay(OVERLAY, true /* mutable */, false /* enabled */, 1 /* priority */);
        configureSystemOverlay(OVERLAY2, true /* mutable */, false /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);

        configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 1 /* priority */);
        configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o3);
        assertEquals(1, o3.priority);
@@ -211,13 +217,17 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
    @Test
    public void testImmutablePriorityChange() throws Exception {
        final OverlayManagerServiceImpl impl = getImpl();
        installPackage(target(TARGET), USER);
        installPackage(overlay(OVERLAY, TARGET), USER);
        installPackage(overlay(OVERLAY2, TARGET), USER);
        configureSystemOverlay(OVERLAY, false /* mutable */, true /* enabled */, 0 /* priority */);
        configureSystemOverlay(OVERLAY2, false /* mutable */, true /* enabled */, 1 /* priority */);
        impl.updateOverlaysForUser(USER);
        addPackage(target(TARGET), USER);
        addPackage(overlay(OVERLAY, TARGET), USER);
        addPackage(overlay(OVERLAY2, TARGET), USER);
        configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
        configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);

        final Set<PackageAndUser> allPackages =
                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER),
                        new PackageAndUser(OVERLAY2, USER));

        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o1);
        assertEquals(0, o1.priority);
@@ -229,10 +239,9 @@ public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceI
        assertTrue(o2.isEnabled());

        // Reorder the overlays
        configureSystemOverlay(OVERLAY, false /* mutable */, true /* enabled */, 1 /* priority */);
        configureSystemOverlay(OVERLAY2, false /* mutable */, true /* enabled */, 0 /* priority */);
        impl.updateOverlaysForUser(USER);

        configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);
        configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
        final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
        assertNotNull(o3);
        assertEquals(1, o3.priority);
+82 −55

File changed.

Preview size limit exceeded, changes collapsed.

+25 −17
Original line number Diff line number Diff line
@@ -139,8 +139,19 @@ class OverlayManagerServiceImplTestsBase {
        mState.add(pkg, userId);
    }

    void configureSystemOverlay(String packageName, boolean mutable, boolean enabled,
    enum ConfigState {
        IMMUTABLE_DISABLED,
        IMMUTABLE_ENABLED,
        MUTABLE_DISABLED,
        MUTABLE_ENABLED
    }

    void configureSystemOverlay(@NonNull String packageName, @NonNull ConfigState state,
            int priority) {
        final boolean mutable = state == ConfigState.MUTABLE_DISABLED
                || state == ConfigState.MUTABLE_ENABLED;
        final boolean enabled = state == ConfigState.IMMUTABLE_ENABLED
                || state == ConfigState.MUTABLE_ENABLED;
        when(mOverlayConfig.getPriority(packageName)).thenReturn(priority);
        when(mOverlayConfig.isEnabled(packageName)).thenReturn(enabled);
        when(mOverlayConfig.isMutable(packageName)).thenReturn(mutable);
@@ -154,13 +165,14 @@ class OverlayManagerServiceImplTestsBase {
     *
     * @throws IllegalStateException if the package is currently installed
     */
    Set<PackageAndUser> installPackage(FakeDeviceState.PackageBuilder pkg, int userId)
    void installAndAssert(@NonNull FakeDeviceState.PackageBuilder pkg, int userId,
            @NonNull Set<PackageAndUser> onAddedUpdatedPackages)
            throws OperationFailedException {
        if (mState.select(pkg.packageName, userId) != null) {
            throw new IllegalStateException("package " + pkg.packageName + " already installed");
        }
        mState.add(pkg, userId);
        return CollectionUtils.emptyIfNull(mImpl.onPackageAdded(pkg.packageName, userId));
        assertEquals(onAddedUpdatedPackages, mImpl.onPackageAdded(pkg.packageName, userId));
    }

    /**
@@ -172,25 +184,20 @@ class OverlayManagerServiceImplTestsBase {
     * {@link android.content.Intent#ACTION_PACKAGE_ADDED} broadcast with the
     * {@link android.content.Intent#EXTRA_REPLACING} extra.
     *
     * @return the two Optional<PackageAndUser> objects from starting and finishing the upgrade
     *
     * @throws IllegalStateException if the package is not currently installed
     */
    Pair<Set<PackageAndUser>, Set<PackageAndUser>> upgradePackage(
            FakeDeviceState.PackageBuilder pkg, int userId) throws OperationFailedException {
    void upgradeAndAssert(FakeDeviceState.PackageBuilder pkg, int userId,
            @NonNull Set<PackageAndUser> onReplacingUpdatedPackages,
            @NonNull Set<PackageAndUser> onReplacedUpdatedPackages)
            throws OperationFailedException {
        final FakeDeviceState.Package replacedPackage = mState.select(pkg.packageName, userId);
        if (replacedPackage == null) {
            throw new IllegalStateException("package " + pkg.packageName + " not installed");
        }

        final Set<PackageAndUser> updatedPackages1 =
                CollectionUtils.emptyIfNull(mImpl.onPackageReplacing(pkg.packageName, userId));

        assertEquals(onReplacingUpdatedPackages, mImpl.onPackageReplacing(pkg.packageName, userId));
        mState.add(pkg, userId);
        final Set<PackageAndUser> updatedPackages2 =
                CollectionUtils.emptyIfNull(mImpl.onPackageReplaced(pkg.packageName, userId));

        return Pair.create(updatedPackages1, updatedPackages2);
        assertEquals(onReplacedUpdatedPackages, mImpl.onPackageReplaced(pkg.packageName, userId));
    }

    /**
@@ -201,13 +208,14 @@ class OverlayManagerServiceImplTestsBase {
     *
     * @throws IllegalStateException if the package is not currently installed
     */
    Set<PackageAndUser> uninstallPackage(String packageName, int userId) {
    void uninstallAndAssert(@NonNull String packageName, int userId,
            @NonNull Set<PackageAndUser> onRemovedUpdatedPackages) {
        final FakeDeviceState.Package pkg = mState.select(packageName, userId);
        if (pkg == null) {
            throw new IllegalStateException("package " + packageName + " not installed");
        }
        mState.remove(pkg.packageName);
        return CollectionUtils.emptyIfNull(mImpl.onPackageRemoved(packageName, userId));
        assertEquals(onRemovedUpdatedPackages, mImpl.onPackageRemoved(pkg.packageName, userId));
    }

    /** Represents the state of packages installed on a fake device. */