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

Commit 0b1f5144 authored by Jiun-Yang Hsu's avatar Jiun-Yang Hsu Committed by Android (Google) Code Review
Browse files

Merge "Initial commit for Settings component test" into sc-dev

parents a7c0036a 7a3635e7
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
//############################################################
// Settings Component test target.                           #
//############################################################
android_test {
    name: "SettingsComponentTests",
    certificate: "platform",
    privileged: true,
    srcs: [
        "src/**/*.java",
    ],

    static_libs: [
        "truth-prebuilt",
        "androidx.test.core",
        "androidx.test.runner",
        "androidx.test.rules",
        "androidx.test.ext.junit",
    ],

    test_suites: ["device-tests"],

    instrumentation_for: "Settings",
}
+40 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
          package="com.android.settings.tests.component">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
    <uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application/>

    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
                     android:targetPackage="com.android.settings"
                     android:label="Settings Test Cases">
    </instrumentation>

</manifest>
 No newline at end of file
+30 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<configuration description="Runs Settings Test Cases.">
    <option name="test-suite-tag" value="apct" />
    <option name="test-suite-tag" value="apct-instrumentation" />
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="SettingsComponentTests.apk" />
    </target_preparer>

    <option name="test-tag" value="SettingsComponentTests" />
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.settings.tests.component" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
        <option name="hidden-api-checks" value="false"/>
    </test>
</configuration>
+3 −0
Original line number Diff line number Diff line
# People who can approve changes for submission
jyhsu@google.com
syaoranx@google.com
 No newline at end of file
+143 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.batterysaver;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Button;

import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.settings.R;
import com.android.settings.Settings.BatterySaverSettingsActivity;
import com.android.settings.testutils.AdbUtils;
import com.android.settings.testutils.UiUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class BatterySaverButtonPreferenceControllerComponentTest {
    private static final String TAG =
            BatterySaverButtonPreferenceControllerComponentTest.class.getSimpleName();
    private Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
    private PowerManager mManager =
            (PowerManager) mInstrumentation.getTargetContext().getSystemService(
                    Context.POWER_SERVICE);

    @Rule
    public ActivityScenarioRule<BatterySaverSettingsActivity> rule = new ActivityScenarioRule<>(
            new Intent(
                    Settings.ACTION_BATTERY_SAVER_SETTINGS).setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK));

    @Before
    public void setUp() throws Exception {
        mInstrumentation.getUiAutomation().executeShellCommand("dumpsys battery unplug");
        mInstrumentation.getUiAutomation().executeShellCommand("settings get global low_power 0");
    }

    @Test
    public void test_check_battery_saver_button() throws Exception {
        ActivityScenario scenario = rule.getScenario();
        scenario.onActivity(activity -> {
            final Button button = activity.findViewById(R.id.state_on_button);
            UiUtils.waitUntilCondition(3000, () -> button.isEnabled());
            button.callOnClick();
            checkPowerSaverMode(true);

            Button offButton = activity.findViewById(R.id.state_off_button);
            offButton.callOnClick();
            checkPowerSaverMode(false);
        });

        //Ideally, we should be able to also create BatteryTipPreferenceController and verify that
        //it is showing battery saver on. Unfortunately, that part of code is tightly coupled with
        //UI, and it's not possible to retrieve that string without reaching very deep into the
        //codes and become very tightly coupled with any future changes. That is not what component
        //tests should do, so either we'll need to do this through UI with another ActivityScenario,
        //or the code needs to be refactored to be less coupled with UI.
    }

    @Test
    public void test_battery_saver_button_changes_when_framework_setting_change() throws Exception {
        ActivityScenario scenario = rule.getScenario();
        scenario.onActivity(activity -> {
            Button buttonOn = activity.findViewById(R.id.state_on_button);
            Button buttonOff = activity.findViewById(R.id.state_off_button);
            assertThat(buttonOn.isVisibleToUser()).isEqualTo(true);
            assertThat(buttonOff.isVisibleToUser()).isEqualTo(false);
        });

        mManager.setPowerSaveModeEnabled(true);
        scenario.recreate();
        scenario.onActivity(activity -> {
            Button buttonOn = activity.findViewById(R.id.state_on_button);
            Button buttonOff = activity.findViewById(R.id.state_off_button);
            assertThat(buttonOn.isVisibleToUser()).isEqualTo(false);
            assertThat(buttonOff.isVisibleToUser()).isEqualTo(true);
        });

        mManager.setPowerSaveModeEnabled(false);
        scenario.recreate();
        scenario.onActivity(activity -> {
            Button buttonOn = activity.findViewById(R.id.state_on_button);
            Button buttonOff = activity.findViewById(R.id.state_off_button);
            assertThat(buttonOn.isVisibleToUser()).isEqualTo(true);
            assertThat(buttonOff.isVisibleToUser()).isEqualTo(false);
        });
    }

    @After
    public void tearDown() {
        mInstrumentation.getUiAutomation().executeShellCommand("settings get global low_power 0");
        mInstrumentation.getUiAutomation().executeShellCommand("dumpsys battery reset");
    }

    private void checkPowerSaverMode(boolean enabled) {
        //Check through adb. Note that this needs to be done first, or a wait and poll needs to be
        //done to the manager.isPowerSaveMode(), because calling isPowerSaveMode immediately after
        //setting it does not return true. It takes a while for isPowerSaveMode() to return the
        //up-to-date value.
        try {
            assertThat(
                    AdbUtils.checkStringInAdbCommandOutput(TAG, "settings get global low_power",
                            null, enabled ? "1" : "0", 1000)).isTrue();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            assert_().fail();
        }

        //Check through manager
        assertThat(mManager.isPowerSaveMode() == enabled).isTrue();
    }
}
Loading