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

Commit b545c70d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Simplify logic for getConnectionStatus in HidDeviceProfile"

parents 0ac2a840 932e63b3
Loading
Loading
Loading
Loading
+6 −16
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import com.android.settingslib.R;
import java.util.List;

/**
 * HidProfile handles Bluetooth HID profile.
 * HidDeviceProfile handles Bluetooth HID Device role
 */
public class HidDeviceProfile implements LocalBluetoothProfile {
    private static final String TAG = "HidDeviceProfile";
@@ -37,7 +37,6 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
    private static final int ORDINAL = 18;
    // HID Device Profile is always preferred.
    private static final int PREFERRED_VALUE = -1;
    private static final boolean DEBUG = true;

    private final LocalBluetoothAdapter mLocalAdapter;
    private final CachedBluetoothDeviceManager mDeviceManager;
@@ -62,9 +61,7 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
            implements BluetoothProfile.ServiceListener {

        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (DEBUG) {
                Log.d(TAG,"Bluetooth service connected :-)");
            }
            Log.d(TAG, "Bluetooth service connected :-), profile:" + profile);
            mService = (BluetoothHidDevice) proxy;
            // We just bound to the service, so refresh the UI for any connected HID devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
@@ -84,9 +81,7 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
        }

        public void onServiceDisconnected(int profile) {
            if (DEBUG) {
                Log.d(TAG, "Bluetooth service disconnected");
            }
            Log.d(TAG, "Bluetooth service disconnected, profile:" + profile);
            mIsProfileReady = false;
        }
    }
@@ -113,6 +108,7 @@ public class HidDeviceProfile implements LocalBluetoothProfile {

    @Override
    public boolean connect(BluetoothDevice device) {
        // Don't invoke method in service because settings is not allowed to connect this profile.
        return false;
    }

@@ -129,11 +125,7 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
        }
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();

        return !deviceList.isEmpty() && deviceList.contains(device)
                ? mService.getConnectionState(device)
                : BluetoothProfile.STATE_DISCONNECTED;
        return mService.getConnectionState(device);
    }

    @Override
@@ -188,9 +180,7 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
    }

    protected void finalize() {
        if (DEBUG) {
        Log.d(TAG, "finalize()");
        }
        if (mService != null) {
            try {
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HID_DEVICE,
+92 −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.BluetoothDevice;
import android.bluetooth.BluetoothHidDevice;
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 HidDeviceProfileTest {

    @Mock
    private LocalBluetoothAdapter mAdapter;
    @Mock
    private CachedBluetoothDeviceManager mDeviceManager;
    @Mock
    private LocalBluetoothProfileManager mProfileManager;
    @Mock
    private BluetoothHidDevice mService;
    @Mock
    private CachedBluetoothDevice mCachedBluetoothDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    private BluetoothProfile.ServiceListener mServiceListener;
    private HidDeviceProfile 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.HID_DEVICE));

        mProfile = new HidDeviceProfile(RuntimeEnvironment.application, mAdapter,
                mDeviceManager, mProfileManager);
        mServiceListener.onServiceConnected(BluetoothProfile.HID_DEVICE, mService);
    }

    @Test
    public void connect_shouldReturnFalse() {
        assertThat(mProfile.connect(mBluetoothDevice)).isFalse();
    }

    @Test
    public void disconnect_shouldDisconnectBluetoothHidDevice() {
        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);
    }
}