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

Commit 1c67bee1 authored by Lee Shombert's avatar Lee Shombert Committed by Android (Google) Code Review
Browse files

Merge "Refactor PropertyInvalidatedCache" into main

parents e10d2348 75f17d4f
Loading
Loading
Loading
Loading
+395 −330

File changed.

Preview size limit exceeded, changes collapsed.

+18 −12
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.app;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.ravenwood.RavenwoodRule;
@@ -26,6 +27,7 @@ import android.platform.test.ravenwood.RavenwoodRule;
import androidx.test.filters.SmallTest;

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

@@ -90,11 +92,10 @@ public class PropertyInvalidatedCacheTests {
        }
    }

    // Clear the test mode after every test, in case this process is used for other
    // tests. This also resets the test property map.
    // Ensure all test nonces are cleared after the test ends.
    @After
    public void tearDown() throws Exception {
        PropertyInvalidatedCache.setTestMode(false);
        PropertyInvalidatedCache.resetAfterTest();
    }

    // This test is disabled pending an sepolicy change that allows any app to set the
@@ -111,9 +112,6 @@ public class PropertyInvalidatedCacheTests {
                new PropertyInvalidatedCache<>(4, MODULE, API, "cache1",
                        new ServerQuery(tester));

        PropertyInvalidatedCache.setTestMode(true);
        testCache.testPropertyName();

        tester.verify(0);
        assertEquals(tester.value(3), testCache.query(3));
        tester.verify(1);
@@ -223,22 +221,16 @@ public class PropertyInvalidatedCacheTests {

        TestCache(String module, String api) {
            this(module, api, new TestQuery());
            setTestMode(true);
            testPropertyName();
        }

        TestCache(String module, String api, TestQuery query) {
            super(4, module, api, api, query);
            mQuery = query;
            setTestMode(true);
            testPropertyName();
        }

        public int getRecomputeCount() {
            return mQuery.getRecomputeCount();
        }


    }

    @Test
@@ -375,4 +367,18 @@ public class PropertyInvalidatedCacheTests {
            PropertyInvalidatedCache.MODULE_BLUETOOTH, "getState");
        assertEquals(n1, "cache_key.bluetooth.get_state");
    }

    // It is illegal to continue to use a cache with a test key after calling setTestMode(false).
    // This test verifies the code detects errors in calling setTestMode().
    @Test
    public void testTestMode() {
        TestCache cache = new TestCache();
        cache.invalidateCache();
        PropertyInvalidatedCache.resetAfterTest();
        try {
            cache.invalidateCache();
            fail("expected an IllegalStateException");
        } catch (IllegalStateException expected) {
        }
    }
}
+19 −16
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.os;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import android.multiuser.Flags;
import android.platform.test.annotations.IgnoreUnderRavenwood;
@@ -26,6 +27,7 @@ import android.platform.test.ravenwood.RavenwoodRule;
import androidx.test.filters.SmallTest;

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

@@ -92,17 +94,17 @@ public class IpcDataCacheTest {
        public Boolean apply(Integer x) {
            return mServer.query(x);
        }

        @Override
        public boolean shouldBypassCache(Integer x) {
            return x % 13 == 0;
        }
    }

    // Clear the test mode after every test, in case this process is used for other
    // tests. This also resets the test property map.
    // Ensure all test nonces are cleared after the test ends.
    @After
    public void tearDown() throws Exception {
        IpcDataCache.setTestMode(false);
        IpcDataCache.resetAfterTest();
    }

    // This test is disabled pending an sepolicy change that allows any app to set the
@@ -119,9 +121,6 @@ public class IpcDataCacheTest {
                new IpcDataCache<>(4, MODULE, API, "testCache1",
                        new ServerQuery(tester));

        IpcDataCache.setTestMode(true);
        testCache.testPropertyName();

        tester.verify(0);
        assertEquals(tester.value(3), testCache.query(3));
        tester.verify(1);
@@ -165,9 +164,6 @@ public class IpcDataCacheTest {
        IpcDataCache<Integer, Boolean> testCache =
                new IpcDataCache<>(config, (x) -> tester.query(x, x % 10 == 9));

        IpcDataCache.setTestMode(true);
        testCache.testPropertyName();

        tester.verify(0);
        assertEquals(tester.value(3), testCache.query(3));
        tester.verify(1);
@@ -205,9 +201,6 @@ public class IpcDataCacheTest {
        IpcDataCache<Integer, Boolean> testCache =
                new IpcDataCache<>(config, (x) -> tester.query(x), (x) -> x % 9 == 0);

        IpcDataCache.setTestMode(true);
        testCache.testPropertyName();

        tester.verify(0);
        assertEquals(tester.value(3), testCache.query(3));
        tester.verify(1);
@@ -313,8 +306,6 @@ public class IpcDataCacheTest {
        TestCache(String module, String api, TestQuery query) {
            super(4, module, api, "testCache7", query);
            mQuery = query;
            setTestMode(true);
            testPropertyName();
        }

        TestCache(IpcDataCache.Config c) {
@@ -324,8 +315,6 @@ public class IpcDataCacheTest {
        TestCache(IpcDataCache.Config c, TestQuery query) {
            super(c, query);
            mQuery = query;
            setTestMode(true);
            testPropertyName();
        }

        int getRecomputeCount() {
@@ -456,4 +445,18 @@ public class IpcDataCacheTest {
        TestCache ec = new TestCache(e);
        assertEquals(ec.isDisabled(), true);
    }

    // It is illegal to continue to use a cache with a test key after calling setTestMode(false).
    // This test verifies the code detects errors in calling setTestMode().
    @Test
    public void testTestMode() {
        TestCache cache = new TestCache();
        cache.invalidateCache();
        IpcDataCache.resetAfterTest();
        try {
            cache.invalidateCache();
            fail("expected an IllegalStateException");
        } catch (IllegalStateException expected) {
        }
    }
}