1 /* 2 * Copyright (C) 2019 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 org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 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 org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class AdaptiveSleepPermissionPreferenceControllerTest { 42 private Context mContext; 43 private AdaptiveSleepPermissionPreferenceController mController; 44 45 @Mock 46 private PackageManager mPackageManager; 47 @Mock 48 private PreferenceScreen mScreen; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mContext = spy(getApplicationContext()); 54 55 doReturn(mPackageManager).when(mContext).getPackageManager(); 56 when(mPackageManager.getAttentionServicePackageName()).thenReturn("some.package"); 57 when(mPackageManager.checkPermission(any(), any())).thenReturn( 58 PackageManager.PERMISSION_GRANTED); 59 60 mController = new AdaptiveSleepPermissionPreferenceController(mContext); 61 } 62 63 @Test addToScreen_normalCase_hidePreference()64 public void addToScreen_normalCase_hidePreference() { 65 mController.addToScreen(mScreen); 66 67 verify(mScreen, never()).addPreference(mController.mPreference); 68 } 69 70 @Test addToScreen_permissionNotGranted_showPreference()71 public void addToScreen_permissionNotGranted_showPreference() { 72 when(mPackageManager.checkPermission(any(), any())).thenReturn( 73 PackageManager.PERMISSION_DENIED); 74 75 mController.addToScreen(mScreen); 76 77 verify(mScreen).addPreference(mController.mPreference); 78 } 79 } 80