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