1 /*
2  * Copyright (C) 2020 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 package com.android.settings.development;
17 
18 import static com.android.settings.development.BluetoothMapVersionPreferenceController.BLUETOOTH_MAP_VERSION_PROPERTY;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.res.Resources;
27 import android.os.SystemProperties;
28 
29 import androidx.preference.ListPreference;
30 import androidx.preference.PreferenceScreen;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class BluetoothMapVersionPreferenceControllerTest {
42     @Mock
43     private ListPreference mPreference;
44     @Mock
45     private PreferenceScreen mPreferenceScreen;
46     private Context mContext;
47     private BluetoothMapVersionPreferenceController mController;
48     /**
49      * 0: MAP 1.2 (Default)
50      * 1: MAP 1.3
51      * 2: MAP 1.4
52      */
53     private String[] mListValues;
54     private String[] mListSummaries;
55     @Before
setup()56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58         mContext = RuntimeEnvironment.application;
59         final Resources resources = mContext.getResources();
60         mListValues = resources.getStringArray(
61                 com.android.settingslib.R.array.bluetooth_map_version_values);
62         mListSummaries = resources.getStringArray(
63                 com.android.settingslib.R.array.bluetooth_map_versions);
64         mController = new BluetoothMapVersionPreferenceController(mContext);
65         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
66             .thenReturn(mPreference);
67         mController.displayPreference(mPreferenceScreen);
68     }
69     @Test
onPreferenceChange_setMap13_shouldEnableMap13()70     public void onPreferenceChange_setMap13_shouldEnableMap13() {
71         mController.onPreferenceChange(mPreference, mListValues[1]);
72 
73         final String currentValue = SystemProperties.get(BLUETOOTH_MAP_VERSION_PROPERTY);
74         assertThat(currentValue).isEqualTo(mListValues[1]);
75     }
76     @Test
onPreferenceChange_setMap14_shouldEnableMap14()77     public void onPreferenceChange_setMap14_shouldEnableMap14() {
78         mController.onPreferenceChange(mPreference, mListValues[2]);
79 
80         final String currentValue = SystemProperties.get(BLUETOOTH_MAP_VERSION_PROPERTY);
81         assertThat(currentValue).isEqualTo(mListValues[2]);
82     }
83     @Test
updateState_setMap13_shouldSetPreferenceToMap13()84     public void updateState_setMap13_shouldSetPreferenceToMap13() {
85         SystemProperties.set(BLUETOOTH_MAP_VERSION_PROPERTY, mListValues[1]);
86 
87         mController.updateState(mPreference);
88 
89         verify(mPreference).setValue(mListValues[1]);
90         verify(mPreference).setSummary(mListSummaries[1]);
91     }
92     @Test
updateState_setMap14_shouldSetPreferenceToMap14()93     public void updateState_setMap14_shouldSetPreferenceToMap14() {
94         SystemProperties.set(BLUETOOTH_MAP_VERSION_PROPERTY, mListValues[2]);
95 
96         mController.updateState(mPreference);
97 
98         verify(mPreference).setValue(mListValues[2]);
99         verify(mPreference).setSummary(mListSummaries[2]);
100     }
101     @Test
updateState_noValueSet_shouldSetDefaultToMap12()102     public void updateState_noValueSet_shouldSetDefaultToMap12() {
103         mController.updateState(mPreference);
104 
105         verify(mPreference).setValue(mListValues[0]);
106         verify(mPreference).setSummary(mListSummaries[0]);
107     }
108 }
109