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 import static com.android.settings.development.BluetoothMapVersionPreferenceController
18         .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 com.android.settings.R;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class BluetoothMapVersionPreferenceControllerTest {
44     @Mock
45     private ListPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48     private Context mContext;
49     private BluetoothMapVersionPreferenceController mController;
50     /**
51      * 0: MAP 1.2 (Default)
52      * 1: MAP 1.3
53      * 2: MAP 1.4
54      */
55     private String[] mListValues;
56     private String[] mListSummaries;
57     @Before
setup()58     public void setup() {
59         MockitoAnnotations.initMocks(this);
60         mContext = RuntimeEnvironment.application;
61         final Resources resources = mContext.getResources();
62         mListValues = resources.getStringArray(R.array.bluetooth_map_version_values);
63         mListSummaries = resources.getStringArray(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