Fix failed test case for BatteryTip in Settings
- Replace the deprecated mocktio rule with MockitoJUnit.rule
- Fix the failed test cases
- Update the testing convention
Bug: 323627875
Test: atest SettingsRoboTests:com.android.settings.fuelgauge.batterytip
Change-Id: I25a39c8e7ad07482352c1e2152d8e16555429ab1
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceControllerTest.java
index eeedccc..c05d9ed 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceControllerTest.java
@@ -20,30 +20,29 @@
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Bundle;
-import android.text.format.DateUtils;
import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
import com.android.settings.SettingsActivity;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
-import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.CardPreference;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@@ -53,8 +52,8 @@
private static final String KEY_PREF = "battery_tip";
private static final String KEY_TIP = "key_battery_tip";
- private static final long AVERAGE_TIME_MS = DateUtils.HOUR_IN_MILLIS;
+ @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTipPreferenceController.BatteryTipListener mBatteryTipListener;
@Mock private PreferenceScreen mPreferenceScreen;
@Mock private BatteryTip mBatteryTip;
@@ -64,21 +63,16 @@
private Context mContext;
private CardPreference mCardPreference;
private BatteryTipPreferenceController mBatteryTipPreferenceController;
- private List<BatteryTip> mOldBatteryTips;
private List<BatteryTip> mNewBatteryTips;
- private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
- mContext = RuntimeEnvironment.application;
+ mContext = ApplicationProvider.getApplicationContext();
- mCardPreference = spy(new CardPreference(mContext));
+ mCardPreference = new CardPreference(mContext);
when(mPreferenceScreen.getContext()).thenReturn(mContext);
doReturn(mCardPreference).when(mPreferenceScreen).findPreference(KEY_PREF);
- mFeatureFactory = FakeFeatureFactory.setupForTest();
- mOldBatteryTips = new ArrayList<>();
mNewBatteryTips = new ArrayList<>();
mBatteryTipPreferenceController = buildBatteryTipPreferenceController();
@@ -87,32 +81,32 @@
}
@Test
- public void testDisplayPreference_isInvisible() {
+ public void displayPreference_isInvisible() {
mBatteryTipPreferenceController.displayPreference(mPreferenceScreen);
assertThat(mCardPreference.isVisible()).isFalse();
}
@Test
- public void testUpdateBatteryTips_tipsStateInvisible_isInvisible() {
+ public void updateBatteryTips_tipsStateInvisible_isInvisible() {
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
assertThat(mCardPreference.isVisible()).isFalse();
}
@Test
- public void testGetCurrentBatteryTip_noTips_isNull() {
+ public void getCurrentBatteryTip_noTips_isNull() {
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
}
@Test
- public void testGetCurrentBatteryTip_tipsInvisible_isNull() {
+ public void getCurrentBatteryTip_tipsInvisible_isNull() {
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
}
@Test
- public void testRestoreFromNull_shouldNotCrash() {
+ public void restoreFromNull_shouldNotCrash() {
final Bundle bundle = new Bundle();
// Battery tip list is null at this time
mBatteryTipPreferenceController.saveInstanceState(bundle);
@@ -124,7 +118,7 @@
}
@Test
- public void testHandlePreferenceTreeClick_noDialog_invokeCallback() {
+ public void handlePreferenceTreeClick_noDialog_invokeCallback() {
when(mBatteryTip.getType()).thenReturn(SMART_BATTERY_MANAGER);
List<BatteryTip> batteryTips = new ArrayList<>();
batteryTips.add(mBatteryTip);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java
index 3f89f9b..4efd850 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java
@@ -17,8 +17,6 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -27,6 +25,7 @@
import android.util.Log;
import androidx.preference.Preference;
+import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory;
@@ -34,12 +33,13 @@
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLog;
@RunWith(RobolectricTestRunner.class)
@@ -49,23 +49,22 @@
private FakeFeatureFactory mFeatureFactory;
private BatteryDefenderTip mBatteryDefenderTip;
private MetricsFeatureProvider mMetricsFeatureProvider;
+ private CardPreference mCardPreference;
+ @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTip mBatteryTip;
@Mock private Preference mPreference;
- @Mock private CardPreference mCardPreference;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
+ mContext = ApplicationProvider.getApplicationContext();
mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
- mContext = RuntimeEnvironment.application;
mBatteryDefenderTip =
- new BatteryDefenderTip(BatteryTip.StateType.NEW, false /* isPluggedIn */);
+ new BatteryDefenderTip(BatteryTip.StateType.NEW, /* isPluggedIn= */ false);
+ mCardPreference = new CardPreference(mContext);
when(mPreference.getContext()).thenReturn(mContext);
- when(mCardPreference.getContext()).thenReturn(mContext);
}
@Test
@@ -87,7 +86,7 @@
}
@Test
- public void testLog_logMetric() {
+ public void log_logMetric() {
mBatteryDefenderTip.updateState(mBatteryTip);
mBatteryDefenderTip.log(mContext, mMetricsFeatureProvider);
@@ -108,7 +107,7 @@
mBatteryDefenderTip.updatePreference(mCardPreference);
- verify(mCardPreference).setPrimaryButtonText(expectedText);
+ assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo(expectedText);
}
@Test
@@ -117,46 +116,31 @@
mBatteryDefenderTip.updatePreference(mCardPreference);
- verify(mCardPreference).setSecondaryButtonText(expected);
+ assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo(expected);
}
@Test
public void updatePreference_shouldSetPrimaryButtonVisible() {
mBatteryDefenderTip.updatePreference(mCardPreference);
- verify(mCardPreference).setPrimaryButtonVisibility(true);
+ assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
}
@Test
public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() {
mBatteryDefenderTip =
- new BatteryDefenderTip(BatteryTip.StateType.NEW, true /* isPluggedIn */);
+ new BatteryDefenderTip(BatteryTip.StateType.NEW, /* isPluggedIn= */ true);
mBatteryDefenderTip.updatePreference(mCardPreference);
- verify(mCardPreference).setPrimaryButtonVisibility(true);
+ assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
}
@Test
public void updatePreference_whenNotCharging_setSecondaryButtonVisibleToBeFalse() {
mBatteryDefenderTip.updatePreference(mCardPreference);
- verify(mCardPreference).setSecondaryButtonVisibility(false);
- }
-
- @Test
- public void updatePreference_whenGetChargingStatusFailed_setSecondaryButtonVisibleToBeFalse() {
- fakeGetChargingStatusFailed();
-
- mBatteryDefenderTip.updatePreference(mCardPreference);
-
- verify(mCardPreference).setSecondaryButtonVisibility(false);
- }
-
- private void fakeGetChargingStatusFailed() {
- Context mockContext = mock(Context.class);
- when(mockContext.getString(anyInt())).thenReturn("fake_string");
- when(mCardPreference.getContext()).thenReturn(mockContext);
+ assertThat(mCardPreference.getSecondaryButtonVisibility()).isFalse();
}
private String getLastErrorLog() {
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
index ea72ff6..097f484 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
@@ -20,11 +20,10 @@
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
-import android.view.View;
import androidx.annotation.DrawableRes;
import androidx.preference.Preference;
-import androidx.preference.PreferenceViewHolder;
+import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.widget.CardPreference;
@@ -32,10 +31,12 @@
import com.android.settingslib.testutils.DrawableTestHelper;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@@ -47,13 +48,15 @@
private static final String SUMMARY = "summary";
@DrawableRes private static final int ICON_ID = R.drawable.ic_fingerprint;
+ @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
+
private Context mContext;
private TestBatteryTip mBatteryTip;
@Before
public void setUp() {
- mContext = RuntimeEnvironment.application;
mBatteryTip = new TestBatteryTip();
+ mContext = ApplicationProvider.getApplicationContext();
}
@Test
@@ -84,19 +87,14 @@
@Test
public void updatePreference_resetLayoutState() {
- mContext.setTheme(R.style.Theme_Settings);
- PreferenceViewHolder holder =
- PreferenceViewHolder.createInstanceForTests(
- View.inflate(
- mContext, R.layout.card_preference_layout, /* parent= */ null));
CardPreference cardPreference = new CardPreference(mContext);
- cardPreference.onBindViewHolder(holder);
cardPreference.setPrimaryButtonVisibility(true);
+ cardPreference.setSecondaryButtonVisibility(true);
mBatteryTip.updatePreference(cardPreference);
- View view = holder.findViewById(R.id.card_preference_buttons);
- assertThat(view.getVisibility()).isEqualTo(View.GONE);
+ assertThat(cardPreference.getPrimaryButtonVisibility()).isFalse();
+ assertThat(cardPreference.getSecondaryButtonVisibility()).isFalse();
}
@Test
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTipTest.java
index c66cf37..7a23332 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTipTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTipTest.java
@@ -25,6 +25,7 @@
import android.util.Log;
import androidx.preference.Preference;
+import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory;
@@ -32,12 +33,13 @@
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLog;
@RunWith(RobolectricTestRunner.class)
@@ -47,22 +49,21 @@
private FakeFeatureFactory mFeatureFactory;
private IncompatibleChargerTip mIncompatibleChargerTip;
private MetricsFeatureProvider mMetricsFeatureProvider;
+ private CardPreference mCardPreference;
+ @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTip mBatteryTip;
@Mock private Preference mPreference;
- @Mock private CardPreference mCardPreference;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
- mContext = RuntimeEnvironment.application;
+ mContext = ApplicationProvider.getApplicationContext();
mIncompatibleChargerTip = new IncompatibleChargerTip(BatteryTip.StateType.NEW);
+ mCardPreference = new CardPreference(mContext);
when(mPreference.getContext()).thenReturn(mContext);
- when(mCardPreference.getContext()).thenReturn(mContext);
}
@Test
@@ -107,13 +108,13 @@
mIncompatibleChargerTip.updatePreference(mCardPreference);
- verify(mCardPreference).setPrimaryButtonText(expected);
+ assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo(expected);
}
@Test
public void updatePreference_shouldSetSecondaryButtonVisible() {
mIncompatibleChargerTip.updatePreference(mCardPreference);
- verify(mCardPreference).setPrimaryButtonVisibility(true);
+ assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
}
private String getLastErrorLog() {