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

Commit 8071a43a authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

tests: Multiple unit test fixes

This fixes failed mocking, unsupported class loader errors
and improves tests stability.

Bug: 230559809
Tag: #feature
Sponsor: jpawlowski@
Test: atest BluetoothInstrumentationTests
Change-Id: I5953c49bb11af13fe9578be709f02582080124f3
parent 1b342688
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -104,7 +104,8 @@ public class HapClientNativeInterface {
        return Utils.getBytesFromAddress(device.getAddress());
    }

    private void sendMessageToService(HapClientStackEvent event) {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    void sendMessageToService(HapClientStackEvent event) {
        HapClientService service = HapClientService.getHapClientService();
        if (service != null) {
            service.messageFromNative(event);
+12 −3
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ import org.mockito.MockitoAnnotations;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class CsipSetCoordinatorServiceTest {
    private final String mFlagDexmarker = System.getProperty("dexmaker.share_classloader", "false");

    public final ServiceTestRule mServiceRule = new ServiceTestRule();
    private Context mTargetContext;
    private BluetoothAdapter mAdapter;
@@ -73,11 +75,14 @@ public class CsipSetCoordinatorServiceTest {
    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private CsipSetCoordinatorNativeInterface mCsipSetCoordinatorNativeInterface;
    @Mock private CsipSetCoordinatorService mCsipSetCoordinatorService;
    @Mock private IBluetoothCsipSetCoordinatorLockCallback mCsipSetCoordinatorLockCallback;

    @Before
    public void setUp() throws Exception {
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", "true");
        }

        mTargetContext = InstrumentationRegistry.getTargetContext();
        if (Looper.myLooper() == null) {
            Looper.prepare();
@@ -132,14 +137,18 @@ public class CsipSetCoordinatorServiceTest {

    @After
    public void tearDown() throws Exception {
        if (mService == null) {
            return;
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", mFlagDexmarker);
        }

        if (Looper.myLooper() == null) {
            return;
        }

        if (mService == null) {
            return;
        }

        stopService();
        mTargetContext.unregisterReceiver(mCsipSetCoordinatorIntentReceiver);
        TestUtils.clearAdapterService(mAdapterService);
+9 −0
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ import org.mockito.MockitoAnnotations;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class CsipSetCoordinatorStateMachineTest {
    private final String mFlagDexmarker = System.getProperty("dexmaker.share_classloader", "false");

    private Context mTargetContext;
    private BluetoothAdapter mAdapter;
    private BluetoothDevice mTestDevice;
@@ -60,6 +62,10 @@ public class CsipSetCoordinatorStateMachineTest {

    @Before
    public void setUp() throws Exception {
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", "true");
        }

        mTargetContext = InstrumentationRegistry.getTargetContext();
        // Set up mocks and test assets
        MockitoAnnotations.initMocks(this);
@@ -83,6 +89,9 @@ public class CsipSetCoordinatorStateMachineTest {

    @After
    public void tearDown() throws Exception {
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", mFlagDexmarker);
        }
        mStateMachine.doQuit();
        mHandlerThread.quit();
        TestUtils.clearAdapterService(mAdapterService);
+36 −3
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ import java.util.concurrent.TimeoutException;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class HapClientTest {
    private final String mFlagDexmarker = System.getProperty("dexmaker.share_classloader", "false");

    private static final int TIMEOUT_MS = 1000;
    @Rule
    public final ServiceTestRule mServiceRule = new ServiceTestRule();
@@ -95,6 +97,10 @@ public class HapClientTest {

    @Before
    public void setUp() throws Exception {
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", "true");
        }

        mTargetContext = InstrumentationRegistry.getTargetContext();
        // Set up mocks and test assets
        MockitoAnnotations.initMocks(this);
@@ -134,6 +140,21 @@ public class HapClientTest {
        mDevice3 = TestUtils.getTestDevice(mAdapter, 2);
        when(mNativeInterface.getDevice(getByteAddress(mDevice3))).thenReturn(mDevice3);

        doCallRealMethod().when(mNativeInterface)
                .sendMessageToService(any(HapClientStackEvent.class));
        doCallRealMethod().when(mNativeInterface).onFeaturesUpdate(any(byte[].class), anyInt());
        doCallRealMethod().when(mNativeInterface).onDeviceAvailable(any(byte[].class), anyInt());
        doCallRealMethod().when(mNativeInterface)
                .onActivePresetSelected(any(byte[].class), anyInt());
        doCallRealMethod().when(mNativeInterface)
                .onActivePresetSelectError(any(byte[].class), anyInt());
        doCallRealMethod().when(mNativeInterface)
                .onPresetNameSetError(any(byte[].class), anyInt(), anyInt());
        doCallRealMethod().when(mNativeInterface)
                .onPresetInfo(any(byte[].class), anyInt(), any(BluetoothHapPresetInfo[].class));
        doCallRealMethod().when(mNativeInterface)
                .onGroupPresetNameSetError(anyInt(), anyInt(), anyInt());

        /* Prepare CAS groups */
        doReturn(Arrays.asList(0x02, 0x03)).when(mCsipService).getAllGroupIds(BluetoothUuid.CAP);

@@ -170,6 +191,10 @@ public class HapClientTest {

    @After
    public void tearDown() throws Exception {
        if (!mFlagDexmarker.equals("true")) {
            System.setProperty("dexmaker.share_classloader", mFlagDexmarker);
        }

        if (mService == null) {
            return;
        }
@@ -177,10 +202,18 @@ public class HapClientTest {
        mService.mCallbacks.unregister(mCallback);

        stopService();

        if (mHasIntentReceiver != null) {
            mTargetContext.unregisterReceiver(mHasIntentReceiver);
        }

        mAdapter = null;

        if (mAdapterService != null) {
            TestUtils.clearAdapterService(mAdapterService);
        }

        if (mIntentQueue != null)
            mIntentQueue.clear();
    }