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