1 /* 2 * Copyright (C) 2020 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.batterysaver; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assert_; 21 22 import android.app.Activity; 23 import android.app.Instrumentation; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.PowerManager; 27 import android.provider.Settings; 28 import android.util.Log; 29 30 import androidx.fragment.app.Fragment; 31 import androidx.fragment.app.FragmentActivity; 32 import androidx.test.core.app.ActivityScenario; 33 import androidx.test.core.app.ApplicationProvider; 34 import androidx.test.ext.junit.rules.ActivityScenarioRule; 35 import androidx.test.ext.junit.runners.AndroidJUnit4; 36 import androidx.test.filters.SmallTest; 37 import androidx.test.platform.app.InstrumentationRegistry; 38 39 import com.android.settings.Settings.BatterySaverSettingsActivity; 40 import com.android.settings.SettingsPreferenceFragment; 41 import com.android.settings.testutils.AdbUtils; 42 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Rule; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 49 @RunWith(AndroidJUnit4.class) 50 @SmallTest 51 public class BatterySaverButtonPreferenceControllerComponentTest { 52 private static final String TAG = 53 BatterySaverButtonPreferenceControllerComponentTest.class.getSimpleName(); 54 private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); 55 private final PowerManager mManager = 56 (PowerManager) mInstrumentation.getTargetContext().getSystemService( 57 Context.POWER_SERVICE); 58 @Rule 59 public ActivityScenarioRule<BatterySaverSettingsActivity> rule = new ActivityScenarioRule<>( 60 new Intent( 61 Settings.ACTION_BATTERY_SAVER_SETTINGS).setFlags( 62 Intent.FLAG_ACTIVITY_NEW_TASK)); 63 64 @Before setUp()65 public void setUp() throws Exception { 66 mInstrumentation.getUiAutomation().executeShellCommand("dumpsys battery unplug"); 67 mInstrumentation.getUiAutomation().executeShellCommand("settings get global low_power 0"); 68 } 69 get_battery_saver_controller(Activity activity)70 private BatterySaverButtonPreferenceController get_battery_saver_controller(Activity activity) { 71 BatterySaverButtonPreferenceController controller = 72 new BatterySaverButtonPreferenceController( 73 ApplicationProvider.getApplicationContext(), "battery_saver"); 74 Fragment f = 75 ((FragmentActivity) activity).getSupportFragmentManager().getFragments().get(0); 76 controller.displayPreference(((SettingsPreferenceFragment) f).getPreferenceScreen()); 77 return controller; 78 } 79 80 @Test test_check_battery_saver_button()81 public void test_check_battery_saver_button() throws Exception { 82 ActivityScenario scenario = rule.getScenario(); 83 scenario.onActivity(activity -> { 84 BatterySaverButtonPreferenceController controller = 85 get_battery_saver_controller(activity); 86 controller.setChecked(true); 87 checkPowerSaverMode(true); 88 89 controller.setChecked(false); 90 checkPowerSaverMode(false); 91 }); 92 } 93 94 @After tearDown()95 public void tearDown() { 96 mInstrumentation.getUiAutomation().executeShellCommand("settings get global low_power 0"); 97 mInstrumentation.getUiAutomation().executeShellCommand("dumpsys battery reset"); 98 } 99 checkPowerSaverMode(boolean enabled)100 private void checkPowerSaverMode(boolean enabled) { 101 //Check through adb. Note that this needs to be done first, or a wait and poll needs to be 102 //done to the manager.isPowerSaveMode(), because calling isPowerSaveMode immediately after 103 //setting it does not return true. It takes a while for isPowerSaveMode() to return the 104 //up-to-date value. 105 try { 106 assertThat( 107 AdbUtils.checkStringInAdbCommandOutput(TAG, "settings get global low_power", 108 null, enabled ? "1" : "0", 1000)).isTrue(); 109 } catch (Exception e) { 110 Log.e(TAG, e.getMessage()); 111 assert_().fail(); 112 } 113 114 //Check through manager 115 assertThat(mManager.isPowerSaveMode() == enabled).isTrue(); 116 } 117 118 } 119