Merge "Impl dismiss action in battery tips cards." into udc-qpr-dev

This commit is contained in:
Treehugger Robot
2023-08-25 08:32:04 +00:00
committed by Android (Google) Code Review
8 changed files with 133 additions and 7 deletions

View File

@@ -46,6 +46,7 @@ import android.view.ViewPropertyAnimator;
import android.widget.LinearLayout;
import com.android.settings.SettingsActivity;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
@@ -407,6 +408,57 @@ public final class BatteryChartPreferenceControllerTest {
assertThat(totalHour).isEqualTo(59);
}
@Test
public void getHighestScoreAnomalyEvent_withEmptyOrNullList_getNull() {
assertThat(mBatteryChartPreferenceController.getHighestScoreAnomalyEvent(null))
.isEqualTo(null);
assertThat(mBatteryChartPreferenceController.getHighestScoreAnomalyEvent(
BatteryTestUtils.createEmptyPowerAnomalyEventList()))
.isEqualTo(null);
}
@Test
public void getHighestScoreAnomalyEvent_withoutDismissed_getHighestScoreEvent() {
final PowerAnomalyEventList eventList =
BatteryTestUtils.createNonEmptyPowerAnomalyEventList();
final PowerAnomalyEvent highestScoreEvent =
mBatteryChartPreferenceController.getHighestScoreAnomalyEvent(eventList);
assertThat(highestScoreEvent)
.isEqualTo(BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent());
}
@Test
public void getHighestScoreAnomalyEvent_withBrightnessDismissed_getScreenTimeout() {
final PowerAnomalyEventList eventList =
BatteryTestUtils.createNonEmptyPowerAnomalyEventList();
DatabaseUtils.removeDismissedPowerAnomalyKeys(mContext);
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, PowerAnomalyKey.KEY_BRIGHTNESS.name());
final PowerAnomalyEvent highestScoreEvent =
mBatteryChartPreferenceController.getHighestScoreAnomalyEvent(eventList);
assertThat(highestScoreEvent)
.isEqualTo(BatteryTestUtils.createScreenTimeoutAnomalyEvent());
}
@Test
public void getHighestScoreAnomalyEvent_withAllDismissed_getNull() {
final PowerAnomalyEventList eventList =
BatteryTestUtils.createNonEmptyPowerAnomalyEventList();
DatabaseUtils.removeDismissedPowerAnomalyKeys(mContext);
for (PowerAnomalyKey key : PowerAnomalyKey.values()) {
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, key.name());
}
final PowerAnomalyEvent highestScoreEvent =
mBatteryChartPreferenceController.getHighestScoreAnomalyEvent(eventList);
assertThat(highestScoreEvent).isEqualTo(null);
}
private static Long generateTimestamp(int index) {
// "2021-04-23 07:00:00 UTC" + index hours
return 1619247600000L + index * DateUtils.HOUR_IN_MILLIS;

View File

@@ -83,6 +83,7 @@ public final class BatteryTipsCardPreferenceTest {
mBatteryTipsController.handleBatteryTipsCardUpdated(adaptiveBrightnessAnomaly);
mBatteryTipsCardPreference.onClick(mFakeView);
assertThat(mBatteryTipsCardPreference.isVisible()).isEqualTo(false);
verify(mContext).startActivity(any(Intent.class));
final Intent intent = captor.getValue();
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
@@ -94,15 +95,21 @@ public final class BatteryTipsCardPreferenceTest {
}
@Test
public void onClick_dismissBtn_metricsLogged() {
public void onClick_dismissBtn_cardDismissAndLogged() {
PowerAnomalyEvent screenTimeoutAnomaly =
BatteryTestUtils.createScreenTimeoutAnomalyEvent();
DatabaseUtils.removeDismissedPowerAnomalyKeys(mContext);
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
when(mFakeView.getId()).thenReturn(R.id.dismiss_button);
mBatteryTipsController.handleBatteryTipsCardUpdated(screenTimeoutAnomaly);
mBatteryTipsCardPreference.onClick(mFakeView);
assertThat(mBatteryTipsCardPreference.isVisible()).isEqualTo(false);
assertThat(DatabaseUtils.getDismissedPowerAnomalyKeys(mContext).size())
.isEqualTo(1);
assertThat(DatabaseUtils.getDismissedPowerAnomalyKeys(mContext))
.contains(PowerAnomalyKey.KEY_SCREEN_TIMEOUT.name());
verify(mFeatureFactory.metricsFeatureProvider).action(
mContext, SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS, "ScreenTimeoutAnomaly");
}

View File

@@ -208,12 +208,21 @@ public class BatteryTestUtils {
return PowerAnomalyEventList.getDefaultInstance();
}
/** Create an non-empty power anomaly event list proto. */
public static PowerAnomalyEventList createNonEmptyPowerAnomalyEventList() {
return PowerAnomalyEventList.newBuilder()
.addPowerAnomalyEvents(0, createAdaptiveBrightnessAnomalyEvent())
.addPowerAnomalyEvents(1, createScreenTimeoutAnomalyEvent())
.build();
}
/** Create a power anomaly event proto of adaptive brightness. */
public static PowerAnomalyEvent createAdaptiveBrightnessAnomalyEvent() {
return PowerAnomalyEvent.newBuilder()
.setEventId("BrightnessAnomaly")
.setType(PowerAnomalyType.TYPE_SETTINGS_BANNER)
.setKey(PowerAnomalyKey.KEY_BRIGHTNESS)
.setScore(1.2f)
.setWarningBannerInfo(WarningBannerInfo.newBuilder()
.setMainButtonDestination(DisplaySettings.class.getName())
.setMainButtonSourceMetricsCategory(SettingsEnums.DISPLAY)
@@ -228,6 +237,7 @@ public class BatteryTestUtils {
.setEventId("ScreenTimeoutAnomaly")
.setType(PowerAnomalyType.TYPE_SETTINGS_BANNER)
.setKey(PowerAnomalyKey.KEY_SCREEN_TIMEOUT)
.setScore(1.1f)
.setWarningBannerInfo(WarningBannerInfo.newBuilder()
.setMainButtonDestination(ScreenTimeoutSettings.class.getName())
.setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT)