Update database clear & job refresh mechanism for time change intent

- Ignore time change intent for time format update
- Clear data after current time in DB and refresh periodic job
- Take a snapshot of current battery usage stats if no periodic job in DB

Bug: 336423923
Bug: 314921894
Fix: 314921894
Test: atest SettingsRoboTests:com.android.settings.fuelgauge.batteryusagei
Change-Id: Iec0f5e8e97f18c4603de711a5884336ba0af23a9
This commit is contained in:
mxyyiyi
2024-05-08 17:08:16 +08:00
parent 658bc03d4f
commit 798340fafd
7 changed files with 87 additions and 36 deletions

View File

@@ -35,7 +35,6 @@ import com.android.settings.testutils.BatteryTestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@@ -64,9 +63,8 @@ public final class BootBroadcastReceiverTest {
// Inserts fake data into database for testing.
final BatteryStateDatabase database = BatteryTestUtils.setUpBatteryStateDatabase(mContext);
BatteryTestUtils.insertDataToBatteryStateTable(
mContext, Clock.systemUTC().millis(), "com.android.systemui");
mDao = database.batteryStateDao();
mDao.clearAll();
clearSharedPreferences();
}
@@ -129,10 +127,13 @@ public final class BootBroadcastReceiverTest {
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
}
@Ignore("b/314921894")
@Test
public void onReceive_withTimeChangedIntent_clearsAllDataAndRefreshesJob()
public void onReceive_withTimeChangedIntentSetEarlierTime_refreshesJob()
throws InterruptedException {
BatteryTestUtils.insertDataToBatteryStateTable(
mContext, Clock.systemUTC().millis() + 60000, "com.android.systemui");
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_TIME_CHANGED));
TimeUnit.MILLISECONDS.sleep(100);
@@ -140,6 +141,38 @@ public final class BootBroadcastReceiverTest {
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNotNull();
}
@Test
public void onReceive_withTimeChangedIntentSetLaterTime_clearNoDataAndRefreshesJob()
throws InterruptedException {
BatteryTestUtils.insertDataToBatteryStateTable(
mContext, Clock.systemUTC().millis() - 60000, "com.android.systemui");
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_TIME_CHANGED));
TimeUnit.MILLISECONDS.sleep(100);
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNotNull();
}
@Test
public void onReceive_withTimeFormatChangedIntent_skipRefreshJob() throws InterruptedException {
BatteryTestUtils.insertDataToBatteryStateTable(
mContext, Clock.systemUTC().millis() + 60000, "com.android.systemui");
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
mReceiver.onReceive(
mContext,
new Intent(Intent.EXTRA_INTENT)
.putExtra(
Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT,
Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR));
TimeUnit.MILLISECONDS.sleep(100);
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
}
@Test
public void invokeJobRecheck_broadcastsIntent() {
BootBroadcastReceiver.invokeJobRecheck(mContext);