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

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

MapClient: static dependency on BluetoothProfile

Test: atest BluetoothInstrumentationTests
Flag: TEST_ONLY
Bug: 386715308
Change-Id: Ic9a33757eec2730d782919ef587d9ca8d1878fcb
parent 20f49cb9
Loading
Loading
Loading
Loading
+45 −102
Original line number Diff line number Diff line
@@ -20,6 +20,10 @@ import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
import static android.bluetooth.BluetoothProfile.EXTRA_PREVIOUS_STATE;
import static android.bluetooth.BluetoothProfile.EXTRA_STATE;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTING;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTING;

import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra;
@@ -38,7 +42,6 @@ import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothMapClient;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.SdpMasRecord;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
@@ -229,7 +232,7 @@ public class MapClientStateMachineTest {
                        mMockDatabase);
        mLooper.dispatchAll();
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING);
                STATE_DISCONNECTED, STATE_CONNECTING);

        when(mMockRequestOwnNumberCompletedWithNumber.isSearchCompleted()).thenReturn(true);
        when(mMockRequestOwnNumberCompletedWithNumber.getOwnNumber())
@@ -267,7 +270,7 @@ public class MapClientStateMachineTest {
    /** Test that default state is STATE_CONNECTING */
    @Test
    public void testDefaultConnectingState() {
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTING, mMceStateMachine.getState());
        Assert.assertEquals(STATE_CONNECTING, mMceStateMachine.getState());
    }

    /**
@@ -279,60 +282,41 @@ public class MapClientStateMachineTest {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_DISCONNECTED);

        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_DISCONNECTED);
        verifyStateTransitionAndIntent(STATE_CONNECTING, STATE_DISCONNECTED);
    }

    /** Test transition from STATE_CONNECTING --> (receive MSG_MAS_CONNECTED) --> STATE_CONNECTED */
    @Test
    public void testStateTransitionFromConnectingToConnected() {
    public void masConnected_whenConnecting_isConnected() {
        setupSdpRecordReceipt();

        int expectedFromState = BluetoothProfile.STATE_CONNECTING;
        int expectedToState = BluetoothProfile.STATE_CONNECTED;
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(expectedFromState, expectedToState);
        verifyStateTransitionAndIntent(STATE_CONNECTING, STATE_CONNECTED);
    }

    /**
     * Test transition from STATE_CONNECTING --> (receive MSG_MAS_CONNECTED) --> STATE_CONNECTED -->
     * (receive MSG_MAS_DISCONNECTED) --> STATE_DISCONNECTING --> STATE_DISCONNECTED
     */
    @Test
    public void testStateTransitionFromConnectedToDisconnected() {
    public void masDisconnected_whenConnected_isDisconnected() {
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        setupSdpRecordReceipt();
        // transition to the connected state
        testStateTransitionFromConnectingToConnected();

        int expectedFromState = BluetoothProfile.STATE_DISCONNECTING;
        int expectedToState = BluetoothProfile.STATE_DISCONNECTED;
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_DISCONNECTED);
        verifyStateTransitionAndIntent(expectedFromState, expectedToState);
        verifyStateTransitionAndIntent(STATE_DISCONNECTING, STATE_DISCONNECTED);
    }

    /** Test receiving an empty event report */
    @Test
    public void testReceiveEmptyEvent() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);

        // broadcast request is sent to change state from STATE_CONNECTING to STATE_CONNECTED
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        // Send an empty notification event, verify the mMceStateMachine is still connected
        sendAndDispatchMessage(MceStateMachine.MSG_NOTIFICATION);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(STATE_CONNECTED);
    }

    /** Test set message status */
    @Test
    public void testSetMessageStatus() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        // broadcast request is sent to change state from STATE_CONNECTING to STATE_CONNECTED
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(STATE_CONNECTED);
        assertThat(mMceStateMachine.setMessageStatus("123456789AB", BluetoothMapClient.READ))
                .isTrue();
    }
