1 /*
2  * Copyright (C) 2024 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.display;
18 
19 
20 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
21 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.content.res.Resources;
29 import android.platform.test.annotations.RequiresFlagsDisabled;
30 import android.platform.test.annotations.RequiresFlagsEnabled;
31 import android.platform.test.flag.junit.CheckFlagsRule;
32 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
33 import android.provider.Settings;
34 
35 import com.android.server.display.feature.flags.Flags;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class EvenDimmerPreferenceControllerTest {
47 
48     private EvenDimmerPreferenceController mController;
49     @Mock
50     private Context mContext;
51     @Mock
52     private Resources mResources;
53 
54     @Rule
55     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         when(mContext.getResources()).thenReturn(mResources);
61         mController = new EvenDimmerPreferenceController(mContext, "key");
62     }
63 
64     @RequiresFlagsDisabled(Flags.FLAG_EVEN_DIMMER)
65     @Test
testGetAvailabilityStatus_flagOffConfigTrue()66     public void testGetAvailabilityStatus_flagOffConfigTrue() {
67         when(mContext.getResources()).thenReturn(mResources);
68         when(mResources.getBoolean(
69                 com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
70         // setup
71         mController = new EvenDimmerPreferenceController(mContext, "key");
72 
73         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
74     }
75 
76     @RequiresFlagsDisabled(Flags.FLAG_EVEN_DIMMER)
77     @Test
testGetCheckedStatus_setTrue()78     public void testGetCheckedStatus_setTrue() throws Settings.SettingNotFoundException {
79         // setup
80         mController = new EvenDimmerPreferenceController(mContext, "key");
81         mController.setChecked(true);
82 
83         assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
84                 Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(0.0f); // false
85     }
86 
87     @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
88     @Test
testGetAvailabilityStatus_flagOnConfigTrue()89     public void testGetAvailabilityStatus_flagOnConfigTrue() {
90         when(mContext.getResources()).thenReturn(mResources);
91         when(mResources.getBoolean(
92                 com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
93         // setup
94         mController = new EvenDimmerPreferenceController(mContext, "key");
95 
96         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
97     }
98 
99 
100     @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
101     @Test
testGetAvailabilityStatus_flagOnConfigFalse()102     public void testGetAvailabilityStatus_flagOnConfigFalse() {
103         when(mContext.getResources()).thenReturn(mResources);
104         when(mResources.getBoolean(
105                 com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(false);
106         // setup
107         mController = new EvenDimmerPreferenceController(mContext, "key");
108 
109         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
110     }
111 
112     @Test
113     @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
testSetChecked_enable()114     public void testSetChecked_enable() throws Settings.SettingNotFoundException {
115         when(mResources.getBoolean(
116                 com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
117         mController.setChecked(true);
118         assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
119                 Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(1.0f); // true
120     }
121 
122     @Test
123     @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
testSetChecked_disable()124     public void testSetChecked_disable() throws Settings.SettingNotFoundException {
125         when(mResources.getBoolean(
126                 com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
127         mController.setChecked(false);
128         assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
129                 Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(0.0f); // false
130     }
131 }
132