Record important battery usage event into SharedPreference for dump()
Fix: 268300677 Test: presubmit Change-Id: If03106c265b984bc2608528bdf45386973e3a14f
This commit is contained in:
@@ -54,11 +54,13 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
|||||||
if (intent == null || intent.getAction() == null) {
|
if (intent == null || intent.getAction() == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Log.d(TAG, "onReceive:" + intent.getAction());
|
final String action = intent.getAction();
|
||||||
|
Log.d(TAG, "onReceive:" + action);
|
||||||
|
DatabaseUtils.recordDateTime(context, action);
|
||||||
final String fullChargeIntentAction = FeatureFactory.getFactory(context)
|
final String fullChargeIntentAction = FeatureFactory.getFactory(context)
|
||||||
.getPowerUsageFeatureProvider(context)
|
.getPowerUsageFeatureProvider(context)
|
||||||
.getFullChargeIntentAction();
|
.getFullChargeIntentAction();
|
||||||
switch (intent.getAction()) {
|
switch (action) {
|
||||||
case Intent.ACTION_BATTERY_LEVEL_CHANGED:
|
case Intent.ACTION_BATTERY_LEVEL_CHANGED:
|
||||||
// Only when fullChargeIntentAction is ACTION_BATTERY_LEVEL_CHANGED,
|
// Only when fullChargeIntentAction is ACTION_BATTERY_LEVEL_CHANGED,
|
||||||
// ACTION_BATTERY_LEVEL_CHANGED will be considered as the full charge event and then
|
// ACTION_BATTERY_LEVEL_CHANGED will be considered as the full charge event and then
|
||||||
|
@@ -57,6 +57,10 @@ public final class BatteryUsageDataLoader {
|
|||||||
}
|
}
|
||||||
final long elapsedTime = System.currentTimeMillis() - start;
|
final long elapsedTime = System.currentTimeMillis() - start;
|
||||||
Log.d(TAG, String.format("getBatteryUsageStats() in %d/ms", elapsedTime));
|
Log.d(TAG, String.format("getBatteryUsageStats() in %d/ms", elapsedTime));
|
||||||
|
if (isFullChargeStart) {
|
||||||
|
DatabaseUtils.recordDateTime(
|
||||||
|
context, DatabaseUtils.KEY_LAST_LOAD_FULL_CHARGE_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
// Uploads the BatteryEntry data into database.
|
// Uploads the BatteryEntry data into database.
|
||||||
DatabaseUtils.sendBatteryEntryData(
|
DatabaseUtils.sendBatteryEntryData(
|
||||||
|
@@ -19,6 +19,7 @@ import android.content.ContentResolver;
|
|||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -37,12 +38,16 @@ import com.android.settings.fuelgauge.BatteryUtils;
|
|||||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
|
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
|
||||||
import com.android.settingslib.fuelgauge.BatteryStatus;
|
import com.android.settingslib.fuelgauge.BatteryStatus;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -50,12 +55,15 @@ import java.util.stream.Collectors;
|
|||||||
/** A utility class to operate battery usage database. */
|
/** A utility class to operate battery usage database. */
|
||||||
public final class DatabaseUtils {
|
public final class DatabaseUtils {
|
||||||
private static final String TAG = "DatabaseUtils";
|
private static final String TAG = "DatabaseUtils";
|
||||||
|
private static final String SHARED_PREFS_FILE = "battery_usage_shared_prefs";
|
||||||
|
|
||||||
/** Clear memory threshold for device booting phase. **/
|
/** Clear memory threshold for device booting phase. **/
|
||||||
private static final long CLEAR_MEMORY_THRESHOLD_MS = Duration.ofMinutes(5).toMillis();
|
private static final long CLEAR_MEMORY_THRESHOLD_MS = Duration.ofMinutes(5).toMillis();
|
||||||
private static final long CLEAR_MEMORY_DELAYED_MS = Duration.ofSeconds(2).toMillis();
|
private static final long CLEAR_MEMORY_DELAYED_MS = Duration.ofSeconds(2).toMillis();
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
static final int DATA_RETENTION_INTERVAL_DAY = 9;
|
static final int DATA_RETENTION_INTERVAL_DAY = 9;
|
||||||
|
static final String KEY_LAST_LOAD_FULL_CHARGE_TIME = "last_load_full_charge_time";
|
||||||
|
static final String KEY_LAST_UPLOAD_FULL_CHARGE_TIME = "last_upload_full_charge_time";
|
||||||
|
|
||||||
/** An authority name of the battery content provider. */
|
/** An authority name of the battery content provider. */
|
||||||
public static final String AUTHORITY = "com.android.settings.battery.usage.provider";
|
public static final String AUTHORITY = "com.android.settings.battery.usage.provider";
|
||||||
@@ -373,10 +381,48 @@ public final class DatabaseUtils {
|
|||||||
resolver.notifyChange(BATTERY_CONTENT_URI, /*observer=*/ null);
|
resolver.notifyChange(BATTERY_CONTENT_URI, /*observer=*/ null);
|
||||||
Log.d(TAG, String.format("sendBatteryEntryData() size=%d in %d/ms",
|
Log.d(TAG, String.format("sendBatteryEntryData() size=%d in %d/ms",
|
||||||
size, (System.currentTimeMillis() - startTime)));
|
size, (System.currentTimeMillis() - startTime)));
|
||||||
|
if (isFullChargeStart) {
|
||||||
|
recordDateTime(context, KEY_LAST_UPLOAD_FULL_CHARGE_TIME);
|
||||||
|
}
|
||||||
clearMemory();
|
clearMemory();
|
||||||
return valuesList;
|
return valuesList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Dump all required data into {@link PrintWriter}. */
|
||||||
|
public static void dump(Context context, PrintWriter writer) {
|
||||||
|
writeString(context, writer, "BatteryLevelChanged",
|
||||||
|
Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
|
writeString(context, writer, "BatteryUnplugging",
|
||||||
|
BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
|
writeString(context, writer, "ClearBatteryCacheData",
|
||||||
|
BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA);
|
||||||
|
writeString(context, writer, "LastLoadFullChargeTime",
|
||||||
|
KEY_LAST_LOAD_FULL_CHARGE_TIME);
|
||||||
|
writeString(context, writer, "LastUploadFullChargeTime",
|
||||||
|
KEY_LAST_UPLOAD_FULL_CHARGE_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
|
static SharedPreferences getSharedPreferences(Context context) {
|
||||||
|
return context.getApplicationContext().getSharedPreferences(
|
||||||
|
SHARED_PREFS_FILE, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void recordDateTime(Context context, String preferenceKey) {
|
||||||
|
final SharedPreferences sharedPreferences = getSharedPreferences(context);
|
||||||
|
if (sharedPreferences != null) {
|
||||||
|
sharedPreferences.edit().putString(preferenceKey, getCurrentDateTime()).apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeString(
|
||||||
|
Context context, PrintWriter writer, String prefix, String key) {
|
||||||
|
final SharedPreferences sharedPreferences = getSharedPreferences(context);
|
||||||
|
if (sharedPreferences != null) {
|
||||||
|
final String content = sharedPreferences.getString(key, "");
|
||||||
|
writer.println(String.format("\t\t%s: %s", prefix, content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static long loadAppUsageLatestTimestampFromContentProvider(
|
private static long loadAppUsageLatestTimestampFromContentProvider(
|
||||||
Context context, final Uri appUsageLatestTimestampUri) {
|
Context context, final Uri appUsageLatestTimestampUri) {
|
||||||
// We have already make sure the context here is with profile parent's user identity. Don't
|
// We have already make sure the context here is with profile parent's user identity. Don't
|
||||||
@@ -473,4 +519,9 @@ public final class DatabaseUtils {
|
|||||||
Log.w(TAG, "invoke clearMemory()");
|
Log.w(TAG, "invoke clearMemory()");
|
||||||
}, CLEAR_MEMORY_DELAYED_MS);
|
}, CLEAR_MEMORY_DELAYED_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getCurrentDateTime() {
|
||||||
|
return new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.getDefault())
|
||||||
|
.format(new Date(System.currentTimeMillis()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge.batteryusage.bugreport;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.android.settings.fuelgauge.batteryusage.DatabaseUtils;
|
||||||
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao;
|
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao;
|
||||||
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
|
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
|
||||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryState;
|
import com.android.settings.fuelgauge.batteryusage.db.BatteryState;
|
||||||
@@ -41,6 +42,8 @@ public final class LogUtils {
|
|||||||
|
|
||||||
@SuppressWarnings("JavaUtilDate")
|
@SuppressWarnings("JavaUtilDate")
|
||||||
static void dumpBatteryUsageDatabaseHist(Context context, PrintWriter writer) {
|
static void dumpBatteryUsageDatabaseHist(Context context, PrintWriter writer) {
|
||||||
|
DatabaseUtils.dump(context, writer);
|
||||||
|
writer.flush();
|
||||||
final BatteryStateDao dao =
|
final BatteryStateDao dao =
|
||||||
BatteryStateDatabase
|
BatteryStateDatabase
|
||||||
.getInstance(context.getApplicationContext())
|
.getInstance(context.getApplicationContext())
|
||||||
@@ -64,6 +67,7 @@ public final class LogUtils {
|
|||||||
writer.println("\t" + formattedTimestamp);
|
writer.println("\t" + formattedTimestamp);
|
||||||
Log.w(TAG, "\t" + formattedTimestamp);
|
Log.w(TAG, "\t" + formattedTimestamp);
|
||||||
});
|
});
|
||||||
|
writer.flush();
|
||||||
|
|
||||||
final List<BatteryState> stateList = dao.getAllAfter(
|
final List<BatteryState> stateList = dao.getAllAfter(
|
||||||
Clock.systemUTC().millis() - DUMP_TIME_OFFSET_FOR_ENTRY.toMillis());
|
Clock.systemUTC().millis() - DUMP_TIME_OFFSET_FOR_ENTRY.toMillis());
|
||||||
@@ -80,6 +84,7 @@ public final class LogUtils {
|
|||||||
final List<AppUsageEventEntity> eventList = dao.getAllAfter(
|
final List<AppUsageEventEntity> eventList = dao.getAllAfter(
|
||||||
Clock.systemUTC().millis() - DUMP_TIME_OFFSET_FOR_ENTRY.toMillis());
|
Clock.systemUTC().millis() - DUMP_TIME_OFFSET_FOR_ENTRY.toMillis());
|
||||||
eventList.stream().forEach(event -> writer.println(event));
|
eventList.stream().forEach(event -> writer.println(event));
|
||||||
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private LogUtils() {}
|
private LogUtils() {}
|
||||||
|
@@ -57,6 +57,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
mBatteryUsageBroadcastReceiver = new BatteryUsageBroadcastReceiver();
|
mBatteryUsageBroadcastReceiver = new BatteryUsageBroadcastReceiver();
|
||||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||||
|
DatabaseUtils.getSharedPreferences(mContext).edit().clear().apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -77,6 +78,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -94,6 +96,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -110,6 +113,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -126,6 +130,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
new Intent(Intent.ACTION_BATTERY_LEVEL_CHANGED));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
|
||||||
|
assertSharedPreferences(Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -139,6 +144,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -156,6 +162,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -172,6 +179,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||||
|
assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -188,6 +196,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||||
|
|
||||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
|
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
|
||||||
|
assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -202,6 +211,8 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA));
|
||||||
|
|
||||||
assertThat(BatteryDiffEntry.sValidForRestriction).isEmpty();
|
assertThat(BatteryDiffEntry.sValidForRestriction).isEmpty();
|
||||||
|
assertSharedPreferences(
|
||||||
|
BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -216,6 +227,8 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
new Intent(BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA));
|
new Intent(BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA));
|
||||||
|
|
||||||
assertThat(BatteryDiffEntry.sValidForRestriction).isNotEmpty();
|
assertThat(BatteryDiffEntry.sValidForRestriction).isNotEmpty();
|
||||||
|
assertSharedPreferences(
|
||||||
|
BatteryUsageBroadcastReceiver.ACTION_CLEAR_BATTERY_CACHE_DATA);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Intent getBatteryIntent(int level, int status) {
|
private static Intent getBatteryIntent(int level, int status) {
|
||||||
@@ -225,4 +238,9 @@ public final class BatteryUsageBroadcastReceiverTest {
|
|||||||
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
|
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertSharedPreferences(String preferenceKey) {
|
||||||
|
assertThat(DatabaseUtils.getSharedPreferences(mContext).contains(preferenceKey))
|
||||||
|
.isTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,6 +46,8 @@ import org.mockito.MockitoAnnotations;
|
|||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -73,6 +75,7 @@ public final class DatabaseUtilsTest {
|
|||||||
doReturn(mMockContentResolver).when(mContext).getContentResolver();
|
doReturn(mMockContentResolver).when(mContext).getContentResolver();
|
||||||
doReturn(mPackageManager).when(mMockContext).getPackageManager();
|
doReturn(mPackageManager).when(mMockContext).getPackageManager();
|
||||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||||
|
DatabaseUtils.getSharedPreferences(mContext).edit().clear().apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -396,6 +399,38 @@ public final class DatabaseUtilsTest {
|
|||||||
assertThat(batteryHistMap).isEmpty();
|
assertThat(batteryHistMap).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void recordDateTime_writeDataIntoSharedPreferences() {
|
||||||
|
final String preferenceKey = "test_preference_key";
|
||||||
|
DatabaseUtils.recordDateTime(mContext, preferenceKey);
|
||||||
|
|
||||||
|
assertThat(DatabaseUtils.getSharedPreferences(mContext).contains(preferenceKey))
|
||||||
|
.isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dump_dumpExpectedData() {
|
||||||
|
DatabaseUtils.recordDateTime(mContext,
|
||||||
|
Intent.ACTION_BATTERY_LEVEL_CHANGED);
|
||||||
|
DatabaseUtils.recordDateTime(mContext,
|
||||||
|
BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
|
||||||
|
DatabaseUtils.recordDateTime(mContext,
|
||||||
|
DatabaseUtils.KEY_LAST_LOAD_FULL_CHARGE_TIME);
|
||||||
|
DatabaseUtils.recordDateTime(mContext,
|
||||||
|
DatabaseUtils.KEY_LAST_UPLOAD_FULL_CHARGE_TIME);
|
||||||
|
final StringWriter stringWriter = new StringWriter();
|
||||||
|
final PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||||
|
|
||||||
|
DatabaseUtils.dump(mContext, printWriter);
|
||||||
|
|
||||||
|
String dumpContent = stringWriter.toString();
|
||||||
|
assertThat(dumpContent.contains("BatteryLevelChanged")).isTrue();
|
||||||
|
assertThat(dumpContent.contains("BatteryUnplugging")).isTrue();
|
||||||
|
assertThat(dumpContent.contains("ClearBatteryCacheData")).isTrue();
|
||||||
|
assertThat(dumpContent.contains("LastLoadFullChargeTime")).isTrue();
|
||||||
|
assertThat(dumpContent.contains("LastUploadFullChargeTime")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
private static void verifyBatteryEntryContentValues(
|
private static void verifyBatteryEntryContentValues(
|
||||||
double consumedPower, ContentValues values) {
|
double consumedPower, ContentValues values) {
|
||||||
final BatteryInformation batteryInformation =
|
final BatteryInformation batteryInformation =
|
||||||
|
Reference in New Issue
Block a user