Use external/robolectric-shadows/run_robotests.mk
This allows Settings to test against the latest framework changes. Also replaced TestConfig with traditional robolectric.properties. Bug: 73173204 Bug: 73892008 Test: make -j56 RunSettingsRoboTests Change-Id: I3135b4fa5f095ba79b282a76f45dd9baa2584bc7
This commit is contained in:
@@ -16,11 +16,11 @@
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -34,9 +34,8 @@ import android.app.LoaderManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.SparseArray;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -44,10 +43,8 @@ import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
@@ -63,6 +60,8 @@ import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
@@ -70,19 +69,14 @@ import org.robolectric.annotation.Config;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PowerUsageSummary}.
|
||||
*/
|
||||
// TODO: Improve this test class so that it starts up the real activity and fragment.
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH,
|
||||
sdk = TestConfig.SDK_VERSION,
|
||||
shadows = {
|
||||
SettingsShadowResources.class,
|
||||
SettingsShadowResources.SettingsShadowTheme.class,
|
||||
})
|
||||
@Config(shadows = {
|
||||
SettingsShadowResources.class,
|
||||
SettingsShadowResources.SettingsShadowTheme.class,
|
||||
})
|
||||
public class PowerUsageSummaryTest {
|
||||
private static final String STUB_STRING = "stub_string";
|
||||
|
||||
private static final int UID = 123;
|
||||
private static final int UID_2 = 234;
|
||||
private static final int POWER_MAH = 100;
|
||||
@@ -91,8 +85,8 @@ public class PowerUsageSummaryTest {
|
||||
TIME_SINCE_LAST_FULL_CHARGE_MS * 1000;
|
||||
private static final long USAGE_TIME_MS = 65 * 60 * 1000;
|
||||
private static final double TOTAL_POWER = 200;
|
||||
public static final String NEW_ML_EST_SUFFIX = "(New ML est)";
|
||||
public static final String OLD_EST_SUFFIX = "(Old est)";
|
||||
private static final String NEW_ML_EST_SUFFIX = "(New ML est)";
|
||||
private static final String OLD_EST_SUFFIX = "(Old est)";
|
||||
private static Intent sAdditionalBatteryInfoIntent;
|
||||
|
||||
@BeforeClass
|
||||
@@ -100,8 +94,6 @@ public class PowerUsageSummaryTest {
|
||||
sAdditionalBatteryInfoIntent = new Intent("com.example.app.ADDITIONAL_BATTERY_INFO");
|
||||
}
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private BatterySipper mNormalBatterySipper;
|
||||
@Mock
|
||||
@@ -117,16 +109,10 @@ public class PowerUsageSummaryTest {
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private BatteryStatsHelper mBatteryHelper;
|
||||
@Mock
|
||||
private PowerManager mPowerManager;
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock
|
||||
private LoaderManager mLoaderManager;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private AnomalyDetectionPolicy mAnomalyDetectionPolicy;
|
||||
@Mock
|
||||
private BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
|
||||
|
||||
private List<BatterySipper> mUsageList;
|
||||
@@ -143,11 +129,9 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
mRealContext = RuntimeEnvironment.application;
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
when(mContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
|
||||
|
||||
mScreenUsagePref = new PowerGaugePreference(mRealContext);
|
||||
mLastFullChargePref = new PowerGaugePreference(mRealContext);
|
||||
mFragment = spy(new TestFragment(mContext));
|
||||
mFragment = spy(new TestFragment(mRealContext));
|
||||
mFragment.initFeatureProvider();
|
||||
mBatteryMeterView = new BatteryMeterView(mRealContext);
|
||||
mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
|
||||
@@ -158,8 +142,8 @@ public class PowerUsageSummaryTest {
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.getAdditionalBatteryInfoIntent())
|
||||
.thenReturn(sAdditionalBatteryInfoIntent);
|
||||
when(mBatteryHelper.getTotalPower()).thenReturn(TOTAL_POWER);
|
||||
when(mBatteryHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn(
|
||||
TIME_SINCE_LAST_FULL_CHARGE_US);
|
||||
when(mBatteryHelper.getStats().computeBatteryRealtime(anyLong(), anyInt()))
|
||||
.thenReturn(TIME_SINCE_LAST_FULL_CHARGE_US);
|
||||
|
||||
when(mNormalBatterySipper.getUid()).thenReturn(UID);
|
||||
mNormalBatterySipper.totalPowerMah = POWER_MAH;
|
||||
@@ -191,7 +175,7 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateLastFullChargePreference_showCorrectSummary() {
|
||||
doReturn(mRealContext).when(mFragment).getContext();
|
||||
when(mFragment.getContext()).thenReturn(mRealContext);
|
||||
|
||||
mFragment.updateLastFullChargePreference(TIME_SINCE_LAST_FULL_CHARGE_MS);
|
||||
|
||||
@@ -201,11 +185,11 @@ public class PowerUsageSummaryTest {
|
||||
@Test
|
||||
public void testNonIndexableKeys_MatchPreferenceKeys() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final List<String> niks = PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER
|
||||
.getNonIndexableKeys(context);
|
||||
final List<String> niks =
|
||||
PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
|
||||
|
||||
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context,
|
||||
R.xml.power_usage_summary);
|
||||
final List<String> keys =
|
||||
XmlTestUtils.getKeysFromPreferenceXml(context, R.xml.power_usage_summary);
|
||||
|
||||
assertThat(keys).containsAllIn(niks);
|
||||
}
|
||||
@@ -214,8 +198,8 @@ public class PowerUsageSummaryTest {
|
||||
public void testPreferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final PowerUsageSummary fragment = new PowerUsageSummary();
|
||||
final List<String> preferenceScreenKeys = XmlTestUtils.getKeysFromPreferenceXml(context,
|
||||
fragment.getPreferenceScreenResId());
|
||||
final List<String> preferenceScreenKeys =
|
||||
XmlTestUtils.getKeysFromPreferenceXml(context, fragment.getPreferenceScreenResId());
|
||||
final List<String> preferenceKeys = new ArrayList<>();
|
||||
|
||||
for (AbstractPreferenceController controller : fragment.createPreferenceControllers(context)) {
|
||||
@@ -249,13 +233,29 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
mFragment.restartBatteryTipLoader();
|
||||
|
||||
verify(mLoaderManager).restartLoader(eq(PowerUsageSummary.BATTERY_TIP_LOADER),
|
||||
eq(Bundle.EMPTY), any());
|
||||
verify(mLoaderManager)
|
||||
.restartLoader(eq(PowerUsageSummary.BATTERY_TIP_LOADER), eq(Bundle.EMPTY), any());
|
||||
}
|
||||
|
||||
@Ignore("b/73892008")
|
||||
@Test
|
||||
public void testShowBothEstimates_summariesAreBothModified() {
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(any()))
|
||||
.thenReturn(true);
|
||||
doAnswer(new Answer() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
return mRealContext.getString(
|
||||
R.string.power_usage_old_debug, invocation.getArguments()[0]);
|
||||
}
|
||||
}).when(mFeatureFactory.powerUsageFeatureProvider).getOldEstimateDebugString(any());
|
||||
doAnswer(new Answer() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
return mRealContext.getString(
|
||||
R.string.power_usage_enhanced_debug, invocation.getArguments()[0]);
|
||||
}
|
||||
}).when(mFeatureFactory.powerUsageFeatureProvider).getEnhancedEstimateDebugString(any());
|
||||
|
||||
doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
|
||||
doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary1);
|
||||
mFragment.onLongClick(new View(mRealContext));
|
||||
@@ -269,6 +269,7 @@ public class PowerUsageSummaryTest {
|
||||
@Test
|
||||
public void testDebugMode() {
|
||||
doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isEstimateDebugEnabled();
|
||||
doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
|
||||
|
||||
mFragment.restartBatteryInfoLoader();
|
||||
ArgumentCaptor<View.OnLongClickListener> listener = ArgumentCaptor.forClass(
|
||||
@@ -310,5 +311,23 @@ public class PowerUsageSummaryTest {
|
||||
protected void refreshUi() {
|
||||
// Leave it empty for toggle apps menu test
|
||||
}
|
||||
|
||||
@Override
|
||||
void showBothEstimates() {
|
||||
List<BatteryInfo> fakeBatteryInfo = new ArrayList<>(2);
|
||||
BatteryInfo info1 = new BatteryInfo();
|
||||
info1.batteryLevel = 10;
|
||||
info1.remainingTimeUs = 10000;
|
||||
info1.discharging = true;
|
||||
|
||||
BatteryInfo info2 = new BatteryInfo();
|
||||
info2.batteryLevel = 10;
|
||||
info2.remainingTimeUs = 10000;
|
||||
info2.discharging = true;
|
||||
|
||||
fakeBatteryInfo.add(info1);
|
||||
fakeBatteryInfo.add(info2);
|
||||
updateViews(fakeBatteryInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user