Merge "Simplify logic for connect in PbapClientProfile"

This commit is contained in:
Treehugger Robot
2018-09-19 04:24:19 +00:00
committed by Gerrit Code Review
2 changed files with 98 additions and 27 deletions

View File

@@ -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;
@@ -57,9 +56,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();
@@ -78,9 +75,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;
}
}
@@ -134,31 +129,16 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
}
public boolean connect(BluetoothDevice device) {
if (V) {
Log.d(TAG,"PBAPClientProfile got connect request");
}
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");
}
Log.d(TAG,"PBAPClientProfile got disconnect request");
if (mService == null) {
return false;
}
@@ -221,9 +201,7 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
}
protected void finalize() {
if (V) {
Log.d(TAG, "finalize()");
}
Log.d(TAG, "finalize()");
if (mService != null) {
try {
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(

View File

@@ -0,0 +1,93 @@
/*
* 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.BluetoothDevice;
import android.bluetooth.BluetoothPbapClient;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsLibRobolectricTestRunner.class)
public class PbapClientProfileTest {
@Mock
private LocalBluetoothAdapter mAdapter;
@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;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doAnswer((invocation) -> {
mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1];
return null;
}).when(mAdapter).getProfileProxy(any(Context.class),
any(BluetoothProfile.ServiceListener.class), eq(BluetoothProfile.PBAP_CLIENT));
mProfile = new PbapClientProfile(RuntimeEnvironment.application, mAdapter,
mDeviceManager, mProfileManager);
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);
}
}