@@ -340,48 +324,36 @@ public class MapClientStateMachineTest {
    /** Test MceStateMachine#disconnect */
    @Test
    public void testDisconnect() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        mMceStateMachine.disconnect();
        mLooper.dispatchAll();
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTING);
        verifyStateTransitionAndIntent(STATE_CONNECTED, STATE_DISCONNECTING);

        verify(mMockMasClient).shutdown();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_DISCONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_DISCONNECTING, BluetoothProfile.STATE_DISCONNECTED);
                STATE_DISCONNECTING, STATE_DISCONNECTED);
    }

    /** Test disconnect timeout */
    @Test
    public void testDisconnectTimeout() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        mMceStateMachine.disconnect();
        mLooper.dispatchAll();
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTING);
        verifyStateTransitionAndIntent(STATE_CONNECTED, STATE_DISCONNECTING);

        mLooper.moveTimeForward(MceStateMachine.DISCONNECT_TIMEOUT.toMillis());
        mLooper.dispatchAll();
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_DISCONNECTING, BluetoothProfile.STATE_DISCONNECTED);
        verifyStateTransitionAndIntent(STATE_DISCONNECTING, STATE_DISCONNECTED);
    }

    /** Test sending a message to a phone */
    @Test
    public void testSendSMSMessageToPhone() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        String testMessage = "Hello World!";
        Uri[] contacts = new Uri[] {Uri.parse("tel://5551212")};
