OSDN Git Service

Fix Settings crash when trying to show work policy info
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / notification / SilentStatusBarPreferenceControllerTest.java
1 /*
2  * Copyright (C) 2019 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.notification;
18
19 import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
20
21 import static com.google.common.truth.Truth.assertThat;
22
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;
27
28 import android.content.Context;
29 import android.provider.Settings;
30
31 import com.android.settings.testutils.FakeFeatureFactory;
32
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;
40
41 import androidx.preference.Preference;
42 import androidx.preference.PreferenceScreen;
43
44 @RunWith(RobolectricTestRunner.class)
45 public class SilentStatusBarPreferenceControllerTest {
46
47     @Mock
48     private NotificationBackend mBackend;
49     @Mock
50     private PreferenceScreen mScreen;
51
52     private Context mContext;
53     private SilentStatusBarPreferenceController mController;
54     private Preference mPreference;
55
56     @Before
57     public void setUp() {
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);
65     }
66
67     @Test
68     public void isAvailable_featureEnabled() {
69         Settings.Secure.putInt(
70                 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
71         assertThat(mController.isAvailable()).isTrue();
72     }
73
74     @Test
75     public void isAvailable_featureDisabled() {
76         Settings.Secure.putInt(
77                 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
78         assertThat(mController.isAvailable()).isFalse();
79     }
80
81     @Test
82     public void isChecked_settingIsOff_false() {
83         when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
84         assertThat(mController.isChecked()).isFalse();
85     }
86
87     @Test
88     public void isChecked_settingIsOn_true() {
89         when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
90         assertThat(mController.isChecked()).isTrue();
91     }
92
93     @Test
94     public void onPreferenceChange_on() {
95         mController.onPreferenceChange(mPreference, true);
96         verify(mBackend).setHideSilentStatusIcons(false);
97     }
98
99     @Test
100     public void onPreferenceChange_off() {
101         mController.onPreferenceChange(mPreference, false);
102         verify(mBackend).setHideSilentStatusIcons(true);
103     }
104
105     @Test
106     public void listenerTriggered() {
107         SilentStatusBarPreferenceController.Listener listener = mock(
108                 SilentStatusBarPreferenceController.Listener.class);
109         mController.setListener(listener);
110
111         mController.setChecked(false);
112         verify(listener).onChange(false);
113
114         mController.setChecked(true);
115         verify(listener).onChange(true);
116     }
117 }
118