From d68c8c59e57fa502b91842061658c1e2257209b5 Mon Sep 17 00:00:00 2001 From: Lei Yu Date: Wed, 24 Apr 2019 13:38:25 -0700 Subject: [PATCH] Fix settings unit test Also remove some obsolete tests Fixes: 126257657 Test: atest Change-Id: Ib9b654b9076176db3c331bf5ca14ac185c9e0b5f --- .../fuelgauge/PowerUsageSummaryTest.java | 57 ------------------- .../fuelgauge/batterytip/RestrictAppTest.java | 36 ++++++------ 2 files changed, 18 insertions(+), 75 deletions(-) delete mode 100644 tests/unit/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java diff --git a/tests/unit/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/unit/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java deleted file mode 100644 index 2c80a825a9e..00000000000 --- a/tests/unit/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.fuelgauge; - -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.matcher.ViewMatchers.withText; - -import android.app.Instrumentation; -import android.content.Intent; - -import androidx.test.InstrumentationRegistry; -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.settings.R; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class PowerUsageSummaryTest { - private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY"; - - @Before - public void SetUp() { - Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); - } - - @Test - public void testClickLastFullCharge_shouldNotCrash() { - onView(withText(R.string.battery_last_full_charge)).perform(click()); - } - - @Test - public void testClickScreenUsage_shouldNotCrash() { - onView(withText(R.string.device_screen_usage)).perform(click()); - } - -} diff --git a/tests/unit/src/com/android/settings/fuelgauge/batterytip/RestrictAppTest.java b/tests/unit/src/com/android/settings/fuelgauge/batterytip/RestrictAppTest.java index d9874f4fe2c..355a7e43c84 100644 --- a/tests/unit/src/com/android/settings/fuelgauge/batterytip/RestrictAppTest.java +++ b/tests/unit/src/com/android/settings/fuelgauge/batterytip/RestrictAppTest.java @@ -27,6 +27,7 @@ import android.app.Instrumentation; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.support.test.uiautomator.UiDevice; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; @@ -42,6 +43,7 @@ import java.util.concurrent.TimeUnit; @RunWith(AndroidJUnit4.class) @SmallTest public class RestrictAppTest { + private static final String WM_DISMISS_KEYGUARD_COMMAND = "wm dismiss-keyguard"; private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY"; private static final String PACKAGE_SETTINGS = "com.android.settings"; private static final String PACKAGE_SYSTEM_UI = "com.android.systemui"; @@ -50,10 +52,16 @@ public class RestrictAppTest { private BatteryDatabaseManager mBatteryDatabaseManager; private PackageManager mPackageManager; + private UiDevice mUiDevice; @Before - public void setUp() { + public void setUp() throws Exception { final Context context = InstrumentationRegistry.getTargetContext(); + + mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + mUiDevice.wakeUp(); + mUiDevice.executeShellCommand(WM_DISMISS_KEYGUARD_COMMAND); + mPackageManager = context.getPackageManager(); mBatteryDatabaseManager = BatteryDatabaseManager.getInstance(context); mBatteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(System.currentTimeMillis() + @@ -68,7 +76,7 @@ public class RestrictAppTest { AnomalyDatabaseHelper.State.NEW, System.currentTimeMillis()); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); + instrumentation.startActivitySync(createBatteryIntent()); onView(withText("Restrict 1 app")).check(matches(isDisplayed())); } @@ -83,25 +91,10 @@ public class RestrictAppTest { AnomalyDatabaseHelper.State.NEW, System.currentTimeMillis()); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); + instrumentation.startActivitySync(createBatteryIntent()); onView(withText("Restrict 2 apps")).check(matches(isDisplayed())); } - @Test - public void batterySettings_hasAutoHandledAnomalies_showAutoHandled() throws - PackageManager.NameNotFoundException { - mBatteryDatabaseManager.insertAnomaly(mPackageManager.getPackageUid(PACKAGE_SETTINGS, 0), - PACKAGE_SETTINGS, 1, - AnomalyDatabaseHelper.State.AUTO_HANDLED, System.currentTimeMillis()); - mBatteryDatabaseManager.insertAnomaly(mPackageManager.getPackageUid(PACKAGE_SYSTEM_UI, 0), - PACKAGE_SYSTEM_UI, 1, - AnomalyDatabaseHelper.State.AUTO_HANDLED, System.currentTimeMillis()); - - Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - instrumentation.startActivitySync(new Intent(BATTERY_INTENT)); - onView(withText("2 apps recently restricted")).check(matches(isDisplayed())); - } - @Test public void insertDuplicateAnomalies_onlyInsertOnce() throws PackageManager.NameNotFoundException { @@ -123,4 +116,11 @@ public class RestrictAppTest { .addAnomalyType(ANOMALY_TYPE) .build()); } + + private Intent createBatteryIntent() { + final Intent intent = new Intent(BATTERY_INTENT); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + return intent; + } }