1 /*
2  * Copyright (C) 2024 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.when;
22 
23 import android.platform.test.annotations.RequiresFlagsDisabled;
24 import android.platform.test.annotations.RequiresFlagsEnabled;
25 import android.platform.test.flag.junit.CheckFlagsRule;
26 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
27 
28 import com.android.settings.accessibility.Flags;
29 import com.android.settingslib.bluetooth.LocalBluetoothManager;
30 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnit;
37 import org.mockito.junit.MockitoRule;
38 import org.robolectric.RobolectricTestRunner;
39 
40 /** Tests for {@link BluetoothDetailsHearingDeviceController}. */
41 @RunWith(RobolectricTestRunner.class)
42 public class BluetoothDetailsHearingDeviceControllerTest extends
43         BluetoothDetailsControllerTestBase {
44 
45     @Rule
46     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
47 
48     @Rule
49     public final MockitoRule mockito = MockitoJUnit.rule();
50 
51     @Mock
52     private LocalBluetoothManager mLocalManager;
53     @Mock
54     private LocalBluetoothProfileManager mProfileManager;
55     @Mock
56     private BluetoothDetailsHearingDeviceController mHearingDeviceController;
57     @Mock
58     private BluetoothDetailsHearingAidsPresetsController mPresetsController;
59     @Mock
60     private BluetoothDetailsHearingDeviceSettingsController mHearingDeviceSettingsController;
61 
62     @Override
setUp()63     public void setUp() {
64         super.setUp();
65 
66         when(mLocalManager.getProfileManager()).thenReturn(mProfileManager);
67         mHearingDeviceController = new BluetoothDetailsHearingDeviceController(mContext,
68                 mFragment, mLocalManager, mCachedDevice, mLifecycle);
69         mHearingDeviceController.setSubControllers(mHearingDeviceSettingsController,
70                 mPresetsController);
71     }
72 
73     @Test
isAvailable_hearingDeviceSettingsAvailable_returnTrue()74     public void isAvailable_hearingDeviceSettingsAvailable_returnTrue() {
75         when(mHearingDeviceSettingsController.isAvailable()).thenReturn(true);
76 
77         assertThat(mHearingDeviceController.isAvailable()).isTrue();
78     }
79 
80     @Test
isAvailable_presetsControlsAvailable_returnTrue()81     public void isAvailable_presetsControlsAvailable_returnTrue() {
82         when(mPresetsController.isAvailable()).thenReturn(true);
83 
84         assertThat(mHearingDeviceController.isAvailable()).isTrue();
85     }
86 
87     @Test
isAvailable_noControllersAvailable_returnFalse()88     public void isAvailable_noControllersAvailable_returnFalse() {
89         when(mHearingDeviceSettingsController.isAvailable()).thenReturn(false);
90         when(mPresetsController.isAvailable()).thenReturn(false);
91 
92         assertThat(mHearingDeviceController.isAvailable()).isFalse();
93     }
94 
95 
96     @Test
initSubControllers_launchFromHearingDevicePage_hearingDeviceSettingsNotExist()97     public void initSubControllers_launchFromHearingDevicePage_hearingDeviceSettingsNotExist() {
98         mHearingDeviceController.initSubControllers(true);
99 
100         assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
101                 c -> c instanceof BluetoothDetailsHearingDeviceSettingsController)).isFalse();
102     }
103 
104     @Test
initSubControllers_notLaunchFromHearingDevicePage_hearingDeviceSettingsExist()105     public void initSubControllers_notLaunchFromHearingDevicePage_hearingDeviceSettingsExist() {
106         mHearingDeviceController.initSubControllers(false);
107 
108         assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
109                 c -> c instanceof BluetoothDetailsHearingDeviceSettingsController)).isTrue();
110     }
111 
112     @Test
113     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HEARING_AID_PRESET_CONTROL)
initSubControllers_flagEnabled_presetControllerExist()114     public void initSubControllers_flagEnabled_presetControllerExist() {
115         mHearingDeviceController.initSubControllers(false);
116 
117         assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
118                 c -> c instanceof BluetoothDetailsHearingAidsPresetsController)).isTrue();
119     }
120 
121     @Test
122     @RequiresFlagsDisabled(Flags.FLAG_ENABLE_HEARING_AID_PRESET_CONTROL)
initSubControllers_flagDisabled_presetControllerNotExist()123     public void initSubControllers_flagDisabled_presetControllerNotExist() {
124         mHearingDeviceController.initSubControllers(false);
125 
126         assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
127                 c -> c instanceof BluetoothDetailsHearingAidsPresetsController)).isFalse();
128     }
129 }
130