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

Commit 8fb80824 authored by William Escande's avatar William Escande
Browse files

MapClient: Inject looper for test

Also delete the constructor that doesn't take a looper in parameter as
this is discouraged

Test: atest BluetoothInstrumentationTests
Flag: TEST_ONLY
Bug: 386715308
Change-Id: I3b5e9929d36daa1dae2d2cae7e221da63dd8dca3
parent 9deb242c
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -172,8 +172,11 @@ public class MapClientService extends ProfileService {
        // When creating a new statemachine, its state is set to CONNECTING - which will trigger
        // connect.
        MceStateMachine mapStateMachine;
        if (mSmLooper != null) mapStateMachine = new MceStateMachine(this, device, mSmLooper);
        else mapStateMachine = new MceStateMachine(this, device);
        if (mSmLooper != null) {
            mapStateMachine = new MceStateMachine(this, device, mSmLooper);
        } else {
            mapStateMachine = new MceStateMachine(this, device);
        }
        mMapInstanceMap.put(device, mapStateMachine);
    }

+31 −35
Original line number Diff line number Diff line
@@ -14,18 +14,28 @@
 * limitations under the License.
 */

/**
 * Bluetooth MAP MCE StateMachine (Disconnected) | ^ CONNECT | | DISCONNECTED V | (Connecting)
 * (Disconnecting) | ^ CONNECTED | | DISCONNECT V | (Connected)
 *
 * <p>Valid Transitions: State + Event -> Transition:
 *
 * <p>Disconnected + CONNECT -> Connecting Connecting + CONNECTED -> Connected Connecting + TIMEOUT
 * -> Disconnecting Connecting + DISCONNECT/CONNECT -> Defer Message Connected + DISCONNECT ->
 * Disconnecting Connected + CONNECT -> Disconnecting + Defer Message Disconnecting + DISCONNECTED
 * -> (Safe) Disconnected Disconnecting + TIMEOUT -> (Force) Disconnected Disconnecting +
 * DISCONNECT/CONNECT : Defer Message
 */
// Bluetooth MAP MCE StateMachine
//         (Disconnected)
//             |    ^
//     CONNECT |    | DISCONNECTED
//             V    |
//    (Connecting) (Disconnecting)
//             |    ^
//   CONNECTED |    | DISCONNECT
//             V    |
//           (Connected)

// Valid Transitions: State + Event -> Transition:

// Disconnected + CONNECT -> Connecting
// Connecting + CONNECTED -> Connected
// Connecting + TIMEOUT -> Disconnecting
// Connecting + DISCONNECT/CONNECT -> Defer Message
// Connected + DISCONNECT -> Disconnecting
// Connected + CONNECT -> Disconnecting + Defer Message
// Disconnecting + DISCONNECTED -> (Safe) Disconnected
// Disconnecting + TIMEOUT -> (Force) Disconnected
// Disconnecting + DISCONNECT/CONNECT : Defer Message
package com.android.bluetooth.mapclient;

import static android.Manifest.permission.BLUETOOTH_CONNECT;
@@ -64,6 +74,7 @@ import com.android.vcard.VCardConstants;
import com.android.vcard.VCardEntry;
import com.android.vcard.VCardProperty;

import java.time.Duration;
import java.time.Instant;
import java.util.Calendar;
import java.util.HashMap;
@@ -100,8 +111,8 @@ class MceStateMachine extends StateMachine {
    // Bluetooth, to work with the default Car Messenger.  This may need to be set to false if the
    // messaging app takes that responsibility.
    private static final Boolean SAVE_OUTBOUND_MESSAGES = true;
    private static final int DISCONNECT_TIMEOUT = 3000;
    private static final int CONNECT_TIMEOUT = 10000;
    @VisibleForTesting static final Duration DISCONNECT_TIMEOUT = Duration.ofSeconds(3);
    private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10);
    private static final int MAX_MESSAGES = 20;
    private static final int MSG_CONNECT = 1;
    private static final int MSG_DISCONNECT = 2;
@@ -215,27 +226,16 @@ class MceStateMachine extends StateMachine {
            new ConcurrentHashMap<String, MessageMetadata>();

    MceStateMachine(MapClientService service, BluetoothDevice device) {
        this(service, device, null, null);
        super(TAG); // Create a state machine with its own separate thread
        mService = service;
        mDevice = device;
        initStateMachine();
    }

    MceStateMachine(MapClientService service, BluetoothDevice device, Looper looper) {
        this(service, device, null, null, looper);
    }

    @VisibleForTesting
    MceStateMachine(
            MapClientService service,
            BluetoothDevice device,
            MasClient masClient,
            MapClientContent database) {
        super(TAG);
        mService = service;
        mMasClient = masClient;
        mDevice = device;
        mDatabase = database;
        initStateMachine();
    }

    @VisibleForTesting
    MceStateMachine(
            MapClientService service,
@@ -552,7 +552,7 @@ class MceStateMachine extends StateMachine {

            // When commanded to connect begin SDP to find the MAS server.
            mDevice.sdpSearch(BluetoothUuid.MAS);
            sendMessageDelayed(MSG_CONNECTING_TIMEOUT, CONNECT_TIMEOUT);
            sendMessageDelayed(MSG_CONNECTING_TIMEOUT, CONNECT_TIMEOUT.toMillis());
            Log.i(TAG, Utils.getLoggableAddress(mDevice) + " [Connecting]: Await SDP results");
        }

@@ -1270,7 +1270,7 @@ class MceStateMachine extends StateMachine {
            if (mMasClient != null) {
                mMasClient.makeRequest(new RequestSetNotificationRegistration(false));
                mMasClient.shutdown();
                sendMessageDelayed(MSG_DISCONNECTING_TIMEOUT, DISCONNECT_TIMEOUT);
                sendMessageDelayed(MSG_DISCONNECTING_TIMEOUT, DISCONNECT_TIMEOUT.toMillis());
            } else {
                // MAP was never connected
                transitionTo(mDisconnected);
@@ -1345,10 +1345,6 @@ class MceStateMachine extends StateMachine {
                return "MSG_GET_MESSAGE_LISTING";
            case MSG_SET_MESSAGE_STATUS:
                return "MSG_SET_MESSAGE_STATUS";
            case DISCONNECT_TIMEOUT:
                return "DISCONNECT_TIMEOUT";
            case CONNECT_TIMEOUT:
                return "CONNECT_TIMEOUT";
            case MSG_CONNECT:
                return "MSG_CONNECT";
            case MSG_DISCONNECT:
+142 −277

File changed.

Preview size limit exceeded, changes collapsed.