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

Commit e4be9a7b authored by Syaoran Kuo's avatar Syaoran Kuo Committed by Android (Google) Code Review
Browse files

Merge "Adjuest battery saver component test for new logic" into sc-dev

parents 33a9f498 47328cc9
Loading
Loading
Loading
Loading
+22 −47
Original line number Diff line number Diff line
@@ -19,24 +19,26 @@ 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.Activity;
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.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
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.SettingsPreferenceFragment;
import com.android.settings.testutils.AdbUtils;
import com.android.settings.testutils.UiUtils;

import org.junit.After;
import org.junit.Before;
@@ -49,11 +51,10 @@ import org.junit.runner.RunWith;
public class BatterySaverButtonPreferenceControllerComponentTest {
    private static final String TAG =
            BatterySaverButtonPreferenceControllerComponentTest.class.getSimpleName();
    private Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
    private PowerManager mManager =
    private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
    private final PowerManager mManager =
            (PowerManager) mInstrumentation.getTargetContext().getSystemService(
                    Context.POWER_SERVICE);

    @Rule
    public ActivityScenarioRule<BatterySaverSettingsActivity> rule = new ActivityScenarioRule<>(
            new Intent(
@@ -66,55 +67,28 @@ public class BatterySaverButtonPreferenceControllerComponentTest {
        mInstrumentation.getUiAutomation().executeShellCommand("settings get global low_power 0");
    }

    private BatterySaverButtonPreferenceController get_battery_saver_controller(Activity activity) {
        BatterySaverButtonPreferenceController controller =
                new BatterySaverButtonPreferenceController(
                        ApplicationProvider.getApplicationContext(), "battery_saver");
        Fragment f =
                ((FragmentActivity) activity).getSupportFragmentManager().getFragments().get(0);
        controller.displayPreference(((SettingsPreferenceFragment) f).getPreferenceScreen());
        return controller;
    }

    @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();
            BatterySaverButtonPreferenceController controller =
                    get_battery_saver_controller(activity);
            controller.setChecked(true);
            checkPowerSaverMode(true);

            Button offButton = activity.findViewById(R.id.state_off_button);
            offButton.callOnClick();
            controller.setChecked(false);
            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
@@ -140,4 +114,5 @@ public class BatterySaverButtonPreferenceControllerComponentTest {
        //Check through manager
        assertThat(mManager.isPowerSaveMode() == enabled).isTrue();
    }

}