• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 android.provider.Settings.Global.ZEN_MODE;
20 import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
21 import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
22 import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
23 import static android.provider.Settings.Global.ZEN_MODE_OFF;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.FragmentManager;
29 import android.app.NotificationManager;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.provider.Settings;
33 import android.support.v7.preference.Preference;
34 import android.support.v7.preference.PreferenceScreen;
35 import android.view.View;
36 import android.widget.Button;
37 
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 import com.android.settingslib.core.lifecycle.Lifecycle;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.shadows.ShadowApplication;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 public class ZenModeButtonPreferenceControllerTest {
52 
53     private ZenModeButtonPreferenceController mController;
54 
55     @Mock
56     private ZenModeBackend mBackend;
57     @Mock
58     private NotificationManager mNotificationManager;
59     @Mock
60     private Preference mockPref;
61     @Mock
62     private NotificationManager.Policy mPolicy;
63     @Mock
64     private Button mZenButtonOn;
65     @Mock
66     private Button mZenButtonOff;
67     @Mock
68     private PreferenceScreen mPreferenceScreen;
69     private ContentResolver mContentResolver;
70     private Context mContext;
71 
72     @Before
setup()73     public void setup() {
74         MockitoAnnotations.initMocks(this);
75         ShadowApplication shadowApplication = ShadowApplication.getInstance();
76         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
77 
78         mContext = shadowApplication.getApplicationContext();
79         mContentResolver = RuntimeEnvironment.application.getContentResolver();
80         mController = new ZenModeButtonPreferenceController(mContext, mock(Lifecycle.class),
81                 mock(FragmentManager.class));
82         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
83         ReflectionHelpers.setField(mController, "mBackend", mBackend);
84         ReflectionHelpers.setField(mController, "mZenButtonOn", mZenButtonOn);
85         ReflectionHelpers.setField(mController, "mZenButtonOff", mZenButtonOff);
86 
87         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
88                 mockPref);
89         mController.displayPreference(mPreferenceScreen);
90     }
91 
92     @Test
updateState_TotalSilence()93     public void updateState_TotalSilence() {
94         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
95         final Preference mockPref = mock(Preference.class);
96         mController.updateState(mockPref);
97 
98         verify(mZenButtonOn).setVisibility(View.GONE);
99         verify(mZenButtonOff).setVisibility(View.VISIBLE);
100     }
101 
102     @Test
updateState_AlarmsOnly()103     public void updateState_AlarmsOnly() {
104         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
105         final Preference mockPref = mock(Preference.class);
106         mController.updateState(mockPref);
107 
108         verify(mZenButtonOn).setVisibility(View.GONE);
109         verify(mZenButtonOff).setVisibility(View.VISIBLE);
110     }
111 
112     @Test
updateState_Priority()113     public void updateState_Priority() {
114         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
115         final Preference mockPref = mock(Preference.class);
116         mController.updateState(mockPref);
117 
118         verify(mZenButtonOn).setVisibility(View.GONE);
119         verify(mZenButtonOff).setVisibility(View.VISIBLE);
120     }
121 
122     @Test
updateState_ZenOff()123     public void updateState_ZenOff() {
124         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
125         final Preference mockPref = mock(Preference.class);
126         mController.updateState(mockPref);
127 
128         verify(mZenButtonOn).setVisibility(View.VISIBLE);
129         verify(mZenButtonOff).setVisibility(View.GONE);
130     }
131 
132     @Test
updateState_otherUserChangedZen()133     public void updateState_otherUserChangedZen() {
134         final Preference mockPref = mock(Preference.class);
135         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
136         mController.updateState(mockPref);
137         verify(mZenButtonOn).setVisibility(View.VISIBLE);
138         verify(mZenButtonOff).setVisibility(View.GONE);
139 
140         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
141         final int GUEST_USER_ID = 10;
142         mController.mSettingObserver.onChange(false,
143                 Settings.Global.getUriFor(Settings.Global.ZEN_MODE), GUEST_USER_ID);
144 
145         verify(mZenButtonOn).setVisibility(View.GONE);
146         verify(mZenButtonOff).setVisibility(View.VISIBLE);
147     }
148 }