Migrate robolectric tests to junit tests

This change do the 2 things:

1. Add new junit tests files which replace robolectric
   RobolectricTestRunner & RuntimeEnvironment with
   AndroidX objects without problem.
2. Remove the robolectric test files which have it's new junit files.

This change migrate 103 files, there are still 1209
files to go.

Bug: 174728471
Test: atest
      make RunSettingsRoboTests
Change-Id: I15ed3f4745b85862f720aabbf710ce1475aced93
This commit is contained in:
Arc Wang
2020-12-09 17:03:43 +08:00
parent f705f7933e
commit 62c78ff3fa
108 changed files with 3082 additions and 338 deletions

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2020 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.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.provider.Settings.Secure;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class BatterySaverScheduleRadioButtonsControllerTest {
private Context mContext;
private ContentResolver mResolver;
private BatterySaverScheduleRadioButtonsController mController;
private BatterySaverScheduleSeekBarController mSeekBarController;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mSeekBarController = new BatterySaverScheduleSeekBarController(mContext);
mController = new BatterySaverScheduleRadioButtonsController(
mContext, mSeekBarController);
mResolver = mContext.getContentResolver();
}
@Test
public void getDefaultKey_routine_returnsCorrectValue() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_ROUTINE);
}
@Test
public void getDefaultKey_automatic_returnsCorrectValue() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE);
}
@Test
public void getDefaultKey_none_returnsCorrectValue() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE);
}
@Test
public void setDefaultKey_any_defaultsToNoScheduleIfWarningNotSeen() {
Secure.putString(
mContext.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
mController.setDefaultKey(BatterySaverScheduleRadioButtonsController.KEY_ROUTINE);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE);
}
@Test
public void setDefaultKey_percentage_shouldSuppressNotification() {
Secure.putInt(
mContext.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
mController.setDefaultKey(BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE);
final int result = Settings.Secure.getInt(mResolver,
Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0);
assertThat(result).isEqualTo(1);
}
@Test
public void setDefaultKey_routine_shouldSuppressNotification() {
Secure.putInt(
mContext.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
mController.setDefaultKey(BatterySaverScheduleRadioButtonsController.KEY_ROUTINE);
final int result = Settings.Secure.getInt(mResolver,
Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0);
assertThat(result).isEqualTo(1);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2020 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.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.Global;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class BatterySaverStickyPreferenceControllerTest {
private static final String PREF_KEY = "battery_saver_sticky";
private Context mContext;
private BatterySaverStickyPreferenceController mController;
@Before
public void setup() {
mContext = ApplicationProvider.getApplicationContext();
mController = new BatterySaverStickyPreferenceController(mContext, PREF_KEY);
}
private int getAutoDisableSetting() {
return Settings.Global.getInt(mContext.getContentResolver(),
Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
1);
}
@Test
public void testOnPreferenceChange_turnOnAutoOff_autoDisableOn() {
mController.setChecked(true);
final int isOn = getAutoDisableSetting();
assertThat(isOn).isEqualTo(1);
}
@Test
public void testOnPreferenceChange_TurnOffAutoOff_autoDisableOff() {
mController.setChecked(false);
final int isOn = getAutoDisableSetting();
assertThat(isOn).isEqualTo(0);
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2020 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.batterytip;
import static com.google.common.truth.Truth.assertThat;
import android.os.Parcel;
import android.text.format.DateUtils;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RunWith(AndroidJUnit4.class)
public class AppInfoTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final int TYPE_WAKELOCK =
StatsManagerConfig.AnomalyType.EXCESSIVE_WAKELOCK_ALL_SCREEN_OFF;
private static final int TYPE_WAKEUP =
StatsManagerConfig.AnomalyType.EXCESSIVE_WAKEUPS_IN_BACKGROUND;
private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS;
private static final int UID = 3452;
private AppInfo mAppInfo;
@Before
public void setUp() {
mAppInfo = new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.addAnomalyType(TYPE_WAKELOCK)
.addAnomalyType(TYPE_WAKEUP)
.setScreenOnTimeMs(SCREEN_TIME_MS)
.setUid(UID)
.build();
}
@Test
public void testParcel() {
Parcel parcel = Parcel.obtain();
mAppInfo.writeToParcel(parcel, mAppInfo.describeContents());
parcel.setDataPosition(0);
final AppInfo appInfo = new AppInfo(parcel);
assertThat(appInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(appInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP);
assertThat(appInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(appInfo.uid).isEqualTo(UID);
}
@Test
public void testCompareTo_hasCorrectOrder() {
final AppInfo appInfo = new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.addAnomalyType(TYPE_WAKELOCK)
.setScreenOnTimeMs(SCREEN_TIME_MS + 100)
.build();
List<AppInfo> appInfos = new ArrayList<>();
appInfos.add(appInfo);
appInfos.add(mAppInfo);
Collections.sort(appInfos);
assertThat(appInfos.get(0).screenOnTimeMs).isLessThan(appInfos.get(1).screenOnTimeMs);
}
@Test
public void testBuilder() {
assertThat(mAppInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(mAppInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP);
assertThat(mAppInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(mAppInfo.uid).isEqualTo(UID);
}
}

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2020 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.batterytip;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import android.text.format.DateUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class BatteryTipPolicyTest {
private static final String BATTERY_TIP_CONSTANTS_VALUE = "battery_tip_enabled=true"
+ ",summary_enabled=false"
+ ",battery_saver_tip_enabled=false"
+ ",high_usage_enabled=true"
+ ",high_usage_app_count=5"
+ ",high_usage_period_ms=2000"
+ ",high_usage_battery_draining=30"
+ ",app_restriction_enabled=true"
+ ",reduced_battery_enabled=true"
+ ",reduced_battery_percent=30"
+ ",low_battery_enabled=false"
+ ",low_battery_hour=10"
+ ",data_history_retain_day=24"
+ ",excessive_bg_drain_percentage=25"
+ ",test_battery_saver_tip=true"
+ ",test_high_usage_tip=false"
+ ",test_smart_battery_tip=true"
+ ",test_low_battery_tip=true"
+ ",app_restriction_active_hour=6";
private Context mContext;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
}
@Test
public void testInit_usesConfigValues() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.BATTERY_TIP_CONSTANTS, BATTERY_TIP_CONSTANTS_VALUE);
final BatteryTipPolicy batteryTipPolicy = new BatteryTipPolicy(mContext);
assertThat(batteryTipPolicy.batteryTipEnabled).isTrue();
assertThat(batteryTipPolicy.summaryEnabled).isFalse();
assertThat(batteryTipPolicy.batterySaverTipEnabled).isFalse();
assertThat(batteryTipPolicy.highUsageEnabled).isTrue();
assertThat(batteryTipPolicy.highUsageAppCount).isEqualTo(5);
assertThat(batteryTipPolicy.highUsagePeriodMs).isEqualTo(2000);
assertThat(batteryTipPolicy.highUsageBatteryDraining).isEqualTo(30);
assertThat(batteryTipPolicy.appRestrictionEnabled).isTrue();
assertThat(batteryTipPolicy.appRestrictionActiveHour).isEqualTo(6);
assertThat(batteryTipPolicy.reducedBatteryEnabled).isTrue();
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(30);
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(24);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(25);
assertThat(batteryTipPolicy.testBatterySaverTip).isTrue();
assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
assertThat(batteryTipPolicy.testSmartBatteryTip).isTrue();
assertThat(batteryTipPolicy.testLowBatteryTip).isTrue();
}
@Test
public void testInit_defaultValues() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.BATTERY_TIP_CONSTANTS, "");
final BatteryTipPolicy batteryTipPolicy = new BatteryTipPolicy(mContext);
assertThat(batteryTipPolicy.batteryTipEnabled).isTrue();
assertThat(batteryTipPolicy.summaryEnabled).isTrue();
assertThat(batteryTipPolicy.batterySaverTipEnabled).isTrue();
assertThat(batteryTipPolicy.highUsageEnabled).isTrue();
assertThat(batteryTipPolicy.highUsageAppCount).isEqualTo(3);
assertThat(batteryTipPolicy.highUsagePeriodMs).isEqualTo(2 * DateUtils.HOUR_IN_MILLIS);
assertThat(batteryTipPolicy.highUsageBatteryDraining).isEqualTo(25);
assertThat(batteryTipPolicy.appRestrictionEnabled).isTrue();
assertThat(batteryTipPolicy.appRestrictionActiveHour).isEqualTo(24);
assertThat(batteryTipPolicy.reducedBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
assertThat(batteryTipPolicy.lowBatteryEnabled).isTrue();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(3);
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
assertThat(batteryTipPolicy.testBatterySaverTip).isFalse();
assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
assertThat(batteryTipPolicy.testSmartBatteryTip).isFalse();
assertThat(batteryTipPolicy.testLowBatteryTip).isFalse();
}
}