1 /* 2 * Copyright (C) 2017 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.development; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.bluetooth.BluetoothCodecConfig; 26 import android.content.Context; 27 28 import androidx.lifecycle.LifecycleOwner; 29 import androidx.preference.ListPreference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settingslib.core.lifecycle.Lifecycle; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class BluetoothAudioSampleRatePreferenceControllerTest { 44 45 @Mock 46 private BluetoothCodecConfig mBluetoothCodecConfig; 47 @Mock 48 private ListPreference mPreference; 49 @Mock 50 private PreferenceScreen mScreen; 51 @Mock 52 private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore; 53 54 /** 55 * 0: Use System Selection (Default) 56 * 1: 44.1 kHz 57 * 2: 48.0 kHz 58 * 3: 88.2 kHz 59 * 4: 96.0 kHz 60 */ 61 private String[] mListValues; 62 private LifecycleOwner mLifecycleOwner; 63 private Lifecycle mLifecycle; 64 private Context mContext; 65 private BluetoothAudioSampleRatePreferenceController mController; 66 67 @Before setup()68 public void setup() { 69 MockitoAnnotations.initMocks(this); 70 mContext = RuntimeEnvironment.application; 71 mLifecycleOwner = () -> mLifecycle; 72 mLifecycle = new Lifecycle(mLifecycleOwner); 73 mController = spy(new BluetoothAudioSampleRatePreferenceController(mContext, mLifecycle, 74 mBluetoothA2dpConfigStore)); 75 mListValues = mController.getListValues(); 76 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 77 mController.displayPreference(mScreen); 78 } 79 80 @Test writeConfigurationValues_option2_shouldWriteOption2ToSharedStore()81 public void writeConfigurationValues_option2_shouldWriteOption2ToSharedStore() { 82 when(mPreference.findIndexOfValue(mListValues[2])).thenReturn(2); 83 mController.writeConfigurationValues(mListValues[2]); 84 85 verify(mBluetoothA2dpConfigStore).setSampleRate(BluetoothCodecConfig.SAMPLE_RATE_48000); 86 } 87 88 @Test getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex()89 public void getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex() { 90 when(mBluetoothCodecConfig.getSampleRate()).thenReturn( 91 BluetoothCodecConfig.SAMPLE_RATE_48000); 92 93 final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig); 94 95 assertThat(index).isEqualTo(2); 96 } 97 98 @Test getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault()99 public void getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault() { 100 when(mBluetoothCodecConfig.getSampleRate()).thenReturn(1381391835); 101 102 final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig); 103 104 assertThat(index).isEqualTo(0); 105 } 106 } 107