Merge "Dump the BatteryReattribute from the database into bugreport (2/5)" into main

This commit is contained in:
YK Hung
2024-06-19 02:29:31 +00:00
committed by Android (Google) Code Review
4 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2024 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.batteryusage.BatteryReattribute;
import com.android.settings.fuelgauge.batteryusage.db.BatteryReattributeDao;
import com.android.settings.fuelgauge.batteryusage.db.BatteryReattributeEntity;
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
import com.android.settings.testutils.BatteryTestUtils;
import org.junit.After;
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 LogUtilsTest {
private StringWriter mTestStringWriter;
private PrintWriter mTestPrintWriter;
private Context mContext;
private BatteryStateDatabase mDatabase;
private BatteryReattributeDao mBatteryReattributeDao;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mTestStringWriter = new StringWriter();
mTestPrintWriter = new PrintWriter(mTestStringWriter);
mDatabase = BatteryTestUtils.setUpBatteryStateDatabase(mContext);
mBatteryReattributeDao = mDatabase.batteryReattributeDao();
}
@After
public void cleanUp() {
mBatteryReattributeDao.clearAll();
}
@Test
public void dumpBatteryReattributeDatabaseHist_noData_printExpectedResult() {
LogUtils.dumpBatteryReattributeDatabaseHist(mBatteryReattributeDao, mTestPrintWriter);
assertThat(mTestStringWriter.toString())
.contains("BatteryReattribute DatabaseHistory:");
}
@Test
public void dumpBatteryReattributeDatabaseHist_printExpectedResult() {
final long currentTimeMillis = System.currentTimeMillis();
// Insert the first testing data.
final BatteryReattribute batteryReattribute1 =
BatteryReattribute.newBuilder()
.setTimestampStart(currentTimeMillis - 20000)
.setTimestampEnd(currentTimeMillis - 10000)
.putReattributeData(1001, 0.1f)
.putReattributeData(1002, 0.99f)
.build();
mBatteryReattributeDao.insert(new BatteryReattributeEntity(batteryReattribute1));
// Insert the second testing data.
final BatteryReattribute batteryReattribute2 =
BatteryReattribute.newBuilder()
.setTimestampStart(currentTimeMillis - 40000)
.setTimestampEnd(currentTimeMillis - 20000)
.putReattributeData(1003, 1f)
.build();
mBatteryReattributeDao.insert(new BatteryReattributeEntity(batteryReattribute2));
LogUtils.dumpBatteryReattributeDatabaseHist(mBatteryReattributeDao, mTestPrintWriter);
final String result = mTestStringWriter.toString();
assertThat(result).contains("BatteryReattribute DatabaseHistory:");
assertThat(result).contains(batteryReattribute1.toString());
assertThat(result).contains(batteryReattribute2.toString());
}
}