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.Secure.NOTIFICATION_BUBBLES;
20 
21 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
22 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
23 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF;
24 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Mockito.when;
30 
31 import android.app.ActivityManager;
32 import android.content.Context;
33 import android.provider.Settings;
34 import android.widget.Switch;
35 
36 import androidx.preference.PreferenceScreen;
37 import androidx.test.core.app.ApplicationProvider;
38 
39 import com.android.settingslib.testutils.shadow.ShadowInteractionJankMonitor;
40 import com.android.settingslib.widget.MainSwitchPreference;
41 
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Answers;
47 import org.mockito.Mock;
48 import org.mockito.junit.MockitoJUnit;
49 import org.mockito.junit.MockitoRule;
50 import org.robolectric.RobolectricTestRunner;
51 import org.robolectric.annotation.Config;
52 import org.robolectric.shadow.api.Shadow;
53 import org.robolectric.shadows.ShadowActivityManager;
54 
55 @RunWith(RobolectricTestRunner.class)
56 @Config(shadows = {
57         ShadowInteractionJankMonitor.class,
58         ShadowActivityManager.class,
59 })
60 public class BubbleNotificationPreferenceControllerTest {
61     @Rule
62     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
63 
64     private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles";
65     private Context mContext;
66     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
67     private PreferenceScreen mScreen;
68 
69     @Mock
70     private Switch mSwitch;
71 
72     private BubbleNotificationPreferenceController mController;
73     private MainSwitchPreference mPreference;
74 
75     private ShadowActivityManager mActivityManager;
76 
77     @Before
setUp()78     public void setUp() {
79         mContext = ApplicationProvider.getApplicationContext();
80         mController = new BubbleNotificationPreferenceController(mContext,
81                 KEY_NOTIFICATION_BUBBLES);
82         mPreference = new MainSwitchPreference(mContext);
83         mPreference.setKey(mController.getPreferenceKey());
84         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
85         mController.displayPreference(mScreen);
86         mActivityManager = Shadow.extract(mContext.getSystemService(ActivityManager.class));
87     }
88 
89     @Test
isAvailable_lowRam_returnsUnsupported()90     public void isAvailable_lowRam_returnsUnsupported() {
91         mActivityManager.setIsLowRamDevice(true);
92         assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus());
93     }
94 
95     @Test
isAvailable_notLowRam_returnsAvailable()96     public void isAvailable_notLowRam_returnsAvailable() {
97         mActivityManager.setIsLowRamDevice(false);
98         assertEquals(AVAILABLE, mController.getAvailabilityStatus());
99     }
100 
101     @Test
updateState_settingIsOff_preferenceSetUnchecked()102     public void updateState_settingIsOff_preferenceSetUnchecked() {
103         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
104         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
105                 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
106 
107         mPreference.updateStatus(false);
108 
109         assertThat(mPreference.isChecked()).isFalse();
110     }
111 
112     @Test
onSwitchChanged_true_settingIsOff_flagShouldOn()113     public void onSwitchChanged_true_settingIsOff_flagShouldOn() {
114         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
115 
116         mController.onCheckedChanged(mSwitch, true);
117 
118         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
119                 NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON);
120     }
121 
122     @Test
onSwitchChanged_false_settingIsOn_flagShouldOff()123     public void onSwitchChanged_false_settingIsOn_flagShouldOff() {
124         Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
125 
126         mController.onCheckedChanged(mSwitch, false);
127 
128         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
129                 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
130     }
131 
132     @Test
setChecked_setFalse_disablesSetting()133     public void setChecked_setFalse_disablesSetting() {
134         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
135 
136         mController.setChecked(false);
137         int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
138                 NOTIFICATION_BUBBLES, ON);
139 
140         assertThat(updatedValue).isEqualTo(OFF);
141     }
142 
143     @Test
setChecked_setTrue_enablesSetting()144     public void setChecked_setTrue_enablesSetting() {
145         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
146 
147         mController.setChecked(true);
148         int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
149                 NOTIFICATION_BUBBLES, OFF);
150 
151         assertThat(updatedValue).isEqualTo(ON);
152     }
153 
154     @Test
isSliceable_returnsFalse()155     public void isSliceable_returnsFalse() {
156         assertThat(mController.isSliceable()).isFalse();
157     }
158 }
159