1 /*
2  * Copyright (C) 2024 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.content.Context;
25 import android.platform.test.flag.junit.SetFlagsRule;
26 import android.provider.Settings;
27 
28 import com.android.server.notification.Flags;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settings.testutils.shadow.ShadowSystemSettings;
31 
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class PoliteNotificationGlobalPreferenceControllerTest {
43     @Rule
44     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
45 
46     private static final String PREFERENCE_KEY = "preference_key";
47 
48     private Context mContext;
49     PoliteNotificationGlobalPreferenceController mController;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         setCoolDownEnabled(true);
56         assertThat(isCoolDownEnabled()).isTrue();
57         // TODO: b/291907312 - remove feature flags
58         mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
59         mController = new PoliteNotificationGlobalPreferenceController(mContext, PREFERENCE_KEY);
60     }
61 
62     @Test
isAvailable_flagEnabled_shouldReturnTrue()63     public void isAvailable_flagEnabled_shouldReturnTrue() {
64         assertThat(mController.isAvailable()).isTrue();
65         assertThat(mController.getAvailabilityStatus()).isEqualTo(
66                 BasePreferenceController.AVAILABLE);
67     }
68 
69     @Test
isAvailable_flagDisabled_shouldReturnFalse()70     public void isAvailable_flagDisabled_shouldReturnFalse() {
71         // TODO: b/291907312 - remove feature flags
72         mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
73         assertThat(mController.isAvailable()).isFalse();
74         assertThat(mController.getAvailabilityStatus()).isEqualTo(
75                 BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
76     }
77 
78     @Test
79     @Config(shadows = ShadowSystemSettings.class)
isChecked_coolDownEnabled_shouldReturnTrue()80     public void isChecked_coolDownEnabled_shouldReturnTrue() {
81         assertThat(mController.isChecked()).isTrue();
82     }
83 
84     @Test
85     @Config(shadows = ShadowSystemSettings.class)
isChecked_coolDownDisabled_shouldReturnFalse()86     public void isChecked_coolDownDisabled_shouldReturnFalse() {
87         setCoolDownEnabled(false);
88         assertThat(isCoolDownEnabled()).isFalse();
89         assertThat(mController.isChecked()).isFalse();
90     }
91 
92     @Test
93     @Config(shadows = ShadowSystemSettings.class)
setChecked_setTrue_shouldEnableCoolDown()94     public void setChecked_setTrue_shouldEnableCoolDown() {
95         setCoolDownEnabled(false);
96         assertThat(isCoolDownEnabled()).isFalse();
97 
98         mController.setChecked(true);
99         assertThat(isCoolDownEnabled()).isTrue();
100     }
101 
102     @Test
103     @Config(shadows = ShadowSystemSettings.class)
setChecked_setFalse_shouldDisableCoolDown()104     public void setChecked_setFalse_shouldDisableCoolDown() {
105         assertThat(isCoolDownEnabled()).isTrue();
106 
107         mController.setChecked(false);
108         assertThat(isCoolDownEnabled()).isFalse();
109     }
110 
setCoolDownEnabled(boolean enabled)111     private void setCoolDownEnabled(boolean enabled) {
112         Settings.System.putInt(mContext.getContentResolver(),
113                 Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF));
114     }
115 
isCoolDownEnabled()116     private boolean isCoolDownEnabled() {
117         return Settings.System.getInt(mContext.getContentResolver(),
118                 Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON;
119     }
120 }
121