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

Commit 0fcb21bb authored by Aditi Katragadda's avatar Aditi Katragadda Committed by Automerger Merge Worker
Browse files

Merge "State transition for PbapClientStateMachine during Disconnect Timeout" am: 6cd7123e

parents 286ba435 6cd7123e
Loading
Loading
Loading
Loading
+16 −2
Original line number Original line Diff line number Diff line
@@ -63,6 +63,7 @@ import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.Utils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IState;
import com.android.internal.util.IState;
import com.android.internal.util.State;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.internal.util.StateMachine;
@@ -71,8 +72,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.List;


class PbapClientStateMachine extends StateMachine {
class PbapClientStateMachine extends StateMachine {
    private static final boolean DBG = false; //Utils.DBG;
    private static final String TAG = "PbapClientStateMachine";
    private static final String TAG = "PbapClientStateMachine";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);


    // Messages for handling connect/disconnect requests.
    // Messages for handling connect/disconnect requests.
    private static final int MSG_DISCONNECT = 2;
    private static final int MSG_DISCONNECT = 2;
@@ -108,10 +109,17 @@ class PbapClientStateMachine extends StateMachine {
    private int mMostRecentState = BluetoothProfile.STATE_DISCONNECTED;
    private int mMostRecentState = BluetoothProfile.STATE_DISCONNECTED;


    PbapClientStateMachine(PbapClientService svc, BluetoothDevice device) {
    PbapClientStateMachine(PbapClientService svc, BluetoothDevice device) {
        this(svc, device, null);
    }

    @VisibleForTesting
    PbapClientStateMachine(PbapClientService svc, BluetoothDevice device,
            PbapClientConnectionHandler connectionHandler) {
        super(TAG);
        super(TAG);


        mService = svc;
        mService = svc;
        mCurrentDevice = device;
        mCurrentDevice = device;
        mConnectionHandler = connectionHandler;
        mLock = new Object();
        mLock = new Object();
        mUserManager = mService.getSystemService(UserManager.class);
        mUserManager = mService.getSystemService(UserManager.class);
        mDisconnected = new Disconnected();
        mDisconnected = new Disconnected();
@@ -158,12 +166,16 @@ class PbapClientStateMachine extends StateMachine {
            mHandlerThread =
            mHandlerThread =
                    new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND);
                    new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND);
            mHandlerThread.start();
            mHandlerThread.start();

            // Keeps mock handler from being overwritten in tests
            if (mConnectionHandler == null) {
                mConnectionHandler =
                mConnectionHandler =
                    new PbapClientConnectionHandler.Builder().setLooper(mHandlerThread.getLooper())
                    new PbapClientConnectionHandler.Builder().setLooper(mHandlerThread.getLooper())
                            .setContext(mService)
                            .setContext(mService)
                            .setClientSM(PbapClientStateMachine.this)
                            .setClientSM(PbapClientStateMachine.this)
                            .setRemoteDevice(mCurrentDevice)
                            .setRemoteDevice(mCurrentDevice)
                            .build();
                            .build();
            }


            sendMessageDelayed(MSG_CONNECT_TIMEOUT, CONNECT_TIMEOUT);
            sendMessageDelayed(MSG_CONNECT_TIMEOUT, CONNECT_TIMEOUT);
        }
        }
@@ -280,6 +292,8 @@ class PbapClientStateMachine extends StateMachine {
                case MSG_DISCONNECT_TIMEOUT:
                case MSG_DISCONNECT_TIMEOUT:
                    Log.w(TAG, "Disconnect Timeout, Forcing");
                    Log.w(TAG, "Disconnect Timeout, Forcing");
                    mConnectionHandler.abort();
                    mConnectionHandler.abort();
                    mHandlerThread.quitSafely();
                    transitionTo(mDisconnected);
                    break;
                    break;


                case MSG_RESUME_DOWNLOAD:
                case MSG_RESUME_DOWNLOAD:
+147 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.bluetooth.pbapclient;

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

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.util.Log;

import android.app.BroadcastOptions;
import android.content.Context;
import android.content.Intent;
import android.os.UserManager;
import android.os.Message;

import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class PbapClientStateMachineTest{
    private static final String TAG = "PbapClientStateMachineTest";

    private PbapClientStateMachine mPbapClientStateMachine = null;

    @Mock
    private PbapClientService mMockPbapClientService;
    @Mock
    private UserManager mMockUserManager;
    @Mock
    private PbapClientConnectionHandler mMockHandler;

    private BluetoothDevice mTestDevice;
    private BluetoothAdapter mAdapter;

    private ArgumentCaptor<Intent> mIntentArgument = ArgumentCaptor.forClass(Intent.class);


    static final int DISCONNECT_TIMEOUT = 3100;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        // This line must be called to make sure relevant objects are initialized properly
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        // Get a device for testing
        mTestDevice = mAdapter.getRemoteDevice("00:01:02:03:04:05");
        when(mMockPbapClientService.getSystemServiceName(UserManager.class))
                .thenReturn(Context.USER_SERVICE);
        when(mMockPbapClientService.getSystemService(UserManager.class))
                .thenReturn(mMockUserManager);
        mPbapClientStateMachine = new PbapClientStateMachine(mMockPbapClientService, mTestDevice,
                mMockHandler);
        mPbapClientStateMachine.start();
    }

    @After
    public void tearDown() throws Exception {
        if (mPbapClientStateMachine != null) {
            mPbapClientStateMachine.doQuit();
        }
    }

    /**
     * Test that default state is STATE_CONNECTING
     */
    @Test
    public void testDefaultConnectingState() {
        Log.i(TAG, "in testDefaultConnectingState");
        // it appears that enter and exit can overlap sometimes when calling doQuit()
        // currently solved by waiting for looper to finish task
        TestUtils.waitForLooperToFinishScheduledTask(mPbapClientStateMachine.getHandler()
                .getLooper());
        assertThat(mPbapClientStateMachine.getConnectionState())
                .isEqualTo(BluetoothProfile.STATE_CONNECTING);
    }

    /**
     * Test transition from STATE_CONNECTING to STATE_DISCONNECTING with MSG_DISCONNECT
     */
    @Test
    public void testStateTransitionFromConnectingToDisconnecting() {
        assertThat(mPbapClientStateMachine.getConnectionState())
                .isEqualTo(BluetoothProfile.STATE_CONNECTING);

        mPbapClientStateMachine.disconnect(mTestDevice);

        TestUtils.waitForLooperToFinishScheduledTask(mPbapClientStateMachine.getHandler()
                .getLooper());
        assertThat(mPbapClientStateMachine.getConnectionState())
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTING);
    }

    /**
     * Test transition from STATE_DISCONNECTING to STATE_DISCONNECTED with MSG_DISCONNECT_TIMEOUT
     */
    @Test
    public void testStateTransitionFromDisconnectingToDisconnected_Timeout() {
        testStateTransitionFromConnectingToDisconnecting();

        //wait until timeout occurs
        verify(mMockPbapClientService,
                timeout(DISCONNECT_TIMEOUT).times(3)).sendBroadcastMultiplePermissions(
                mIntentArgument.capture(), any(String[].class),
                any(BroadcastOptions.class));
        assertThat(mPbapClientStateMachine.getConnectionState())
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
    }
}
 No newline at end of file