1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.display;
16 
17 import static com.google.common.truth.Truth.assertThat;
18 
19 import static org.mockito.ArgumentMatchers.anyString;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.Context;
23 import android.hardware.display.ColorDisplayManager;
24 
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.testutils.shadow.SettingsShadowResources;
28 import com.android.settingslib.widget.MainSwitchPreference;
29 
30 import org.junit.After;
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 import org.robolectric.annotation.Config;
39 
40 @RunWith(RobolectricTestRunner.class)
41 @Config(shadows = SettingsShadowResources.class)
42 public class NightDisplayActivationPreferenceControllerTest {
43 
44     @Mock
45     private PreferenceScreen mScreen;
46     private MainSwitchPreference mPreference;
47     private Context mContext;
48     private ColorDisplayManager mColorDisplayManager;
49     private NightDisplayActivationPreferenceController mPreferenceController;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class);
56         mPreference = new MainSwitchPreference(mContext);
57         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
58         mPreferenceController = new NightDisplayActivationPreferenceController(mContext,
59                 "night_display_activation");
60         mPreferenceController.displayPreference(mScreen);
61     }
62 
63     @After
tearDown()64     public void tearDown() {
65         SettingsShadowResources.reset();
66     }
67 
68     @Test
isAvailable_configuredAvailable()69     public void isAvailable_configuredAvailable() {
70         SettingsShadowResources.overrideResource(
71                 com.android.internal.R.bool.config_nightDisplayAvailable, true);
72         assertThat(mPreferenceController.isAvailable()).isTrue();
73     }
74 
75     @Test
isAvailable_configuredUnavailable()76     public void isAvailable_configuredUnavailable() {
77         SettingsShadowResources.overrideResource(
78                 com.android.internal.R.bool.config_nightDisplayAvailable, false);
79         assertThat(mPreferenceController.isAvailable()).isFalse();
80     }
81 
82     @Test
isSliceableCorrectKey_returnsTrue()83     public void isSliceableCorrectKey_returnsTrue() {
84         final NightDisplayActivationPreferenceController controller =
85                 new NightDisplayActivationPreferenceController(mContext, "night_display_activated");
86         assertThat(controller.isSliceable()).isTrue();
87     }
88 
89     @Test
isSliceableIncorrectKey_returnsFalse()90     public void isSliceableIncorrectKey_returnsFalse() {
91         final NightDisplayActivationPreferenceController controller =
92                 new NightDisplayActivationPreferenceController(mContext, "bad_key");
93         assertThat(controller.isSliceable()).isFalse();
94     }
95 
96     @Test
isPublicSlice_returnTrue()97     public void isPublicSlice_returnTrue() {
98         assertThat(mPreferenceController.isPublicSlice()).isTrue();
99     }
100 
101     @Test
onClick_activates()102     public void onClick_activates() {
103         mColorDisplayManager.setNightDisplayActivated(false);
104 
105         final NightDisplayActivationPreferenceController controller =
106                 new NightDisplayActivationPreferenceController(mContext, "night_display_activated");
107         controller.onSwitchChanged(null, true);
108 
109         assertThat(mColorDisplayManager.isNightDisplayActivated()).isEqualTo(true);
110     }
111 
112     @Test
onClick_deactivates()113     public void onClick_deactivates() {
114         mColorDisplayManager.setNightDisplayActivated(true);
115 
116         final NightDisplayActivationPreferenceController controller =
117                 new NightDisplayActivationPreferenceController(mContext, "night_display_activated");
118         controller.onSwitchChanged(null, false);
119 
120         assertThat(mColorDisplayManager.isNightDisplayActivated()).isEqualTo(false);
121     }
122 }
123