Merge "Use new time format in BatteryInfo"

This commit is contained in:
Lei Yu
2017-06-05 17:19:04 +00:00
committed by Android (Google) Code Review
4 changed files with 46 additions and 66 deletions

View File

@@ -23,53 +23,51 @@ import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.SystemClock;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.AdditionalMatchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.spy;
@RunWith(RobolectricTestRunner.class)
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BatteryInfoTest {
private static final String STATUS_FULL = "Full";
private static final String STATUS_CHARGING_NO_TIME = "Charging";
private static final String STATUS_CHARGING_TIME = "Charging - 2h left";
private static final String STATUS_CHARGING_NO_TIME = "50% - charging";
private static final String STATUS_CHARGING_TIME = "50% - 0m until fully charged";
private static final int PLUGGED_IN = 1;
private static final long REMAINING_TIME_NULL = -1;
private static final long REMAINING_TIME = 2;
public static final String ENHANCED_STRING_SUFFIX = "left based on your usage";
private Intent mDisChargingBatteryBroadcast;
private Intent mChargingBatteryBroadcast;
private Context mRealContext;
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BatteryStats mBatteryStats;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Resources mResources;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
FakeFeatureFactory.setupForTest(mContext);
mDisChargingBatteryBroadcast = new Intent();
@@ -86,14 +84,6 @@ public class BatteryInfoTest {
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
when(mContext.getResources().getString(R.string.battery_info_status_full))
.thenReturn(STATUS_FULL);
when(mContext.getResources().getString(eq(R.string.power_charging), any(),
any())).thenReturn(STATUS_CHARGING_NO_TIME);
when(mContext.getResources().getString(eq(R.string.power_charging_duration), any(),
any())).thenReturn(STATUS_CHARGING_TIME);
mRealContext = RuntimeEnvironment.application;
}
@Test
@@ -112,7 +102,7 @@ public class BatteryInfoTest {
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
assertThat(info.chargeLabelString).isEqualTo(STATUS_CHARGING_TIME);
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_TIME);
}
@Test
@@ -121,7 +111,7 @@ public class BatteryInfoTest {
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
assertThat(info.chargeLabelString).isEqualTo(STATUS_CHARGING_NO_TIME);
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_NO_TIME);
}
@Test
@@ -134,12 +124,6 @@ public class BatteryInfoTest {
@Test
public void testGetBatteryInfo_basedOnUsageTrue_usesUsageString() {
doReturn(mResources).when(mContext).getResources();
when(mResources.getString(eq(R.string.battery_info_status_full))).thenReturn("");
when(mResources.getString(eq(R.string.power_remaining_duration_only_enhanced), any()))
.thenReturn(ENHANCED_STRING_SUFFIX);
when(mResources.getString(eq(R.string.power_remaining_duration_only_short_enhanced), any()))
.thenReturn(ENHANCED_STRING_SUFFIX);
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */,
1000, true /* basedOnUsage */);
@@ -147,18 +131,12 @@ public class BatteryInfoTest {
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */,
1000, true /* basedOnUsage */);
assertThat(info.remainingLabel).contains(ENHANCED_STRING_SUFFIX);
assertThat(info2.remainingLabel).contains(ENHANCED_STRING_SUFFIX);
assertThat(info.remainingLabel.toString()).contains(ENHANCED_STRING_SUFFIX);
assertThat(info2.remainingLabel.toString()).contains(ENHANCED_STRING_SUFFIX);
}
@Test
public void testGetBatteryInfo_basedOnUsageFalse_usesDefaultString() {
doReturn(mResources).when(mContext).getResources();
when(mResources.getString(eq(R.string.battery_info_status_full))).thenReturn("");
when(mResources.getString(not(eq(R.string.power_remaining_duration_only_enhanced)), any()))
.thenReturn(ENHANCED_STRING_SUFFIX);
when(mResources.getString(not(eq(R.string.power_remaining_duration_only_short_enhanced)),
any())).thenReturn("");
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */,
1000, false /* basedOnUsage */);
@@ -166,7 +144,7 @@ public class BatteryInfoTest {
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */,
1000, false /* basedOnUsage */);
assertThat(info.remainingLabel).doesNotContain(ENHANCED_STRING_SUFFIX);
assertThat(info2.remainingLabel).doesNotContain(ENHANCED_STRING_SUFFIX);
assertThat(info.remainingLabel.toString()).doesNotContain(ENHANCED_STRING_SUFFIX);
assertThat(info2.remainingLabel.toString()).doesNotContain(ENHANCED_STRING_SUFFIX);
}
}