2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.display;
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;
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;
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;
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;
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(shadows = ShadowSecureSettings.class)
52 public class AmbientDisplayNotificationsPreferenceControllerTest {
55 private AmbientDisplayConfiguration mConfig;
57 private SwitchPreference mSwitchPreference;
59 private MetricsFeatureProvider mMetricsFeatureProvider;
61 private Context mContext;
63 private ContentResolver mContentResolver;
65 private AmbientDisplayNotificationsPreferenceController mController;
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);
79 public void updateState_enabled() {
80 when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(true);
82 mController.updateState(mSwitchPreference);
84 verify(mSwitchPreference).setChecked(true);
88 public void updateState_disabled() {
89 when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(false);
91 mController.updateState(mSwitchPreference);
93 verify(mSwitchPreference).setChecked(false);
97 public void onPreferenceChange_enable() {
98 mController.onPreferenceChange(mSwitchPreference, true);
100 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
105 public void onPreferenceChange_disable() {
106 mController.onPreferenceChange(mSwitchPreference, false);
108 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, -1))
113 public void isAvailable_available() {
114 when(mConfig.pulseOnNotificationAvailable()).thenReturn(true);
116 assertThat(mController.isAvailable()).isTrue();
120 public void isAvailable_unavailable() {
121 when(mConfig.pulseOnNotificationAvailable()).thenReturn(false);
123 assertThat(mController.isAvailable()).isFalse();
127 public void isChecked_checked_shouldReturnTrue() {
128 when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(true);
130 assertThat(mController.isChecked()).isTrue();
134 public void isChecked_checked_shouldReturnFalse() {
135 when(mConfig.pulseOnNotificationEnabled(UserHandle.myUserId())).thenReturn(false);
137 assertThat(mController.isChecked()).isFalse();
141 public void handlePreferenceTreeClick_reportsEventForItsPreference() {
142 when(mSwitchPreference.getKey()).thenReturn(
143 AmbientDisplayNotificationsPreferenceController.KEY_AMBIENT_DISPLAY_NOTIFICATIONS);
145 mController.handlePreferenceTreeClick(mSwitchPreference);
147 verify(mMetricsFeatureProvider).action(any(), eq(ACTION_AMBIENT_DISPLAY));
151 public void handlePreferenceTreeClick_doesntReportEventForOtherPreferences() {
152 when(mSwitchPreference.getKey()).thenReturn("some_other_key");
154 mController.handlePreferenceTreeClick(mSwitchPreference);
156 verifyNoMoreInteractions(mMetricsFeatureProvider);
160 public void testPreferenceController_ProperResultPayloadType() {
161 assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
165 public void testSetValue_updatesCorrectly() {
167 Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ENABLED, 0);
169 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
171 Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ENABLED, 1);
173 assertThat(updatedValue).isEqualTo(newValue);
177 public void testGetValue_correctValueReturned() {
178 int currentValue = 1;
179 Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ENABLED, currentValue);
181 int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
183 assertThat(newValue).isEqualTo(currentValue);