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

Commit d28a91ff authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Use test looper to prevent unexpected handler executions

Bug: 307693381
Test: atest BluetoothInstrumentationTests
Change-Id: I7b49676a960bb7bc6e0e6f59f28183adf44edae1
parent bbdafa4f
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.sysprop.BluetoothProperties;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.IObexConnectionHandler;
import com.android.bluetooth.ObexServerSockets;
import com.android.bluetooth.R;
@@ -468,7 +469,6 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
                sNotificationManager.cancelAll();
            }
        }

    }
    private class PbapHandler extends Handler {
        private PbapHandler(Looper looper) {
@@ -710,8 +710,9 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        mContext = this;
        mContactsLoaded = false;
        mHandlerThread = new HandlerThread("PbapHandlerThread");
        mHandlerThread.start();
        mSessionStatusHandler = new PbapHandler(mHandlerThread.getLooper());
        BluetoothMethodProxy mp = BluetoothMethodProxy.getInstance();
        mp.threadStart(mHandlerThread);
        mSessionStatusHandler = new PbapHandler(mp.handlerThreadGetLooper(mHandlerThread));
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
+14 −0
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.bluetooth.pbap;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -29,11 +31,13 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Message;
import android.os.test.TestLooper;

import androidx.test.filters.MediumTest;
import androidx.test.rule.ServiceTestRule;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
@@ -46,6 +50,7 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

@MediumTest
@RunWith(AndroidJUnit4.class)
@@ -57,15 +62,22 @@ public class BluetoothPbapServiceTest {
    private BluetoothDevice mRemoteDevice;
    private boolean mIsAdapterServiceSet;
    private boolean mIsBluetoothPabpServiceStarted;
    private TestLooper mTestLooper;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;
    @Spy private BluetoothMethodProxy mMethodProxy = BluetoothMethodProxy.getInstance();

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mTestLooper = new TestLooper();
        BluetoothMethodProxy.setInstanceForTesting(mMethodProxy);
        doReturn(mTestLooper.getLooper()).when(mMethodProxy).handlerThreadGetLooper(any());
        doNothing().when(mMethodProxy).threadStart(any());
        mTestLooper.startAutoDispatch();
        TestUtils.setAdapterService(mAdapterService);
        mIsAdapterServiceSet = true;
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
@@ -82,6 +94,8 @@ public class BluetoothPbapServiceTest {

    @After
    public void tearDown() throws Exception {
        mTestLooper.stopAutoDispatchAndIgnoreExceptions();
        BluetoothMethodProxy.setInstanceForTesting(null);
        if (!mIsAdapterServiceSet) {
            return;
        }