Impl dismiss action in battery tips cards.

- Use SharedPreferences to record and filter the already dimissed anomaly.

Bug: 291689623
Test: manual
Change-Id: I4fd4a39066591a4a201857f9586b6595b7d5c43b
This commit is contained in:
mxyyiyi
2023-08-17 15:30:22 +08:00
parent 5cbf3b0f74
commit 737b9ab5b7
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");
}