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.lifecycle.Lifecycle;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = ShadowSensorPrivacyManager.class)
46 public class AdaptiveSleepCameraStatePreferenceControllerTest {
47     private Context mContext;
48     private AdaptiveSleepCameraStatePreferenceController mController;
49 
50     @Mock
51     private PackageManager mPackageManager;
52     @Mock
53     private PreferenceScreen mScreen;
54     @Mock
55     private Lifecycle mLifecycle;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mContext = spy(getApplicationContext());
61 
62         doReturn(mPackageManager).when(mContext).getPackageManager();
63         when(mPackageManager.getAttentionServicePackageName()).thenReturn("some.package");
64         when(mPackageManager.checkPermission(any(), any())).thenReturn(
65                 PackageManager.PERMISSION_GRANTED);
66 
67         mController = new AdaptiveSleepCameraStatePreferenceController(mContext, mLifecycle);
68         when(mController.isCameraLocked()).thenReturn(false);
69     }
70 
71     @Test
addToScreen_normalCase_hidePreference()72     public void addToScreen_normalCase_hidePreference() {
73         mController.addToScreen(mScreen);
74 
75         assertThat(mController.mPreference.isVisible()).isFalse();
76     }
77 
78     @Test
addToScreen_cameraIsLocked_showPreference()79     public void addToScreen_cameraIsLocked_showPreference() {
80         when(mController.isCameraLocked()).thenReturn(true);
81 
82         mController.addToScreen(mScreen);
83 
84         assertThat(mController.mPreference.isVisible()).isTrue();
85     }
86 }
87