@@ -395,10 +367,7 @@ public class MapClientStateMachineTest {
    /** Test sending a message to an email */
    @Test
    public void testSendSMSMessageToEmail() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        String testMessage = "Hello World!";
        Uri[] contacts = new Uri[] {Uri.parse("mailto://sms-test@google.com")};
@@ -412,10 +381,7 @@ public class MapClientStateMachineTest {
    /** Test message sent successfully */
    @Test
    public void testSMSMessageSent() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        when(mMockRequestPushMessage.getMsgHandle()).thenReturn(mTestMessageSmsHandle);
        when(mMockRequestPushMessage.getBMsg()).thenReturn(mTestIncomingSmsBmessage);
@@ -436,7 +402,7 @@ public class MapClientStateMachineTest {
     * MessageListing of INBOX folder not sent
     */
    private void testGetOwnNumber_setup() {
        testStateTransitionFromConnectingToConnected();
        masConnected_whenConnecting_isConnected();
        verify(mMockMasClient, never()).makeRequest(any(RequestSetNotificationRegistration.class));
        verify(mMockMasClient, never()).makeRequest(any(RequestGetMessagesListing.class));
        assertThat(
@@ -564,10 +530,7 @@ public class MapClientStateMachineTest {
    /** Test seen status set for new SMS */
    @Test
    public void testReceivedNewSms_messageStoredAsUnseen() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        String dateTime = new ObexTime(Instant.now()).toString();
        EventReport event =
@@ -596,10 +559,7 @@ public class MapClientStateMachineTest {
    /** Test seen status set for new MMS */
    @Test
    public void testReceivedNewMms_messageStoredAsUnseen() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        String dateTime = new ObexTime(Instant.now()).toString();
        EventReport event =
@@ -630,10 +590,7 @@ public class MapClientStateMachineTest {

    @Test
    public void testReceiveNewMessage_handleNotRecognized_messageDropped() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        // Send new message event with handle A
        String dateTime = new ObexTime(Instant.now()).toString();
@@ -664,9 +621,7 @@ public class MapClientStateMachineTest {
    /** Test seen status set in database on initial download */
    @Test
    public void testDownloadExistingSms_messageStoredAsSeen() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        com.android.bluetooth.mapclient.Message testMessageListingSms =
                createNewMessage("SMS_GSM", mTestMessageSmsHandle);
@@ -692,9 +647,7 @@ public class MapClientStateMachineTest {
    /** Test seen status set in database on initial download */
    @Test
    public void testDownloadExistingMms_messageStoredAsSeen() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        com.android.bluetooth.mapclient.Message testMessageListingMms =
                createNewMessage("MMS", mTestMessageMmsHandle);
@@ -723,9 +676,7 @@ public class MapClientStateMachineTest {
    /** Test receiving a new message notification. */
    @Test
    public void testReceiveNewMessageNotification() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        // Receive a new message notification.
        String dateTime = new ObexTime(Instant.now()).toString();
@@ -756,11 +707,10 @@ public class MapClientStateMachineTest {
     */
    @Test
    public void testMsgGetMessageListing_unsupportedMessageTypesNotRequested() {
        setupSdpRecordReceipt();
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        clearInvocations(mMockMasClient);
        byte expectedFilter = MessagesFilter.MESSAGE_TYPE_EMAIL | MessagesFilter.MESSAGE_TYPE_IM;
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);

        sendAndDispatchMessage(
                MceStateMachine.MSG_GET_MESSAGE_LISTING, MceStateMachine.FOLDER_INBOX);
@@ -788,9 +738,7 @@ public class MapClientStateMachineTest {

    @Test
    public void testReceivedNewMmsNoSMSDefaultPackage_broadcastToSMSReplyPackage() {
        setupSdpRecordReceipt();
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        masConnected_whenConnecting_isConnected(); // transition to the connected state

        String dateTime = new ObexTime(Instant.now()).toString();
        EventReport event =
@@ -814,7 +762,7 @@ public class MapClientStateMachineTest {

    @Test
    public void testSdpBusyWhileConnecting_sdpRetried() {
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_CONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_CONNECTING);

        // Send SDP Failed with status "busy"
        // Note: There's no way to validate the BluetoothDevice#sdpSearch call
@@ -824,12 +772,12 @@ public class MapClientStateMachineTest {
        SdpMasRecord record = new SdpMasRecord(1, 1, 1, 1, 1, 1, "MasRecord");
        mMceStateMachine.sendSdpResult(MceStateMachine.SDP_SUCCESS, record);
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);
        verifyStateTransitionAndIntent(STATE_CONNECTING, STATE_CONNECTED);
    }

    @Test
    public void testSdpBusyWhileConnectingAndRetryResultsReceivedAfterTimeout_resultsIgnored() {
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_CONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_CONNECTING);

        // Send SDP Failed with status "busy"
        // Note: There's no way to validate the BluetoothDevice#sdpSearch call
@@ -839,10 +787,7 @@ public class MapClientStateMachineTest {
        mLooper.moveTimeForward(MceStateMachine.CONNECT_TIMEOUT.toMillis());
        mLooper.dispatchAll();

        // Verify we move into the disconnecting state
        verifyStateTransitionAndIntent(
                BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_DISCONNECTING);
        assertThat(mMceStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_DISCONNECTING);
        verifyStateTransitionAndIntent(STATE_CONNECTING, STATE_DISCONNECTING);

        // Send successful SDP record, then send MAS Client connected
        SdpMasRecord record = new SdpMasRecord(1, 1, 1, 1, 1, 1, "MasRecord");
@@ -854,23 +799,23 @@ public class MapClientStateMachineTest {

    @Test
    public void testSdpFailedWithNoRecordWhileConnecting_deviceDisconnecting() {
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_CONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_CONNECTING);

        // Send SDP process success with no record found
        mMceStateMachine.sendSdpResult(MceStateMachine.SDP_SUCCESS, null);

        // Verify we move into the disconnecting state
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_DISCONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_DISCONNECTING);
    }

    @Test
    public void testSdpOrganicFailure_deviceDisconnecting() {
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_CONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_CONNECTING);

        // Send SDP Failed entirely
        mMceStateMachine.sendSdpResult(MceStateMachine.SDP_FAILED, null);

        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_DISCONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_DISCONNECTING);
    }

    /**
@@ -968,10 +913,8 @@ public class MapClientStateMachineTest {
     *     'Success'/'Failure'.
     */
    private void testSendMapMessagePendingIntents_base(String action, EventReport.Type type) {
        int expectedFromState = BluetoothProfile.STATE_CONNECTING;
        int expectedToState = BluetoothProfile.STATE_CONNECTED;
        sendAndDispatchMessage(MceStateMachine.MSG_MAS_CONNECTED);
        verifyStateTransitionAndIntent(expectedFromState, expectedToState);
        verifyStateTransitionAndIntent(STATE_CONNECTING, STATE_CONNECTED);

        PendingIntent pendingIntentSent;
        PendingIntent pendingIntentDelivered;
@@ -1031,7 +974,7 @@ public class MapClientStateMachineTest {
    }

    private void setupSdpRecordReceipt() {
        assertCurrentStateAfterScheduledTask(BluetoothProfile.STATE_CONNECTING);
        assertCurrentStateAfterScheduledTask(STATE_CONNECTING);

        // Setup receipt of SDP record
        SdpMasRecord record = new SdpMasRecord(1, 1, 1, 1, 1, 1, "MasRecord");