2 * Copyright (C) 2019 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.notification;
19 import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
21 import static com.google.common.truth.Truth.assertThat;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import android.content.Context;
29 import android.provider.Settings;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
39 import androidx.preference.Preference;
40 import androidx.preference.PreferenceScreen;
42 @RunWith(RobolectricTestRunner.class)
43 public class SilentLockscreenPreferenceControllerTest {
46 private PreferenceScreen mScreen;
48 private Context mContext;
49 private SilentLockscreenPreferenceController mController;
50 private Preference mPreference;
54 MockitoAnnotations.initMocks(this);
55 mContext = RuntimeEnvironment.application;
56 mController = new SilentLockscreenPreferenceController(mContext);
57 mPreference = new Preference(mContext);
58 mPreference.setKey(mController.getPreferenceKey());
59 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
63 public void isAvailable_featureEnabled() {
64 Settings.Secure.putInt(
65 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
66 assertThat(mController.isAvailable()).isTrue();
70 public void isAvailable_featureDisabled() {
71 Settings.Secure.putInt(
72 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
73 assertThat(mController.isAvailable()).isFalse();
77 public void isChecked_settingIsOff_false() {
78 Settings.Secure.putInt(mContext.getContentResolver(),
79 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
80 assertThat(mController.isChecked()).isFalse();
84 public void isChecked_settingIsOn_true() {
85 Settings.Secure.putInt(mContext.getContentResolver(),
86 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
87 assertThat(mController.isChecked()).isTrue();
91 public void onPreferenceChange_on() {
92 mController.onPreferenceChange(mPreference, true);
93 assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
94 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0)).isEqualTo(1);
98 public void onPreferenceChange_off() {
99 mController.onPreferenceChange(mPreference, false);
100 assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
101 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0);
105 public void listenerTriggered() {
106 SilentLockscreenPreferenceController.Listener listener = mock(
107 SilentLockscreenPreferenceController.Listener.class);
108 mController.setListener(listener);
110 mController.setChecked(false);
111 verify(listener).onChange(false);
113 mController.setChecked(true);
114 verify(listener).onChange(true);