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