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