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 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 AdaptiveSleepBatterySaverPreferenceControllerTest { 42 private Context mContext; 43 private AdaptiveSleepBatterySaverPreferenceController 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 = spy(new AdaptiveSleepBatterySaverPreferenceController(mContext)); 61 when(mController.isPowerSaveMode()).thenReturn(false); 62 } 63 64 @Test addToScreen_normalCase_hidePreference()65 public void addToScreen_normalCase_hidePreference() { 66 mController.addToScreen(mScreen); 67 68 assertThat(mController.mPreference.isVisible()).isFalse(); 69 } 70 71 @Test addToScreen_powerSaveEnabled_showPreference()72 public void addToScreen_powerSaveEnabled_showPreference() { 73 when(mController.isPowerSaveMode()).thenReturn(true); 74 75 mController.addToScreen(mScreen); 76 77 assertThat(mController.mPreference.isVisible()).isTrue(); 78 } 79 } 80