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.content.Context; 25 import android.provider.Settings; 26 import android.support.v14.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 Context mContext; 48 private FakeFeatureFactory mFeatureFactory; 49 50 51 @Before 52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 55 mFeatureFactory = FakeFeatureFactory.setupForTest(); 56 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 57 mController = new SmartBatteryPreferenceController(RuntimeEnvironment.application); 58 mPreference = new SwitchPreference(RuntimeEnvironment.application); 59 } 60 61 @Test 62 public void testUpdateState_smartBatteryOn_preferenceChecked() { 63 putSmartBatteryValue(ON); 64 65 mController.updateState(mPreference); 66 67 assertThat(mPreference.isChecked()).isTrue(); 68 } 69 70 @Test 71 public void testUpdateState_smartBatteryOff_preferenceUnchecked() { 72 putSmartBatteryValue(OFF); 73 74 mController.updateState(mPreference); 75 76 assertThat(mPreference.isChecked()).isFalse(); 77 } 78 79 @Test 80 public void testUpdateState_checkPreference_smartBatteryOn() { 81 mController.onPreferenceChange(mPreference, true); 82 83 assertThat(getSmartBatteryValue()).isEqualTo(ON); 84 } 85 86 @Test 87 public void testUpdateState_unCheckPreference_smartBatteryOff() { 88 mController.onPreferenceChange(mPreference, false); 89 90 assertThat(getSmartBatteryValue()).isEqualTo(OFF); 91 } 92 93 @Test 94 public void testGetAvailabilityStatus_smartBatterySupported_returnAvailable() { 95 doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported(); 96 97 assertThat(mController.getAvailabilityStatus()).isEqualTo( 98 BasePreferenceController.AVAILABLE); 99 } 100 101 @Test 102 public void testGetAvailabilityStatus_smartBatteryUnSupported_returnDisabled() { 103 doReturn(false).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported(); 104 105 assertThat(mController.getAvailabilityStatus()).isEqualTo( 106 BasePreferenceController.UNSUPPORTED_ON_DEVICE); 107 } 108 109 private void putSmartBatteryValue(int value) { 110 Settings.Global.putInt(mContentResolver, 111 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, value); 112 } 113 114 private int getSmartBatteryValue() { 115 return Settings.Global.getInt(mContentResolver, 116 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON); 117 } 118 119 @Test 120 public void isSliceableCorrectKey_returnsTrue() { 121 final SmartBatteryPreferenceController controller = 122 new SmartBatteryPreferenceController(mContext); 123 assertThat(controller.isSliceable()).isTrue(); 124 } 125 } 126