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

Commit c283e94e authored by Harshit Mahajan's avatar Harshit Mahajan
Browse files

Fixing recovery related tests

Tests needs to be updated in according with various refactorings done
for crashrecovery module.

Test: atest PackageWatchdogTest;
Test: atest RollbackPackageHealthObserverTest
Bug: 368397616
Flag: android.crashrecovery.flags.refactor_crashrecovery
Change-Id: I674d91b20951c3dcd2719a605103765d0519631d
parent caa2c9be
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
@@ -36,6 +37,7 @@ import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.VersionedPackage;
import android.content.rollback.PackageRollbackInfo;
@@ -47,6 +49,7 @@ import android.os.MessageQueue;
import android.os.SystemProperties;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.dx.mockito.inline.extended.ExtendedMockito;
@@ -145,6 +148,22 @@ public class RollbackPackageHealthObserverTest {
                }
        ).when(() -> SystemProperties.getBoolean(anyString(), anyBoolean()));

        try {
            when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).then(inv -> {
                final PackageInfo res = new PackageInfo();
                res.packageName = inv.getArgument(0);
                res.setApexPackageName(res.packageName);
                return res;
            });
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

        Context testContext = InstrumentationRegistry.getInstrumentation()
                .getTargetContext();
        when(mMockContext.getUser()).thenReturn(testContext.getUser());
        when(mMockContext.getPackageName()).thenReturn(testContext.getPackageName());

        SystemProperties.set(PROP_DISABLE_HIGH_IMPACT_ROLLBACK_FLAG, Boolean.toString(false));
    }

+15 −3
Original line number Diff line number Diff line
@@ -727,7 +727,17 @@ public class CrashRecoveryTest {
        when(mRollbackManager.getAvailableRollbacks()).thenReturn(List.of(ROLLBACK_INFO_LOW,
                ROLLBACK_INFO_HIGH, ROLLBACK_INFO_MANUAL));
        when(mSpyContext.getPackageManager()).thenReturn(mMockPackageManager);

        try {
            when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).then(inv -> {
                final PackageInfo res = new PackageInfo();
                res.packageName = inv.getArgument(0);
                res.setApexPackageName(res.packageName);
                res.setLongVersionCode(VERSION_CODE);
                return res;
            });
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        watchdog.registerHealthObserver(rollbackObserver);
        return rollbackObserver;
    }
@@ -787,9 +797,11 @@ public class CrashRecoveryTest {
            // Verify controller by default is started when packages are ready
            assertThat(controller.mIsEnabled).isTrue();

            if (!Flags.refactorCrashrecovery()) {
                verify(mConnectivityModuleConnector).registerHealthListener(
                        mConnectivityModuleCallbackCaptor.capture());
            }
        }
        mAllocatedWatchdogs.add(watchdog);
        return watchdog;
    }
+12 −2
Original line number Diff line number Diff line
@@ -46,6 +46,9 @@ import android.net.ConnectivityModuleConnector.ConnectivityModuleHealthListener;
import android.os.Handler;
import android.os.SystemProperties;
import android.os.test.TestLooper;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DeviceConfig;
import android.util.AtomicFile;
@@ -111,6 +114,9 @@ public class PackageWatchdogTest {
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Rule
    public CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    private final TestClock mTestClock = new TestClock();
    private TestLooper mTestLooper;
    private Context mSpyContext;
@@ -966,6 +972,7 @@ public class PackageWatchdogTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_REFACTOR_CRASHRECOVERY)
    public void testNetworkStackFailure() {
        mSetFlagsRule.disableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
        final PackageWatchdog wd = createWatchdog();
@@ -986,6 +993,7 @@ public class PackageWatchdogTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_REFACTOR_CRASHRECOVERY)
    public void testNetworkStackFailureRecoverabilityDetection() {
        final PackageWatchdog wd = createWatchdog();

@@ -1758,9 +1766,11 @@ public class PackageWatchdogTest {
            // Verify controller by default is started when packages are ready
            assertThat(controller.mIsEnabled).isTrue();

            if (!Flags.refactorCrashrecovery()) {
                verify(mConnectivityModuleConnector).registerHealthListener(
                        mConnectivityModuleCallbackCaptor.capture());
            }
        }
        mAllocatedWatchdogs.add(watchdog);
        return watchdog;
    }