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

Commit 08cab972 authored by Chienyuan's avatar Chienyuan Committed by android-build-merger
Browse files

Merge "Simplify logic for getConnectionStatus in HidDeviceProfile" am: b545c70d

am: f374663b

Change-Id: I977d3acb03ab828f66657afab3ba36a96de88893
parents f9c56d0a f374663b
Loading
Loading
Loading
Loading
+6 −16
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import java.util.Collection;
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";
@@ -38,7 +38,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;
@@ -63,9 +62,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();
@@ -85,9 +82,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;
        }
    }
@@ -114,6 +109,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;
    }

@@ -130,11 +126,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
@@ -189,9 +181,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);
    }
}