Add a mechanism to log battery usage periodic job events
Example history log: Jul 07, 2023, 15:28:51 SCHEDULE_JOB triggerTime=Jul 07, 2023, 16:00:00 Jul 07, 2023, 15:32:16 FETCH_USAGE_DATA Jul 07, 2023, 15:32:17 INSERT_USAGE_DATA size=37 Jul 07, 2023, 15:43:45 FETCH_USAGE_DATA Jul 07, 2023, 15:43:48 INSERT_USAGE_DATA size=47 Jul 07, 2023, 15:43:49 SCHEDULE_JOB triggerTime=Jul 07, 2023, 16:00:00 Bug: 284893240 Test: make test RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge Change-Id: I45a1ce0ce9b70f095702727e53d7b7ce8824abdb
This commit is contained in:
@@ -33,7 +33,7 @@ import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatteryHistoricalLogUtilTest {
|
||||
public final class BatteryOptimizeLogUtilsTest {
|
||||
|
||||
private final StringWriter mTestStringWriter = new StringWriter();
|
||||
private final PrintWriter mTestPrintWriter = new PrintWriter(mTestStringWriter);
|
||||
@@ -43,19 +43,19 @@ public final class BatteryHistoricalLogUtilTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
BatteryHistoricalLogUtil.getSharedPreferences(mContext).edit().clear().commit();
|
||||
BatteryOptimizeLogUtils.getSharedPreferences(mContext).edit().clear().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printHistoricalLog_withDefaultLogs() {
|
||||
BatteryHistoricalLogUtil.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
assertThat(mTestStringWriter.toString()).contains("nothing to dump");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeLog_withExpectedLogs() {
|
||||
BatteryHistoricalLogUtil.writeLog(mContext, Action.APPLY, "pkg1", "logs");
|
||||
BatteryHistoricalLogUtil.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
BatteryOptimizeLogUtils.writeLog(mContext, Action.APPLY, "pkg1", "logs");
|
||||
BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
|
||||
assertThat(mTestStringWriter.toString()).contains(
|
||||
"pkg1\taction:APPLY\tevent:logs");
|
||||
@@ -63,21 +63,27 @@ public final class BatteryHistoricalLogUtilTest {
|
||||
|
||||
@Test
|
||||
public void writeLog_multipleLogs_withCorrectCounts() {
|
||||
for (int i = 0; i < BatteryHistoricalLogUtil.MAX_ENTRIES; i++) {
|
||||
BatteryHistoricalLogUtil.writeLog(mContext, Action.LEAVE, "pkg" + i, "logs");
|
||||
final int expectedCount = 10;
|
||||
for (int i = 0; i < expectedCount; i++) {
|
||||
BatteryOptimizeLogUtils.writeLog(mContext, Action.LEAVE, "pkg" + i, "logs");
|
||||
}
|
||||
BatteryHistoricalLogUtil.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
|
||||
assertThat(mTestStringWriter.toString().split("LEAVE").length).isEqualTo(41);
|
||||
assertActionCount("LEAVE", expectedCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeLog_overMaxEntriesLogs_withCorrectCounts() {
|
||||
for (int i = 0; i < BatteryHistoricalLogUtil.MAX_ENTRIES + 10; i++) {
|
||||
BatteryHistoricalLogUtil.writeLog(mContext, Action.RESET, "pkg" + i, "logs");
|
||||
for (int i = 0; i < BatteryOptimizeLogUtils.MAX_ENTRIES + 10; i++) {
|
||||
BatteryOptimizeLogUtils.writeLog(mContext, Action.RESET, "pkg" + i, "logs");
|
||||
}
|
||||
BatteryHistoricalLogUtil.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
|
||||
|
||||
assertThat(mTestStringWriter.toString().split("RESET").length).isEqualTo(41);
|
||||
assertActionCount("RESET", BatteryOptimizeLogUtils.MAX_ENTRIES);
|
||||
}
|
||||
|
||||
private void assertActionCount(String token, int count) {
|
||||
final String dumpResults = mTestStringWriter.toString();
|
||||
assertThat(dumpResults.split(token).length).isEqualTo(count + 1);
|
||||
}
|
||||
}
|
@@ -40,6 +40,7 @@ import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.batteryusage.BatteryEntry.NameAndIcon;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -232,6 +233,7 @@ public class BatteryEntryTest {
|
||||
assertThat(entry.getTimeInBackgroundMs()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testUidCache_switchLocale_shouldCleanCache() {
|
||||
Locale.setDefault(new Locale("en_US"));
|
||||
|
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.batteryusage.bugreport;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryUsageHistoricalLogEntry.Action;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatteryUsageLogUtilsTest {
|
||||
|
||||
private StringWriter mTestStringWriter;
|
||||
private PrintWriter mTestPrintWriter;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mTestStringWriter = new StringWriter();
|
||||
mTestPrintWriter = new PrintWriter(mTestStringWriter);
|
||||
BatteryUsageLogUtils.getSharedPreferences(mContext).edit().clear().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printHistoricalLog_withDefaultLogs() {
|
||||
final String expectedInformation = "nothing to dump";
|
||||
// Environment checking.
|
||||
assertThat(mTestStringWriter.toString().contains(expectedInformation)).isFalse();
|
||||
|
||||
BatteryUsageLogUtils.printHistoricalLog(mContext, mTestPrintWriter);
|
||||
assertThat(mTestStringWriter.toString()).contains(expectedInformation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeLog_multipleLogs_withCorrectCounts() {
|
||||
final int expectedCount = 10;
|
||||
for (int i = 0; i < expectedCount; i++) {
|
||||
BatteryUsageLogUtils.writeLog(mContext, Action.SCHEDULE_JOB, "");
|
||||
}
|
||||
BatteryUsageLogUtils.writeLog(mContext, Action.EXECUTE_JOB, "");
|
||||
|
||||
BatteryUsageLogUtils.printHistoricalLog(mContext, mTestPrintWriter);
|
||||
|
||||
assertActionCount("SCHEDULE_JOB", expectedCount);
|
||||
assertActionCount("EXECUTE_JOB", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeLog_overMaxEntriesLogs_withCorrectCounts() {
|
||||
BatteryUsageLogUtils.writeLog(mContext, Action.SCHEDULE_JOB, "");
|
||||
BatteryUsageLogUtils.writeLog(mContext, Action.SCHEDULE_JOB, "");
|
||||
for (int i = 0; i < BatteryUsageLogUtils.MAX_ENTRIES * 2; i++) {
|
||||
BatteryUsageLogUtils.writeLog(mContext, Action.EXECUTE_JOB, "");
|
||||
}
|
||||
|
||||
BatteryUsageLogUtils.printHistoricalLog(mContext, mTestPrintWriter);
|
||||
|
||||
final String dumpResults = mTestStringWriter.toString();
|
||||
assertThat(dumpResults.contains("SCHEDULE_JOB")).isFalse();
|
||||
assertActionCount("EXECUTE_JOB", BatteryUsageLogUtils.MAX_ENTRIES);
|
||||
}
|
||||
|
||||
private void assertActionCount(String token, int count) {
|
||||
final String dumpResults = mTestStringWriter.toString();
|
||||
assertThat(dumpResults.split(token).length).isEqualTo(count + 1);
|
||||
}
|
||||
}
|
@@ -87,6 +87,7 @@ public final class BugReportContentProviderTest {
|
||||
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
|
||||
|
||||
String dumpContent = mStringWriter.toString();
|
||||
assertThat(dumpContent).contains("Battery PeriodicJob History");
|
||||
assertThat(dumpContent).contains("Battery DatabaseHistory");
|
||||
assertThat(dumpContent).contains(PACKAGE_NAME1);
|
||||
assertThat(dumpContent).contains(PACKAGE_NAME2);
|
||||
|
Reference in New Issue
Block a user