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 android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS; 20 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Matchers.any; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.when; 27 28 import android.app.NotificationManager; 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.support.v7.preference.ListPreference; 33 import android.support.v7.preference.PreferenceScreen; 34 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.shadows.ShadowApplication; 44 import org.robolectric.util.ReflectionHelpers; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 public class ZenModeStarredContactsPreferenceControllerTest { 48 49 private ZenModeStarredContactsPreferenceController mCallsController; 50 private ZenModeStarredContactsPreferenceController mMessagesController; 51 52 @Mock 53 private ZenModeBackend mBackend; 54 @Mock 55 private NotificationManager mNotificationManager; 56 @Mock 57 private ListPreference mockPref; 58 @Mock 59 private NotificationManager.Policy mPolicy; 60 @Mock 61 private PreferenceScreen mPreferenceScreen; 62 @Mock 63 private Intent testIntent; 64 @Mock 65 private ComponentName mComponentName; 66 private Context mContext; 67 68 @Before 69 public void setup() { 70 MockitoAnnotations.initMocks(this); 71 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 72 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 73 74 mContext = shadowApplication.getApplicationContext(); 75 when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); 76 when(testIntent.resolveActivity(any())).thenReturn(mComponentName); 77 78 mCallsController = new ZenModeStarredContactsPreferenceController( 79 mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_CALLS); 80 ReflectionHelpers.setField(mCallsController, "mBackend", mBackend); 81 ReflectionHelpers.setField(mCallsController, "mStarredContactsIntent", testIntent); 82 when(mPreferenceScreen.findPreference(mCallsController.getPreferenceKey())) 83 .thenReturn(mockPref); 84 mCallsController.displayPreference(mPreferenceScreen); 85 86 mMessagesController = new ZenModeStarredContactsPreferenceController( 87 mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_MESSAGES); 88 ReflectionHelpers.setField(mMessagesController, "mBackend", mBackend); 89 ReflectionHelpers.setField(mMessagesController, "mStarredContactsIntent", testIntent); 90 when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey())) 91 .thenReturn(mockPref); 92 mMessagesController.displayPreference(mPreferenceScreen); 93 } 94 95 @Test 96 public void isAvailable_noCallers() { 97 when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 98 .thenReturn(false); 99 assertThat(mCallsController.isAvailable()).isFalse(); 100 } 101 102 @Test 103 public void isAvailable_anyCallers() { 104 when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 105 .thenReturn(true); 106 when(mBackend.getPriorityCallSenders()) 107 .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY); 108 109 110 assertThat(mCallsController.isAvailable()).isFalse(); 111 } 112 113 @Test 114 public void isAvailable_starredCallers() { 115 when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 116 .thenReturn(true); 117 when(mBackend.getPriorityCallSenders()) 118 .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED); 119 120 assertThat(mCallsController.isAvailable()).isTrue(); 121 } 122 123 @Test 124 public void isAvailable_noMessages() { 125 when(mBackend.isPriorityCategoryEnabled( 126 NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(false); 127 assertThat(mMessagesController.isAvailable()).isFalse(); 128 } 129 130 @Test 131 public void isAvailable_anyMessages() { 132 when(mBackend.isPriorityCategoryEnabled( 133 NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true); 134 when(mBackend.getPriorityMessageSenders()) 135 .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY); 136 137 assertThat(mMessagesController.isAvailable()).isFalse(); 138 } 139 140 @Test 141 public void isAvailable_starredMessageContacts() { 142 when(mBackend.isPriorityCategoryEnabled( 143 NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true); 144 when(mBackend.getPriorityMessageSenders()) 145 .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED); 146 147 assertThat(mMessagesController.isAvailable()).isTrue(); 148 } 149 } 150