1 /* 2 * Copyright (C) 2018 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.fuelgauge; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 23 import android.content.ContentResolver; 24 import android.provider.Settings; 25 26 import androidx.preference.SwitchPreference; 27 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class SmartBatteryPreferenceControllerTest { 40 41 private static final int ON = 1; 42 private static final int OFF = 0; 43 44 private SmartBatteryPreferenceController mController; 45 private SwitchPreference mPreference; 46 private ContentResolver mContentResolver; 47 private FakeFeatureFactory mFeatureFactory; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 53 mFeatureFactory = FakeFeatureFactory.setupForTest(); 54 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 55 mController = new SmartBatteryPreferenceController(RuntimeEnvironment.application); 56 mPreference = new SwitchPreference(RuntimeEnvironment.application); 57 } 58 59 @Test testUpdateState_smartBatteryOn_preferenceChecked()60 public void testUpdateState_smartBatteryOn_preferenceChecked() { 61 putSmartBatteryValue(ON); 62 63 mController.updateState(mPreference); 64 65 assertThat(mPreference.isChecked()).isTrue(); 66 } 67 68 @Test testUpdateState_smartBatteryOff_preferenceUnchecked()69 public void testUpdateState_smartBatteryOff_preferenceUnchecked() { 70 putSmartBatteryValue(OFF); 71 72 mController.updateState(mPreference); 73 74 assertThat(mPreference.isChecked()).isFalse(); 75 } 76 77 @Test testUpdateState_checkPreference_smartBatteryOn()78 public void testUpdateState_checkPreference_smartBatteryOn() { 79 mController.onPreferenceChange(mPreference, true); 80 81 assertThat(getSmartBatteryValue()).isEqualTo(ON); 82 } 83 84 @Test testUpdateState_unCheckPreference_smartBatteryOff()85 public void testUpdateState_unCheckPreference_smartBatteryOff() { 86 mController.onPreferenceChange(mPreference, false); 87 88 assertThat(getSmartBatteryValue()).isEqualTo(OFF); 89 } 90 91 @Test testGetAvailabilityStatus_smartBatterySupported_returnAvailable()92 public void testGetAvailabilityStatus_smartBatterySupported_returnAvailable() { 93 doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported(); 94 95 assertThat(mController.getAvailabilityStatus()).isEqualTo( 96 BasePreferenceController.AVAILABLE); 97 } 98 99 @Test testGetAvailabilityStatus_smartBatteryUnSupported_returnDisabled()100 public void testGetAvailabilityStatus_smartBatteryUnSupported_returnDisabled() { 101 doReturn(false).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported(); 102 103 assertThat(mController.getAvailabilityStatus()).isEqualTo( 104 BasePreferenceController.UNSUPPORTED_ON_DEVICE); 105 } 106 putSmartBatteryValue(int value)107 private void putSmartBatteryValue(int value) { 108 Settings.Global.putInt(mContentResolver, 109 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, value); 110 } 111 getSmartBatteryValue()112 private int getSmartBatteryValue() { 113 return Settings.Global.getInt(mContentResolver, 114 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON); 115 } 116 117 @Test isSliceableCorrectKey_returnsTrue()118 public void isSliceableCorrectKey_returnsTrue() { 119 final SmartBatteryPreferenceController controller = 120 new SmartBatteryPreferenceController(null); 121 assertThat(controller.isSliceable()).isTrue(); 122 } 123 124 @Test isPublicSlice_returnsTrue()125 public void isPublicSlice_returnsTrue() { 126 assertThat(mController.isPublicSlice()).isTrue(); 127 } 128 } 129