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.display;
18 
19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_AMBIENT_DISPLAY;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyNoMoreInteractions;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.hardware.display.AmbientDisplayConfiguration;
33 import android.os.UserHandle;
34 import android.provider.Settings;
35 
36 import androidx.preference.SwitchPreference;
37 
38 import com.android.settings.testutils.shadow.ShadowSecureSettings;
39 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.util.ReflectionHelpers;
50 
51 @RunWith(RobolectricTestRunner.class)
52 @Config(shadows = ShadowSecureSettings.class)
53 public class AmbientDisplayNotificationsPreferenceControllerTest {
54 
55     @Mock
56     private AmbientDisplayConfiguration mConfig;
57     @Mock
58     private SwitchPreference mSwitchPreference;
59     @Mock
60     private MetricsFeatureProvider mMetricsFeatureProvider;
61 
62     private Context mContext;
63 
64     private ContentResolver mContentResolver;
65 
66     private AmbientDisplayNotificationsPreferenceController mController;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mContext = RuntimeEnvironment.application;
72         mContentResolver = mContext.getContentResolver();
73         mController = new AmbientDisplayNotificationsPreferenceController(mContext,
74                 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
75         mController.setConfig(mConfig);
76         ReflectionHelpers.setField(mController, "mMetricsFeatureProvider", mMetricsFeatureProvider);
77     }
78 
79     @Test
updateState_enabled()80     public void updateState_enabled() {
81         when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(true);
82 
83         mController.updateState(mSwitchPreference);
84 
85         verify(mSwitchPreference).setChecked(true);
86     }
87 
88     @Test
updateState_disabled()89     public void updateState_disabled() {
90         when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(false);
91 
92         mController.updateState(mSwitchPreference);
93 
94         verify(mSwitchPreference).setChecked(false);
95     }
96 
97     @Test
onPreferenceChange_enable()98     public void onPreferenceChange_enable() {
99         mController.onPreferenceChange(mSwitchPreference, true);
100 
101         assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
102                 .isEqualTo(1);
103     }
104 
105     @Test
onPreferenceChange_disable()106     public void onPreferenceChange_disable() {
107         mController.onPreferenceChange(mSwitchPreference, false);
108 
109         assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
110                 .isEqualTo(0);
111     }
112 
113     @Test
isAvailable_available()114     public void isAvailable_available() {
115         when(mConfig.pulseOnNotificationAvailable()).thenReturn(true);
116 
117         assertThat(mController.isAvailable()).isTrue();
118     }
119 
120     @Test
isAvailable_unavailable()121     public void isAvailable_unavailable() {
122         when(mConfig.pulseOnNotificationAvailable()).thenReturn(false);
123 
124         assertThat(mController.isAvailable()).isFalse();
125     }
126 
127     @Test
isChecked_checked_shouldReturnTrue()128     public void isChecked_checked_shouldReturnTrue() {
129         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(true);
130 
131         assertThat(mController.isChecked()).isTrue();
132     }
133 
134     @Test
isChecked_checked_shouldReturnFalse()135     public void isChecked_checked_shouldReturnFalse() {
136         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(false);
137 
138         assertThat(mController.isChecked()).isFalse();
139     }
140 
141     @Test
handlePreferenceTreeClick_reportsEventForItsPreference()142     public void handlePreferenceTreeClick_reportsEventForItsPreference() {
143         when(mSwitchPreference.getKey()).thenReturn(
144                 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
145 
146         mController.handlePreferenceTreeClick(mSwitchPreference);
147 
148         verify(mMetricsFeatureProvider).action(any(), eq(ACTION_AMBIENT_DISPLAY));
149     }
150 
151     @Test
handlePreferenceTreeClick_doesntReportEventForOtherPreferences()152     public void handlePreferenceTreeClick_doesntReportEventForOtherPreferences() {
153         when(mSwitchPreference.getKey()).thenReturn("some_other_key");
154 
155         mController.handlePreferenceTreeClick(mSwitchPreference);
156 
157         verifyNoMoreInteractions(mMetricsFeatureProvider);
158     }
159 
160     @Test
isSliceableCorrectKey_returnsTrue()161     public void isSliceableCorrectKey_returnsTrue() {
162         final AmbientDisplayNotificationsPreferenceController controller =
163                 new AmbientDisplayNotificationsPreferenceController(mContext,
164                         "ambient_display_notification");
165         assertThat(controller.isSliceable()).isTrue();
166     }
167 
168     @Test
isSliceableIncorrectKey_returnsFalse()169     public void isSliceableIncorrectKey_returnsFalse() {
170         final AmbientDisplayNotificationsPreferenceController controller =
171                 new AmbientDisplayNotificationsPreferenceController(mContext, "bad_key");
172         assertThat(controller.isSliceable()).isFalse();
173     }
174 
175     @Test
isPublicSlice_returnTrue()176     public void isPublicSlice_returnTrue() {
177         assertThat(mController.isPublicSlice()).isTrue();
178     }
179 }
180