Home | History | Annotate | Download | only in notification
      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 
     17 package com.android.settings.notification;
     18 
     19 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
     20 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
     21 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
     22 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
     23 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
     24 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
     25 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
     26 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
     27 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
     28 
     29 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
     30         .ACTION_ZEN_SOUND_AND_VIS_EFFECTS;
     31 
     32 import static org.junit.Assert.assertTrue;
     33 import static org.mockito.ArgumentMatchers.anyBoolean;
     34 import static org.mockito.ArgumentMatchers.anyInt;
     35 import static org.mockito.ArgumentMatchers.eq;
     36 import static org.mockito.ArgumentMatchers.nullable;
     37 import static org.mockito.Mockito.mock;
     38 import static org.mockito.Mockito.never;
     39 import static org.mockito.Mockito.verify;
     40 import static org.mockito.Mockito.when;
     41 
     42 import android.app.NotificationManager;
     43 import android.content.Context;
     44 import android.support.v7.preference.PreferenceScreen;
     45 
     46 import com.android.settings.testutils.FakeFeatureFactory;
     47 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     48 import com.android.settings.widget.DisabledCheckBoxPreference;
     49 import com.android.settings.widget.RadioButtonPreference;
     50 import com.android.settingslib.core.lifecycle.Lifecycle;
     51 
     52 import org.junit.Before;
     53 import org.junit.Test;
     54 import org.junit.runner.RunWith;
     55 import org.mockito.Mock;
     56 import org.mockito.MockitoAnnotations;
     57 import org.robolectric.shadows.ShadowApplication;
     58 import org.robolectric.util.ReflectionHelpers;
     59 
     60 @RunWith(SettingsRobolectricTestRunner.class)
     61 public class ZenModeVisEffectsAllPreferenceControllerTest {
     62     private ZenModeVisEffectsAllPreferenceController mController;
     63 
     64     @Mock
     65     private ZenModeBackend mBackend;
     66     @Mock
     67     private ZenCustomRadioButtonPreference mockPref;
     68     private Context mContext;
     69     private FakeFeatureFactory mFeatureFactory;
     70     @Mock
     71     private PreferenceScreen mScreen;
     72     @Mock NotificationManager mNotificationManager;
     73 
     74     private static final String PREF_KEY = "main_pref";
     75 
     76     @Before
     77     public void setup() {
     78         MockitoAnnotations.initMocks(this);
     79         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     80         mContext = shadowApplication.getApplicationContext();
     81         mFeatureFactory = FakeFeatureFactory.setupForTest();
     82         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
     83         when(mNotificationManager.getNotificationPolicy()).thenReturn(
     84                 mock(NotificationManager.Policy.class));
     85         mController = new ZenModeVisEffectsAllPreferenceController(
     86                 mContext, mock(Lifecycle.class), PREF_KEY);
     87         ReflectionHelpers.setField(mController, "mBackend", mBackend);
     88 
     89         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
     90         mController.displayPreference(mScreen);
     91     }
     92 
     93     @Test
     94     public void isAvailable() {
     95         assertTrue(mController.isAvailable());
     96     }
     97 
     98     @Test
     99     public void updateState_notChecked() {
    100         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
    101         mController.updateState(mockPref);
    102 
    103         verify(mockPref).setChecked(false);
    104     }
    105 
    106     @Test
    107     public void updateState_checked() {
    108         int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
    109                 | SUPPRESSED_EFFECT_SCREEN_ON
    110                 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
    111                 | SUPPRESSED_EFFECT_AMBIENT
    112                 | SUPPRESSED_EFFECT_STATUS_BAR
    113                 | SUPPRESSED_EFFECT_BADGE
    114                 | SUPPRESSED_EFFECT_LIGHTS
    115                 | SUPPRESSED_EFFECT_PEEK
    116                 | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
    117         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
    118         mController.updateState(mockPref);
    119 
    120         verify(mockPref).setChecked(true);
    121     }
    122 
    123     @Test
    124     public void onPreferenceChanged_checkedTrue() {
    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, 1);
    135         mController.onRadioButtonClick(mockPref);
    136         verify(mBackend).saveVisualEffectsPolicy(allSuppressed, true);
    137         verify(mFeatureFactory.metricsFeatureProvider).action(eq(mContext),
    138                 eq(ACTION_ZEN_SOUND_AND_VIS_EFFECTS),
    139                 eq(true));
    140     }
    141 }
    142