1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.display; 16 17 import static com.google.common.truth.Truth.assertThat; 18 19 import static org.mockito.ArgumentMatchers.eq; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.hardware.display.ColorDisplayManager; 24 import android.location.LocationManager; 25 26 import com.android.settings.testutils.shadow.SettingsShadowResources; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mockito; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 37 @RunWith(RobolectricTestRunner.class) 38 @Config(shadows = SettingsShadowResources.class) 39 public class NightDisplayAutoModePreferenceControllerTest { 40 41 private Context mContext; 42 private NightDisplayAutoModePreferenceController mController; 43 private LocationManager mLocationManager; 44 45 @Before setUp()46 public void setUp() { 47 mContext = Mockito.spy(RuntimeEnvironment.application); 48 mLocationManager = Mockito.mock(LocationManager.class); 49 when(mLocationManager.isLocationEnabled()).thenReturn(true); 50 when(mContext.getSystemService(eq(LocationManager.class))).thenReturn(mLocationManager); 51 mController = new NightDisplayAutoModePreferenceController(mContext, 52 "night_display_auto_mode"); 53 } 54 55 @After tearDown()56 public void tearDown() { 57 SettingsShadowResources.reset(); 58 } 59 60 @Test isAvailable_configuredAvailable()61 public void isAvailable_configuredAvailable() { 62 SettingsShadowResources.overrideResource( 63 com.android.internal.R.bool.config_nightDisplayAvailable, true); 64 assertThat(mController.isAvailable()).isTrue(); 65 } 66 67 @Test isAvailable_configuredUnavailable()68 public void isAvailable_configuredUnavailable() { 69 SettingsShadowResources.overrideResource( 70 com.android.internal.R.bool.config_nightDisplayAvailable, false); 71 assertThat(mController.isAvailable()).isFalse(); 72 } 73 74 @Test onPreferenceChange_changesAutoMode()75 public void onPreferenceChange_changesAutoMode() { 76 mController.onPreferenceChange(null, 77 String.valueOf(ColorDisplayManager.AUTO_MODE_TWILIGHT)); 78 assertThat(mContext.getSystemService(ColorDisplayManager.class).getNightDisplayAutoMode()) 79 .isEqualTo(ColorDisplayManager.AUTO_MODE_TWILIGHT); 80 } 81 } 82