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

Commit dd4301e1 authored by William Escande's avatar William Escande
Browse files

Remove binder tests from AdapterServiceTest + unification

The binder is already tested in AdapterServiceBinderTest.java. I am
simplifying the test logic by not having to deal with the binder.

This is a base change for incoming change (maybe having a split file for
the AdapterServiceBinder ?)

Unification is required as all theses test will moved to a TestLooper in
order to have a constant reproducability and remove flakiness

Bug: 292141273
Bug: 291815510
Test: atest AdapterServiceFactoryResetTest
Test: atest AdapterServiceBinderTest
Test: atest AdapterServiceTest
Test: atest AdapterServiceRestartTest
Change-Id: Icede80c625f6fa7c96b5a43935316786d3e3a1ab
parent 14db3a08
Loading
Loading
Loading
Loading
+52 −28
Original line number Diff line number Diff line
@@ -2426,9 +2426,8 @@ public class AdapterService extends Service {
            }
        }

        @VisibleForTesting
        @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN)
        int getScanMode(AttributionSource attributionSource) {
        private int getScanMode(AttributionSource attributionSource) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "getScanMode")
@@ -2437,7 +2436,7 @@ public class AdapterService extends Service {
                return BluetoothAdapter.SCAN_MODE_NONE;
            }

            return service.mAdapterProperties.getScanMode();
            return service.getScanMode();
        }

        @Override
