Improve battery tips cards.

Test: manual
Change-Id: I356648d0fcec5dd1ea724297187ecafb5be55fd8
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:13fec0dbdb2c809f20a1e3b711e2491745f6d506)
Bug: 291689623
This commit is contained in:
mxyyiyi
2023-08-05 14:18:03 +08:00
committed by Xinyi Mao
parent 0fcc479625
commit 14d0a1c102
12 changed files with 452 additions and 111 deletions

View File

@@ -73,8 +73,8 @@ public class PowerUsageFeatureProviderImplTest {
}
@Test
public void testIsBatteryTipsFeedbackEnabled_returnTrue() {
assertThat(mPowerFeatureProvider.isBatteryTipsFeedbackEnabled()).isTrue();
public void testIsBatteryTipsFeedbackEnabled_returnFalse() {
assertThat(mPowerFeatureProvider.isBatteryTipsFeedbackEnabled()).isFalse();
}
@Test
public void testGetBatteryUsageListConsumePowerThreshold_return0() {

View File

@@ -19,14 +19,20 @@ package com.android.settings.fuelgauge.batteryusage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import com.android.settings.R;
import com.android.settings.testutils.BatteryTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@@ -35,11 +41,17 @@ public final class BatteryTipsCardPreferenceTest {
private Context mContext;
private BatteryTipsCardPreference mBatteryTipsCardPreference;
private BatteryTipsController mBatteryTipsController;
@Mock
private View mMockButton;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mBatteryTipsCardPreference = new BatteryTipsCardPreference(mContext, /*attrs=*/ null);
mBatteryTipsController = new BatteryTipsController(mContext);
mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference;
mMockButton.setId(R.id.action_button);
}
@Test
@@ -47,4 +59,16 @@ public final class BatteryTipsCardPreferenceTest {
assertThat(mBatteryTipsCardPreference.getLayoutResource()).isEqualTo(
R.layout.battery_tips_card);
}
@Test
public void onClick_actionBtn_getAdaptiveBrightnessLauncher() {
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
PowerAnomalyEvent adaptiveBrightnessAnomaly =
BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent();
mBatteryTipsController.handleBatteryTipsCardUpdated(adaptiveBrightnessAnomaly);
mBatteryTipsCardPreference.onClick(mMockButton);
verify(mContext).startActivity(captor.capture());
}
}

View File

@@ -0,0 +1,148 @@
/*
* Copyright (C) 2023 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.batteryusage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.res.Resources;
import android.os.LocaleList;
import com.android.settings.testutils.BatteryTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.Locale;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
public final class BatteryTipsControllerTest {
private Context mContext;
private BatteryTipsController mBatteryTipsController;
@Mock
private BatteryTipsCardPreference mBatteryTipsCardPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Locale.setDefault(new Locale("en_US"));
org.robolectric.shadows.ShadowSettings.set24HourTimeFormat(false);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
mContext = spy(RuntimeEnvironment.application);
final Resources resources = spy(mContext.getResources());
resources.getConfiguration().setLocales(new LocaleList(new Locale("en_US")));
doReturn(resources).when(mContext).getResources();
mBatteryTipsController = new BatteryTipsController(mContext);
mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference;
}
@Test
public void parsePowerAnomalyKey_preDefinedKeys_returnTrue() {
final String[] keys = {"adaptive_brightness", "screen_timeout"};
for (int index = 0; index < keys.length; index++) {
assertThat(mBatteryTipsController.getPowerAnomalyEventIndex(keys[index]))
.isEqualTo(index);
}
}
@Test
public void parsePowerAnomalyKey_unknownKey_returnTrue() {
final String key = "unknown_key_for_test";
assertThat(mBatteryTipsController.getPowerAnomalyEventIndex(key)).isEqualTo(-1);
}
@Test
public void handleBatteryTipsCardUpdated_null_hidePreference() {
mBatteryTipsController.handleBatteryTipsCardUpdated(/* powerAnomalyEvents= */ null);
verify(mBatteryTipsCardPreference).setVisible(false);
}
@Test
public void handleBatteryTipsCardUpdated_adaptiveBrightnessAnomaly_showAnomaly() {
PowerAnomalyEvent event = BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent();
mBatteryTipsController.handleBatteryTipsCardUpdated(event);
// Check pre-defined string
verify(mBatteryTipsCardPreference).setTitle(
"Turn on adaptive brightness to extend battery life");
verify(mBatteryTipsCardPreference).setMainButtonLabel(
"View Settings");
verify(mBatteryTipsCardPreference).setDismissButtonLabel(
"Got it");
// Check proto info
verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo(
"com.android.settings.display.AutoBrightnessSettings",
1381);
verify(mBatteryTipsCardPreference).setVisible(true);
}
@Test
public void handleBatteryTipsCardUpdated_screenTimeoutAnomaly_showAnomaly() {
PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent();
mBatteryTipsController.handleBatteryTipsCardUpdated(event);
verify(mBatteryTipsCardPreference).setTitle(
"Reduce screen timeout to extend battery life");
verify(mBatteryTipsCardPreference).setMainButtonLabel(
"View Settings");
verify(mBatteryTipsCardPreference).setDismissButtonLabel(
"Got it");
verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo(
"com.android.settings.display.ScreenTimeoutSettings",
1852);
verify(mBatteryTipsCardPreference).setVisible(true);
}
@Test
public void handleBatteryTipsCardUpdated_screenTimeoutAnomalyHasTitle_showAnomaly() {
PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent();
String testTitle = "TestTitle";
event = event.toBuilder()
.setWarningBannerInfo(
event.getWarningBannerInfo().toBuilder()
.setTitleString(testTitle)
.build())
.build();
mBatteryTipsController.handleBatteryTipsCardUpdated(event);
verify(mBatteryTipsCardPreference).setTitle(
testTitle);
verify(mBatteryTipsCardPreference).setMainButtonLabel(
"View Settings");
verify(mBatteryTipsCardPreference).setDismissButtonLabel(
"Got it");
verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo(
"com.android.settings.display.ScreenTimeoutSettings",
1852);
verify(mBatteryTipsCardPreference).setVisible(true);
}
}

