1 /* 2 * Copyright (C) 2019 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.Secure.SHOW_NOTIFICATION_SNOOZE; 20 21 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF; 22 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.when; 29 30 import android.app.admin.DevicePolicyManager; 31 import android.content.Context; 32 import android.provider.Settings; 33 34 import androidx.preference.Preference; 35 import androidx.preference.PreferenceScreen; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Answers; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class SnoozeNotificationPreferenceControllerTest { 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Context mContext; 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private PreferenceScreen mScreen; 53 54 private SnoozeNotificationPreferenceController mController; 55 private Preference mPreference; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 doReturn(mock(DevicePolicyManager.class)).when(mContext) 61 .getSystemService(Context.DEVICE_POLICY_SERVICE); 62 mController = new SnoozeNotificationPreferenceController(mContext, 63 "key"); 64 mPreference = new Preference(RuntimeEnvironment.application); 65 mPreference.setKey(mController.getPreferenceKey()); 66 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 67 } 68 69 @Test display_configIsTrue_shouldDisplay()70 public void display_configIsTrue_shouldDisplay() { 71 mController.displayPreference(mScreen); 72 assertThat(mPreference.isVisible()).isTrue(); 73 } 74 75 @Test isChecked_settingIsOff_shouldReturnFalse()76 public void isChecked_settingIsOff_shouldReturnFalse() { 77 Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, OFF); 78 79 assertThat(mController.isChecked()).isFalse(); 80 } 81 82 @Test isChecked_settingIsOn_shouldReturnTrue()83 public void isChecked_settingIsOn_shouldReturnTrue() { 84 Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, ON); 85 86 assertThat(mController.isChecked()).isTrue(); 87 } 88 89 @Test setChecked_setFalse_disablesSetting()90 public void setChecked_setFalse_disablesSetting() { 91 Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, ON); 92 93 mController.setChecked(false); 94 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 95 SHOW_NOTIFICATION_SNOOZE, -1); 96 97 assertThat(updatedValue).isEqualTo(OFF); 98 } 99 100 @Test setChecked_setTrue_enablesSetting()101 public void setChecked_setTrue_enablesSetting() { 102 Settings.Secure.putInt(mContext.getContentResolver(), SHOW_NOTIFICATION_SNOOZE, OFF); 103 104 mController.setChecked(true); 105 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 106 SHOW_NOTIFICATION_SNOOZE, -1); 107 108 assertThat(updatedValue).isEqualTo(ON); 109 } 110 } 111