[Pair hearing devices] Add pair hearing device functionality

* Add setFilter(List<ScanFilter>) in DeviceListPreferenceFragment to enable BluetoothLeScanner

Bug: 237625815
Test: make RunSettingsRoboTests ROBOTEST_FILTER=DeviceListPreferenceFragmentTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=HearingDevicePairingDetailTest
Change-Id: I13495cad7260789845fad9a7e77e96b692a5cbd0
This commit is contained in:
jasonwshsu
2023-02-07 17:29:43 +08:00
parent 723c385c18
commit 031c5f0354
8 changed files with 363 additions and 50 deletions

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2023 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.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.bluetooth.BluetoothProgressCategory;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
/** Tests for {@link HearingDevicePairingDetail}. */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class HearingDevicePairingDetailTest {
@Rule
public final MockitoRule mockito = MockitoJUnit.rule();
private final Context mContext = ApplicationProvider.getApplicationContext();
@Mock
private CachedBluetoothDevice mCachedBluetoothDevice;
private BluetoothProgressCategory mProgressCategory;
private TestHearingDevicePairingDetail mFragment;
@Before
public void setUp() {
final BluetoothAdapter bluetoothAdapter = spy(BluetoothAdapter.getDefaultAdapter());
final ShadowBluetoothAdapter shadowBluetoothAdapter = Shadow.extract(
BluetoothAdapter.getDefaultAdapter());
shadowBluetoothAdapter.setEnabled(true);
mProgressCategory = spy(new BluetoothProgressCategory(mContext));
mFragment = spy(new TestHearingDevicePairingDetail());
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.findPreference(
HearingDevicePairingDetail.KEY_AVAILABLE_HEARING_DEVICES)).thenReturn(
mProgressCategory);
mFragment.setBluetoothAdapter(bluetoothAdapter);
}
@Test
public void getDeviceListKey_expectedKey() {
assertThat(mFragment.getDeviceListKey()).isEqualTo(
HearingDevicePairingDetail.KEY_AVAILABLE_HEARING_DEVICES);
}
@Test
public void onDeviceBondStateChanged_bondNone_setProgressFalse() {
mFragment.initPreferencesFromPreferenceScreen();
mFragment.onDeviceBondStateChanged(mCachedBluetoothDevice, BluetoothDevice.BOND_NONE);
verify(mProgressCategory).setProgress(true);
}
@Test
public void onDeviceBondStateChanged_bonding_setProgressTrue() {
mFragment.initPreferencesFromPreferenceScreen();
mFragment.onDeviceBondStateChanged(mCachedBluetoothDevice, BluetoothDevice.BOND_BONDING);
verify(mProgressCategory).setProgress(false);
}
private static class TestHearingDevicePairingDetail extends HearingDevicePairingDetail {
TestHearingDevicePairingDetail() {
super();
}
public void setBluetoothAdapter(BluetoothAdapter bluetoothAdapter) {
this.mBluetoothAdapter = bluetoothAdapter;
}
public void enableScanning() {
super.enableScanning();
}
}
}

View File

@@ -26,6 +26,11 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.res.Resources;
@@ -45,6 +50,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.Collections;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@@ -57,10 +63,14 @@ public class DeviceListPreferenceFragmentTest {
private Resources mResource;
@Mock
private Context mContext;
@Mock
private BluetoothLeScanner mBluetoothLeScanner;
private TestFragment mFragment;
private Preference mMyDevicePreference;
private BluetoothAdapter mBluetoothAdapter;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -68,7 +78,8 @@ public class DeviceListPreferenceFragmentTest {
mFragment = spy(new TestFragment());
doReturn(mContext).when(mFragment).getContext();
doReturn(mResource).when(mFragment).getResources();
mFragment.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter = spy(BluetoothAdapter.getDefaultAdapter());
mFragment.mBluetoothAdapter = mBluetoothAdapter;
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
}
@@ -169,6 +180,20 @@ public class DeviceListPreferenceFragmentTest {
verify(mFragment, times(1)).startScanning();
}
@Test
public void startScanning_setLeScanFilter_shouldStartLeScan() {
final ScanFilter leScanFilter = new ScanFilter.Builder()
.setServiceData(BluetoothUuid.HEARING_AID, new byte[]{0}, new byte[]{0})
.build();
doReturn(mBluetoothLeScanner).when(mBluetoothAdapter).getBluetoothLeScanner();
mFragment.setFilter(Collections.singletonList(leScanFilter));
mFragment.startScanning();
verify(mBluetoothLeScanner).startScan(eq(Collections.singletonList(leScanFilter)),
any(ScanSettings.class), any(ScanCallback.class));
}
/**
* Fragment to test since {@code DeviceListPreferenceFragment} is abstract
*/
@@ -187,7 +212,7 @@ public class DeviceListPreferenceFragmentTest {
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {}
@Override
void initPreferencesFromPreferenceScreen() {}
protected void initPreferencesFromPreferenceScreen() {}
@Override
public String getDeviceListKey() {