1 /* 2 * Copyright (C) 2017 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 com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.when; 26 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.pm.ApplicationInfo; 30 import android.content.pm.PackageManager; 31 import android.hardware.display.AmbientDisplayConfiguration; 32 import android.os.PowerManager; 33 import android.provider.Settings; 34 35 import com.android.internal.R; 36 import com.android.settings.testutils.shadow.ShadowSecureSettings; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(shadows = ShadowSecureSettings.class) 49 public class AmbientDisplayAlwaysOnPreferenceControllerTest { 50 51 private static final String TEST_PACKAGE = "com.android.test"; 52 53 @Mock 54 private AmbientDisplayConfiguration mConfig; 55 @Mock 56 private PackageManager mPackageManager; 57 @Mock 58 private PowerManager mPowerManager; 59 @Mock 60 private ApplicationInfo mApplicationInfo; 61 62 private Context mContext; 63 64 private ContentResolver mContentResolver; 65 66 private AmbientDisplayAlwaysOnPreferenceController mController; 67 68 @Before setUp()69 public void setUp() throws Exception { 70 MockitoAnnotations.initMocks(this); 71 mContext = spy(RuntimeEnvironment.application); 72 mContentResolver = mContext.getContentResolver(); 73 mController = new AmbientDisplayAlwaysOnPreferenceController(mContext, "key"); 74 mController.setConfig(mConfig); 75 76 mApplicationInfo.uid = 1; 77 when(mContext.getString(R.string.config_systemWellbeing)).thenReturn(TEST_PACKAGE); 78 79 when(mContext.getPackageManager()).thenReturn(mPackageManager); 80 doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo( 81 TEST_PACKAGE, /* flag= */0); 82 83 doReturn(mPowerManager).when(mContext).getSystemService(PowerManager.class); 84 when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt())) 85 .thenReturn(false); 86 } 87 88 @Test getAvailabilityStatus_available()89 public void getAvailabilityStatus_available() { 90 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(true); 91 92 assertThat(mController.getAvailabilityStatus()).isEqualTo( 93 AmbientDisplayAlwaysOnPreferenceController.AVAILABLE); 94 } 95 96 @Test getAvailabilityStatus_disabled_unsupported()97 public void getAvailabilityStatus_disabled_unsupported() { 98 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 99 100 assertThat(mController.getAvailabilityStatus()).isEqualTo( 101 AmbientDisplayAlwaysOnPreferenceController.UNSUPPORTED_ON_DEVICE); 102 } 103 104 @Test isChecked_enabled()105 public void isChecked_enabled() { 106 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(true); 107 108 assertThat(mController.isChecked()).isTrue(); 109 } 110 111 @Test isChecked_disabled()112 public void isChecked_disabled() { 113 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 114 115 assertThat(mController.isChecked()).isFalse(); 116 } 117 118 @Test setChecked_enabled()119 public void setChecked_enabled() { 120 mController.setChecked(true); 121 122 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 123 .isEqualTo(1); 124 } 125 126 @Test setChecked_disabled()127 public void setChecked_disabled() { 128 mController.setChecked(false); 129 130 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 131 .isEqualTo(0); 132 } 133 134 @Test isPublicSliceCorrectKey_returnsTrue()135 public void isPublicSliceCorrectKey_returnsTrue() { 136 final AmbientDisplayAlwaysOnPreferenceController controller = 137 new AmbientDisplayAlwaysOnPreferenceController(mContext, 138 "ambient_display_always_on"); 139 assertThat(controller.isPublicSlice()).isTrue(); 140 } 141 142 @Test isPublicSliceIncorrectKey_returnsFalse()143 public void isPublicSliceIncorrectKey_returnsFalse() { 144 final AmbientDisplayAlwaysOnPreferenceController controller = 145 new AmbientDisplayAlwaysOnPreferenceController(mContext, "bad_key"); 146 assertThat(controller.isPublicSlice()).isFalse(); 147 } 148 149 @Test isSliceable_returnTrue()150 public void isSliceable_returnTrue() { 151 assertThat(mController.isSliceable()).isTrue(); 152 } 153 154 @Test isAodSuppressedByBedtime_bedTimeModeOn_returnTrue()155 public void isAodSuppressedByBedtime_bedTimeModeOn_returnTrue() { 156 when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt())) 157 .thenReturn(true); 158 159 assertThat(AmbientDisplayAlwaysOnPreferenceController 160 .isAodSuppressedByBedtime(mContext)).isTrue(); 161 } 162 163 @Test isAodSuppressedByBedtime_bedTimeModeOff_returnFalse()164 public void isAodSuppressedByBedtime_bedTimeModeOff_returnFalse() { 165 assertThat(AmbientDisplayAlwaysOnPreferenceController 166 .isAodSuppressedByBedtime(mContext)).isFalse(); 167 } 168 169 @Test isAodSuppressedByBedtime_notFoundWellbeingPackage_returnFalse()170 public void isAodSuppressedByBedtime_notFoundWellbeingPackage_returnFalse() 171 throws PackageManager.NameNotFoundException { 172 when(mPackageManager.getApplicationInfo(TEST_PACKAGE, /* flag= */0)).thenThrow( 173 new PackageManager.NameNotFoundException()); 174 175 assertThat(AmbientDisplayAlwaysOnPreferenceController 176 .isAodSuppressedByBedtime(mContext)).isFalse(); 177 } 178 179 @Test getSummary_bedTimeModeOn_shouldReturnUnavailableSummary()180 public void getSummary_bedTimeModeOn_shouldReturnUnavailableSummary() { 181 when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt())) 182 .thenReturn(true); 183 184 final CharSequence summary = mController.getSummary(); 185 assertThat(summary).isEqualTo(mContext.getString( 186 com.android.settings.R.string.aware_summary_when_bedtime_on)); 187 } 188 } 189