1 /* 2 * Copyright (C) 2023 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.LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS; 20 21 import static com.android.settings.notification.ShowOnlyUnseenNotificationsOnLockscreenPreferenceController.OFF; 22 import static com.android.settings.notification.ShowOnlyUnseenNotificationsOnLockscreenPreferenceController.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.platform.test.annotations.DisableFlags; 33 import android.platform.test.annotations.EnableFlags; 34 import android.platform.test.flag.junit.SetFlagsRule; 35 import android.provider.Settings; 36 37 import androidx.preference.Preference; 38 import androidx.preference.PreferenceScreen; 39 40 import com.android.server.notification.Flags; 41 42 import org.junit.Before; 43 import org.junit.Rule; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Answers; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.RobolectricTestRunner; 50 import org.robolectric.RuntimeEnvironment; 51 52 /** 53 * Disable FLAG_NOTIFICATION_LOCK_SCREEN_SETTINGS because this toggle will be replaced by the new 54 * settings page. 55 */ 56 @RunWith(RobolectricTestRunner.class) 57 @DisableFlags(Flags.FLAG_NOTIFICATION_LOCK_SCREEN_SETTINGS) 58 public class ShowOnlyUnseenNotificationsOnLockscreenPreferenceControllerTest { 59 @Rule 60 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 61 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 62 private Context mContext; 63 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 64 private PreferenceScreen mScreen; 65 66 private ShowOnlyUnseenNotificationsOnLockscreenPreferenceController mController; 67 private Preference mPreference; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 doReturn(mock(DevicePolicyManager.class)).when(mContext) 73 .getSystemService(Context.DEVICE_POLICY_SERVICE); 74 mController = new ShowOnlyUnseenNotificationsOnLockscreenPreferenceController(mContext, 75 "key"); 76 mPreference = new Preference(RuntimeEnvironment.application); 77 mPreference.setKey(mController.getPreferenceKey()); 78 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 79 } 80 81 @Test 82 @DisableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) display_configUnset_shouldNotDisplay()83 public void display_configUnset_shouldNotDisplay() { 84 mController.displayPreference(mScreen); 85 assertThat(mPreference.isVisible()).isFalse(); 86 } 87 88 @Test 89 @DisableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) display_configSet_showDisplay()90 public void display_configSet_showDisplay() { 91 Settings.Secure.putInt(mContext.getContentResolver(), 92 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, OFF); 93 mController.displayPreference(mScreen); 94 assertThat(mPreference.isVisible()).isTrue(); 95 } 96 97 @Test 98 @EnableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) display_configUnset_minimalismEnabled_shouldDisplay()99 public void display_configUnset_minimalismEnabled_shouldDisplay() { 100 Settings.Secure.putInt(mContext.getContentResolver(), 101 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, ON); 102 mController.displayPreference(mScreen); 103 assertThat(mPreference.isVisible()).isTrue(); 104 } 105 106 @Test isChecked_settingIsOff_shouldReturnFalse()107 public void isChecked_settingIsOff_shouldReturnFalse() { 108 Settings.Secure.putInt(mContext.getContentResolver(), 109 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, OFF); 110 111 assertThat(mController.isChecked()).isFalse(); 112 } 113 114 @Test isChecked_settingIsOn_shouldReturnTrue()115 public void isChecked_settingIsOn_shouldReturnTrue() { 116 Settings.Secure.putInt(mContext.getContentResolver(), 117 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, ON); 118 119 assertThat(mController.isChecked()).isTrue(); 120 } 121 122 @Test setChecked_setFalse_disablesSetting()123 public void setChecked_setFalse_disablesSetting() { 124 Settings.Secure.putInt(mContext.getContentResolver(), 125 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, ON); 126 127 mController.setChecked(false); 128 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 129 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, -1); 130 131 assertThat(updatedValue).isEqualTo(OFF); 132 } 133 134 @Test setChecked_setTrue_enablesSetting()135 public void setChecked_setTrue_enablesSetting() { 136 Settings.Secure.putInt(mContext.getContentResolver(), 137 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, OFF); 138 139 mController.setChecked(true); 140 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 141 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, -1); 142 143 assertThat(updatedValue).isEqualTo(ON); 144 } 145 } 146