1 /* 2 * Copyright (C) 2022 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.display; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.res.Resources; 27 import android.os.UserHandle; 28 import android.os.UserManager; 29 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 @RunWith(AndroidJUnit4.class) 39 public class ScreenSaverPreferenceControllerTest { 40 @Mock 41 private Context mContext; 42 @Mock 43 private Resources mResources; 44 @Mock 45 private UserManager mUserManager; 46 47 private ScreenSaverPreferenceController mController; 48 49 private final String mPrefKey = "test_screensaver"; 50 51 @Before setup()52 public void setup() { 53 MockitoAnnotations.initMocks(this); 54 55 when(mContext.getResources()).thenReturn(mResources); 56 when(mContext.getSystemServiceName(UserManager.class)) 57 .thenReturn(Context.USER_SERVICE); 58 when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 59 when(mUserManager.getMainUser()).thenReturn(UserHandle.of(0)); 60 when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext); 61 62 mController = new ScreenSaverPreferenceController(mContext, mPrefKey); 63 } 64 65 @Test isAvailable_dreamsEnabledForAllUsers_shouldBeTrueForMainUser()66 public void isAvailable_dreamsEnabledForAllUsers_shouldBeTrueForMainUser() { 67 when(mResources.getBoolean( 68 com.android.internal.R.bool.config_dreamsSupported)).thenReturn(true); 69 when(mResources.getBoolean( 70 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser)) 71 .thenReturn(false); 72 when(mUserManager.isUserForeground()).thenReturn(true); 73 assertTrue(mController.isAvailable()); 74 } 75 76 @Test isAvailable_dreamsEnabledForAllUsers_shouldBeTrueForNonMainUser()77 public void isAvailable_dreamsEnabledForAllUsers_shouldBeTrueForNonMainUser() { 78 when(mResources.getBoolean( 79 com.android.internal.R.bool.config_dreamsSupported)).thenReturn(true); 80 when(mResources.getBoolean( 81 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser)) 82 .thenReturn(false); 83 when(mUserManager.isUserForeground()).thenReturn(false); 84 assertTrue(mController.isAvailable()); 85 } 86 87 @Test isAvailable_dreamsDisabled_shouldBeFalseForMainUser()88 public void isAvailable_dreamsDisabled_shouldBeFalseForMainUser() { 89 when(mResources.getBoolean( 90 com.android.internal.R.bool.config_dreamsSupported)).thenReturn(false); 91 when(mResources.getBoolean( 92 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser)) 93 .thenReturn(false); 94 when(mUserManager.isUserForeground()).thenReturn(true); 95 assertFalse(mController.isAvailable()); 96 } 97 98 @Test isAvailable_dreamsOnlyEnabledForDockUser_shouldBeTrueForMainUser()99 public void isAvailable_dreamsOnlyEnabledForDockUser_shouldBeTrueForMainUser() { 100 when(mResources.getBoolean( 101 com.android.internal.R.bool.config_dreamsSupported)).thenReturn(true); 102 when(mResources.getBoolean( 103 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser)) 104 .thenReturn(true); 105 when(mUserManager.isUserForeground()).thenReturn(true); 106 assertTrue(mController.isAvailable()); 107 } 108 109 @Test isAvailable_dreamsOnlyEnabledForDockUser_shouldBeFalseForNonMainUser()110 public void isAvailable_dreamsOnlyEnabledForDockUser_shouldBeFalseForNonMainUser() { 111 when(mResources.getBoolean( 112 com.android.internal.R.bool.config_dreamsSupported)).thenReturn(true); 113 when(mResources.getBoolean( 114 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser)) 115 .thenReturn(true); 116 when(mUserManager.isUserForeground()).thenReturn(false); 117 assertFalse(mController.isAvailable()); 118 } 119 } 120