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