1 /* 2 * Copyright (C) 2020 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 android.provider.Settings.System.SCREEN_OFF_TIMEOUT; 20 21 import static androidx.test.core.app.ApplicationProvider.getApplicationContext; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.ArgumentMatchers.any; 26 import static org.mockito.ArgumentMatchers.anyInt; 27 import static org.mockito.ArgumentMatchers.isA; 28 import static org.mockito.Mockito.atLeast; 29 import static org.mockito.Mockito.doNothing; 30 import static org.mockito.Mockito.doReturn; 31 import static org.mockito.Mockito.never; 32 import static org.mockito.Mockito.spy; 33 import static org.mockito.Mockito.verify; 34 import static org.mockito.Mockito.when; 35 36 import android.app.admin.DevicePolicyManager; 37 import android.content.ContentResolver; 38 import android.content.Context; 39 import android.content.Intent; 40 import android.content.pm.PackageManager; 41 import android.content.pm.ResolveInfo; 42 import android.content.pm.ServiceInfo; 43 import android.content.res.Resources; 44 import android.provider.SearchIndexableResource; 45 import android.provider.Settings; 46 47 import androidx.preference.PreferenceScreen; 48 49 import com.android.settings.R; 50 import com.android.settings.testutils.FakeFeatureFactory; 51 import com.android.settingslib.RestrictedLockUtils; 52 import com.android.settingslib.widget.FooterPreference; 53 54 import org.junit.Before; 55 import org.junit.Test; 56 import org.junit.runner.RunWith; 57 import org.mockito.Mock; 58 import org.mockito.MockitoAnnotations; 59 import org.robolectric.RobolectricTestRunner; 60 61 import java.util.List; 62 63 @RunWith(RobolectricTestRunner.class) 64 public class ScreenTimeoutSettingsTest { 65 private static final String[] TIMEOUT_ENTRIES = new String[]{"15 secs", "30 secs"}; 66 private static final String[] TIMEOUT_VALUES = new String[]{"15000", "30000"}; 67 68 private ScreenTimeoutSettings mSettings; 69 private Context mContext; 70 private ContentResolver mContentResolver; 71 private Resources mResources; 72 73 @Mock 74 private PreferenceScreen mPreferenceScreen; 75 76 @Mock 77 AdaptiveSleepPermissionPreferenceController mPermissionPreferenceController; 78 79 @Mock 80 AdaptiveSleepPreferenceController mAdaptiveSleepPreferenceController; 81 82 @Mock 83 AdaptiveSleepCameraStatePreferenceController mAdaptiveSleepCameraStatePreferenceController; 84 85 @Mock 86 AdaptiveSleepBatterySaverPreferenceController mAdaptiveSleepBatterySaverPreferenceController; 87 88 @Mock 89 FooterPreference mDisableOptionsPreference; 90 91 @Mock 92 private PackageManager mPackageManager; 93 94 @Before setup()95 public void setup() { 96 MockitoAnnotations.initMocks(this); 97 FakeFeatureFactory.setupForTest(); 98 mContext = spy(getApplicationContext()); 99 mSettings = spy(new ScreenTimeoutSettings()); 100 mSettings.mContext = mContext; 101 mContentResolver = mContext.getContentResolver(); 102 mResources = spy(mContext.getResources()); 103 104 doReturn(mPackageManager).when(mContext).getPackageManager(); 105 when(mPackageManager.getAttentionServicePackageName()).thenReturn("some.package"); 106 when(mPackageManager.checkPermission(any(), any())).thenReturn( 107 PackageManager.PERMISSION_GRANTED); 108 final ResolveInfo attentionServiceResolveInfo = new ResolveInfo(); 109 attentionServiceResolveInfo.serviceInfo = new ServiceInfo(); 110 when(mPackageManager.resolveService(isA(Intent.class), anyInt())).thenReturn( 111 attentionServiceResolveInfo); 112 113 doReturn(TIMEOUT_ENTRIES).when(mResources).getStringArray(R.array.screen_timeout_entries); 114 doReturn(TIMEOUT_VALUES).when(mResources).getStringArray(R.array.screen_timeout_entries); 115 doReturn(true).when(mResources).getBoolean( 116 com.android.internal.R.bool.config_adaptive_sleep_available); 117 118 doReturn(null).when(mContext).getSystemService(DevicePolicyManager.class); 119 doReturn(mResources).when(mContext).getResources(); 120 121 doReturn(mResources).when(mSettings).getResources(); 122 doReturn(mContext).when(mSettings).getContext(); 123 doReturn(mPreferenceScreen).when(mSettings).getPreferenceScreen(); 124 125 mSettings.mAdaptiveSleepController = mAdaptiveSleepPreferenceController; 126 mSettings.mAdaptiveSleepPermissionController = mPermissionPreferenceController; 127 mSettings.mAdaptiveSleepCameraStatePreferenceController = 128 mAdaptiveSleepCameraStatePreferenceController; 129 mSettings.mAdaptiveSleepBatterySaverPreferenceController = 130 mAdaptiveSleepBatterySaverPreferenceController; 131 } 132 133 @Test searchIndexProvider_shouldIndexResource()134 public void searchIndexProvider_shouldIndexResource() { 135 final List<SearchIndexableResource> indexRes = 136 ScreenTimeoutSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex( 137 mContext, true /* enabled */); 138 139 assertThat(indexRes).isNotNull(); 140 assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId()); 141 } 142 143 @Test getDefaultKey_returnCurrentTimeout()144 public void getDefaultKey_returnCurrentTimeout() { 145 long timeout = Long.parseLong(TIMEOUT_VALUES[1]); 146 Settings.System.putLong(mContentResolver, SCREEN_OFF_TIMEOUT, timeout); 147 148 String key = mSettings.getDefaultKey(); 149 150 assertThat(key).isEqualTo(TIMEOUT_VALUES[1]); 151 } 152 153 @Test updateCandidates_screenAttentionAvailable_showAdaptiveSleepPreference()154 public void updateCandidates_screenAttentionAvailable_showAdaptiveSleepPreference() { 155 mSettings.updateCandidates(); 156 157 verify(mSettings.mAdaptiveSleepController).addToScreen(mPreferenceScreen); 158 } 159 160 @Test updateCandidates_screenAttentionNotAvailable_doNotShowAdaptiveSleepPreference()161 public void updateCandidates_screenAttentionNotAvailable_doNotShowAdaptiveSleepPreference() { 162 doReturn(false).when(mResources).getBoolean( 163 com.android.internal.R.bool.config_adaptive_sleep_available); 164 165 mSettings.updateCandidates(); 166 167 verify(mSettings.mAdaptiveSleepController, never()).addToScreen(mPreferenceScreen); 168 } 169 170 @Test updateCandidates_AttentionServiceNotInstalled_doNoShowAdaptiveSleepPreference()171 public void updateCandidates_AttentionServiceNotInstalled_doNoShowAdaptiveSleepPreference() { 172 when(mPackageManager.resolveService(isA(Intent.class), anyInt())).thenReturn(null); 173 174 verify(mSettings.mAdaptiveSleepController, never()).addToScreen(mPreferenceScreen); 175 } 176 177 @Test updateCandidates_enforcedAdmin_showDisabledByAdminPreference()178 public void updateCandidates_enforcedAdmin_showDisabledByAdminPreference() { 179 mSettings.mAdmin = new RestrictedLockUtils.EnforcedAdmin(); 180 mSettings.mDisableOptionsPreference = mDisableOptionsPreference; 181 doNothing().when(mSettings).setupDisabledFooterPreference(); 182 183 mSettings.updateCandidates(); 184 185 verify(mPreferenceScreen, atLeast(1)).addPreference(mDisableOptionsPreference); 186 } 187 188 @Test setDefaultKey_controlCurrentScreenTimeout()189 public void setDefaultKey_controlCurrentScreenTimeout() { 190 mSettings.setDefaultKey(TIMEOUT_VALUES[0]); 191 192 long timeout = Settings.System.getLong(mContentResolver, SCREEN_OFF_TIMEOUT, 193 30000 /* default */); 194 195 assertThat(Long.toString(timeout)).isEqualTo(TIMEOUT_VALUES[0]); 196 } 197 } 198