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

Commit 97d96c61 authored by shafik's avatar shafik Committed by Shafik Nassar
Browse files

Update PackageWatchdogTest

Make PackageWatchdogTest compatible to the changes that added
DeviceConfig flags to PackageWatchdog. This includes:
    * Make PackageWatchdog#setExplicitHealthCheckEnabled private and
    use DeviceConfig mechanism for changing that value instead
    * Disable TestLooper#startAutoDispatch
    * Other minor refinements that solve compatibility issues

Bug: 129335707
Test: atest com.android.server.PackageWatchdogTest
Change-Id: I7323dc65ec2957aeab128224864441bdf63c6f81
parent ba036ade
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -357,7 +357,7 @@ public class PackageWatchdog {
     * <p> If explicit health checks are disabled, pending explicit health check requests are
     * passed and the health check service is stopped.
     */
    public void setExplicitHealthCheckEnabled(boolean enabled) {
    private void setExplicitHealthCheckEnabled(boolean enabled) {
        synchronized (mLock) {
            mIsHealthCheckEnabled = enabled;
            mHealthCheckController.setEnabled(enabled);
+59 −20
Original line number Diff line number Diff line
@@ -18,25 +18,27 @@ package com.android.server;

import static android.service.watchdog.ExplicitHealthCheckService.PackageConfig;

import static com.android.server.PackageWatchdog.MonitoredPackage;
import static com.android.server.PackageWatchdog.TRIGGER_FAILURE_COUNT;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.Manifest;
import android.content.Context;
import android.content.pm.VersionedPackage;
import android.os.Handler;
import android.os.test.TestLooper;
import android.provider.DeviceConfig;
import android.util.AtomicFile;

import androidx.test.InstrumentationRegistry;

import com.android.server.PackageWatchdog.MonitoredPackage;
import com.android.server.PackageWatchdog.PackageHealthObserver;
import com.android.server.PackageWatchdog.PackageHealthObserverImpact;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

@@ -73,8 +75,13 @@ public class PackageWatchdogTest {
    public void setUp() throws Exception {
        new File(InstrumentationRegistry.getContext().getFilesDir(),
                "package-watchdog.xml").delete();
        adoptShellPermissions(Manifest.permission.READ_DEVICE_CONFIG);
        mTestLooper = new TestLooper();
        mTestLooper.startAutoDispatch();
    }

    @After
    public void tearDown() throws Exception {
        dropShellPermissions();
    }

    /**
@@ -204,7 +211,7 @@ public class PackageWatchdogTest {
        // Verify random observer not saved returns null
        assertNull(watchdog2.getPackages(new TestObserver(OBSERVER_NAME_3)));

        // Then regiser observer1
        // Then register observer1
        watchdog2.registerHealthObserver(observer1);
        watchdog2.registerHealthObserver(observer2);

@@ -231,7 +238,7 @@ public class PackageWatchdogTest {
        watchdog.startObservingHealth(observer1, Arrays.asList(APP_A), SHORT_DURATION);

        // Then fail APP_A below the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT - 1; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount() - 1; i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }

@@ -258,7 +265,7 @@ public class PackageWatchdogTest {
        watchdog.startObservingHealth(observer1, Arrays.asList(APP_B), SHORT_DURATION);

        // Then fail APP_C (not observed) above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_C, VERSION_CODE)));
        }

@@ -292,7 +299,7 @@ public class PackageWatchdogTest {
        watchdog.startObservingHealth(observer, Arrays.asList(APP_A), SHORT_DURATION);

        // Then fail APP_A (different version) above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(
                            new VersionedPackage(APP_A, differentVersionCode)));
        }
@@ -331,7 +338,7 @@ public class PackageWatchdogTest {
                SHORT_DURATION);

        // Then fail all apps above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE),
                    new VersionedPackage(APP_B, VERSION_CODE),
                    new VersionedPackage(APP_C, VERSION_CODE),
@@ -384,7 +391,7 @@ public class PackageWatchdogTest {
        watchdog.startObservingHealth(observerSecond, Arrays.asList(APP_A), LONG_DURATION);

        // Then fail APP_A above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }
        // Run handler so package failures are dispatched to observers
@@ -401,7 +408,7 @@ public class PackageWatchdogTest {
        observerSecond.mFailedPackages.clear();

        // Then fail APP_A again above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }
        // Run handler so package failures are dispatched to observers
@@ -418,7 +425,7 @@ public class PackageWatchdogTest {
        observerSecond.mFailedPackages.clear();

        // Then fail APP_A again above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }
        // Run handler so package failures are dispatched to observers
@@ -435,7 +442,7 @@ public class PackageWatchdogTest {
        observerSecond.mFailedPackages.clear();

        // Then fail APP_A again above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }
        // Run handler so package failures are dispatched to observers
@@ -462,7 +469,7 @@ public class PackageWatchdogTest {
        watchdog.startObservingHealth(observer1, Arrays.asList(APP_A), SHORT_DURATION);

        // Then fail APP_A above the threshold
        for (int i = 0; i < TRIGGER_FAILURE_COUNT; i++) {
        for (int i = 0; i < watchdog.getTriggerFailureCount(); i++) {
            watchdog.onPackageFailure(Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));
        }

@@ -539,6 +546,10 @@ public class PackageWatchdogTest {
     */
    @Test
    public void testExplicitHealthCheckStateChanges() throws Exception {
        adoptShellPermissions(
                Manifest.permission.WRITE_DEVICE_CONFIG,
                Manifest.permission.READ_DEVICE_CONFIG);

        TestController controller = new TestController();
        PackageWatchdog watchdog = createWatchdog(controller, true /* withPackagesReady */);
        TestObserver observer = new TestObserver(OBSERVER_NAME_1,
@@ -559,7 +570,7 @@ public class PackageWatchdogTest {
        assertEquals(APP_B, requestedPackages.get(1));

        // Disable explicit health checks (marks APP_A and APP_B as passed)
        watchdog.setExplicitHealthCheckEnabled(false);
        setExplicitHealthCheckEnabled(false);

        // Run handler so requests/cancellations are dispatched to the controller
        mTestLooper.dispatchAll();
@@ -575,7 +586,7 @@ public class PackageWatchdogTest {
        assertEquals(0, observer.mFailedPackages.size());

        // Re-enable explicit health checks
        watchdog.setExplicitHealthCheckEnabled(true);
        setExplicitHealthCheckEnabled(true);

        // Run handler so requests/cancellations are dispatched to the controller
        mTestLooper.dispatchAll();
@@ -643,11 +654,13 @@ public class PackageWatchdogTest {
    /** Tests {@link MonitoredPackage} health check state transitions. */
    @Test
    public void testPackageHealthCheckStateTransitions() {
        MonitoredPackage m1 = new MonitoredPackage(APP_A, LONG_DURATION,
        TestController controller = new TestController();
        PackageWatchdog wd = createWatchdog(controller, true /* withPackagesReady */);
        MonitoredPackage m1 = wd.new MonitoredPackage(APP_A, LONG_DURATION,
                false /* hasPassedHealthCheck */);
        MonitoredPackage m2 = new MonitoredPackage(APP_B, LONG_DURATION, false);
        MonitoredPackage m3 = new MonitoredPackage(APP_C, LONG_DURATION, false);
        MonitoredPackage m4 = new MonitoredPackage(APP_D, LONG_DURATION, SHORT_DURATION, true);
        MonitoredPackage m2 = wd.new MonitoredPackage(APP_B, LONG_DURATION, false);
        MonitoredPackage m3 = wd.new MonitoredPackage(APP_C, LONG_DURATION, false);
        MonitoredPackage m4 = wd.new MonitoredPackage(APP_D, LONG_DURATION, SHORT_DURATION, true);

        // Verify transition: inactive -> active -> passed
        // Verify initially inactive
@@ -683,6 +696,32 @@ public class PackageWatchdogTest {
        assertEquals(MonitoredPackage.STATE_PASSED, m4.handleElapsedTimeLocked(LONG_DURATION));
    }

    private void adoptShellPermissions(String... permissions) {
        InstrumentationRegistry
                .getInstrumentation()
                .getUiAutomation()
                .adoptShellPermissionIdentity(permissions);
    }

    private void dropShellPermissions() {
        InstrumentationRegistry
                .getInstrumentation()
                .getUiAutomation()
                .dropShellPermissionIdentity();
    }

    private void setExplicitHealthCheckEnabled(boolean enabled) {
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ROLLBACK,
                PackageWatchdog.PROPERTY_WATCHDOG_EXPLICIT_HEALTH_CHECK_ENABLED,
                Boolean.toString(enabled), /*makeDefault*/false);
        //give time for DeviceConfig to broadcast the property value change
        try {
            Thread.sleep(SHORT_DURATION);
        } catch (InterruptedException e) {
            fail("Thread.sleep unexpectedly failed!");
        }
    }

    private PackageWatchdog createWatchdog() {
        return createWatchdog(new TestController(), true /* withPackagesReady */);
    }