1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * 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 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18 package com.android.settings.fuelgauge.batterysaver; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.content.Context; 23 import android.provider.Settings; 24 import android.support.v14.preference.SwitchPreference; 25 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.testutils.shadow.SettingsShadowResources; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 @Config(shadows = SettingsShadowResources.class) 38 public class AutoBatterySaverPreferenceControllerTest { 39 40 private AutoBatterySaverPreferenceController mController; 41 private Context mContext; 42 private SwitchPreference mPreference; 43 44 @Before 45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 48 mContext = RuntimeEnvironment.application; 49 mPreference = new SwitchPreference(mContext); 50 mController = new AutoBatterySaverPreferenceController(mContext); 51 } 52 53 @Test 54 public void testUpdateState_lowPowerLevelZero_preferenceNotChecked() { 55 Settings.Global.putInt(mContext.getContentResolver(), 56 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 57 mController.updateState(mPreference); 58 59 assertThat(mPreference.isChecked()).isFalse(); 60 } 61 62 @Test 63 public void testUpdateState_lowPowerLevelZero_preferenceChecked() { 64 Settings.Global.putInt(mContext.getContentResolver(), 65 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15); 66 mController.updateState(mPreference); 67 68 assertThat(mPreference.isChecked()).isTrue(); 69 } 70 71 @Test 72 public void testOnPreferenceChange_turnOn_setValueNotZero() { 73 mController.onPreferenceChange(mPreference, true); 74 75 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 76 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isNotEqualTo(0); 77 } 78 79 @Test 80 public void testOnPreferenceChange_turnOff_setValueZero() { 81 mController.onPreferenceChange(mPreference, false); 82 83 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 84 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0); 85 } 86 87 @Test 88 public void testIsChecked_useDefaultValue_returnFalse() { 89 assertThat(mController.isChecked()).isFalse(); 90 } 91 } 92