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

Commit 75b9c5f3 authored by Chienyuan's avatar Chienyuan
Browse files

Simplify logic for getConnectionStatus in HidDeviceProfile

* getConnectionStatus: remove connected device check logic.
  HidDeviceService will check it.
* rewrite the annotation about this class
* remove member variable DEBUG and related checks.
* add robotest for HidDeviceProfile

Bug: 111812003
Test: make -j42 RunSettingsLibRoboTests ROBOTEST_FILTER=HidDeviceProfileTest
Change-Id: I2e7fdd69a46707339a32db4304666e50fd0cf954
(cherry picked from commit 89b1e5c013d250d846832634be34d8696d7160e9)
parent edc46f42
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 CachedBluetoothDeviceManager mDeviceManager;
    private final LocalBluetoothProfileManager mProfileManager;
@@ -59,9 +58,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();
@@ -81,9 +78,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;
        }
    }
@@ -110,6 +105,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;
    }

@@ -126,11 +122,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
@@ -185,9 +177,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,
+90 −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.BluetoothHidDevice;
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 HidDeviceProfileTest {

    @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;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mProfile = new HidDeviceProfile(RuntimeEnvironment.application,
                mDeviceManager, mProfileManager);
        mServiceListener = mShadowBluetoothAdapter.getServiceListener();
        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);
    }
}