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.android.settings.development.BluetoothAvrcpVersionPreferenceController.BLUETOOTH_AVRCP_VERSION_PROPERTY; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.os.SystemProperties; 27 import android.support.v7.preference.ListPreference; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 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 BluetoothAvrcpVersionPreferenceControllerTest { 42 43 @Mock 44 private ListPreference mPreference; 45 @Mock 46 private PreferenceScreen mPreferenceScreen; 47 48 private Context mContext; 49 private BluetoothAvrcpVersionPreferenceController mController; 50 51 /** 52 * 0: AVRCP 1.4 (Default) 53 * 1: AVRCP 1.3 54 * 2: AVRCP 1.5 55 * 3: AVRCP 1.6 56 */ 57 private String[] mListValues; 58 private String[] mListSummaries; 59 60 @Before setup()61 public void setup() { 62 MockitoAnnotations.initMocks(this); 63 mContext = RuntimeEnvironment.application; 64 final Resources resources = mContext.getResources(); 65 mListValues = resources.getStringArray(R.array.bluetooth_avrcp_version_values); 66 mListSummaries = resources.getStringArray(R.array.bluetooth_avrcp_versions); 67 mController = new BluetoothAvrcpVersionPreferenceController(mContext); 68 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 69 .thenReturn(mPreference); 70 mController.displayPreference(mPreferenceScreen); 71 } 72 73 @Test onPreferenceChange_setAvrcp13_shouldEnableAvrcp13()74 public void onPreferenceChange_setAvrcp13_shouldEnableAvrcp13() { 75 mController.onPreferenceChange(mPreference, mListValues[1]); 76 77 final String currentValue = SystemProperties.get(BLUETOOTH_AVRCP_VERSION_PROPERTY); 78 79 assertThat(currentValue).isEqualTo(mListValues[1]); 80 } 81 82 @Test onPreferenceChange_setAvrcp15_shouldEnableAvrcp15()83 public void onPreferenceChange_setAvrcp15_shouldEnableAvrcp15() { 84 mController.onPreferenceChange(mPreference, mListValues[2]); 85 86 final String currentValue = SystemProperties.get(BLUETOOTH_AVRCP_VERSION_PROPERTY); 87 88 assertThat(currentValue).isEqualTo(mListValues[2]); 89 } 90 91 @Test updateState_setAvrcp13_shouldSetPreferenceToAvrcp13()92 public void updateState_setAvrcp13_shouldSetPreferenceToAvrcp13() { 93 SystemProperties.set(BLUETOOTH_AVRCP_VERSION_PROPERTY, mListValues[1]); 94 95 mController.updateState(mPreference); 96 97 verify(mPreference).setValue(mListValues[1]); 98 verify(mPreference).setSummary(mListSummaries[1]); 99 } 100 101 @Test updateState_setAvrcp15_shouldSetPreferenceToAvrcp15()102 public void updateState_setAvrcp15_shouldSetPreferenceToAvrcp15() { 103 SystemProperties.set(BLUETOOTH_AVRCP_VERSION_PROPERTY, mListValues[2]); 104 105 mController.updateState(mPreference); 106 107 verify(mPreference).setValue(mListValues[2]); 108 verify(mPreference).setSummary(mListSummaries[2]); 109 } 110 111 @Test updateState_noValueSet_shouldSetDefaultToAvrcp14()112 public void updateState_noValueSet_shouldSetDefaultToAvrcp14() { 113 mController.updateState(mPreference); 114 115 verify(mPreference).setValue(mListValues[0]); 116 verify(mPreference).setSummary(mListSummaries[0]); 117 } 118 } 119