Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d68c8c59 authored by Lei Yu's avatar Lei Yu
Browse files

Fix settings unit test

Also remove some obsolete tests

Fixes: 126257657
Test: atest
Change-Id: Ib9b654b9076176db3c331bf5ca14ac185c9e0b5f
parent 846f77ab
Loading
Loading
Loading
Loading
+0 −57
Original line number Diff line number Diff line
/*
 * 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());
    }

}
+18 −18
Original line number Diff line number Diff line
@@ -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;
    }
}