1 /* 2 * Copyright (C) 2024 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_NOTIFICATION_MINIMALISM; 20 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS; 21 22 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 23 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.mock; 29 import static org.mockito.Mockito.when; 30 31 import android.app.admin.DevicePolicyManager; 32 import android.content.Context; 33 import android.platform.test.annotations.DisableFlags; 34 import android.platform.test.annotations.EnableFlags; 35 import android.platform.test.flag.junit.SetFlagsRule; 36 import android.provider.Settings; 37 38 import androidx.preference.Preference; 39 import androidx.preference.PreferenceScreen; 40 41 import com.android.server.notification.Flags; 42 43 import org.junit.Before; 44 import org.junit.Rule; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Answers; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 53 /** 54 * Disable FLAG_NOTIFICATION_LOCK_SCREEN_SETTINGS because this toggle will be replaced by the new 55 * settings page. 56 */ 57 @RunWith(RobolectricTestRunner.class) 58 @DisableFlags(Flags.FLAG_NOTIFICATION_LOCK_SCREEN_SETTINGS) 59 public class LockscreenNotificationMinimalismPreferenceControllerTest { 60 @Rule 61 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 62 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 63 private Context mContext; 64 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 65 private PreferenceScreen mScreen; 66 67 private LockscreenNotificationMinimalismPreferenceController mController; 68 private Preference mPreference; 69 static final int ON = 1; 70 static final int OFF = 0; 71 72 @Before setUp()73 public void setUp() { 74 MockitoAnnotations.initMocks(this); 75 doReturn(mock(DevicePolicyManager.class)).when(mContext) 76 .getSystemService(Context.DEVICE_POLICY_SERVICE); 77 mController = new LockscreenNotificationMinimalismPreferenceController(mContext, 78 "key"); 79 mPreference = new Preference(RuntimeEnvironment.application); 80 mPreference.setKey(mController.getPreferenceKey()); 81 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 82 } 83 84 @Test 85 @DisableFlags(Flags.FLAG_NOTIFICATION_MINIMALISM) display_featureFlagOff_shouldNotDisplay()86 public void display_featureFlagOff_shouldNotDisplay() { 87 // Given: lockscreen show notifications, FLAG_NOTIFICATION_MINIMALISM is disabled 88 Settings.Secure.putInt(mContext.getContentResolver(), 89 LOCK_SCREEN_SHOW_NOTIFICATIONS, ON); 90 91 // When: displayPreference 92 mController.displayPreference(mScreen); 93 94 // Then: The controller is CONDITIONALLY_UNAVAILABLE, preference is not visible 95 assertThat(mPreference.isVisible()).isFalse(); 96 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 97 } 98 99 @Test 100 @EnableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) display_featureFlagOn_shouldDisplay()101 public void display_featureFlagOn_shouldDisplay() { 102 // Given: lockscreen show notifications, FLAG_NOTIFICATION_MINIMALISM is enabled 103 Settings.Secure.putInt(mContext.getContentResolver(), 104 LOCK_SCREEN_SHOW_NOTIFICATIONS, ON); 105 106 // When: displayPreference 107 mController.displayPreference(mScreen); 108 109 // Then: The controller is AVAILABLE, preference is visible 110 assertThat(mPreference.isVisible()).isTrue(); 111 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 112 } 113 114 @Test 115 @EnableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) isChecked_settingIsOff_shouldReturnFalse()116 public void isChecked_settingIsOff_shouldReturnFalse() { 117 Settings.Secure.putInt(mContext.getContentResolver(), 118 LOCK_SCREEN_NOTIFICATION_MINIMALISM, OFF); 119 120 assertThat(mController.isChecked()).isFalse(); 121 } 122 123 @Test 124 @EnableFlags(com.android.server.notification.Flags.FLAG_NOTIFICATION_MINIMALISM) isChecked_settingIsOn_shouldReturnTrue()125 public void isChecked_settingIsOn_shouldReturnTrue() { 126 Settings.Secure.putInt(mContext.getContentResolver(), 127 LOCK_SCREEN_NOTIFICATION_MINIMALISM, ON); 128 129 assertThat(mController.isChecked()).isTrue(); 130 } 131 } 132