1 /* 2 * Copyright (C) 2021 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 androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = ShadowSensorPrivacyManager.class) 45 public class AdaptiveSleepCameraStatePreferenceControllerTest { 46 private Context mContext; 47 private AdaptiveSleepCameraStatePreferenceController mController; 48 49 @Mock 50 private PackageManager mPackageManager; 51 @Mock 52 private PreferenceScreen mScreen; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mContext = spy(getApplicationContext()); 58 59 doReturn(mPackageManager).when(mContext).getPackageManager(); 60 when(mPackageManager.getAttentionServicePackageName()).thenReturn("some.package"); 61 when(mPackageManager.checkPermission(any(), any())).thenReturn( 62 PackageManager.PERMISSION_GRANTED); 63 64 mController = new AdaptiveSleepCameraStatePreferenceController(mContext); 65 when(mController.isCameraLocked()).thenReturn(false); 66 } 67 68 @Test addToScreen_normalCase_hidePreference()69 public void addToScreen_normalCase_hidePreference() { 70 mController.addToScreen(mScreen); 71 72 assertThat(mController.mPreference.isVisible()).isFalse(); 73 } 74 75 @Test addToScreen_cameraIsLocked_showPreference()76 public void addToScreen_cameraIsLocked_showPreference() { 77 when(mController.isCameraLocked()).thenReturn(true); 78 79 mController.addToScreen(mScreen); 80 81 assertThat(mController.mPreference.isVisible()).isTrue(); 82 } 83 } 84