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 
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.provider.Settings;
27 
28 import androidx.preference.ListPreference;
29 import androidx.preference.PreferenceScreen;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class SecondaryDisplayPreferenceControllerTest {
41 
42     @Mock
43     private ListPreference mPreference;
44     @Mock
45     private PreferenceScreen mScreen;
46 
47     /**
48      * 0: None
49      * 1: 480p
50      * 2: 480p (secure)
51      * 3: 720p
52      * 4: 720p (secure)
53      * 5: 1080p
54      * 6: 1080p (secure)
55      * 7: 4K
56      * 8: 4K (secure)
57      * 9: 4K (upscaled)
58      * 10: 4K (upscaled, secure)
59      * 11: 720p, 1080p (dual screen)
60      */
61     private String[] mListValues;
62     private String[] mListSummaries;
63     private Context mContext;
64     private SecondaryDisplayPreferenceController mController;
65 
66     @Before
setup()67     public void setup() {
68         MockitoAnnotations.initMocks(this);
69         mContext = RuntimeEnvironment.application;
70         final Resources resources = mContext.getResources();
71         mListValues = resources.getStringArray(
72                 com.android.settingslib.R.array.overlay_display_devices_values);
73         mListSummaries = resources.getStringArray(
74                 com.android.settingslib.R.array.overlay_display_devices_entries);
75         mController = new SecondaryDisplayPreferenceController(mContext);
76         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
77         mController.displayPreference(mScreen);
78     }
79 
80     @Test
onPreferenceChange_set480p_shouldEnable480p()81     public void onPreferenceChange_set480p_shouldEnable480p() {
82         mController.onPreferenceChange(mPreference, mListValues[1]);
83 
84         final String value = Settings.Global.getString(mContext.getContentResolver(),
85                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
86         assertThat(value).isEqualTo(mListValues[1]);
87     }
88 
89     @Test
onPreferenceChange_set720p_shouldEnable720p()90     public void onPreferenceChange_set720p_shouldEnable720p() {
91         mController.onPreferenceChange(mPreference, mListValues[3]);
92 
93         final String value = Settings.Global.getString(mContext.getContentResolver(),
94                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
95         assertThat(value).isEqualTo(mListValues[3]);
96     }
97 
98     @Test
updateState_set480p_shouldSetPreferenceTo480p()99     public void updateState_set480p_shouldSetPreferenceTo480p() {
100         Settings.Global.putString(mContext.getContentResolver(),
101                 Settings.Global.OVERLAY_DISPLAY_DEVICES, mListValues[1]);
102 
103         mController.updateState(mPreference);
104 
105         verify(mPreference).setValue(mListValues[1]);
106         verify(mPreference).setSummary(mListSummaries[1]);
107     }
108 
109     @Test
updateState_set720p_shouldSetPreferenceTo720p()110     public void updateState_set720p_shouldSetPreferenceTo720p() {
111         Settings.Global.putString(mContext.getContentResolver(),
112                 Settings.Global.OVERLAY_DISPLAY_DEVICES, mListValues[3]);
113 
114         mController.updateState(mPreference);
115 
116         verify(mPreference).setValue(mListValues[3]);
117         verify(mPreference).setSummary(mListSummaries[3]);
118     }
119 
120     @Test
updateState_nothingSet_shouldSetDefaultToNone()121     public void updateState_nothingSet_shouldSetDefaultToNone() {
122         mController.updateState(mPreference);
123 
124         verify(mPreference).setValue(mListValues[0]);
125         verify(mPreference).setSummary(mListSummaries[0]);
126     }
127 
128     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreferenceAndSetToNone()129     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreferenceAndSetToNone() {
130         mController.onDeveloperOptionsSwitchDisabled();
131 
132         final String value = Settings.Global.getString(mContext.getContentResolver(),
133                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
134         assertThat(value).isNull();
135         verify(mPreference).setEnabled(false);
136         verify(mPreference).setValue(mListValues[0]);
137         verify(mPreference).setSummary(mListSummaries[0]);
138     }
139 }
140