diff options
author | jackqdyulei <jackqdyulei@google.com> | 2017-08-17 15:06:06 -0700 |
---|---|---|
committer | jackqdyulei <jackqdyulei@google.com> | 2017-08-25 14:02:39 -0700 |
commit | d8bfc742569ca4c4697c788f03dfb504037f846c (patch) | |
tree | 28667072a799f9c872ac92e743c9b8510de38edb /tests/anomaly-tester | |
parent | e4298941b4fa1a3cb5ffd9b8e58f06096d8d2b84 (diff) | |
download | Settings-d8bfc742569ca4c4697c788f03dfb504037f846c.tar.gz |
Add functional test for wakelock anomaly detector
Bug: 63815938
Test: runtest -x WakelockAnomalyTest
Change-Id: I4d10d5be8b38264f29c67c2c945fdbbd1090430d
Diffstat (limited to 'tests/anomaly-tester')
-rw-r--r-- | tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java new file mode 100644 index 00000000000..a2f38048977 --- /dev/null +++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.android.settings.anomaly.tests; + +import static com.google.common.truth.Truth.assertWithMessage; + +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.Until; +import android.text.format.DateUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Functional test for bluetooth unoptimized scanning anomaly detector + * + * @see com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector + */ +@RunWith(AndroidJUnit4.class) +public class WakelockAnomalyTest { + private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY"; + private static final String RES_WAKELOCK_EDITTEXT = + "com.android.settings.anomaly.tester:id/wakelock_run_time"; + private static final String RES_WAKELOCK_BUTTON = + "com.android.settings.anomaly.tester:id/wakelock_button"; + private static final long TIME_OUT = 3000; + private UiDevice mDevice; + + @Before + public void setUp() { + final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + final Context context = instrumentation.getContext(); + mDevice = UiDevice.getInstance(instrumentation); + + // setup environment + TestUtils.setUp(instrumentation); + // start anomaly-tester app + TestUtils.startAnomalyApp(context, mDevice); + } + + @After + public void tearDown() { + TestUtils.tearDown(InstrumentationRegistry.getInstrumentation()); + } + + @Test + public void testWakelockAnomaly_longTimeWhileRunning_report() throws InterruptedException { + // Set running time + final long durationMs = DateUtils.SECOND_IN_MILLIS * 15; + TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs); + + // Click start button + TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON); + + // Wait for its running + mDevice.pressHome(); + // Sleeping time less than running time, so the app still holding wakelock when we check + TestUtils.wait(mDevice, durationMs - TIME_OUT); + + // Check it in battery main page + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); + assertWithMessage("Doesn't have wakelock anomaly").that( + mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")), + TIME_OUT)).isNotNull(); + } + + @Test + public void testWakelockAnomaly_shortTime_notReport() throws InterruptedException { + // Set running time + final long durationMs = DateUtils.SECOND_IN_MILLIS; + TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs); + + // Click start button + TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON); + + // Wait for its running + mDevice.pressHome(); + TestUtils.wait(mDevice, durationMs); + + // Check it in battery main page + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); + assertWithMessage("Shouldn't have wakelock anomaly").that( + mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")), + TIME_OUT)).isNull(); + } + + @Test + public void testWakelockAnomaly_longTimeWhileNotRunning_notReport() + throws InterruptedException { + // Set running time + final long durationMs = DateUtils.SECOND_IN_MILLIS * 10; + TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs); + + // Click start button + TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON); + + // Wait for its running + mDevice.pressHome(); + // Wait more time for releasing the wakelock + TestUtils.wait(mDevice, durationMs + TIME_OUT); + + // Check it in battery main page + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); + assertWithMessage("Shouldn't have wakelock anomaly").that( + mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")), + TIME_OUT)).isNull(); + } + +} |