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 com.android.settings.testutils.FakeFeatureFactory;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
41 import androidx.preference.Preference;
42 import androidx.preference.PreferenceScreen;
44 @RunWith(RobolectricTestRunner.class)
45 public class SilentStatusBarPreferenceControllerTest {
48 private NotificationBackend mBackend;
50 private PreferenceScreen mScreen;
52 private Context mContext;
53 private SilentStatusBarPreferenceController mController;
54 private Preference mPreference;
58 MockitoAnnotations.initMocks(this);
59 mContext = RuntimeEnvironment.application;
60 mController = new SilentStatusBarPreferenceController(mContext);
61 mController.setBackend(mBackend);
62 mPreference = new Preference(mContext);
63 mPreference.setKey(mController.getPreferenceKey());
64 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
68 public void isAvailable_featureEnabled() {
69 Settings.Secure.putInt(
70 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
71 assertThat(mController.isAvailable()).isTrue();
75 public void isAvailable_featureDisabled() {
76 Settings.Secure.putInt(
77 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
78 assertThat(mController.isAvailable()).isFalse();
82 public void isChecked_settingIsOff_false() {
83 when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
84 assertThat(mController.isChecked()).isFalse();
88 public void isChecked_settingIsOn_true() {
89 when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
90 assertThat(mController.isChecked()).isTrue();
94 public void onPreferenceChange_on() {
95 mController.onPreferenceChange(mPreference, true);
96 verify(mBackend).setHideSilentStatusIcons(false);
100 public void onPreferenceChange_off() {
101 mController.onPreferenceChange(mPreference, false);
102 verify(mBackend).setHideSilentStatusIcons(true);
106 public void listenerTriggered() {
107 SilentStatusBarPreferenceController.Listener listener = mock(
108 SilentStatusBarPreferenceController.Listener.class);
109 mController.setListener(listener);
111 mController.setChecked(false);
112 verify(listener).onChange(false);
114 mController.setChecked(true);
115 verify(listener).onChange(true);