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 package android.os.cts.batterysaving; 17 18 import static com.android.compatibility.common.util.BatteryUtils.enableBatterySaver; 19 import static com.android.compatibility.common.util.BatteryUtils.runDumpsysBatteryReset; 20 import static com.android.compatibility.common.util.BatteryUtils.turnOnScreen; 21 import static com.android.compatibility.common.util.SystemUtil.runCommandAndPrintOnLogcat; 22 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 23 import static com.android.compatibility.common.util.TestUtils.waitUntil; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.location.LocationManager; 28 import android.os.BatteryManager; 29 import android.os.PowerManager; 30 import android.util.Log; 31 32 import androidx.test.InstrumentationRegistry; 33 34 import com.android.compatibility.common.util.BatteryUtils; 35 import com.android.compatibility.common.util.BeforeAfterRule; 36 import com.android.compatibility.common.util.OnFailureRule; 37 import com.android.compatibility.common.util.ProtoUtils; 38 import com.android.server.job.nano.JobSchedulerServiceDumpProto; 39 import com.android.server.job.nano.StateControllerProto; 40 41 import org.junit.Rule; 42 import org.junit.rules.RuleChain; 43 import org.junit.runner.Description; 44 import org.junit.runners.model.Statement; 45 46 public class BatterySavingTestBase { 47 private static final String TAG = "BatterySavingTestBase"; 48 49 public static final int DEFAULT_TIMEOUT_SECONDS = 30; 50 51 public static final boolean DEBUG = false; 52 53 protected final BroadcastRpc mRpc = new BroadcastRpc(); 54 55 private final OnFailureRule mDumpOnFailureRule = new OnFailureRule(TAG) { 56 @Override 57 protected void onTestFailure(Statement base, Description description, Throwable t) { 58 runCommandAndPrintOnLogcat(TAG, "dumpsys power"); 59 runCommandAndPrintOnLogcat(TAG, "dumpsys alarm"); 60 runCommandAndPrintOnLogcat(TAG, "dumpsys jobscheduler"); 61 runCommandAndPrintOnLogcat(TAG, "dumpsys content"); 62 runCommandAndPrintOnLogcat(TAG, "dumpsys battery"); 63 } 64 }; 65 66 private final BeforeAfterRule mInitializeAndCleanupRule = new BeforeAfterRule() { 67 @Override 68 protected void onBefore(Statement base, Description description) throws Throwable { 69 BatteryUtils.assumeBatterySaverFeature(); 70 71 turnOnScreen(true); 72 } 73 74 @Override 75 protected void onAfter(Statement base, Description description) throws Throwable { 76 runDumpsysBatteryReset(); 77 turnOnScreen(true); 78 enableBatterySaver(false); 79 } 80 }; 81 82 @Rule 83 public RuleChain Rules = RuleChain.outerRule(mInitializeAndCleanupRule) 84 .around(mDumpOnFailureRule); 85 getLogTag()86 public String getLogTag() { 87 return TAG; 88 } 89 90 /** Print a debug log on logcat. */ debug(String message)91 public void debug(String message) { 92 if (DEBUG || Log.isLoggable(TAG, Log.DEBUG)) { 93 Log.d(getLogTag(), message); 94 } 95 } 96 waitUntilAlarmForceAppStandby(boolean expected)97 public void waitUntilAlarmForceAppStandby(boolean expected) throws Exception { 98 waitUntil("Force all apps standby still " + !expected + " (alarm)", () -> 99 runShellCommand("dumpsys alarm").contains("Force all apps standby: " + expected)); 100 } 101 waitUntilJobForceAppStandby(boolean expected)102 public void waitUntilJobForceAppStandby(boolean expected) throws Exception { 103 waitUntil("Force all apps standby still " + !expected + " (job)", () -> { 104 JobSchedulerServiceDumpProto proto = ProtoUtils.getProto( 105 InstrumentationRegistry.getInstrumentation().getUiAutomation(), 106 JobSchedulerServiceDumpProto.class, 107 ProtoUtils.DUMPSYS_JOB_SCHEDULER); 108 for (StateControllerProto controllerProto : proto.controllers) { 109 if (controllerProto.hasBackground()) { 110 return controllerProto.getBackground().appStateTracker.forceAllAppsStandby 111 == expected; 112 } 113 } 114 return false; 115 }); 116 } 117 waitUntilForceBackgroundCheck(boolean expected)118 public void waitUntilForceBackgroundCheck(boolean expected) throws Exception { 119 waitUntil("Force background check still " + !expected + " (job)", () -> 120 runShellCommand("dumpsys activity").contains("mForceBackgroundCheck=" + expected)); 121 } 122 getContext()123 public static Context getContext() { 124 return InstrumentationRegistry.getContext(); 125 } 126 getPackageManager()127 public PackageManager getPackageManager() { 128 return getContext().getPackageManager(); 129 } 130 getPowerManager()131 public PowerManager getPowerManager() { 132 return getContext().getSystemService(PowerManager.class); 133 } 134 getBatteryManager()135 public BatteryManager getBatteryManager() { 136 return getContext().getSystemService(BatteryManager.class); 137 } 138 getLocationManager()139 public LocationManager getLocationManager() { 140 return getContext().getSystemService(LocationManager.class); 141 } 142 } 143