Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.settings.anomaly.tests;
     16 
     17 import static com.google.common.truth.Truth.assertWithMessage;
     18 
     19 import android.app.Instrumentation;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.support.test.InstrumentationRegistry;
     23 import android.support.test.runner.AndroidJUnit4;
     24 import android.support.test.uiautomator.By;
     25 import android.support.test.uiautomator.UiDevice;
     26 import android.support.test.uiautomator.Until;
     27 import android.text.format.DateUtils;
     28 
     29 import org.junit.After;
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 
     34 /**
     35  * Functional test for bluetooth unoptimized scanning anomaly detector
     36  *
     37  * @see com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector
     38  */
     39 @RunWith(AndroidJUnit4.class)
     40 public class WakelockAnomalyTest {
     41     private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY";
     42     private static final String RES_WAKELOCK_EDITTEXT =
     43             "com.android.settings.anomaly.tester:id/wakelock_run_time";
     44     private static final String RES_WAKELOCK_BUTTON =
     45             "com.android.settings.anomaly.tester:id/wakelock_button";
     46     private static final long TIME_OUT = 3000;
     47     private UiDevice mDevice;
     48 
     49     @Before
     50     public void setUp() {
     51         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
     52         final Context context = instrumentation.getContext();
     53         mDevice = UiDevice.getInstance(instrumentation);
     54 
     55         // setup environment
     56         TestUtils.setUp(instrumentation);
     57         // start anomaly-tester app
     58         TestUtils.startAnomalyApp(context, mDevice);
     59     }
     60 
     61     @After
     62     public void tearDown() {
     63         TestUtils.tearDown(InstrumentationRegistry.getInstrumentation());
     64     }
     65 
     66     @Test
     67     public void testWakelockAnomaly_longTimeWhileRunning_report() throws InterruptedException {
     68         // Set running time
     69         final long durationMs = DateUtils.SECOND_IN_MILLIS * 15;
     70         TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
     71 
     72         // Click start button
     73         TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
     74 
     75         // Wait for its running
     76         mDevice.pressHome();
     77         // Sleeping time less than running time, so the app still holding wakelock when we check
     78         TestUtils.wait(mDevice, durationMs - TIME_OUT);
     79 
     80         // Check it in battery main page
     81         Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
     82         instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
     83         assertWithMessage("Doesn't have wakelock anomaly").that(
     84                 mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
     85                         TIME_OUT)).isNotNull();
     86     }
     87 
     88     @Test
     89     public void testWakelockAnomaly_shortTime_notReport() throws InterruptedException {
     90         // Set running time
     91         final long durationMs = DateUtils.SECOND_IN_MILLIS;
     92         TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
     93 
     94         // Click start button
     95         TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
     96 
     97         // Wait for its running
     98         mDevice.pressHome();
     99         TestUtils.wait(mDevice, durationMs);
    100 
    101         // Check it in battery main page
    102         Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    103         instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
    104         assertWithMessage("Shouldn't have wakelock anomaly").that(
    105                 mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
    106                         TIME_OUT)).isNull();
    107     }
    108 
    109     @Test
    110     public void testWakelockAnomaly_longTimeWhileNotRunning_notReport()
    111             throws InterruptedException {
    112         // Set running time
    113         final long durationMs = DateUtils.SECOND_IN_MILLIS * 10;
    114         TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
    115 
    116         // Click start button
    117         TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
    118 
    119         // Wait for its running
    120         mDevice.pressHome();
    121         // Wait more time for releasing the wakelock
    122         TestUtils.wait(mDevice, durationMs + TIME_OUT);
    123 
    124         // Check it in battery main page
    125         Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    126         instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
    127         assertWithMessage("Shouldn't have wakelock anomaly").that(
    128                 mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
    129                         TIME_OUT)).isNull();
    130     }
    131 
    132 }
    133