• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.bluetooth.BluetoothAdapter;
26 import android.bluetooth.BluetoothProfile;
27 import android.content.Context;
28 import android.platform.test.annotations.EnableFlags;
29 import android.platform.test.flag.junit.SetFlagsRule;
30 import android.telephony.TelephonyManager;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import com.android.settings.R;
35 import com.android.settings.bluetooth.Utils;
36 import com.android.settings.testutils.XmlTestUtils;
37 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
38 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
39 import com.android.settingslib.bluetooth.LocalBluetoothManager;
40 
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.Spy;
47 import org.mockito.junit.MockitoJUnit;
48 import org.mockito.junit.MockitoRule;
49 import org.robolectric.RobolectricTestRunner;
50 import org.robolectric.annotation.Config;
51 import org.robolectric.shadow.api.Shadow;
52 
53 import java.util.List;
54 import java.util.Objects;
55 
56 /** Tests for {@link AccessibilityHearingAidsFragment}. */
57 @RunWith(RobolectricTestRunner.class)
58 @Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
59 public class AccessibilityHearingAidsFragmentTest {
60 
61     @Rule
62     public MockitoRule mMockitoRule = MockitoJUnit.rule();
63     @Rule
64     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
65     @Spy
66     private final Context mContext = ApplicationProvider.getApplicationContext();
67 
68     @Mock
69     private LocalBluetoothManager mLocalBluetoothManager;
70     private ShadowBluetoothAdapter mShadowBluetoothAdapter;
71     private BluetoothAdapter mBluetoothAdapter;
72     private TelephonyManager mTelephonyManager;
73 
74     @Before
setUp()75     public void setUp() {
76         mTelephonyManager = spy(mContext.getSystemService(TelephonyManager.class));
77         when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
78         doReturn(true).when(mTelephonyManager).isHearingAidCompatibilitySupported();
79 
80         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
81         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
82         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
83         mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter);
84     }
85 
86     @Test
getNonIndexableKeys_existInXmlLayout()87     public void getNonIndexableKeys_existInXmlLayout() {
88         mShadowBluetoothAdapter.clearSupportedProfiles();
89         mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
90 
91         final List<String> niks = AccessibilityHearingAidsFragment.SEARCH_INDEX_DATA_PROVIDER
92                 .getNonIndexableKeys(mContext).stream()
93                 .filter(Objects::nonNull)
94                 .toList();
95         final List<String> keys =
96                 XmlTestUtils.getKeysFromPreferenceXml(mContext, R.xml.accessibility_hearing_aids);
97 
98         assertThat(keys).containsAtLeastElementsIn(niks);
99     }
100 
101     @Test
102     @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
deviceSupportsHearingAid_isPageSearchEnabled_returnTrue()103     public void deviceSupportsHearingAid_isPageSearchEnabled_returnTrue() {
104         mShadowBluetoothAdapter.clearSupportedProfiles();
105         mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
106 
107         assertThat(AccessibilityHearingAidsFragment.isPageSearchEnabled(mContext)).isTrue();
108     }
109 
110     @Test
111     @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
deviceDoesNotSupportHearingAid_isPageSearchEnabled_returnFalse()112     public void deviceDoesNotSupportHearingAid_isPageSearchEnabled_returnFalse() {
113         mShadowBluetoothAdapter.clearSupportedProfiles();
114         mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEADSET);
115 
116         assertThat(AccessibilityHearingAidsFragment.isPageSearchEnabled(mContext)).isFalse();
117     }
118 }
119