1 /*
2  * Copyright (C) 2020 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.Global.NOTIFICATION_BUBBLES;
20 
21 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
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.junit.Assert.assertEquals;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.content.Context;
33 import android.provider.Settings;
34 
35 import androidx.preference.Preference;
36 import androidx.preference.PreferenceScreen;
37 import androidx.preference.TwoStatePreference;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Answers;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class BubbleNotificationPreferenceControllerTest {
50 
51     private Context mContext;
52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
53     private PreferenceScreen mScreen;
54 
55     private BubbleNotificationPreferenceController mController;
56     private Preference mPreference;
57 
58     private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles";
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64         mController = new BubbleNotificationPreferenceController(mContext,
65                 KEY_NOTIFICATION_BUBBLES);
66         mPreference = new Preference(RuntimeEnvironment.application);
67         mPreference.setKey(mController.getPreferenceKey());
68         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
69     }
70 
71     @Test
getAvilabilityStatus_returnsAvailable()72     public void getAvilabilityStatus_returnsAvailable() {
73         assertEquals(AVAILABLE, mController.getAvailabilityStatus());
74     }
75 
76     @Test
updateState_settingIsOn_preferenceSetChecked()77     public void updateState_settingIsOn_preferenceSetChecked() {
78         final TwoStatePreference preference = mock(TwoStatePreference.class);
79         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
80 
81         mController.updateState(preference);
82 
83         verify(preference).setChecked(true);
84     }
85 
86     @Test
updateState_settingIsOff_preferenceSetUnchecked()87     public void updateState_settingIsOff_preferenceSetUnchecked() {
88         final TwoStatePreference preference = mock(TwoStatePreference.class);
89         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
90         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
91                 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
92 
93         mController.updateState(preference);
94 
95         verify(preference).setChecked(false);
96     }
97 
98     @Test
isChecked_settingIsOff_shouldReturnFalse()99     public void isChecked_settingIsOff_shouldReturnFalse() {
100         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
101 
102         assertThat(mController.isChecked()).isFalse();
103     }
104 
105     @Test
isChecked_settingIsOn_shouldReturnTrue()106     public void isChecked_settingIsOn_shouldReturnTrue() {
107         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
108 
109         assertThat(mController.isChecked()).isTrue();
110     }
111 
112     @Test
setChecked_setFalse_disablesSetting()113     public void setChecked_setFalse_disablesSetting() {
114         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
115 
116         mController.setChecked(false);
117         int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
118                 NOTIFICATION_BUBBLES, -1);
119 
120         assertThat(updatedValue).isEqualTo(OFF);
121     }
122 
123     @Test
setChecked_setTrue_enablesSetting()124     public void setChecked_setTrue_enablesSetting() {
125         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
126 
127         mController.setChecked(true);
128         int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
129                 NOTIFICATION_BUBBLES, -1);
130 
131         assertThat(updatedValue).isEqualTo(ON);
132     }
133 
134     @Test
isSliceable_returnsFalse()135     public void isSliceable_returnsFalse() {
136         assertThat(mController.isSliceable()).isFalse();
137     }
138 }
139