• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
61                 "pkg1\taction:APPLY\tevent:logs");
62     }
63 
64     @Test
writeLog_multipleLogs_withCorrectCounts()65     public void writeLog_multipleLogs_withCorrectCounts() {
66         final int expectedCount = 10;
67         for (int i = 0; i < expectedCount; i++) {
68             BatteryOptimizeLogUtils.writeLog(mContext, Action.LEAVE, "pkg" + i, "logs");
69         }
70         BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
71 
72         assertActionCount("LEAVE", expectedCount);
73     }
74 
75     @Test
writeLog_overMaxEntriesLogs_withCorrectCounts()76     public void writeLog_overMaxEntriesLogs_withCorrectCounts() {
77         for (int i = 0; i < BatteryOptimizeLogUtils.MAX_ENTRIES + 10; i++) {
78             BatteryOptimizeLogUtils.writeLog(mContext, Action.RESET, "pkg" + i, "logs");
79         }
80         BatteryOptimizeLogUtils.printBatteryOptimizeHistoricalLog(mContext, mTestPrintWriter);
81 
82         assertActionCount("RESET", BatteryOptimizeLogUtils.MAX_ENTRIES);
83     }
84 
assertActionCount(String token, int count)85     private void assertActionCount(String token, int count) {
86         final String dumpResults = mTestStringWriter.toString();
87         assertThat(dumpResults.split(token).length).isEqualTo(count + 1);
88     }
89 }
90