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 BluetoothAudioBitsPerSamplePreferenceControllerTest { 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: 16 bits/sample 55 * 2: 24 bits/sample 56 * 3: 32 bits/sample 57 */ 58 private String[] mListValues; 59 private Context mContext; 60 private BluetoothAudioBitsPerSamplePreferenceController mController; 61 private LifecycleOwner mLifecycleOwner; 62 private Lifecycle mLifecycle; 63 64 @Before 65 public void setup() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 mLifecycleOwner = () -> mLifecycle; 69 mLifecycle = new Lifecycle(mLifecycleOwner); 70 mController = spy(new BluetoothAudioBitsPerSamplePreferenceController(mContext, 71 mLifecycle, mBluetoothA2dpConfigStore)); 72 mListValues = mController.getListValues(); 73 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 74 mController.displayPreference(mScreen); 75 } 76 77 @Test 78 public void writeConfigurationValues_option2_shouldWriteOption2ToSharedStore() { 79 when(mPreference.findIndexOfValue(mListValues[2])).thenReturn(2); 80 mController.writeConfigurationValues(mListValues[2]); 81 82 verify(mBluetoothA2dpConfigStore).setBitsPerSample(BluetoothCodecConfig.BITS_PER_SAMPLE_24); 83 } 84 85 @Test 86 public void writeConfigurationValues_default_shouldSetDefault() { 87 when(mPreference.findIndexOfValue(mListValues[0])).thenReturn(0); 88 mController.writeConfigurationValues(mListValues[0]); 89 90 verify(mBluetoothA2dpConfigStore).setBitsPerSample( 91 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE); 92 } 93 94 @Test 95 public void getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex() { 96 when(mBluetoothCodecConfig.getBitsPerSample()).thenReturn( 97 BluetoothCodecConfig.BITS_PER_SAMPLE_24); 98 99 final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig); 100 101 assertThat(index).isEqualTo(2); 102 } 103 104 @Test 105 public void getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault() { 106 when(mBluetoothCodecConfig.getCodecType()).thenReturn(1381391835); 107 108 final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig); 109 110 assertThat(index).isEqualTo(0); 111 } 112 } 113