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 android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 20 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 21 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 22 23 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; 24 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 import android.content.ContentResolver; 29 import android.content.Context; 30 import android.provider.Settings; 31 32 import com.android.settings.R; 33 import com.android.settings.testutils.shadow.SettingsShadowResources; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = {SettingsShadowResources.class}) 45 public class AutoBrightnessPreferenceControllerTest { 46 47 private static final String PREFERENCE_KEY = "auto_brightness"; 48 49 private Context mContext; 50 private AutoBrightnessPreferenceController mController; 51 private ContentResolver mContentResolver; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 57 mContext = RuntimeEnvironment.application; 58 mContentResolver = mContext.getContentResolver(); 59 mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY); 60 } 61 62 @Test onPreferenceChange_TurnOnAuto_ReturnAuto()63 public void onPreferenceChange_TurnOnAuto_ReturnAuto() { 64 mController.onPreferenceChange(null, true); 65 66 final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 67 SCREEN_BRIGHTNESS_MODE_MANUAL); 68 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 69 } 70 71 @Test onPreferenceChange_TurnOffAuto_ReturnManual()72 public void onPreferenceChange_TurnOffAuto_ReturnManual() { 73 mController.onPreferenceChange(null, false); 74 75 final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 76 SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 77 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL); 78 } 79 80 @Test setChecked_updatesCorrectly()81 public void setChecked_updatesCorrectly() { 82 mController.setChecked(true); 83 84 assertThat(mController.isChecked()).isTrue(); 85 86 mController.setChecked(false); 87 88 assertThat(mController.isChecked()).isFalse(); 89 } 90 91 @Test isChecked_no()92 public void isChecked_no() { 93 Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 94 SCREEN_BRIGHTNESS_MODE_MANUAL); 95 96 assertThat(mController.isChecked()).isFalse(); 97 } 98 99 @Test isChecked_yes()100 public void isChecked_yes() { 101 Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 102 SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 103 104 assertThat(mController.isChecked()).isTrue(); 105 } 106 107 @Test getSummary_settingOn_shouldReturnOnSummary()108 public void getSummary_settingOn_shouldReturnOnSummary() { 109 mController.setChecked(true); 110 111 assertThat(mController.getSummary()) 112 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_on)); 113 } 114 115 @Test getSummary_settingOff_shouldReturnOffSummary()116 public void getSummary_settingOff_shouldReturnOffSummary() { 117 mController.setChecked(false); 118 119 assertThat(mController.getSummary()) 120 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_off)); 121 } 122 123 @Test getAvailabilityStatus_configTrueSet_shouldReturnAvailableUnsearchable()124 public void getAvailabilityStatus_configTrueSet_shouldReturnAvailableUnsearchable() { 125 SettingsShadowResources.overrideResource( 126 com.android.internal.R.bool.config_automatic_brightness_available, true); 127 128 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE); 129 } 130 131 @Test getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice()132 public void getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice() { 133 SettingsShadowResources.overrideResource( 134 com.android.internal.R.bool.config_automatic_brightness_available, false); 135 136 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 137 } 138 } 139