1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 23 import androidx.test.core.app.ApplicationProvider; 24 25 import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 32 import java.io.PrintWriter; 33 import java.io.StringWriter; 34 35 @RunWith(RobolectricTestRunner.class) 36 public final class BatteryOptimizeLogUtilsTest { 37 38 private final StringWriter mTestStringWriter = new StringWriter(); 39 private final PrintWriter mTestPrintWriter = new PrintWriter(mTestStringWriter); 40 41 private Context mContext; 42 43 @Before setUp()44 public void setUp() { 45 mContext = ApplicationProvider.getApplicationContext(); 46 BatteryOptimizeLogUtils.getSharedPreferences(mContext).edit().clear().commit(); 47 } 48 49 @Test printHistoricalLog_withDefaultLogs()50 public void printHistoricalLog_withDefaultLogs() { 51 BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter); 52 assertThat(mTestStringWriter.toString()).contains("nothing to dump"); 53 } 54 55 @Test writeLog_withExpectedLogs()56 public void writeLog_withExpectedLogs() { 57 BatteryOptimizeLogUtils.writeLog(mContext, Action.APPLY, "pkg1", "logs"); 58 BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter); 59 60 assertThat(mTestStringWriter.toString()).contains("pkg1\taction:APPLY\tevent:logs"); 61 } 62 63 @Test writeLog_multipleLogs_withCorrectCounts()64 public void writeLog_multipleLogs_withCorrectCounts() { 65 final int expectedCount = 10; 66 for (int i = 0; i < expectedCount; i++) { 67 BatteryOptimizeLogUtils.writeLog(mContext, Action.LEAVE, "pkg" + i, "logs"); 68 } 69 BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter); 70 71 assertActionCount("LEAVE", expectedCount); 72 } 73 74 @Test writeLog_overMaxEntriesLogs_withCorrectCounts()75 public void writeLog_overMaxEntriesLogs_withCorrectCounts() { 76 for (int i = 0; i < BatteryOptimizeLogUtils.MAX_ENTRIES + 10; i++) { 77 BatteryOptimizeLogUtils.writeLog(mContext, Action.RESET, "pkg" + i, "logs"); 78 } 79 BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter); 80 81 assertActionCount("RESET", BatteryOptimizeLogUtils.MAX_ENTRIES); 82 } 83 assertActionCount(String token, int count)84 private void assertActionCount(String token, int count) { 85 final String dumpResults = mTestStringWriter.toString(); 86 assertThat(dumpResults.split(token).length).isEqualTo(count + 1); 87 } 88 } 89