• 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 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.anyInt;
25 import static org.mockito.Matchers.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.provider.Settings;
33 import android.support.v14.preference.SwitchPreference;
34 
35 import com.android.internal.hardware.AmbientDisplayConfiguration;
36 import com.android.settings.search.InlinePayload;
37 import com.android.settings.search.InlineSwitchPayload;
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 import com.android.settings.TestConfig;
40 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
41 import com.android.settings.testutils.shadow.ShadowSecureSettings;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.annotation.Config;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
52         shadows = {ShadowSecureSettings.class})
53 public class AmbientDisplayNotificationsPreferenceControllerTest {
54 
55     @Mock Context mContext;
56     @Mock AmbientDisplayConfiguration mConfig;
57     @Mock SwitchPreference mSwitchPreference;
58     @Mock MetricsFeatureProvider mMetricsFeatureProvider;
59 
60     AmbientDisplayNotificationsPreferenceController mController;
61 
62     @Before
setUp()63     public void setUp() throws Exception {
64         MockitoAnnotations.initMocks(this);
65         mController = new AmbientDisplayNotificationsPreferenceController(mContext, mConfig,
66                 mMetricsFeatureProvider);
67     }
68 
69     @Test
updateState_enabled()70     public void updateState_enabled() throws Exception {
71         when(mConfig.pulseOnNotificationEnabled(anyInt()))
72                 .thenReturn(true);
73 
74         mController.updateState(mSwitchPreference);
75 
76         verify(mSwitchPreference).setChecked(true);
77     }
78 
79     @Test
updateState_disabled()80     public void updateState_disabled() throws Exception {
81         when(mConfig.pulseOnNotificationEnabled(anyInt()))
82                 .thenReturn(false);
83 
84         mController.updateState(mSwitchPreference);
85 
86         verify(mSwitchPreference).setChecked(false);
87     }
88 
89     @Test
onPreferenceChange_enable()90     public void onPreferenceChange_enable() throws Exception {
91         mController.onPreferenceChange(mSwitchPreference, true);
92 
93         assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ENABLED, -1))
94                 .isEqualTo(1);
95     }
96 
97     @Test
onPreferenceChange_disable()98     public void onPreferenceChange_disable() throws Exception {
99         mController.onPreferenceChange(mSwitchPreference, false);
100 
101         assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ENABLED, -1))
102                 .isEqualTo(0);
103     }
104 
105     @Test
isAvailable_available()106     public void isAvailable_available() throws Exception {
107         when(mConfig.pulseOnNotificationAvailable())
108                 .thenReturn(true);
109 
110         assertThat(mController.isAvailable()).isTrue();
111     }
112 
113     @Test
isAvailable_unavailable()114     public void isAvailable_unavailable() throws Exception {
115         when(mConfig.pulseOnNotificationAvailable())
116                 .thenReturn(false);
117 
118         assertThat(mController.isAvailable()).isFalse();
119     }
120 
121     @Test
handlePreferenceTreeClick_reportsEventForItsPreference()122     public void handlePreferenceTreeClick_reportsEventForItsPreference() throws Exception {
123         when(mSwitchPreference.getKey()).thenReturn(
124                 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
125 
126         mController.handlePreferenceTreeClick(mSwitchPreference);
127 
128         verify(mMetricsFeatureProvider).action(any(), eq(ACTION_AMBIENT_DISPLAY));
129     }
130 
131     @Test
handlePreferenceTreeClick_doesntReportEventForOtherPreferences()132     public void handlePreferenceTreeClick_doesntReportEventForOtherPreferences() throws Exception {
133         when(mSwitchPreference.getKey()).thenReturn("some_other_key");
134 
135         mController.handlePreferenceTreeClick(mSwitchPreference);
136 
137         verifyNoMoreInteractions(mMetricsFeatureProvider);
138     }
139 
140     @Test
testPreferenceController_ProperResultPayloadType()141     public void testPreferenceController_ProperResultPayloadType() {
142         assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
143     }
144 
145     @Test
146     @Config(shadows = ShadowSecureSettings.class)
testSetValue_updatesCorrectly()147     public void testSetValue_updatesCorrectly() {
148         int newValue = 1;
149         ContentResolver resolver = mContext.getContentResolver();
150         Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ENABLED, 0);
151 
152         ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
153         int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ENABLED, 1);
154 
155         assertThat(updatedValue).isEqualTo(newValue);
156     }
157 
158     @Test
159     @Config(shadows = ShadowSecureSettings.class)
testGetValue_correctValueReturned()160     public void testGetValue_correctValueReturned() {
161         int currentValue = 1;
162         ContentResolver resolver = mContext.getContentResolver();
163         Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ENABLED, currentValue);
164 
165         int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
166 
167         assertThat(newValue).isEqualTo(currentValue);
168     }
169 }
170