2 * Copyright (C) 2016 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 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;
27 import android.app.NotificationManager;
28 import android.app.NotificationManager.Policy;
29 import android.content.Context;
30 import android.support.v7.preference.Preference;
32 import com.android.settings.R;
33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
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;
43 @RunWith(SettingsRobolectricTestRunner.class)
44 public class ZenModePreferenceControllerTest {
47 private Preference mPreference;
49 private NotificationManager mNotificationManager;
51 private Policy mPolicy;
53 private Context mContext;
54 private ZenModePreferenceController mController;
55 private ZenModeSettings.SummaryBuilder mSummaryBuilder;
56 private static final String KEY_ZEN_MODE = "zen_mode";
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();
72 public void isAlwaysAvailable() {
73 assertThat(mController.isAvailable()).isTrue();
77 public void updateState_automaticRuleEnabled_shouldSetSummary() {
78 when(mPreference.isEnabled()).thenReturn(true);
80 mController.updateState(mPreference);
81 verify(mPreference).setSummary(mContext.getString(R.string.zen_mode_sound_summary_off));
83 doReturn(1).when(mSummaryBuilder).getEnabledAutomaticRulesCount();
84 mController.updateState(mPreference);
85 verify(mPreference).setSummary(mSummaryBuilder.getSoundSummary());
89 public void updateState_preferenceDisabled_shouldNotSetSummary() {
90 when(mPreference.isEnabled()).thenReturn(false);
92 mController.updateState(mPreference);
94 verify(mPreference, never()).setSummary(anyString());