1/*
2 * Copyright (C) 2018 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
17package com.android.settings.notification;
18
19import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
20import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
22import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
23import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
24import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
25import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
26import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
27import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
28
29import static com.google.common.truth.Truth.assertThat;
30
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.verify;
33import static org.mockito.Mockito.when;
34
35import android.app.NotificationManager;
36import android.content.Context;
37import android.support.v7.preference.PreferenceScreen;
38
39import com.android.settings.R;
40import com.android.settings.testutils.SettingsRobolectricTestRunner;
41import com.android.settingslib.core.lifecycle.Lifecycle;
42import com.android.settingslib.widget.FooterPreference;
43
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49import org.robolectric.shadows.ShadowApplication;
50import org.robolectric.util.ReflectionHelpers;
51
52@RunWith(SettingsRobolectricTestRunner.class)
53public class ZenFooterPreferenceControllerTest {
54    private ZenFooterPreferenceController mController;
55
56    @Mock
57    private ZenModeBackend mBackend;
58    @Mock
59    private FooterPreference mockPref;
60    private Context mContext;
61    @Mock
62    private PreferenceScreen mScreen;
63    @Mock NotificationManager mNotificationManager;
64
65    private static final String PREF_KEY = "main_pref";
66
67    @Before
68    public void setup() {
69        MockitoAnnotations.initMocks(this);
70        ShadowApplication shadowApplication = ShadowApplication.getInstance();
71        mContext = shadowApplication.getApplicationContext();
72        shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
73        when(mNotificationManager.getNotificationPolicy()).thenReturn(
74                mock(NotificationManager.Policy.class));
75        mController = new ZenFooterPreferenceController(
76                mContext, mock(Lifecycle.class), PREF_KEY);
77        ReflectionHelpers.setField(mController, "mBackend", mBackend);
78
79        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
80    }
81
82    @Test
83    public void isAvailable_noVisEffects() {
84        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
85        assertThat(mController.isAvailable()).isTrue();
86    }
87
88    @Test
89    public void isAvailable_someVisEffects() {
90        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
91        assertThat(mController.isAvailable()).isFalse();
92    }
93
94    @Test
95    public void isAvailable_allVisEffects() {
96        int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
97                | SUPPRESSED_EFFECT_SCREEN_ON
98                | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
99                | SUPPRESSED_EFFECT_AMBIENT
100                | SUPPRESSED_EFFECT_STATUS_BAR
101                | SUPPRESSED_EFFECT_BADGE
102                | SUPPRESSED_EFFECT_LIGHTS
103                | SUPPRESSED_EFFECT_PEEK
104                | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
105        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
106        assertThat(mController.isAvailable()).isTrue();
107    }
108
109    @Test
110    public void updateSummary_noVisEffects() {
111        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
112        mController.updateState(mockPref);
113        verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_mute_footer);
114    }
115
116    @Test
117    public void getSummary_someVisEffects() {
118        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
119        mController.updateState(mockPref);
120        verify(mockPref).setTitle(null);
121    }
122
123    @Test
124    public void getSummary_allVisEffects() {
125        int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
126                | SUPPRESSED_EFFECT_SCREEN_ON
127                | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
128                | SUPPRESSED_EFFECT_AMBIENT
129                | SUPPRESSED_EFFECT_STATUS_BAR
130                | SUPPRESSED_EFFECT_BADGE
131                | SUPPRESSED_EFFECT_LIGHTS
132                | SUPPRESSED_EFFECT_PEEK
133                | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
134        mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
135        mController.updateState(mockPref);
136        verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_hide_footer);
137    }
138}
139