OSDN Git Service

dcfdea59a8a7e6ded685682fadb64d11e0203fe9
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / notification / SilentLockscreenPreferenceControllerTest.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 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;
38
39 import androidx.preference.Preference;
40 import androidx.preference.PreferenceScreen;
41
42 @RunWith(RobolectricTestRunner.class)
43 public class SilentLockscreenPreferenceControllerTest {
44
45     @Mock
46     private PreferenceScreen mScreen;
47
48     private Context mContext;
49     private SilentLockscreenPreferenceController mController;
50     private Preference mPreference;
51
52     @Before
53     public void setUp() {
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);
60     }
61
62     @Test
63     public void isAvailable_featureEnabled() {
64         Settings.Secure.putInt(
65                 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
66         assertThat(mController.isAvailable()).isTrue();
67     }
68
69     @Test
70     public void isAvailable_featureDisabled() {
71         Settings.Secure.putInt(
72                 mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
73         assertThat(mController.isAvailable()).isFalse();
74     }
75
76     @Test
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();
81     }
82
83     @Test
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();
88     }
89
90     @Test
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);
95     }
96
97     @Test
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);
102     }
103
104     @Test
105     public void listenerTriggered() {
106         SilentLockscreenPreferenceController.Listener listener = mock(
107                 SilentLockscreenPreferenceController.Listener.class);
108         mController.setListener(listener);
109
110         mController.setChecked(false);
111         verify(listener).onChange(false);
112
113         mController.setChecked(true);
114         verify(listener).onChange(true);
115     }
116 }
117