1 /* 2 * Copyright (C) 2020 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 com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.any; 25 import static org.mockito.Mockito.times; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 import static org.robolectric.Shadows.shadowOf; 29 30 import android.content.Context; 31 import android.content.pm.UserInfo; 32 import android.os.UserHandle; 33 import android.os.UserManager; 34 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceScreen; 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.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.shadows.ShadowUserManager; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class SilentStatusBarPreferenceControllerTest { 49 50 @Mock 51 private NotificationBackend mBackend; 52 @Mock 53 private PreferenceScreen mScreen; 54 55 private Context mContext; 56 private SilentStatusBarPreferenceController mController; 57 private Preference mPreference; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mContext = RuntimeEnvironment.application; 63 mController = new SilentStatusBarPreferenceController(mContext); 64 mController.setBackend(mBackend); 65 mPreference = new Preference(mContext); 66 mPreference.setKey(mController.getPreferenceKey()); 67 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 68 } 69 70 @Test isAvailable_systemUser_available()71 public void isAvailable_systemUser_available() { 72 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 73 } 74 75 @Test isAvailable_extraUser_available()76 public void isAvailable_extraUser_available() { 77 ShadowUserManager um = shadowOf(mContext.getSystemService(UserManager.class)); 78 um.addUser(UserHandle.myUserId(), "Another User", UserInfo.FLAG_FULL); 79 80 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 81 } 82 83 @Test isAvailable_guestUser_disabled()84 public void isAvailable_guestUser_disabled() { 85 ShadowUserManager um = shadowOf(mContext.getSystemService(UserManager.class)); 86 um.addUser(UserHandle.myUserId(), "Guest", UserInfo.FLAG_GUEST); 87 88 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER); 89 } 90 91 @Test isChecked_settingIsOff()92 public void isChecked_settingIsOff() { 93 when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false); 94 assertThat(mController.isChecked()).isFalse(); 95 } 96 97 @Test isChecked_settingIsOn()98 public void isChecked_settingIsOn() { 99 when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true); 100 assertThat(mController.isChecked()).isTrue(); 101 } 102 103 @Test setChecked_Off()104 public void setChecked_Off() { 105 mController.setChecked(false); 106 verify(mBackend, times(1)).setHideSilentStatusIcons(false); 107 } 108 109 @Test setChecked_On()110 public void setChecked_On() { 111 mController.setChecked(true); 112 verify(mBackend, times(1)).setHideSilentStatusIcons(true); 113 } 114 }