View File

@@ -18,6 +18,7 @@ package com.android.settings.testutils;
import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
@@ -25,11 +26,17 @@ import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.os.BatteryManager;
import android.os.UserManager;
import androidx.room.Room;
import com.android.settings.display.AutoBrightnessSettings;
import com.android.settings.display.ScreenTimeoutSettings;
import com.android.settings.fuelgauge.batteryusage.BatteryInformation;
import com.android.settings.fuelgauge.batteryusage.ConvertUtils;
import com.android.settings.fuelgauge.batteryusage.DeviceBatteryState;
import com.android.settings.fuelgauge.batteryusage.PowerAnomalyEvent;
import com.android.settings.fuelgauge.batteryusage.PowerAnomalyEventList;
import com.android.settings.fuelgauge.batteryusage.WarningBannerInfo;
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao;
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
import com.android.settings.fuelgauge.batteryusage.db.BatteryState;
@@ -193,4 +200,33 @@ public class BatteryTestUtils {
when(mockUsbPortStatus.getComplianceWarnings())
.thenReturn(new int[]{UsbPortStatus.COMPLIANCE_WARNING_OTHER});
}
/** Create an empty power anomaly event list proto. */
public static PowerAnomalyEventList createEmptyPowerAnomalyEventList() {
return PowerAnomalyEventList.getDefaultInstance();
}
/** Create a power anomaly event proto of adaptive brightness. */
public static PowerAnomalyEvent createAdaptiveBrightnessAnomalyEvent() {
return PowerAnomalyEvent.newBuilder()
.setType("settings banner")
.setKey("adaptive_brightness")
.setWarningBannerInfo(WarningBannerInfo.newBuilder()
.setMainButtonDestination(AutoBrightnessSettings.class.getName())
.setMainButtonSourceMetricsCategory(SettingsEnums.SETTINGS_AUTO_BRIGHTNESS)
.build())
.build();
}
/** Create a power anomaly event proto of screen timeout. */
public static PowerAnomalyEvent createScreenTimeoutAnomalyEvent() {
return PowerAnomalyEvent.newBuilder()
.setType("settings banner")
.setKey("screen_timeout")
.setWarningBannerInfo(WarningBannerInfo.newBuilder()
.setMainButtonDestination(ScreenTimeoutSettings.class.getName())
.setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT)
.build())
.build();
}
}