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

Commit 45b9567d authored by Biswarup Pal's avatar Biswarup Pal
Browse files

Fix SettingsStateTest#testNoWriteForVirtualDevice

The test testReadWriteForDefaultDevice calls
SettingsState#insertSettingsLocked which posts messages
into a handler with a delay, which causes write operation
into a file (to persist settings). SettingsStae#waitForHandler
doesn't handle this case, as it immediately posts a message
into the handler and waits for it to execute. As a result, the
file write operation sometimes happens asynchronously while
testNoWriteForVirtualDevice is executing (where we verify that a
settings file should not exist for virtual device settings), and this
makes the test flaky. To solve this, use a different File object
for testNoWriteForVirtualDevice.

Test: atest SettingsStateTest#testNoWriteForVirtualDevice
Fixes: 413049126
Flag: EXEMPT test fix
Change-Id: Ia3eb9a47565c856c98e1929e85e139ddc7982088
parent 2379ed99
Loading
Loading
Loading
Loading
+17 −15
Original line number Diff line number Diff line
@@ -358,13 +358,12 @@ public class SettingsStateTest {
                        SettingsState.makeKey(SettingsState.SETTINGS_TYPE_GLOBAL, 1,
                                Context.DEVICE_ID_DEFAULT),
                        SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
        synchronized (lock) {
            ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSION_NEW_ENCODING);

            ssWriter.insertSettingLocked("k1", "\u0000", null, false, "package");
            ssWriter.insertSettingLocked("k2", "abc", null, false, "p2");
            ssWriter.insertSettingLocked("k3", null, null, false, "p2");
            ssWriter.insertSettingLocked("k4", CRAZY_STRING, null, false, "p3");
        synchronized (lock) {
            ssWriter.persistSettingsLocked();
        }
        ssWriter.waitForHandler();
@@ -391,25 +390,28 @@ public class SettingsStateTest {
    @Parameters(method = "getVirtualDeviceIds")
    public void testNoWriteForVirtualDevice(int deviceId) {
        final Object lock = new Object();
        File settingsFile = new File(InstrumentationRegistry.getContext().getCacheDir(),
                "vdsetting.xml");
        settingsFile.delete();
        assertFalse(settingsFile.exists());

        assertFalse(mSettingsFile.exists());
        final SettingsState ssWriter =
                new SettingsState(
                        InstrumentationRegistry.getContext(), lock, mSettingsFile,
                        InstrumentationRegistry.getContext(), lock, settingsFile,
                        SettingsState.makeKey(SettingsState.SETTINGS_TYPE_GLOBAL, 1,
                                deviceId),
                        SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
        synchronized (lock) {
            ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSION_NEW_ENCODING);

            ssWriter.insertSettingLocked("k1", "\u0000", null, false, "package");
            ssWriter.insertSettingLocked("k2", "abc", null, false, "p2");
            ssWriter.insertSettingLocked("k3", null, null, false, "p2");
            ssWriter.insertSettingLocked("k4", CRAZY_STRING, null, false, "p3");
        synchronized (lock) {
            ssWriter.persistSettingsLocked();
        }
        ssWriter.waitForHandler();
        assertFalse(mSettingsFile.exists());

        assertFalse(settingsFile.exists());
    }

    /**