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 com.android.settings.R;
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.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class SecondaryDisplayPreferenceControllerTest {
43 
44     @Mock
45     private ListPreference mPreference;
46     @Mock
47     private PreferenceScreen mScreen;
48 
49     /**
50      * 0: None
51      * 1: 480p
52      * 2: 480p (secure)
53      * 3: 720p
54      * 4: 720p (secure)
55      * 5: 1080p
56      * 6: 1080p (secure)
57      * 7: 4K
58      * 8: 4K (secure)
59      * 9: 4K (upscaled)
60      * 10: 4K (upscaled, secure)
61      * 11: 720p, 1080p (dual screen)
62      */
63     private String[] mListValues;
64     private String[] mListSummaries;
65     private Context mContext;
66     private SecondaryDisplayPreferenceController mController;
67 
68     @Before
setup()69     public void setup() {
70         MockitoAnnotations.initMocks(this);
71         mContext = RuntimeEnvironment.application;
72         final Resources resources = mContext.getResources();
73         mListValues = resources.getStringArray(R.array.overlay_display_devices_values);
74         mListSummaries = resources.getStringArray(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