Copy Battery Usage Database from SettingsIntelligence to Settings.

Bug: 253395332
Test: make RunSettingsRoboTests + manually
Change-Id: Ibdd2ace10d9e0893b3d96b345d563307b1890df6
This commit is contained in:
Kuan Wang
2022-10-13 17:32:10 +08:00
parent 234689945d
commit f41ae57f17
7 changed files with 825 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
/*
* 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.db;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.database.Cursor;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.BatteryTestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.List;
/** Tests for {@link BatteryStateDao}. */
@RunWith(RobolectricTestRunner.class)
public final class BatteryStateDaoTest {
private static final int CURSOR_COLUMN_SIZE = 19;
private static final long TIMESTAMP1 = System.currentTimeMillis();
private static final long TIMESTAMP2 = System.currentTimeMillis() + 2;
private static final long TIMESTAMP3 = System.currentTimeMillis() + 4;
private static final String PACKAGE_NAME1 = "com.android.apps.settings";
private static final String PACKAGE_NAME2 = "com.android.apps.calendar";
private static final String PACKAGE_NAME3 = "com.android.apps.gmail";
private Context mContext;
private BatteryStateDatabase mDatabase;
private BatteryStateDao mBatteryStateDao;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mDatabase = BatteryTestUtils.setUpBatteryStateDatabase(mContext);
mBatteryStateDao = mDatabase.batteryStateDao();
BatteryTestUtils.insertDataToBatteryStateDatabase(mContext, TIMESTAMP3, PACKAGE_NAME3);
BatteryTestUtils.insertDataToBatteryStateDatabase(mContext, TIMESTAMP2, PACKAGE_NAME2);
BatteryTestUtils.insertDataToBatteryStateDatabase(
mContext, TIMESTAMP1, PACKAGE_NAME1, /*multiple=*/ true);
}
@After
public void closeDb() {
mDatabase.close();
BatteryStateDatabase.setBatteryStateDatabase(/*database=*/ null);
}
@Test
public void batteryStateDao_insertAll() throws Exception {
final List<BatteryState> states = mBatteryStateDao.getAllAfter(TIMESTAMP1);
assertThat(states).hasSize(2);
// Verifies the queried battery states.
assertBatteryState(states.get(0), TIMESTAMP3, PACKAGE_NAME3);
assertBatteryState(states.get(1), TIMESTAMP2, PACKAGE_NAME2);
}
@Test
public void batteryStateDao_getCursorAfter() throws Exception {
final Cursor cursor = mBatteryStateDao.getCursorAfter(TIMESTAMP2);
assertThat(cursor.getCount()).isEqualTo(2);
assertThat(cursor.getColumnCount()).isEqualTo(CURSOR_COLUMN_SIZE);
// Verifies the queried first battery state.
cursor.moveToFirst();
assertThat(cursor.getString(4 /*packageName*/)).isEqualTo(PACKAGE_NAME3);
// Verifies the queried second battery state.
cursor.moveToNext();
assertThat(cursor.getString(4 /*packageName*/)).isEqualTo(PACKAGE_NAME2);
}
@Test
public void batteryStateDao_clearAllBefore() throws Exception {
mBatteryStateDao.clearAllBefore(TIMESTAMP2);
final List<BatteryState> states = mBatteryStateDao.getAllAfter(0);
assertThat(states).hasSize(1);
// Verifies the queried battery state.
assertBatteryState(states.get(0), TIMESTAMP3, PACKAGE_NAME3);
}
@Test
public void batteryStateDao_clearAll() throws Exception {
assertThat(mBatteryStateDao.getAllAfter(0)).hasSize(3);
mBatteryStateDao.clearAll();
assertThat(mBatteryStateDao.getAllAfter(0)).isEmpty();
}
@Test
public void getInstance_createNewInstance() throws Exception {
BatteryStateDatabase.setBatteryStateDatabase(/*database=*/ null);
assertThat(BatteryStateDatabase.getInstance(mContext)).isNotNull();
}
@Test
public void getDistinctTimestampCount_returnsExpectedResult() {
assertThat(mBatteryStateDao.getDistinctTimestampCount(/*timestamp=*/ 0))
.isEqualTo(3);
assertThat(mBatteryStateDao.getDistinctTimestampCount(TIMESTAMP1))
.isEqualTo(2);
}
@Test
public void getDistinctTimestamps_returnsExpectedResult() {
final List<Long> timestamps =
mBatteryStateDao.getDistinctTimestamps(/*timestamp=*/ 0);
assertThat(timestamps).hasSize(3);
assertThat(timestamps).containsExactly(TIMESTAMP1, TIMESTAMP2, TIMESTAMP3);
}
private static void assertBatteryState(
BatteryState state, long timestamp, String packageName) {
assertThat(state.timestamp).isEqualTo(timestamp);
assertThat(state.packageName).isEqualTo(packageName);
}
}

View File