@@ -4036,13 +4035,12 @@ public class AdapterService extends Service {
            }
        }

        @VisibleForTesting
        @RequiresPermission(
                allOf = {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                })
        boolean factoryReset(AttributionSource source) {
        private boolean factoryReset(AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || !Utils.checkConnectPermissionForDataDelivery(service, source, TAG)) {
@@ -4050,20 +4048,7 @@ public class AdapterService extends Service {
            }

            enforceBluetoothPrivilegedPermission(service);

            if (service.mDatabaseManager != null) {
                service.mDatabaseManager.factoryReset();
            }

            if (service.mBluetoothKeystoreService != null) {
                service.mBluetoothKeystoreService.factoryReset();
            }

            if (service.mBtCompanionManager != null) {
                service.mBtCompanionManager.factoryReset();
            }

            return service.factoryResetNative();
            return service.factoryReset();
        }

        @Override
@@ -4140,13 +4125,12 @@ public class AdapterService extends Service {
            }
        }

        @VisibleForTesting
        @RequiresPermission(
                allOf = {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                })
        void registerCallback(IBluetoothCallback callback, AttributionSource source) {
        private void registerCallback(IBluetoothCallback callback, AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "registerCallback")
@@ -4156,7 +4140,7 @@ public class AdapterService extends Service {

            enforceBluetoothPrivilegedPermission(service);

            service.mCallbacks.register(callback);
            service.registerCallback(callback);
        }

        @Override
@@ -4178,7 +4162,7 @@ public class AdapterService extends Service {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                })
        void unregisterCallback(IBluetoothCallback callback, AttributionSource source) {
        private void unregisterCallback(IBluetoothCallback callback, AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || service.mCallbacks == null
@@ -4189,7 +4173,7 @@ public class AdapterService extends Service {

            enforceBluetoothPrivilegedPermission(service);

            service.mCallbacks.unregister(callback);
            service.unregisterCallback(callback);
        }

        @Override
@@ -4739,13 +4723,12 @@ public class AdapterService extends Service {
            }
        }

        @VisibleForTesting
        @RequiresPermission(
                allOf = {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                })
        void startBrEdr(AttributionSource source) {
        private void startBrEdr(AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "startBrEdr")
@@ -4768,13 +4751,12 @@ public class AdapterService extends Service {
            }
        }

        @VisibleForTesting
        @RequiresPermission(
                allOf = {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                })
        void stopBle(AttributionSource source) {
        private void stopBle(AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "stopBle")
@@ -6781,6 +6763,48 @@ public class AdapterService extends Service {
        return mAdapterProperties.isA2dpOffloadEnabled();
    }

    @VisibleForTesting
    void registerCallback(IBluetoothCallback callback) {
        mCallbacks.register(callback);
    }

    @VisibleForTesting
    void unregisterCallback(IBluetoothCallback callback) {
        mCallbacks.unregister(callback);
    }

    @VisibleForTesting
    void startBrEdr() {
        mAdapterStateMachine.sendMessage(AdapterState.USER_TURN_ON);
    }

    @VisibleForTesting
    void stopBle() {
        mAdapterStateMachine.sendMessage(AdapterState.BLE_TURN_OFF);
    }

    @VisibleForTesting
    boolean factoryReset() {
        if (mDatabaseManager != null) {
            mDatabaseManager.factoryReset();
        }

        if (mBluetoothKeystoreService != null) {
            mBluetoothKeystoreService.factoryReset();
        }

        if (mBtCompanionManager != null) {
            mBtCompanionManager.factoryReset();
        }

        return factoryResetNative();
    }

    @VisibleForTesting
    int getScanMode() {
        return mAdapterProperties.getScanMode();
    }

    public String[] getAllowlistedMediaPlayers() {
        return mAdapterProperties.getAllowlistedMediaPlayers();
    }
+1 −1
Original line number Diff line number Diff line
@@ -110,7 +110,7 @@ public class AdapterServiceBinderTest {
    @Test
    public void getScanMode() {
        mBinder.getScanMode(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getScanMode();
        verify(mService).getScanMode();
    }

    @Test
+13 −148
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.bluetooth.btservice;

import static android.Manifest.permission.BLUETOOTH_SCAN;

import static com.google.common.truth.Truth.assertThat;

@@ -31,7 +30,6 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.IBluetoothCallback;
import android.companion.CompanionDeviceManager;
import android.content.AttributionSource;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -45,7 +43,6 @@ import android.os.Bundle;
import android.os.Looper;
import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.permission.PermissionCheckerManager;
@@ -61,31 +58,6 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.avrcp.AvrcpTargetService;
import com.android.bluetooth.avrcpcontroller.AvrcpControllerService;
import com.android.bluetooth.bas.BatteryService;
import com.android.bluetooth.bass_client.BassClientService;
import com.android.bluetooth.csip.CsipSetCoordinatorService;
import com.android.bluetooth.gatt.GattService;
import com.android.bluetooth.hap.HapClientService;
import com.android.bluetooth.hearingaid.HearingAidService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.hfpclient.HeadsetClientService;
import com.android.bluetooth.hid.HidDeviceService;
import com.android.bluetooth.hid.HidHostService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.bluetooth.map.BluetoothMapService;
import com.android.bluetooth.mapclient.MapClientService;
import com.android.bluetooth.mcp.McpService;
import com.android.bluetooth.opp.BluetoothOppService;
import com.android.bluetooth.pan.PanService;
import com.android.bluetooth.pbap.BluetoothPbapService;
import com.android.bluetooth.pbapclient.PbapClientService;
import com.android.bluetooth.sap.SapService;
import com.android.bluetooth.tbs.TbsService;
import com.android.bluetooth.vc.VolumeControlService;
import com.android.internal.app.IBatteryStats;

import org.junit.After;
@@ -99,6 +71,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashMap;
import java.util.List;

@MediumTest
@RunWith(AndroidJUnit4.class)
@@ -106,7 +79,6 @@ public class AdapterServiceFactoryResetTest {
    private static final String TAG = AdapterServiceFactoryResetTest.class.getSimpleName();

    private AdapterService mAdapterService;
    private AdapterService.AdapterServiceBinder mServiceBinder;

    private @Mock Context mMockContext;
    private @Mock ApplicationInfo mMockApplicationInfo;
@@ -143,59 +115,14 @@ public class AdapterServiceFactoryResetTest {
    private static final int ONE_SECOND_MS = 1000;
    private static final int NATIVE_INIT_MS = 8000;

    private final AttributionSource mAttributionSource = new AttributionSource.Builder(
            Process.myUid()).build();

    private PackageManager mMockPackageManager;
    private MockContentResolver mMockContentResolver;
    private HashMap<String, HashMap<String, String>> mAdapterConfig;
    private int mForegroundUserId;

    private void configureEnabledProfiles() {
        Log.e(TAG, "configureEnabledProfiles");
        Config.setProfileEnabled(PanService.class, true);
        Config.setProfileEnabled(BluetoothPbapService.class, true);
        Config.setProfileEnabled(GattService.class, true);

        Config.setProfileEnabled(A2dpService.class, false);
        Config.setProfileEnabled(A2dpSinkService.class, false);
        Config.setProfileEnabled(AvrcpTargetService.class, false);
        Config.setProfileEnabled(AvrcpControllerService.class, false);
        Config.setProfileEnabled(BassClientService.class, false);
        Config.setProfileEnabled(BatteryService.class, false);
        Config.setProfileEnabled(CsipSetCoordinatorService.class, false);
        Config.setProfileEnabled(HapClientService.class, false);
        Config.setProfileEnabled(HeadsetService.class, false);
        Config.setProfileEnabled(HeadsetClientService.class, false);
        Config.setProfileEnabled(HearingAidService.class, false);
        Config.setProfileEnabled(HidDeviceService.class, false);
        Config.setProfileEnabled(HidHostService.class, false);
        Config.setProfileEnabled(LeAudioService.class, false);
        Config.setProfileEnabled(TbsService.class, false);
        Config.setProfileEnabled(BluetoothMapService.class, false);
        Config.setProfileEnabled(MapClientService.class, false);
        Config.setProfileEnabled(McpService.class, false);
        Config.setProfileEnabled(BluetoothOppService.class, false);
        Config.setProfileEnabled(PbapClientService.class, false);
        Config.setProfileEnabled(SapService.class, false);
        Config.setProfileEnabled(VolumeControlService.class, false);
    }

    @BeforeClass
    public static void setupClass() {
        Log.e(TAG, "setupClass");
        // Bring native layer up and down to make sure config files are properly loaded
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        assertThat(Looper.myLooper()).isNotNull();
        AdapterService adapterService = new AdapterService();
        adapterService.initNative(false /* is_restricted */, false /* is_common_criteria_mode */,
                0 /* config_compare_result */, new String[0], false, "");
        adapterService.cleanupNative();
        HashMap<String, HashMap<String, String>> adapterConfig = TestUtils.readAdapterConfig();
        assertThat(adapterConfig).isNotNull();
        assertThat(AdapterServiceTest.getMetricsSalt(adapterConfig)).isNotNull();
        AdapterServiceTest.setupClass();
    }

    <T> void mockGetSystemService(String serviceName, Class<T> serviceClass, T mockService) {
@@ -223,7 +150,6 @@ public class AdapterServiceFactoryResetTest {

        androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().runOnMainSync(
                () -> mAdapterService = new AdapterService());
        mServiceBinder = new AdapterService.AdapterServiceBinder(mAdapterService);
        mMockPackageManager = mock(PackageManager.class);
        when(mMockPackageManager.getPermissionInfo(any(), anyInt()))
                .thenReturn(new PermissionInfo());
@@ -290,7 +216,6 @@ public class AdapterServiceFactoryResetTest {
                                .getSharedPreferences(
                                        "AdapterServiceTestPrefs", Context.MODE_PRIVATE));

        when(mMockContext.getAttributionSource()).thenReturn(mAttributionSource);
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            return InstrumentationRegistry.getTargetContext().getDatabasePath((String) args[0]);
@@ -316,7 +241,7 @@ public class AdapterServiceFactoryResetTest {
        when(mMockMetricsLogger.init(any())).thenReturn(true);
        when(mMockMetricsLogger.close()).thenReturn(true);

        configureEnabledProfiles();
        AdapterServiceTest.configureEnabledProfiles();
        Config.init(mMockContext);

        mAdapterService.setMetricsLogger(mMockMetricsLogger);
@@ -328,8 +253,6 @@ public class AdapterServiceFactoryResetTest {
        // Wait for any async events to drain
        androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        mServiceBinder.registerCallback(mIBluetoothCallback, mAttributionSource);

        mAdapterConfig = TestUtils.readAdapterConfig();
        assertThat(mAdapterConfig).isNotNull();
    }
@@ -344,7 +267,6 @@ public class AdapterServiceFactoryResetTest {
        // Restores the foregroundUserId to the ID prior to the test setup
        Utils.setForegroundUserId(mForegroundUserId);

        mServiceBinder.unregisterCallback(mIBluetoothCallback, mAttributionSource);
        mAdapterService.cleanup();
    }

@@ -353,71 +275,14 @@ public class AdapterServiceFactoryResetTest {
        AsyncTask.setDefaultExecutor(AsyncTask.SERIAL_EXECUTOR);
    }

    private void verifyStateChange(int prevState, int currState, int callNumber, int timeoutMs) {
        try {
            verify(mIBluetoothCallback, timeout(timeoutMs).times(callNumber))
                .onBluetoothStateChange(prevState, currState);
        } catch (RemoteException e) {
            // the mocked onBluetoothStateChange doesn't throw RemoteException
        }
    }

    private void doEnable() {
        Log.e("AdapterServiceTest", "doEnable() start");
        assertThat(mAdapterService.getState()).isNotEqualTo(BluetoothAdapter.STATE_ON);

        mAdapterService.enable(false);

        verifyStateChange(
                BluetoothAdapter.STATE_OFF,
                BluetoothAdapter.STATE_BLE_TURNING_ON,
                1,
                CONTEXT_SWITCH_MS);

        // Start GATT
        verify(mMockContext, timeout(GATT_START_TIME_MS).times(1))
                .bindServiceAsUser(any(), any(), anyInt(), any());
        mAdapterService.addProfile(mMockGattService);
        mAdapterService.onProfileServiceStateChanged(mMockGattService, BluetoothAdapter.STATE_ON);

        verifyStateChange(
                BluetoothAdapter.STATE_BLE_TURNING_ON,
                BluetoothAdapter.STATE_BLE_ON,
                1,
                NATIVE_INIT_MS);

        mServiceBinder.startBrEdr(mAttributionSource);

        verifyStateChange(
                BluetoothAdapter.STATE_BLE_ON,
                BluetoothAdapter.STATE_TURNING_ON,
                1,
                CONTEXT_SWITCH_MS);

        // Start Mock PBAP and PAN services
        verify(mMockContext, timeout(ONE_SECOND_MS).times(2)).startService(any());

        mAdapterService.addProfile(mMockService);
        mAdapterService.addProfile(mMockService2);
        mAdapterService.onProfileServiceStateChanged(mMockService, BluetoothAdapter.STATE_ON);
        mAdapterService.onProfileServiceStateChanged(mMockService2, BluetoothAdapter.STATE_ON);

        verifyStateChange(
                BluetoothAdapter.STATE_TURNING_ON,
                BluetoothAdapter.STATE_ON,
    void doEnable() {
        AdapterServiceTest.doEnable(
                mMockGattService,
                mAdapterService,
                mMockContext,
                1,
                PROFILE_SERVICE_TOGGLE_TIME_MS);

        verify(mMockContext, timeout(CONTEXT_SWITCH_MS).times(2))
                .sendBroadcast(any(), eq(BLUETOOTH_SCAN), any(Bundle.class));
        final int scanMode = mServiceBinder.getScanMode(mAttributionSource);
        assertThat(
                        scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE
                                || scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
                .isTrue();
        assertThat(mAdapterService.getState()).isEqualTo(BluetoothAdapter.STATE_ON);

        Log.e("AdapterServiceTest", "doEnable() complete success");
                false,
                List.of(mMockService, mMockService2));
    }

    /**
@@ -444,7 +309,7 @@ public class AdapterServiceFactoryResetTest {
        byte[] obfuscatedAddress1 = mAdapterService.obfuscateAddress(device);
        assertThat(obfuscatedAddress1).isNotEmpty();
        assertThat(AdapterServiceTest.isByteArrayAllZero(obfuscatedAddress1)).isFalse();
        mServiceBinder.factoryReset(mAttributionSource);
        mAdapterService.factoryReset();
        byte[] obfuscatedAddress2 = mAdapterService.obfuscateAddress(device);
        assertThat(obfuscatedAddress2).isNotEmpty();
        assertThat(AdapterServiceTest.isByteArrayAllZero(obfuscatedAddress2)).isFalse();
@@ -454,7 +319,7 @@ public class AdapterServiceFactoryResetTest {
        assertThat(obfuscatedAddress3).isNotEmpty();
        assertThat(AdapterServiceTest.isByteArrayAllZero(obfuscatedAddress3)).isFalse();
        assertThat(obfuscatedAddress3).isEqualTo(obfuscatedAddress2);
        mServiceBinder.factoryReset(mAttributionSource);
        mAdapterService.factoryReset();
        byte[] obfuscatedAddress4 = mAdapterService.obfuscateAddress(device);
        assertThat(obfuscatedAddress4).isNotEmpty();
        assertThat(AdapterServiceTest.isByteArrayAllZero(obfuscatedAddress4)).isFalse();
@@ -477,7 +342,7 @@ public class AdapterServiceFactoryResetTest {
        assertThat(AdapterServiceTest.isByteArrayAllZero(obfuscatedAddress1)).isFalse();
        assertThat(AdapterServiceTest.obfuscateInJava(metricsSalt1, device))
                .isEqualTo(obfuscatedAddress1);
        mServiceBinder.factoryReset(mAttributionSource);
        mAdapterService.factoryReset();
        tearDown();
        setUp();
        // Cannot verify metrics salt since it is not written to disk until native cleanup
+4 −78
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.IBluetoothCallback;
import android.companion.CompanionDeviceManager;
import android.content.AttributionSource;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -58,31 +57,6 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.avrcp.AvrcpTargetService;
import com.android.bluetooth.avrcpcontroller.AvrcpControllerService;
import com.android.bluetooth.bas.BatteryService;
import com.android.bluetooth.bass_client.BassClientService;
import com.android.bluetooth.csip.CsipSetCoordinatorService;
import com.android.bluetooth.gatt.GattService;
import com.android.bluetooth.hap.HapClientService;
import com.android.bluetooth.hearingaid.HearingAidService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.hfpclient.HeadsetClientService;
import com.android.bluetooth.hid.HidDeviceService;
import com.android.bluetooth.hid.HidHostService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.bluetooth.map.BluetoothMapService;
import com.android.bluetooth.mapclient.MapClientService;
import com.android.bluetooth.mcp.McpService;
import com.android.bluetooth.opp.BluetoothOppService;
import com.android.bluetooth.pan.PanService;
import com.android.bluetooth.pbap.BluetoothPbapService;
import com.android.bluetooth.pbapclient.PbapClientService;
import com.android.bluetooth.sap.SapService;
import com.android.bluetooth.tbs.TbsService;
import com.android.bluetooth.vc.VolumeControlService;
import com.android.internal.app.IBatteryStats;

import org.junit.After;
@@ -102,7 +76,6 @@ public class AdapterServiceRestartTest {
    private static final String TAG = AdapterServiceTest.class.getSimpleName();

    private AdapterService mAdapterService;
    private AdapterService.AdapterServiceBinder mServiceBinder;

    private @Mock Context mMockContext;
    private @Mock ApplicationInfo mMockApplicationInfo;
@@ -130,59 +103,14 @@ public class AdapterServiceRestartTest {
    final BatteryStatsManager mBatteryStatsManager =
            new BatteryStatsManager(mock(IBatteryStats.class));

    private final AttributionSource mAttributionSource = new AttributionSource.Builder(
            Process.myUid()).build();

    private PackageManager mMockPackageManager;
    private MockContentResolver mMockContentResolver;
    private HashMap<String, HashMap<String, String>> mAdapterConfig;
    private int mForegroundUserId;

    private void configureEnabledProfiles() {
        Log.e(TAG, "configureEnabledProfiles");
        Config.setProfileEnabled(PanService.class, true);
        Config.setProfileEnabled(BluetoothPbapService.class, true);
        Config.setProfileEnabled(GattService.class, true);

        Config.setProfileEnabled(A2dpService.class, false);
        Config.setProfileEnabled(A2dpSinkService.class, false);
        Config.setProfileEnabled(AvrcpTargetService.class, false);
        Config.setProfileEnabled(AvrcpControllerService.class, false);
        Config.setProfileEnabled(BassClientService.class, false);
        Config.setProfileEnabled(BatteryService.class, false);
        Config.setProfileEnabled(CsipSetCoordinatorService.class, false);
        Config.setProfileEnabled(HapClientService.class, false);
        Config.setProfileEnabled(HeadsetService.class, false);
        Config.setProfileEnabled(HeadsetClientService.class, false);
        Config.setProfileEnabled(HearingAidService.class, false);
        Config.setProfileEnabled(HidDeviceService.class, false);
        Config.setProfileEnabled(HidHostService.class, false);
        Config.setProfileEnabled(LeAudioService.class, false);
        Config.setProfileEnabled(TbsService.class, false);
        Config.setProfileEnabled(BluetoothMapService.class, false);
        Config.setProfileEnabled(MapClientService.class, false);
        Config.setProfileEnabled(McpService.class, false);
        Config.setProfileEnabled(BluetoothOppService.class, false);
        Config.setProfileEnabled(PbapClientService.class, false);
        Config.setProfileEnabled(SapService.class, false);
        Config.setProfileEnabled(VolumeControlService.class, false);
    }

    @BeforeClass
    public static void setupClass() {
        Log.e(TAG, "setupClass");
        // Bring native layer up and down to make sure config files are properly loaded
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        assertThat(Looper.myLooper()).isNotNull();
        AdapterService adapterService = new AdapterService();
        adapterService.initNative(false /* is_restricted */, false /* is_common_criteria_mode */,
                0 /* config_compare_result */, new String[0], false, "");
        adapterService.cleanupNative();
        HashMap<String, HashMap<String, String>> adapterConfig = TestUtils.readAdapterConfig();
        assertThat(adapterConfig).isNotNull();
        assertThat(AdapterServiceTest.getMetricsSalt(adapterConfig)).isNotNull();
        AdapterServiceTest.setupClass();
    }

    <T> void mockGetSystemService(String serviceName, Class<T> serviceClass, T mockService) {
@@ -210,7 +138,6 @@ public class AdapterServiceRestartTest {

        androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().runOnMainSync(
                () -> mAdapterService = new AdapterService());
        mServiceBinder = new AdapterService.AdapterServiceBinder(mAdapterService);
        mMockPackageManager = mock(PackageManager.class);
        when(mMockPackageManager.getPermissionInfo(any(), anyInt()))
                .thenReturn(new PermissionInfo());
@@ -275,7 +202,6 @@ public class AdapterServiceRestartTest {
                .thenReturn(InstrumentationRegistry.getTargetContext()
                        .getSharedPreferences("AdapterServiceTestPrefs", Context.MODE_PRIVATE));

        when(mMockContext.getAttributionSource()).thenReturn(mAttributionSource);
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            return InstrumentationRegistry.getTargetContext().getDatabasePath((String) args[0]);
@@ -297,7 +223,7 @@ public class AdapterServiceRestartTest {
        when(mMockMetricsLogger.init(any())).thenReturn(true);
        when(mMockMetricsLogger.close()).thenReturn(true);

        configureEnabledProfiles();
        AdapterServiceTest.configureEnabledProfiles();
        Config.init(mMockContext);

        mAdapterService.setMetricsLogger(mMockMetricsLogger);
@@ -309,7 +235,7 @@ public class AdapterServiceRestartTest {
        // Wait for any async events to drain
        androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        mServiceBinder.registerCallback(mIBluetoothCallback, mAttributionSource);
        mAdapterService.registerCallback(mIBluetoothCallback);

        mAdapterConfig = TestUtils.readAdapterConfig();
        assertThat(mAdapterConfig).isNotNull();
@@ -322,7 +248,7 @@ public class AdapterServiceRestartTest {
        // Restores the foregroundUserId to the ID prior to the test setup
        Utils.setForegroundUserId(mForegroundUserId);

        mServiceBinder.unregisterCallback(mIBluetoothCallback, mAttributionSource);
        mAdapterService.unregisterCallback(mIBluetoothCallback);
        mAdapterService.cleanup();
    }

+127 −78

File changed.

Preview size limit exceeded, changes collapsed.