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.android.settings.bluetooth.BluetoothDetailsAmbientVolumePreferenceController.KEY_AMBIENT_VOLUME; 20 import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import androidx.preference.PreferenceCategory; 30 31 import com.android.settings.testutils.shadow.ShadowThreadUtils; 32 import com.android.settingslib.bluetooth.AmbientVolumeUiController; 33 import com.android.settingslib.bluetooth.BluetoothEventManager; 34 import com.android.settingslib.bluetooth.LocalBluetoothManager; 35 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 36 import com.android.settingslib.bluetooth.VolumeControlProfile; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.junit.MockitoJUnit; 44 import org.mockito.junit.MockitoRule; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.annotation.Config; 47 48 import java.util.List; 49 50 /** Tests for {@link BluetoothDetailsAmbientVolumePreferenceController}. */ 51 @RunWith(RobolectricTestRunner.class) 52 @Config(shadows = { 53 ShadowThreadUtils.class 54 }) 55 public class BluetoothDetailsAmbientVolumePreferenceControllerTest extends 56 BluetoothDetailsControllerTestBase { 57 @Rule 58 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 59 60 @Mock 61 private LocalBluetoothManager mBluetoothManager; 62 @Mock 63 private BluetoothEventManager mEventManager; 64 @Mock 65 private LocalBluetoothProfileManager mProfileManager; 66 @Mock 67 private VolumeControlProfile mVolumeControlProfile; 68 @Mock 69 private AmbientVolumeUiController mUiController; 70 71 private BluetoothDetailsAmbientVolumePreferenceController mController; 72 73 @Before setUp()74 public void setUp() { 75 super.setUp(); 76 77 mContext = spy(mContext); 78 79 when(mBluetoothManager.getProfileManager()).thenReturn(mProfileManager); 80 when(mBluetoothManager.getEventManager()).thenReturn(mEventManager); 81 mController = spy( 82 new BluetoothDetailsAmbientVolumePreferenceController(mContext, mBluetoothManager, 83 mFragment, mCachedDevice, mLifecycle, mUiController)); 84 85 PreferenceCategory deviceControls = new PreferenceCategory(mContext); 86 deviceControls.setKey(KEY_HEARING_DEVICE_GROUP); 87 mScreen.addPreference(deviceControls); 88 } 89 90 @Test init_preferenceAdded()91 public void init_preferenceAdded() { 92 mController.init(mScreen); 93 94 AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME); 95 assertThat(preference).isNotNull(); 96 } 97 98 @Test isAvailable_notHearingDevice_returnFalse()99 public void isAvailable_notHearingDevice_returnFalse() { 100 when(mCachedDevice.isHearingDevice()).thenReturn(false); 101 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 102 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 103 104 assertThat(mController.isAvailable()).isFalse(); 105 } 106 107 @Test isAvailable_notSupportVcp_returnFalse()108 public void isAvailable_notSupportVcp_returnFalse() { 109 when(mCachedDevice.isHearingDevice()).thenReturn(true); 110 when(mCachedDevice.getProfiles()).thenReturn(List.of()); 111 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 112 113 assertThat(mController.isAvailable()).isFalse(); 114 } 115 116 @Test isAvailable_noValidAmbientControlPoint_returnFalse()117 public void isAvailable_noValidAmbientControlPoint_returnFalse() { 118 when(mCachedDevice.isHearingDevice()).thenReturn(true); 119 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 120 when(mUiController.isAmbientControlAvailable()).thenReturn(false); 121 122 assertThat(mController.isAvailable()).isFalse(); 123 } 124 125 @Test isAvailable_isHearingDevice_supportVcp_validAmbientControl_returnTrue()126 public void isAvailable_isHearingDevice_supportVcp_validAmbientControl_returnTrue() { 127 when(mCachedDevice.isHearingDevice()).thenReturn(true); 128 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 129 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 130 131 assertThat(mController.isAvailable()).isTrue(); 132 } 133 134 @Test refresh_notHearingDevice_verifyUiControllerNotRefresh()135 public void refresh_notHearingDevice_verifyUiControllerNotRefresh() { 136 when(mCachedDevice.isHearingDevice()).thenReturn(false); 137 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 138 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 139 140 mController.refresh(); 141 142 verify(mUiController, never()).refresh(); 143 } 144 145 @Test refresh_notSupportVcp_verifyUiControllerNotRefresh()146 public void refresh_notSupportVcp_verifyUiControllerNotRefresh() { 147 when(mCachedDevice.isHearingDevice()).thenReturn(true); 148 when(mCachedDevice.getProfiles()).thenReturn(List.of()); 149 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 150 151 mController.refresh(); 152 153 verify(mUiController, never()).refresh(); 154 } 155 156 @Test refresh_noValidAmbientControl_verifyUiControllerNotRefresh()157 public void refresh_noValidAmbientControl_verifyUiControllerNotRefresh() { 158 when(mCachedDevice.isHearingDevice()).thenReturn(true); 159 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 160 when(mUiController.isAmbientControlAvailable()).thenReturn(false); 161 162 mController.refresh(); 163 164 verify(mUiController, never()).refresh(); 165 } 166 167 @Test refresh_isHearingDevice_supportVcp_validAmbientControl_verifyUiControllerRefresh()168 public void refresh_isHearingDevice_supportVcp_validAmbientControl_verifyUiControllerRefresh() { 169 when(mCachedDevice.isHearingDevice()).thenReturn(true); 170 when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile)); 171 when(mUiController.isAmbientControlAvailable()).thenReturn(true); 172 173 mController.refresh(); 174 175 verify(mUiController).refresh(); 176 } 177 178 @Test onStart_verifyUiControllerStart()179 public void onStart_verifyUiControllerStart() { 180 mController.onStart(); 181 182 verify(mUiController).start(); 183 } 184 185 @Test onStop_verifyUiControllerStop()186 public void onStop_verifyUiControllerStop() { 187 mController.onStop(); 188 189 verify(mUiController).stop(); 190 } 191 } 192