OSDN Git Service

Wake notifications to TogglePreferenceController
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / display / AmbientDisplayNotificationsPreferenceControllerTest.java
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
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
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
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
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
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
113     public void isAvailable_available() {
114         when(mConfig.pulseOnNotificationAvailable()).thenReturn(true);
115
116         assertThat(mController.isAvailable()).isTrue();
117     }
118
119     @Test
120     public void isAvailable_unavailable() {
121         when(mConfig.pulseOnNotificationAvailable()).thenReturn(false);
122
123         assertThat(mController.isAvailable()).isFalse();
124     }
125
126     @Test
127     public void isChecked_checked_shouldReturnTrue() {
128         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(true);
129
130         assertThat(mController.isChecked()).isTrue();
131     }
132
133     @Test
134     public void isChecked_checked_shouldReturnFalse() {
135         when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(false);
136
137         assertThat(mController.isChecked()).isFalse();
138     }
139
140     @Test
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
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
160     public void testPreferenceController_ProperResultPayloadType() {
161         assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
162     }
163
164     @Test
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
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 }