Add BugReportContentProvider to support new battery usage architecture.

Test: make RunSettingsRoboTests + manually
Bug: 256123932
Change-Id: I8cc48f5c66e5cbd958bdd73ff8e7d6e555bd9c3b
This commit is contained in:
Kuan Wang
2022-12-01 17:31:29 +08:00
parent 7c9f11071c
commit bc4548b29e
4 changed files with 254 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
/*
* 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.testutils.BatteryTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
/** Tests of {@link BugReportContentProvider}. */
@RunWith(RobolectricTestRunner.class)
public final class BugReportContentProviderTest {
private static final String PACKAGE_NAME1 = "com.android.settings";
private static final String PACKAGE_NAME2 = "com.android.systemui";
private Context mContext;
private PrintWriter mPrintWriter;
private StringWriter mStringWriter;
private BugReportContentProvider mBugReportContentProvider;
@Before
public void setUp() {
mStringWriter = new StringWriter();
mPrintWriter = new PrintWriter(mStringWriter);
mContext = ApplicationProvider.getApplicationContext();
mBugReportContentProvider = new BugReportContentProvider();
mBugReportContentProvider.attachInfo(mContext, /*info=*/ null);
// Inserts fake data into database for testing.
BatteryTestUtils.setUpBatteryStateDatabase(mContext);
BatteryTestUtils.insertDataToBatteryStateDatabase(
mContext, System.currentTimeMillis(), PACKAGE_NAME1);
BatteryTestUtils.insertDataToBatteryStateDatabase(
mContext, System.currentTimeMillis(), PACKAGE_NAME2);
}
@Test
public void dump_nullContext_notDumpsBatteryUsageData() {
mBugReportContentProvider = new BugReportContentProvider();
mBugReportContentProvider.attachInfo(/*context=*/ null, /*info=*/ null);
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
assertThat(mStringWriter.toString()).isEmpty();
}
@Test
public void dump_inWorkProfileMode_notDumpsBatteryUsageData() {
BatteryTestUtils.setWorkProfile(mContext);
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
assertThat(mStringWriter.toString()).isEmpty();
}
@Test
public void dump_dumpsBatteryUsageHistory() {
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
String dumpContent = mStringWriter.toString();
assertThat(dumpContent).contains("DatabaseHistory");
assertThat(dumpContent).contains(PACKAGE_NAME1);
assertThat(dumpContent).contains(PACKAGE_NAME2);
assertThat(dumpContent).contains("distinct timestamp count:2");
}
}