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_BADGING;
20 import static android.provider.Settings.Secure.SHOW_NOTIFICATION_SNOOZE;
21 
22 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF;
23 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.content.Context;
32 import android.provider.Settings;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Answers;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 import androidx.preference.Preference;
44 import androidx.preference.PreferenceScreen;
45 import androidx.preference.TwoStatePreference;
46 
47 @RunWith(RobolectricTestRunner.class)
48 public class SnoozeNotificationPreferenceControllerTest {
49 
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private Context mContext;
52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
53     private PreferenceScreen mScreen;
54 
55     private SnoozeNotificationPreferenceController mController;
56     private Preference mPreference;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         mController = new SnoozeNotificationPreferenceController(mContext,
62                 "key");
63         mPreference = new Preference(RuntimeEnvironment.application);
64         mPreference.setKey(mController.getPreferenceKey());
65         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
66     }
67 
68     @Test
display_configIsTrue_shouldDisplay()69     public void display_configIsTrue_shouldDisplay() {
70         mController.displayPreference(mScreen);
71         assertThat(mPreference.isVisible()).isTrue();
72     }
73 
74     @Test
isChecked_settingIsOff_shouldReturnFalse()75     public void isChecked_settingIsOff_shouldReturnFalse() {
76         Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, OFF);
77 
78         assertThat(mController.isChecked()).isFalse();
79     }
80 
81     @Test
isChecked_settingIsOn_shouldReturnTrue()82     public void isChecked_settingIsOn_shouldReturnTrue() {
83         Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, ON);
84 
85         assertThat(mController.isChecked()).isTrue();
86     }
87 
88     @Test
setChecked_setFalse_disablesSetting()89     public void setChecked_setFalse_disablesSetting() {
90         Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, ON);
91 
92         mController.setChecked(false);
93         int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
94                 SHOW_NOTIFICATION_SNOOZE, -1);
95 
96         assertThat(updatedValue).isEqualTo(OFF);
97     }
98 
99     @Test
setChecked_setTrue_enablesSetting()100     public void setChecked_setTrue_enablesSetting() {
101         Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, OFF);
102 
103         mController.setChecked(true);
104         int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
105                 SHOW_NOTIFICATION_SNOOZE, -1);
106 
107         assertThat(updatedValue).isEqualTo(ON);
108     }
109 }
110