Clear cache data to recalculate usage slot after receiving timezone change intent.
- Clear usage slot & even hour calculate event. Bug: 355084572 Test: atest BootBroadcastReceiverTest Test: atest BatteryEventDaoTest Flag: EXEMPT bug fix Change-Id: I0bc8b71219ce8cea3987a7bfc39b69e0c6047e3d
This commit is contained in:
@@ -33,6 +33,7 @@ import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryEventDao;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDao;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
@@ -56,7 +57,8 @@ import java.util.concurrent.TimeUnit;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BootBroadcastReceiverTest {
|
||||
private Context mContext;
|
||||
private BatteryStateDao mDao;
|
||||
private BatteryStateDao mBatteryStateDao;
|
||||
private BatteryEventDao mBatteryEventDao;
|
||||
private BootBroadcastReceiver mReceiver;
|
||||
private ShadowAlarmManager mShadowAlarmManager;
|
||||
private PeriodicJobManager mPeriodicJobManager;
|
||||
@@ -76,8 +78,10 @@ public final class BootBroadcastReceiverTest {
|
||||
|
||||
// Inserts fake data into database for testing.
|
||||
final BatteryStateDatabase database = BatteryTestUtils.setUpBatteryStateDatabase(mContext);
|
||||
mDao = database.batteryStateDao();
|
||||
mDao.clearAll();
|
||||
mBatteryStateDao = database.batteryStateDao();
|
||||
mBatteryStateDao.clearAll();
|
||||
mBatteryEventDao = database.batteryEventDao();
|
||||
mBatteryEventDao.clearAll();
|
||||
clearSharedPreferences();
|
||||
}
|
||||
|
||||
@@ -151,36 +155,32 @@ public final class BootBroadcastReceiverTest {
|
||||
@Test
|
||||
public void onReceive_withTimeChangedIntentSetEarlierTime_refreshesJob()
|
||||
throws InterruptedException {
|
||||
BatteryTestUtils.insertDataToBatteryStateTable(
|
||||
mContext, Clock.systemUTC().millis() + 60000, "com.android.systemui");
|
||||
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
|
||||
insertDataToTable(Clock.systemUTC().millis() + 60000);
|
||||
|
||||
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_TIME_CHANGED));
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
assertThat(mDao.getAllAfter(0)).isEmpty();
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).isEmpty();
|
||||
assertThat(mBatteryEventDao.getAllAfterForLog(0)).isEmpty();
|
||||
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);
|
||||
insertDataToTable(Clock.systemUTC().millis() - 60000);
|
||||
|
||||
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_TIME_CHANGED));
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(1);
|
||||
assertThat(mBatteryEventDao.getAllAfterForLog(0)).hasSize(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);
|
||||
insertDataToTable(Clock.systemUTC().millis() + 60000);
|
||||
|
||||
mReceiver.onReceive(
|
||||
mContext,
|
||||
@@ -190,21 +190,24 @@ public final class BootBroadcastReceiverTest {
|
||||
Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR));
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(1);
|
||||
assertThat(mBatteryEventDao.getAllAfterForLog(0)).hasSize(1);
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_withTimeZoneChangedIntent_clearAllDataAndRefreshesJob()
|
||||
public void onReceive_withTimeZoneChangedIntent_clearCacheDataAndRefreshesJob()
|
||||
throws InterruptedException {
|
||||
BatteryTestUtils.insertDataToBatteryStateTable(
|
||||
mContext, Clock.systemUTC().millis(), "com.android.systemui");
|
||||
assertThat(mDao.getAllAfter(0).size()).isEqualTo(1);
|
||||
insertDataToTable(Clock.systemUTC().millis());
|
||||
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(1);
|
||||
|
||||
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_TIMEZONE_CHANGED));
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
assertThat(mDao.getAllAfter(0)).isEmpty();
|
||||
// Only clear cache data.
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(1);
|
||||
assertThat(mBatteryEventDao.getAllAfterForLog(0)).isEmpty();
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNotNull();
|
||||
}
|
||||
|
||||
@@ -221,4 +224,13 @@ public final class BootBroadcastReceiverTest {
|
||||
private void clearSharedPreferences() {
|
||||
DatabaseUtils.getSharedPreferences(mContext).edit().clear().apply();
|
||||
}
|
||||
|
||||
private void insertDataToTable(long recordTimeMs) {
|
||||
BatteryTestUtils.insertDataToBatteryStateTable(
|
||||
mContext, recordTimeMs, "com.android.systemui");
|
||||
BatteryTestUtils.insertDataToBatteryEventTable(
|
||||
mContext, recordTimeMs, BatteryEventType.EVEN_HOUR.getNumber(), 50);
|
||||
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(1);
|
||||
assertThat(mBatteryEventDao.getAllAfterForLog(0)).hasSize(1);
|
||||
}
|
||||
}
|
||||
|
@@ -175,4 +175,31 @@ public final class BatteryEventDaoTest {
|
||||
mBatteryEventDao.clearAll();
|
||||
assertThat(mBatteryEventDao.getAll()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearEvenHourEvent_normalFlow_expectedBehavior() {
|
||||
mBatteryEventDao.insert(
|
||||
BatteryEventEntity.newBuilder()
|
||||
.setTimestamp(100L)
|
||||
.setBatteryEventType(1)
|
||||
.setBatteryLevel(66)
|
||||
.build());
|
||||
mBatteryEventDao.insert(
|
||||
BatteryEventEntity.newBuilder()
|
||||
.setTimestamp(200L)
|
||||
.setBatteryEventType(4)
|
||||
.setBatteryLevel(88)
|
||||
.build());
|
||||
assertThat(mBatteryEventDao.getAll()).hasSize(2);
|
||||
|
||||
mBatteryEventDao.clearEvenHourEvent();
|
||||
|
||||
final List<BatteryEventEntity> events = mBatteryEventDao.getAll();
|
||||
assertThat(events).hasSize(1);
|
||||
assertThat(events.get(0).timestamp).isEqualTo(100L);
|
||||
assertThat(events.get(0).batteryEventType).isEqualTo(1);
|
||||
assertThat(events.get(0).batteryLevel).isEqualTo(66);
|
||||
mBatteryEventDao.clearAll();
|
||||
assertThat(mBatteryEventDao.getAll()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
@@ -43,6 +43,8 @@ import com.android.settings.fuelgauge.batteryusage.WarningBannerInfo;
|
||||
import com.android.settings.fuelgauge.batteryusage.WarningItemInfo;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryEventDao;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryEventEntity;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryState;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDao;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
|
||||
@@ -184,6 +186,15 @@ public class BatteryTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/** Inserts a fake data into the database for testing. */
|
||||
public static void insertDataToBatteryEventTable(
|
||||
Context context, long timestamp, int batteryEventType, int batteryLevel) {
|
||||
final BatteryEventEntity entity =
|
||||
new BatteryEventEntity(timestamp, batteryEventType, batteryLevel);
|
||||
BatteryEventDao dao = BatteryStateDatabase.getInstance(context).batteryEventDao();
|
||||
dao.insert(entity);
|
||||
}
|
||||
|
||||
/** Gets customized battery changed intent. */
|
||||
public static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
|
||||
Intent intent = new Intent();
|
||||
|
Reference in New Issue
Block a user