• 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.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.Intent;
26 import android.util.FeatureFlagUtils;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.SettingsActivity;
31 import com.android.settings.accessibility.AccessibilityHearingAidsFragment;
32 import com.android.settings.testutils.FakeFeatureFactory;
33 
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Captor;
39 import org.mockito.junit.MockitoJUnit;
40 import org.mockito.junit.MockitoRule;
41 import org.robolectric.RobolectricTestRunner;
42 
43 /** Tests for {@link BluetoothDetailsHearingDeviceControlsController}. */
44 @RunWith(RobolectricTestRunner.class)
45 public class BluetoothDetailsHearingDeviceControlsControllerTest extends
46         BluetoothDetailsControllerTestBase {
47     @Rule
48     public final MockitoRule mockito = MockitoJUnit.rule();
49 
50     @Captor
51     private ArgumentCaptor<Intent> mIntentArgumentCaptor;
52     private BluetoothDetailsHearingDeviceControlsController mController;
53 
54     @Override
setUp()55     public void setUp() {
56         super.setUp();
57 
58         FakeFeatureFactory.setupForTest();
59         mController = new BluetoothDetailsHearingDeviceControlsController(mActivity, mFragment,
60                 mCachedDevice, mLifecycle);
61         when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
62     }
63 
64     @Test
isAvailable_isHearingAidDevice_available()65     public void isAvailable_isHearingAidDevice_available() {
66         FeatureFlagUtils.setEnabled(mContext,
67                 FeatureFlagUtils.SETTINGS_ACCESSIBILITY_HEARING_AID_PAGE, true);
68         when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
69 
70         assertThat(mController.isAvailable()).isTrue();
71     }
72 
73     @Test
isAvailable_isNotHearingAidDevice_notAvailable()74     public void isAvailable_isNotHearingAidDevice_notAvailable() {
75         FeatureFlagUtils.setEnabled(mContext,
76                 FeatureFlagUtils.SETTINGS_ACCESSIBILITY_HEARING_AID_PAGE, true);
77         when(mCachedDevice.isHearingAidDevice()).thenReturn(false);
78 
79         assertThat(mController.isAvailable()).isFalse();
80     }
81 
82     @Test
onPreferenceClick_hearingDeviceControlsKey_LaunchExpectedFragment()83     public void onPreferenceClick_hearingDeviceControlsKey_LaunchExpectedFragment() {
84         final Preference hearingControlsKeyPreference = new Preference(mContext);
85         hearingControlsKeyPreference.setKey(
86                 BluetoothDetailsHearingDeviceControlsController.KEY_HEARING_DEVICE_CONTROLS);
87 
88         mController.onPreferenceClick(hearingControlsKeyPreference);
89 
90         assertStartActivityWithExpectedFragment(mActivity,
91                 AccessibilityHearingAidsFragment.class.getName());
92     }
93 
assertStartActivityWithExpectedFragment(Context mockContext, String fragmentName)94     private void assertStartActivityWithExpectedFragment(Context mockContext, String fragmentName) {
95         verify(mockContext).startActivity(mIntentArgumentCaptor.capture());
96         assertThat(mIntentArgumentCaptor.getValue()
97                 .getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
98                 .isEqualTo(fragmentName);
99     }
100 }
101