@@ -0,0 +1,98 @@
/*
* 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.db;
import static com.google.common.truth.Truth.assertThat;
import android.content.Intent;
import android.os.BatteryManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link BatteryState}. */
@RunWith(RobolectricTestRunner.class)
public final class BatteryStateTest {
private static final int BATTERY_LEVEL = 45;
private static final int BATTERY_STATUS = BatteryManager.BATTERY_STATUS_FULL;
private static final int BATTERY_HEALTH = BatteryManager.BATTERY_HEALTH_COLD;
private Intent mBatteryIntent;
@Before
public void setUp() {
mBatteryIntent = new Intent(Intent.ACTION_BATTERY_CHANGED);
// Inserts the battery states into intent.
mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL);
mBatteryIntent.putExtra(BatteryManager.EXTRA_STATUS, BATTERY_STATUS);
mBatteryIntent.putExtra(BatteryManager.EXTRA_HEALTH, BATTERY_HEALTH);
}
@Test
public void testBuilder_returnsExpectedResult() {
mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100);
BatteryState state = create(mBatteryIntent);
// Verifies the app relative information.
assertThat(state.uid).isEqualTo(1001L);
assertThat(state.userId).isEqualTo(100L);
assertThat(state.appLabel).isEqualTo("Settings");
assertThat(state.packageName).isEqualTo("com.android.settings");
assertThat(state.isHidden).isTrue();
assertThat(state.bootTimestamp).isEqualTo(101L);
assertThat(state.timestamp).isEqualTo(100001L);
// Verifies the battery relative information.
assertThat(state.totalPower).isEqualTo(100);
assertThat(state.consumePower).isEqualTo(3);
assertThat(state.percentOfTotal).isEqualTo(10);
assertThat(state.foregroundUsageTimeInMs).isEqualTo(60000);
assertThat(state.backgroundUsageTimeInMs).isEqualTo(10000);
assertThat(state.drainType).isEqualTo(1);
assertThat(state.consumerType).isEqualTo(2);
assertThat(state.batteryLevel).isEqualTo(BATTERY_LEVEL);
assertThat(state.batteryStatus).isEqualTo(BATTERY_STATUS);
assertThat(state.batteryHealth).isEqualTo(BATTERY_HEALTH);
}
@Test
public void create_withoutBatteryScale_returnsStateWithInvalidLevel() {
BatteryState state = create(mBatteryIntent);
assertThat(state.batteryLevel).isEqualTo(-1);
}
private static BatteryState create(Intent intent) {
return BatteryState.newBuilder()
.setUid(1001L)
.setUserId(100L)
.setAppLabel("Settings")
.setPackageName("com.android.settings")
.setIsHidden(true)
.setBootTimestamp(101L)
.setTimestamp(100001L)
.setTotalPower(100f)
.setConsumePower(3f)
.setPercentOfTotal(10f)
.setForegroundUsageTimeInMs(60000)
.setBackgroundUsageTimeInMs(10000)
.setDrainType(1)
.setConsumerType(2)
.setBatteryIntent(intent)
.build();
}
}

View File

@@ -16,8 +16,21 @@
package com.android.settings.testutils;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.UserManager;
import androidx.room.Room;
import com.android.settings.fuelgauge.batteryusage.db.BatteryState;
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDao;
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
import com.google.common.collect.ImmutableList;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowUserManager;
public class BatteryTestUtils {
@@ -37,6 +50,65 @@ public class BatteryTestUtils {
BatteryManager.BATTERY_STATUS_DISCHARGING);
}
/** Sets the work profile mode. */
public static void setWorkProfile(Context context) {
final UserManager userManager = context.getSystemService(UserManager.class);
Shadows.shadowOf(userManager).setManagedProfile(true);
// Changes out of the default system user so isSystemUser() returns false.
final int userId = 1001;
Shadows.shadowOf(userManager)
.addUser(userId, "name", /*flags=*/ ShadowUserManager.FLAG_PRIMARY);
Shadows.shadowOf(userManager).switchUser(userId);
}
/** Creates and sets up the in-memory {@link BatteryStateDatabase}. */
public static BatteryStateDatabase setUpBatteryStateDatabase(Context context) {
final BatteryStateDatabase inMemoryDatabase =
Room.inMemoryDatabaseBuilder(context, BatteryStateDatabase.class)
.allowMainThreadQueries()
.build();
BatteryStateDatabase.setBatteryStateDatabase(inMemoryDatabase);
return inMemoryDatabase;
}
/** Inserts a fake data into the database for testing. */
public static void insertDataToBatteryStateDatabase(
Context context, long timestamp, String packageName) {
insertDataToBatteryStateDatabase(context, timestamp, packageName, /*multiple=*/ false);
}
/** Inserts a fake data into the database for testing. */
public static void insertDataToBatteryStateDatabase(
Context context, long timestamp, String packageName, boolean multiple) {
final BatteryState state =
new BatteryState(
/*uid=*/ 1001L,
/*userId=*/ 100L,
/*appLabel=*/ "Settings",
packageName,
/*isHidden=*/ true,
/*bootTimestamp=*/ timestamp - 1,
timestamp,
/*zoneId=*/ "Europe/Paris",
/*totalPower=*/ 100f,
/*consumePower=*/ 0.3f,
/*percentOfTotal=*/ 10f,
/*foregroundUsageTimeInMs=*/ 60000,
/*backgroundUsageTimeInMs=*/ 10000,
/*drainType=*/ 1,
/*consumerType=*/ 2,
/*batteryLevel=*/ 31,
/*batteryStatus=*/ 0,
/*batteryHealth=*/ 0);
BatteryStateDao dao =
BatteryStateDatabase.getInstance(context).batteryStateDao();
if (multiple) {
dao.insertAll(ImmutableList.of(state));
} else {
dao.insert(state);
}
}
private static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
Intent intent = new Intent();
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);