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