Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2016 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.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Matchers.anyString;
     21 import static org.mockito.Mockito.doReturn;
     22 import static org.mockito.Mockito.never;
     23 import static org.mockito.Mockito.spy;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.app.NotificationManager;
     28 import android.app.NotificationManager.Policy;
     29 import android.content.Context;
     30 import android.support.v7.preference.Preference;
     31 
     32 import com.android.settings.R;
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.shadows.ShadowApplication;
     41 import org.robolectric.util.ReflectionHelpers;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 public class ZenModePreferenceControllerTest {
     45 
     46     @Mock
     47     private Preference mPreference;
     48     @Mock
     49     private NotificationManager mNotificationManager;
     50     @Mock
     51     private Policy mPolicy;
     52 
     53     private Context mContext;
     54     private ZenModePreferenceController mController;
     55     private ZenModeSettings.SummaryBuilder mSummaryBuilder;
     56     private static final String KEY_ZEN_MODE = "zen_mode";
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     62         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
     63         mContext = shadowApplication.getApplicationContext();
     64         mController = new ZenModePreferenceController(mContext, null, KEY_ZEN_MODE);
     65         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
     66         mSummaryBuilder = spy(new ZenModeSettings.SummaryBuilder(mContext));
     67         ReflectionHelpers.setField(mController, "mSummaryBuilder", mSummaryBuilder);
     68         doReturn(0).when(mSummaryBuilder).getEnabledAutomaticRulesCount();
     69     }
     70 
     71     @Test
     72     public void isAlwaysAvailable() {
     73         assertThat(mController.isAvailable()).isTrue();
     74     }
     75 
     76     @Test
     77     public void updateState_automaticRuleEnabled_shouldSetSummary() {
     78         when(mPreference.isEnabled()).thenReturn(true);
     79 
     80         mController.updateState(mPreference);
     81         verify(mPreference).setSummary(mContext.getString(R.string.zen_mode_sound_summary_off));
     82 
     83         doReturn(1).when(mSummaryBuilder).getEnabledAutomaticRulesCount();
     84         mController.updateState(mPreference);
     85         verify(mPreference).setSummary(mSummaryBuilder.getSoundSummary());
     86     }
     87 
     88     @Test
     89     public void updateState_preferenceDisabled_shouldNotSetSummary() {
     90         when(mPreference.isEnabled()).thenReturn(false);
     91 
     92         mController.updateState(mPreference);
     93 
     94         verify(mPreference, never()).setSummary(anyString());
     95     }
     96 }
     97