2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
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;
31 import com.android.settings.R;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
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;
45 @RunWith(SettingsRobolectricTestRunner.class)
46 public class ZenModeDurationPreferenceControllerTest {
47 private ZenModeDurationPreferenceController mController;
50 private ZenModeBackend mBackend;
52 private NotificationManager mNotificationManager;
54 private Preference mockPref;
56 private NotificationManager.Policy mPolicy;
58 private PreferenceScreen mPreferenceScreen;
59 private ContentResolver mContentResolver;
60 private Context mContext;
64 MockitoAnnotations.initMocks(this);
65 ShadowApplication shadowApplication = ShadowApplication.getInstance();
66 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
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(
76 mController.displayPreference(mPreferenceScreen);
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);
86 verify(mockPref).setSummary(mContext.getString(R.string.zen_mode_duration_summary_forever));
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);
96 verify(mockPref).setSummary(mContext.getString(
97 R.string.zen_mode_duration_summary_always_prompt));
101 public void updateState_DurationCustom() {
102 int zenDuration = 45;
103 Settings.Global.putInt(mContentResolver, Settings.Global.ZEN_DURATION,
105 final Preference mockPref = mock(Preference.class);
106 mController.updateState(mockPref);
108 verify(mockPref).setSummary(mContext.getResources().getString(
109 R.string.zen_mode_duration_summary_time_minutes, zenDuration));