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 android.content.Context; 20 import android.provider.Settings; 21 import com.android.settings.SettingsRobolectricTestRunner; 22 import com.android.settings.TestConfig; 23 import com.android.settings.gestures.DoubleTapPowerPreferenceController; 24 import com.android.settings.search2.InlineSwitchPayload; 25 import com.android.settings.search2.ResultPayload; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.Answers; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.robolectric.annotation.Config; 33 import org.robolectric.shadows.ShadowApplication; 34 35 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 36 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 37 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 38 import static com.google.common.truth.Truth.assertThat; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 42 public class AutoBrightnessPreferenceControllerTest { 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private Context mContext; 45 private AutoBrightnessPreferenceController mController; 46 private final String PREFERENCE_KEY = "auto_brightness"; 47 48 @Before 49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 52 mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY); 53 } 54 55 @Test 56 public void testOnPreferenceChange_TurnOnAuto_ReturnAuto() { 57 mController.onPreferenceChange(null, true); 58 59 final int mode = Settings.System.getInt(mContext.getContentResolver(), 60 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL); 61 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 62 } 63 64 @Test 65 public void testOnPreferenceChange_TurnOffAuto_ReturnManual() { 66 mController.onPreferenceChange(null, false); 67 68 final int mode = Settings.System.getInt(mContext.getContentResolver(), 69 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 70 assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL); 71 } 72 73 @Test 74 public void testPreferenceController_ProperResultPayloadType() { 75 final Context context = ShadowApplication.getInstance().getApplicationContext(); 76 mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY); 77 ResultPayload payload = mController.getResultPayload(); 78 assertThat(payload).isInstanceOf(InlineSwitchPayload.class); 79 } 80 81 @Test 82 public void testPreferenceController_CorrectPayload() { 83 final Context context = ShadowApplication.getInstance().getApplicationContext(); 84 mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY); 85 InlineSwitchPayload payload = (InlineSwitchPayload) mController.getResultPayload(); 86 assertThat(payload.settingsUri).isEqualTo("screen_brightness_mode"); 87 assertThat(payload.settingSource).isEqualTo(ResultPayload.SettingsSource.SYSTEM); 88 assertThat(payload.valueMap.get(1)).isEqualTo(true); 89 assertThat(payload.valueMap.get(0)).isEqualTo(false); 90 } 91 } 92