• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Matchers.anyInt;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.os.UserHandle;
31 import android.provider.Settings;
32 import android.support.v14.preference.SwitchPreference;
33 
34 import com.android.internal.hardware.AmbientDisplayConfiguration;
35 import com.android.settings.search.InlinePayload;
36 import com.android.settings.search.InlineSwitchPayload;
37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
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.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(shadows = ShadowSecureSettings.class)
52 public class AmbientDisplayNotificationsPreferenceControllerTest {
53 
54     @Mock
55     private AmbientDisplayConfiguration mConfig;
56     @Mock
57     private SwitchPreference mSwitchPreference;
58     @Mock
59     private MetricsFeatureProvider mMetricsFeatureProvider;
60 
61     private Context mContext;
62 
63     private ContentResolver mContentResolver;
64 
65     private AmbientDisplayNotificationsPreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = RuntimeEnvironment.application;
71         mContentResolver = mContext.getContentResolver();
72         mController = new AmbientDisplayNotificationsPreferenceController(mContext,
73                 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
74         mController.setConfig(mConfig);
75         ReflectionHelpers.setField(mController, "mMetricsFeatureProvider", mMetricsFeatureProvider);
76     }
77 
78     @Test
updateState_enabled()79     public void updateState_enabled() {
80         when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(true);
81 
82         mController.updateState(mSwitchPreference);
83 
84         verify(mSwitchPreference).setChecked(true);
85     }
86 
87     @Test
updateState_disabled()88     public void updateState_disabled() {
89         when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(false);
90 
91         mController.updateState(mSwitchPreference);
92 
93         verify(mSwitchPreference).setChecked(false);
94     }
95 
96     @Test
onPreferenceChange_enable()97     public void onPreferenceChange_enable() {
98         mController.onPreferenceChange(mSwitchPreference, true);
99 
100         assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
101             .isEqualTo(1);
102     }
103 
104     @Test
onPreferenceChange_disable()105     public void onPreferenceChange_disable() {
106         mController.onPreferenceChange(mSwitchPreference, false);
107 
108         assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
109             .isEqualTo(0);
110     }
111 
112     @Test
isAvailable_available()113     public void isAvailable_available() {
114         when(mConfig.pulseOnNotificationAvailable()).thenReturn(true);
115 
116         assertThat(mController.isAvailable()).isTrue();
117     }
118 
119     @Test
isAvailable_unavailable()120     public void isAvailable_unavailable() {
121         when(mConfig.pulseOnNotificationAvailable()).thenReturn(false);
122 
123         assertThat(mController.isAvailable()).isFalse();
124     }
125 
126     @Test
isChecked_checked_shouldReturnTrue()127     public void isChecked_checked_shouldReturnTrue() {
128         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(true);
129 
130         assertThat(mController.isChecked()).isTrue();
131     }
132 
133     @Test
isChecked_checked_shouldReturnFalse()134     public void isChecked_checked_shouldReturnFalse() {
135         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(false);
136 
137         assertThat(mController.isChecked()).isFalse();
138     }
139 
140     @Test
handlePreferenceTreeClick_reportsEventForItsPreference()141     public void handlePreferenceTreeClick_reportsEventForItsPreference() {
142         when(mSwitchPreference.getKey()).thenReturn(
143                 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
144 
145         mController.handlePreferenceTreeClick(mSwitchPreference);
146 
147         verify(mMetricsFeatureProvider).action(any(), eq(ACTION_AMBIENT_DISPLAY));
148     }
149 
150     @Test
handlePreferenceTreeClick_doesntReportEventForOtherPreferences()151     public void handlePreferenceTreeClick_doesntReportEventForOtherPreferences() {
152         when(mSwitchPreference.getKey()).thenReturn("some_other_key");
153 
154         mController.handlePreferenceTreeClick(mSwitchPreference);
155 
156         verifyNoMoreInteractions(mMetricsFeatureProvider);
157     }
158 
159     @Test
testPreferenceController_ProperResultPayloadType()160     public void testPreferenceController_ProperResultPayloadType() {
161         assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
162     }
163 
164     @Test
testSetValue_updatesCorrectly()165     public void testSetValue_updatesCorrectly() {
166         int newValue = 1;
167         Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ENABLED, 0);
168 
169         ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
170         int updatedValue =
171             Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, 1);
172 
173         assertThat(updatedValue).isEqualTo(newValue);
174     }
175 
176     @Test
testGetValue_correctValueReturned()177     public void testGetValue_correctValueReturned() {
178         int currentValue = 1;
179         Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ENABLED, currentValue);
180 
181         int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
182 
183         assertThat(newValue).isEqualTo(currentValue);
184     }
185 
186     @Test
isSliceableCorrectKey_returnsTrue()187     public void isSliceableCorrectKey_returnsTrue() {
188         final AmbientDisplayNotificationsPreferenceController controller =
189                 new AmbientDisplayNotificationsPreferenceController(mContext,
190                         "ambient_display_notification");
191         assertThat(controller.isSliceable()).isTrue();
192     }
193 
194     @Test
isSliceableIncorrectKey_returnsFalse()195     public void isSliceableIncorrectKey_returnsFalse() {
196         final AmbientDisplayNotificationsPreferenceController controller =
197                 new AmbientDisplayNotificationsPreferenceController(mContext, "bad_key");
198         assertThat(controller.isSliceable()).isFalse();
199     }
200 }
201