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 import static com.google.common.truth.Truth.assertThat; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.provider.Settings; 27 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RuntimeEnvironment; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 public class AutoBrightnessPreferenceControllerTest { 38 39 private static final String PREFERENCE_KEY = "auto_brightness"; 40 41 private Context mContext; 42 private AutoBrightnessPreferenceController mController; 43 private ContentResolver mContentResolver; 44 45 @Before 46 public void setUp() { 47 MockitoAnnotations.initMocks(this); 48 49 mContext = RuntimeEnvironment.application; 50 mContentResolver = mContext.getContentResolver(); 51 mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY); 52 } 53 54 @Test 55 public void testOnPreferenceChange_TurnOnAuto_ReturnAuto() { 56 mController.onPreferenceChange(null, true); 57 58 final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 59 SCREEN_BRIGHTNESS_MODE_MANUAL); 60 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 61 } 62 63 @Test 64 public void testOnPreferenceChange_TurnOffAuto_ReturnManual() { 65 mController.onPreferenceChange(null, false); 66 67 final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 68 SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 69 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL); 70 } 71 72 @Test 73 public void testSetValue_updatesCorrectly() { 74 boolean newValue = true; 75 Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 76 SCREEN_BRIGHTNESS_MODE_MANUAL); 77 78 mController.setChecked(newValue); 79 boolean updatedValue = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, -1) 80 != SCREEN_BRIGHTNESS_MODE_MANUAL; 81 82 assertThat(updatedValue).isEqualTo(newValue); 83 } 84 85 @Test 86 public void testGetValue_correctValueReturned() { 87 Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE, 88 SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 89 90 int newValue = mController.isChecked() ? 91 SCREEN_BRIGHTNESS_MODE_AUTOMATIC 92 : SCREEN_BRIGHTNESS_MODE_MANUAL; 93 94 assertThat(newValue).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 95 } 96 97 @Test 98 public void isSliceableCorrectKey_returnsTrue() { 99 final AutoBrightnessPreferenceController controller = 100 new AutoBrightnessPreferenceController(mContext, 101 "auto_brightness"); 102 assertThat(controller.isSliceable()).isTrue(); 103 } 104 105 @Test 106 public void isSliceableIncorrectKey_returnsFalse() { 107 final AutoBrightnessPreferenceController controller = 108 new AutoBrightnessPreferenceController(mContext, "bad_key"); 109 assertThat(controller.isSliceable()).isFalse(); 110 } 111 } 112