• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.settings.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.Activity;
29 import android.bluetooth.BluetoothAdapter;
30 import android.bluetooth.BluetoothDevice;
31 import android.bluetooth.BluetoothHearingAid;
32 import android.bluetooth.BluetoothManager;
33 import android.bluetooth.BluetoothProfile;
34 import android.content.BroadcastReceiver;
35 import android.content.Intent;
36 
37 import androidx.preference.Preference;
38 
39 import com.android.settings.R;
40 import com.android.settings.bluetooth.Utils;
41 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
42 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
43 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
44 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
45 import com.android.settingslib.bluetooth.HearingAidProfile;
46 import com.android.settingslib.bluetooth.LocalBluetoothManager;
47 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
48 
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mock;
54 import org.mockito.MockitoAnnotations;
55 import org.robolectric.Robolectric;
56 import org.robolectric.RobolectricTestRunner;
57 import org.robolectric.annotation.Config;
58 import org.robolectric.shadow.api.Shadow;
59 import org.robolectric.shadows.ShadowApplication;
60 
61 import java.util.ArrayList;
62 import java.util.List;
63 
64 @RunWith(RobolectricTestRunner.class)
65 @Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
66 public class AccessibilityHearingAidPreferenceControllerTest {
67     private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
68     private static final String TEST_DEVICE_NAME = "TEST_HEARING_AID_BT_DEVICE_NAME";
69     private static final String HEARING_AID_PREFERENCE = "hearing_aid_preference";
70 
71     private BluetoothAdapter mBluetoothAdapter;
72     private ShadowBluetoothAdapter mShadowBluetoothAdapter;
73     private BluetoothManager mBluetoothManager;
74     private BluetoothDevice mBluetoothDevice;
75     private Activity mContext;
76     private Preference mHearingAidPreference;
77     private AccessibilityHearingAidPreferenceController mPreferenceController;
78     private ShadowApplication mShadowApplication;
79 
80     @Mock
81     private CachedBluetoothDevice mCachedBluetoothDevice;
82     @Mock
83     private CachedBluetoothDeviceManager mCachedDeviceManager;
84     @Mock
85     private LocalBluetoothManager mLocalBluetoothManager;
86     @Mock
87     private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
88     @Mock
89     private HearingAidProfile mHearingAidProfile;
90 
91     @Before
setUp()92     public void setUp() {
93         MockitoAnnotations.initMocks(this);
94         mShadowApplication = ShadowApplication.getInstance();
95         mContext = spy(Robolectric.setupActivity(Activity.class));
96         setupBluetoothEnvironment();
97         setupHearingAidEnvironment();
98         mHearingAidPreference = new Preference(mContext);
99         mHearingAidPreference.setKey(HEARING_AID_PREFERENCE);
100         mPreferenceController = new AccessibilityHearingAidPreferenceController(mContext,
101                 HEARING_AID_PREFERENCE);
102         mPreferenceController.setPreference(mHearingAidPreference);
103         mHearingAidPreference.setSummary("");
104     }
105 
106     @After
tearDown()107     public void tearDown() {
108         ShadowBluetoothUtils.reset();
109     }
110 
111     @Test
onHearingAidStateChanged_connected_updateHearingAidSummary()112     public void onHearingAidStateChanged_connected_updateHearingAidSummary() {
113         when(mHearingAidProfile.getConnectedDevices()).thenReturn(generateHearingAidDeviceList());
114         mPreferenceController.onStart();
115         Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
116         intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED);
117         sendIntent(intent);
118 
119         assertThat(mHearingAidPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME);
120     }
121 
122     @Test
onHearingAidStateChanged_disconnected_updateHearingAidSummary()123     public void onHearingAidStateChanged_disconnected_updateHearingAidSummary() {
124         mPreferenceController.onStart();
125         Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
126         intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_DISCONNECTED);
127         sendIntent(intent);
128 
129         assertThat(mHearingAidPreference.getSummary()).isEqualTo(
130                 mContext.getText(R.string.accessibility_hearingaid_not_connected_summary));
131     }
132 
133     @Test
onBluetoothStateChanged_bluetoothOff_updateHearingAidSummary()134     public void onBluetoothStateChanged_bluetoothOff_updateHearingAidSummary() {
135         mPreferenceController.onStart();
136         Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
137         intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
138         sendIntent(intent);
139 
140         assertThat(mHearingAidPreference.getSummary()).isEqualTo(
141                 mContext.getText(R.string.accessibility_hearingaid_not_connected_summary));
142     }
143 
144     @Test
handleHearingAidPreferenceClick_noHearingAid_launchHearingAidInstructionDialog()145     public void handleHearingAidPreferenceClick_noHearingAid_launchHearingAidInstructionDialog() {
146         mPreferenceController = spy(new AccessibilityHearingAidPreferenceController(mContext,
147                 HEARING_AID_PREFERENCE));
148         mPreferenceController.setPreference(mHearingAidPreference);
149         doNothing().when(mPreferenceController).launchHearingAidInstructionDialog();
150         mPreferenceController.handlePreferenceTreeClick(mHearingAidPreference);
151 
152         verify(mPreferenceController).launchHearingAidInstructionDialog();
153     }
154 
155     @Test
handleHearingAidPreferenceClick_withHearingAid_launchBluetoothDeviceDetailSetting()156     public void handleHearingAidPreferenceClick_withHearingAid_launchBluetoothDeviceDetailSetting
157             () {
158         mPreferenceController = spy(new AccessibilityHearingAidPreferenceController(mContext,
159                 HEARING_AID_PREFERENCE));
160         mPreferenceController.setPreference(mHearingAidPreference);
161         when(mHearingAidProfile.getConnectedDevices()).thenReturn(generateHearingAidDeviceList());
162         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
163         mPreferenceController.handlePreferenceTreeClick(mHearingAidPreference);
164 
165         verify(mPreferenceController).launchBluetoothDeviceDetailSetting(mCachedBluetoothDevice);
166     }
167 
168     @Test
onNotSupportHearingAidProfile_doNotDoReceiverOperation()169     public void onNotSupportHearingAidProfile_doNotDoReceiverOperation() {
170         //clear bluetooth supported profile
171         mShadowBluetoothAdapter.clearSupportedProfiles();
172         mPreferenceController = new AccessibilityHearingAidPreferenceController(mContext,
173                 HEARING_AID_PREFERENCE);
174         mPreferenceController.setPreference(mHearingAidPreference);
175         //not call registerReceiver()
176         mPreferenceController.onStart();
177         verify(mContext, never()).registerReceiver(any(), any());
178 
179         //not call unregisterReceiver()
180         mPreferenceController.onStop();
181         verify(mContext, never()).unregisterReceiver(any());
182     }
183 
184     @Test
getConnectedHearingAidDevice_doNotReturnSubDevice()185     public void getConnectedHearingAidDevice_doNotReturnSubDevice() {
186         when(mHearingAidProfile.getConnectedDevices()).thenReturn(generateHearingAidDeviceList());
187         when(mLocalBluetoothManager.getCachedDeviceManager().isSubDevice(mBluetoothDevice))
188                 .thenReturn(true);
189 
190         assertThat(mPreferenceController.getConnectedHearingAidDevice()).isNull();
191     }
192 
setupBluetoothEnvironment()193     private void setupBluetoothEnvironment() {
194         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
195         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
196         mBluetoothManager = new BluetoothManager(mContext);
197         mBluetoothAdapter = mBluetoothManager.getAdapter();
198         when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
199         when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
200         when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
201     }
202 
setupHearingAidEnvironment()203     private void setupHearingAidEnvironment() {
204         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
205         mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter);
206         mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
207         mBluetoothAdapter.enable();
208         mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
209         when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
210         when(mCachedBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME);
211         when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
212     }
213 
sendIntent(Intent intent)214     private void sendIntent(Intent intent) {
215         for (BroadcastReceiver receiver : mShadowApplication.getReceiversForIntent(intent)) {
216             receiver.onReceive(mContext, intent);
217         }
218     }
219 
generateHearingAidDeviceList()220     private List<BluetoothDevice> generateHearingAidDeviceList() {
221         final List<BluetoothDevice> deviceList = new ArrayList<>(1);
222         deviceList.add(mBluetoothDevice);
223         return deviceList;
224     }
225 }
226