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

Commit 869bf1d2 authored by William Escande's avatar William Escande
Browse files

SystemServer: use test cache in test

The test does not run as the SystemServer. Therefor it can't invalidate
cache due to se policy. We need the unit test to use a fake cache

Test: atest ServiceBluetoothTests
Bug: 362569584
Flag: com.android.bluetooth.get_state_from_system_server
Change-Id: If81c0d359f467a3ceed601a6208fde188b9d96ee
parent db360885
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ android_test {

    static_libs: [
        "androidx.test.rules",
        "flag-junit",
        "frameworks-base-testutils",
        "kotlinx_coroutines_test",
        "libprotobuf-java-nano",
+21 −22
Original line number Diff line number Diff line
@@ -18,8 +18,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.server.bluetooth.test">

    <application android:debuggable="true"
         android:largeHeap="true">
    <application android:debuggable="true" android:largeHeap="true">
        <uses-library android:name="android.test.runner"/>
        <activity android:label="BluetoothTestFakeLabel"
            android:name="BluetoothTestFakeName"
+25 −0
Original line number Diff line number Diff line
@@ -41,8 +41,10 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.validateMockitoUsage;
import static org.mockito.Mockito.verify;

import android.app.PropertyInvalidatedCache;
import android.bluetooth.IBluetooth;
import android.bluetooth.IBluetoothCallback;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothManagerCallback;
import android.bluetooth.IBluetoothStateChangeCallback;
import android.content.ComponentName;
@@ -95,6 +97,19 @@ public class BluetoothManagerServiceTest {

    TestLooper mLooper;

    private static class ServerQuery
            extends PropertyInvalidatedCache.QueryHandler<IBluetoothManager, Integer> {
        @Override
        public Integer apply(IBluetoothManager x) {
            return -1;
        }

        @Override
        public boolean shouldBypassCache(IBluetoothManager x) {
            return true;
        }
    }

    static {
        // Required for reading DeviceConfig.
        InstrumentationRegistry.getInstrumentation()
@@ -107,6 +122,15 @@ public class BluetoothManagerServiceTest {
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        PropertyInvalidatedCache<IBluetoothManager, Integer> testCache =
                new PropertyInvalidatedCache<>(
                        8,
                        IBluetoothManager.IPC_CACHE_MODULE_SYSTEM,
                        IBluetoothManager.GET_SYSTEM_STATE_API,
                        IBluetoothManager.GET_SYSTEM_STATE_API,
                        new ServerQuery());
        PropertyInvalidatedCache.setTestMode(true);
        testCache.testPropertyName();
        // Mock these functions so security errors won't throw
        doReturn("name")
                .when(mBluetoothServerProxy)
@@ -162,6 +186,7 @@ public class BluetoothManagerServiceTest {

    @After
    public void tearDown() {
        PropertyInvalidatedCache.setTestMode(false);
        if (mManagerService != null) {
            mManagerService.unregisterAdapter(mManagerCallback);
            mManagerService = null;
+16 −0
Original line number Diff line number Diff line
@@ -48,11 +48,16 @@ import android.content.ContextWrapper;
import android.os.IBinder;
import android.os.Process;
import android.os.UserManager;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

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

import com.android.bluetooth.flags.Flags;

import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;

@@ -80,6 +85,8 @@ public class BluetoothServiceBinderTest {

    @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(STRICT_STUBS);

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Rule public TestRule compatChangeRule = new PlatformCompatChangeRule();

    @Mock private BluetoothManagerService mManagerService;
@@ -217,6 +224,7 @@ public class BluetoothServiceBinderTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_GET_STATE_FROM_SYSTEM_SERVER)
    public void getState() {
        // TODO(b/280518177): add more test from not System / ...
        // TODO(b/280518177): add more test when caller is not in foreground
@@ -227,6 +235,14 @@ public class BluetoothServiceBinderTest {
        verifyMock();
    }

    @Test
    @EnableFlags(Flags.FLAG_GET_STATE_FROM_SYSTEM_SERVER)
    public void getStateFromSystemServer() {
        mBinder.getState();
        verify(mManagerService).getState();
        verifyMock();
    }

    @Test
    public void getAddress() {
        assertThrows(NullPointerException.class, () -> mBinder.getAddress(null));