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.settingslib.notification;
     18 
     19 import static junit.framework.Assert.assertFalse;
     20 import static junit.framework.Assert.assertNotNull;
     21 import static junit.framework.Assert.assertNull;
     22 import static junit.framework.Assert.assertTrue;
     23 import static org.mockito.ArgumentMatchers.any;
     24 import static org.mockito.ArgumentMatchers.anyInt;
     25 import static org.mockito.ArgumentMatchers.anyLong;
     26 import static org.mockito.ArgumentMatchers.anyString;
     27 import static org.mockito.Mockito.doNothing;
     28 import static org.mockito.Mockito.doReturn;
     29 import static org.mockito.Mockito.spy;
     30 import static org.mockito.Mockito.when;
     31 
     32 import android.app.Fragment;
     33 import android.app.NotificationManager;
     34 import android.content.Context;
     35 import android.content.res.Resources;
     36 import android.net.Uri;
     37 import android.service.notification.Condition;
     38 import android.view.LayoutInflater;
     39 
     40 import com.android.settingslib.SettingsLibRobolectricTestRunner;
     41 
     42 import org.junit.Before;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 import org.mockito.Mock;
     46 import org.mockito.MockitoAnnotations;
     47 import org.robolectric.RuntimeEnvironment;
     48 
     49 @RunWith(SettingsLibRobolectricTestRunner.class)
     50 public class EnableZenModeDialogTest {
     51     private EnableZenModeDialog mController;
     52 
     53     @Mock
     54     private Context mContext;
     55     @Mock
     56     private Resources mResources;
     57     @Mock
     58     private Fragment mFragment;
     59     @Mock
     60     private NotificationManager mNotificationManager;
     61 
     62     private Context mShadowContext;
     63     private LayoutInflater mLayoutInflater;
     64     private Condition mCountdownCondition;
     65     private Condition mAlarmCondition;
     66 
     67     @Before
     68     public void setup() {
     69         MockitoAnnotations.initMocks(this);
     70         mShadowContext = RuntimeEnvironment.application;
     71         when(mContext.getApplicationContext()).thenReturn(mContext);
     72         when(mContext.getResources()).thenReturn(mResources);
     73         when(mFragment.getContext()).thenReturn(mShadowContext);
     74         mLayoutInflater = LayoutInflater.from(mShadowContext);
     75 
     76         mController = spy(new EnableZenModeDialog(mContext));
     77         mController.mContext = mContext;
     78         mController.mLayoutInflater = mLayoutInflater;
     79         mController.mForeverId =  Condition.newId(mContext).appendPath("forever").build();
     80         when(mContext.getString(com.android.internal.R.string.zen_mode_forever))
     81                 .thenReturn("testSummary");
     82         NotificationManager.Policy alarmsEnabledPolicy = new NotificationManager.Policy(
     83                 NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, 0, 0, 0);
     84         doReturn(alarmsEnabledPolicy).when(mNotificationManager).getNotificationPolicy();
     85         mController.mNotificationManager = mNotificationManager;
     86         mController.getContentView();
     87 
     88         // these methods use static calls to ZenModeConfig which would normally fail in robotests,
     89         // so instead do nothing:
     90         doNothing().when(mController).bindGenericCountdown();
     91         doReturn(null).when(mController).getTimeUntilNextAlarmCondition();
     92         doReturn(0L).when(mController).getNextAlarm();
     93         doNothing().when(mController).bindNextAlarm(any());
     94 
     95         // as a result of doing nothing above, must bind manually:
     96         Uri alarm =  Condition.newId(mContext).appendPath("alarm").build();
     97         mAlarmCondition = new Condition(alarm, "alarm", "", "", 0, 0, 0);
     98         Uri countdown =  Condition.newId(mContext).appendPath("countdown").build();
     99         mCountdownCondition = new Condition(countdown, "countdown", "", "", 0, 0, 0);
    100         mController.bind(mCountdownCondition,
    101                 mController.mZenRadioGroupContent.getChildAt(
    102                         EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX),
    103                 EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX);
    104         mController.bind(mAlarmCondition,
    105                 mController.mZenRadioGroupContent.getChildAt(
    106                         EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX),
    107                 EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX);
    108     }
    109 
    110     @Test
    111     public void testForeverChecked() {
    112         mController.bindConditions(mController.forever());
    113 
    114         assertTrue(mController.getConditionTagAt(EnableZenModeDialog.FOREVER_CONDITION_INDEX).rb
    115                 .isChecked());
    116         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX).rb
    117                 .isChecked());
    118         assertFalse(mController.getConditionTagAt(
    119                 EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX).rb.isChecked());
    120     }
    121 
    122     @Test
    123     public void testNoneChecked() {
    124         mController.bindConditions(null);
    125         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.FOREVER_CONDITION_INDEX).rb
    126                 .isChecked());
    127         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX).rb
    128                 .isChecked());
    129         assertFalse(mController.getConditionTagAt(
    130                 EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX).rb.isChecked());
    131     }
    132 
    133     @Test
    134     public void testAlarmChecked() {
    135         doReturn(false).when(mController).isCountdown(mAlarmCondition);
    136         doReturn(true).when(mController).isAlarm(mAlarmCondition);
    137 
    138         mController.bindConditions(mAlarmCondition);
    139         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.FOREVER_CONDITION_INDEX).rb
    140                 .isChecked());
    141         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX).rb
    142                 .isChecked());
    143         assertTrue(mController.getConditionTagAt(
    144                 EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX).rb.isChecked());
    145     }
    146 
    147     @Test
    148     public void testCountdownChecked() {
    149         doReturn(false).when(mController).isAlarm(mCountdownCondition);
    150         doReturn(true).when(mController).isCountdown(mCountdownCondition);
    151 
    152         mController.bindConditions(mCountdownCondition);
    153         assertFalse(mController.getConditionTagAt(EnableZenModeDialog.FOREVER_CONDITION_INDEX).rb
    154                 .isChecked());
    155         assertTrue(mController.getConditionTagAt(EnableZenModeDialog.COUNTDOWN_CONDITION_INDEX).rb
    156                 .isChecked());
    157         assertFalse(mController.getConditionTagAt(
    158                 EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX).rb.isChecked());
    159     }
    160 
    161     @Test
    162     public void testNoAlarmWarning() {
    163         // setup alarm
    164         long now = System.currentTimeMillis();
    165         doReturn(now + 100000).when(mController).getNextAlarm();
    166         doReturn("").when(mController).getTime(anyLong(), anyLong());
    167 
    168         // allow alarms
    169         when(mNotificationManager.getNotificationPolicy()).thenReturn(
    170                 new NotificationManager.Policy(
    171                         NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, 0, 0, 0));
    172 
    173         // alarm warning should be null
    174         assertNull(mController.computeAlarmWarningText(null));
    175     }
    176 
    177     @Test
    178     public void testAlarmWarning() {
    179         // setup alarm
    180         long now = System.currentTimeMillis();
    181         doReturn(now + 1000000).when(mController).getNextAlarm();
    182         doReturn("").when(mController).getTime(anyLong(), anyLong());
    183 
    184         // don't allow alarms to bypass dnd
    185         when(mNotificationManager.getNotificationPolicy()).thenReturn(
    186                 new NotificationManager.Policy(0, 0, 0, 0));
    187 
    188         // return a string if mResources is asked to retrieve a string
    189         when(mResources.getString(anyInt(), anyString())).thenReturn("");
    190 
    191         // alarm warning should NOT be null
    192         assertNotNull(mController.computeAlarmWarningText(null));
    193     }
    194 }