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

Commit a874dc1d authored by Chienyuan's avatar Chienyuan
Browse files

Simplify logic for connect in PbapClientProfile

* connect: remove connected device check logic. PbapClientService
  will check it.
* remove member variable V and related checks.
* wrap if/else statement in curly brackets.
* add robotest for PbapClientProfile.

Bug: 111812003
Test: make -j42 RunSettingsLibRoboTests ROBOTEST_FILTER=PbapClientProfileTest
Change-Id: Ide43ee5cec560945ad69639782ad6f214ea0cece
(cherry picked from commit 12ed2468d290d5c861e5d1d88a763630dabe97da)
parent bf383ccf
Loading
Loading
Loading
Loading
+5 −27
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import java.util.List;

public final class PbapClientProfile implements LocalBluetoothProfile {
    private static final String TAG = "PbapClientProfile";
    private static boolean V = false;

    private BluetoothPbapClient mService;
    private boolean mIsProfileReady;
@@ -56,9 +55,7 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
            implements BluetoothProfile.ServiceListener {

        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (V) {
                Log.d(TAG,"Bluetooth service connected");
            }
            Log.d(TAG, "Bluetooth service connected, profile:" + profile);
            mService = (BluetoothPbapClient) proxy;
            // We just bound to the service, so refresh the UI for any connected PBAP devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
@@ -77,9 +74,7 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
        }

        public void onServiceDisconnected(int profile) {
            if (V) {
                Log.d(TAG,"Bluetooth service disconnected");
            }
            Log.d(TAG, "Bluetooth service disconnected, profile:" + profile);
            mIsProfileReady = false;
        }
    }
@@ -131,31 +126,16 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
    }

    public boolean connect(BluetoothDevice device) {
        if (V) {
        Log.d(TAG,"PBAPClientProfile got connect request");
        }
        if (mService == null) {
            return false;
        }
        List<BluetoothDevice> srcs = getConnectedDevices();
        if (srcs != null) {
            for (BluetoothDevice src : srcs) {
                if (src.equals(device)) {
                    // Connect to same device, Ignore it
                    Log.d(TAG,"Ignoring Connect");
                    return true;
                }
            }
        }
        Log.d(TAG,"PBAPClientProfile attempting to connect to " + device.getAddress());

        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (V) {
        Log.d(TAG,"PBAPClientProfile got disconnect request");
        }
        if (mService == null) {
            return false;
        }
@@ -218,9 +198,7 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
    }

    protected void finalize() {
        if (V) {
        Log.d(TAG, "finalize()");
        }
        if (mService != null) {
            try {
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.settingslib.bluetooth;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothPbapClient;
import android.bluetooth.BluetoothProfile;

import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadow.api.Shadow;

@RunWith(SettingsLibRobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class PbapClientProfileTest {

    @Mock
    private CachedBluetoothDeviceManager mDeviceManager;
    @Mock
    private LocalBluetoothProfileManager mProfileManager;
    @Mock
    private BluetoothPbapClient mService;
    @Mock
    private CachedBluetoothDevice mCachedBluetoothDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    private BluetoothProfile.ServiceListener mServiceListener;
    private PbapClientProfile mProfile;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mProfile = new PbapClientProfile(RuntimeEnvironment.application,
                mDeviceManager, mProfileManager);
        mServiceListener = mShadowBluetoothAdapter.getServiceListener();
        mServiceListener.onServiceConnected(BluetoothProfile.PBAP_CLIENT, mService);
    }

    @Test
    public void connect_shouldConnectBluetoothPbapClient() {
        mProfile.connect(mBluetoothDevice);
        verify(mService).connect(mBluetoothDevice);
    }

    @Test
    public void disconnect_shouldDisconnectBluetoothPbapClient() {
        mProfile.disconnect(mBluetoothDevice);
        verify(mService).disconnect(mBluetoothDevice);
    }

    @Test
    public void getConnectionStatus_shouldReturnConnectionState() {
        when(mService.getConnectionState(mBluetoothDevice)).
                thenReturn(BluetoothProfile.STATE_CONNECTED);
        assertThat(mProfile.getConnectionStatus(mBluetoothDevice)).
                isEqualTo(BluetoothProfile.STATE_CONNECTED);
    }
}
 No newline at end of file