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 org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.FragmentManager; 24 import android.app.NotificationManager; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings; 28 import android.support.v7.preference.Preference; 29 import android.support.v7.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 import com.android.settingslib.core.lifecycle.Lifecycle; 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.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 import org.robolectric.shadows.ShadowApplication; 43 import org.robolectric.util.ReflectionHelpers; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 public class ZenModeDurationPreferenceControllerTest { 47 private ZenModeDurationPreferenceController mController; 48 49 @Mock 50 private ZenModeBackend mBackend; 51 @Mock 52 private NotificationManager mNotificationManager; 53 @Mock 54 private Preference mockPref; 55 @Mock 56 private NotificationManager.Policy mPolicy; 57 @Mock 58 private PreferenceScreen mPreferenceScreen; 59 private ContentResolver mContentResolver; 60 private Context mContext; 61 62 @Before 63 public void setup() { 64 MockitoAnnotations.initMocks(this); 65 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 66 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 67 68 mContext = shadowApplication.getApplicationContext(); 69 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 70 mController = new ZenModeDurationPreferenceController(mContext, mock(Lifecycle.class), 71 mock(FragmentManager.class)); 72 when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); 73 ReflectionHelpers.setField(mController, "mBackend", mBackend); 74 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 75 mockPref); 76 mController.displayPreference(mPreferenceScreen); 77 } 78 79 @Test 80 public void updateState_DurationForever() { 81 Settings.Global.putInt(mContentResolver, Settings.Global.ZEN_DURATION, 82 Settings.Global.ZEN_DURATION_FOREVER); 83 final Preference mockPref = mock(Preference.class); 84 mController.updateState(mockPref); 85 86 verify(mockPref).setSummary(mContext.getString(R.string.zen_mode_duration_summary_forever)); 87 } 88 89 @Test 90 public void updateState_DurationPrompt() { 91 Settings.Global.putInt(mContentResolver, Settings.Global.ZEN_DURATION, 92 Settings.Global.ZEN_DURATION_PROMPT); 93 final Preference mockPref = mock(Preference.class); 94 mController.updateState(mockPref); 95 96 verify(mockPref).setSummary(mContext.getString( 97 R.string.zen_mode_duration_summary_always_prompt)); 98 } 99 100 @Test 101 public void updateState_DurationCustom() { 102 int zenDuration = 45; 103 Settings.Global.putInt(mContentResolver, Settings.Global.ZEN_DURATION, 104 zenDuration); 105 final Preference mockPref = mock(Preference.class); 106 mController.updateState(mockPref); 107 108 verify(mockPref).setSummary(mContext.getResources().getString( 109 R.string.zen_mode_duration_summary_time_minutes, zenDuration)); 110 } 111 }