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 
17 package com.android.settings.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 
24 import androidx.preference.SwitchPreference;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.RuntimeEnvironment;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class OverlaySettingsPreferenceControllerTest {
34 
35     private Context mContext;
36     private SwitchPreference mPreference;
37     private OverlaySettingsPreferenceController mController;
38 
39     @Before
setUp()40     public void setUp() {
41         mContext = RuntimeEnvironment.application;
42         mController = new OverlaySettingsPreferenceController(mContext);
43         mPreference = new SwitchPreference(mContext);
44     }
45 
46     @Test
isAvailable_shouldReturnTrue()47     public void isAvailable_shouldReturnTrue() {
48         assertThat(mController.isAvailable()).isTrue();
49     }
50 
51     @Test
updateState_isOverlaySettingsEnabled_shouldCheckPreference()52     public void updateState_isOverlaySettingsEnabled_shouldCheckPreference() {
53         OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
54 
55         mController.updateState(mPreference);
56 
57         assertThat(mPreference.isChecked()).isTrue();
58     }
59 
60     @Test
updateState_isOverlaySettingsDisabled_shouldUncheckPreference()61     public void updateState_isOverlaySettingsDisabled_shouldUncheckPreference() {
62         OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
63 
64         mController.updateState(mPreference);
65 
66         assertThat(mPreference.isChecked()).isFalse();
67     }
68 
69     @Test
onPreferenceChange_preferenceChecked_shouldEnableSettings()70     public void onPreferenceChange_preferenceChecked_shouldEnableSettings() {
71         mController.onPreferenceChange(mPreference, true);
72 
73         assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
74     }
75 
76     @Test
onPreferenceChange_preferenceUnchecked_shouldDisableSettings()77     public void onPreferenceChange_preferenceUnchecked_shouldDisableSettings() {
78         mController.onPreferenceChange(mPreference, false);
79 
80         assertThat(
81                 OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
82     }
83 
84     @Test
isOverlaySettingsEnabled_sharePreferenceSetTrue_shouldReturnTrue()85     public void isOverlaySettingsEnabled_sharePreferenceSetTrue_shouldReturnTrue() {
86         final SharedPreferences editor = mContext.getSharedPreferences(
87                 OverlaySettingsPreferenceController.SHARE_PERFS,
88                 Context.MODE_PRIVATE);
89         editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, true).apply();
90 
91         assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
92     }
93 
94     @Test
isOverlaySettingsEnabled_sharePreferenceSetFalse_shouldReturnFalse()95     public void isOverlaySettingsEnabled_sharePreferenceSetFalse_shouldReturnFalse() {
96         final SharedPreferences editor = mContext.getSharedPreferences(
97                 OverlaySettingsPreferenceController.SHARE_PERFS,
98                 Context.MODE_PRIVATE);
99         editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, false).apply();
100 
101         assertThat(
102                 OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
103     }
104 
105     @Test
setOverlaySettingsEnabled_setTrue_shouldStoreTrue()106     public void setOverlaySettingsEnabled_setTrue_shouldStoreTrue() {
107         OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
108 
109         assertThat(
110                 OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
111     }
112 
113     @Test
setOverlaySettingsEnabled_setFalse_shouldStoreTrue()114     public void setOverlaySettingsEnabled_setFalse_shouldStoreTrue() {
115         OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
116 
117         assertThat(
118                 OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
119     }
120 }
121