Update Robolectric tests to use google truth

Bug: 31545801
Test: make RunSettingsRoboTests
Change-Id: Idb801d51b6a4dc02ea062bc32086e1a8fc28b94b
This commit is contained in:
Matthew Fritze
2016-09-16 11:14:33 -07:00
parent e590183dcc
commit 9a3dc4e2a8
7 changed files with 76 additions and 79 deletions

View File

@@ -8,7 +8,8 @@ LOCAL_SRC_FILES := $(call all-java-files-under, src)
# Include the testing libraries (JUnit4 + Robolectric libs). # Include the testing libraries (JUnit4 + Robolectric libs).
LOCAL_STATIC_JAVA_LIBRARIES := \ LOCAL_STATIC_JAVA_LIBRARIES := \
platform-system-robolectric platform-system-robolectric \
truth-prebuilt
LOCAL_JAVA_LIBRARIES := \ LOCAL_JAVA_LIBRARIES := \
junit4-target \ junit4-target \

View File

@@ -29,7 +29,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import static org.junit.Assert.assertTrue; import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -55,6 +55,6 @@ public class MetricsFeatureProviderTest {
MetricsFeatureProvider feature2 = MetricsFeatureProvider feature2 =
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(); FeatureFactory.getFactory(mContext).getMetricsFeatureProvider();
assertTrue(feature1 == feature2); assertThat(feature1 == feature2).isTrue();
} }
} }

View File

@@ -33,7 +33,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.util.ActivityController; import org.robolectric.util.ActivityController;
import org.robolectric.util.FragmentController; import org.robolectric.util.FragmentController;
import static org.junit.Assert.assertTrue; import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -109,15 +109,15 @@ public class LifecycleTest {
TestActivity activity = ac.get(); TestActivity activity = ac.get();
ac.start(); ac.start();
assertTrue(activity.mActObserver.mOnStartObserved); assertThat(activity.mActObserver.mOnStartObserved).isTrue();
ac.resume(); ac.resume();
assertTrue(activity.mActObserver.mOnResumeObserved); assertThat(activity.mActObserver.mOnResumeObserved).isTrue();
ac.pause(); ac.pause();
assertTrue(activity.mActObserver.mOnPauseObserved); assertThat(activity.mActObserver.mOnPauseObserved).isTrue();
ac.stop(); ac.stop();
assertTrue(activity.mActObserver.mOnStopObserved); assertThat(activity.mActObserver.mOnStopObserved).isTrue();
ac.destroy(); ac.destroy();
assertTrue(activity.mActObserver.mOnDestroyObserved); assertThat(activity.mActObserver.mOnDestroyObserved).isTrue();
} }
@Test @Test
@@ -128,12 +128,12 @@ public class LifecycleTest {
fragmentController.attach().create().start().resume().pause().stop().destroy(); fragmentController.attach().create().start().resume().pause().stop().destroy();
assertTrue(fragment.mFragObserver.mOnAttachObserved); assertThat(fragment.mFragObserver.mOnAttachObserved).isTrue();
assertTrue(fragment.mFragObserver.mOnAttachHasContext); assertThat(fragment.mFragObserver.mOnAttachHasContext).isTrue();
assertTrue(fragment.mFragObserver.mOnStartObserved); assertThat(fragment.mFragObserver.mOnStartObserved).isTrue();
assertTrue(fragment.mFragObserver.mOnResumeObserved); assertThat(fragment.mFragObserver.mOnResumeObserved).isTrue();
assertTrue(fragment.mFragObserver.mOnPauseObserved); assertThat(fragment.mFragObserver.mOnPauseObserved).isTrue();
assertTrue(fragment.mFragObserver.mOnStopObserved); assertThat(fragment.mFragObserver.mOnStopObserved).isTrue();
assertTrue(fragment.mFragObserver.mOnDestroyObserved); assertThat(fragment.mFragObserver.mOnDestroyObserved).isTrue();
} }
} }

View File

@@ -3,23 +3,24 @@ package com.android.settings.datausage;
import android.net.NetworkPolicy; import android.net.NetworkPolicy;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import junit.framework.Assert; import com.android.settingslib.net.DataUsageController.DataUsageInfo;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import static com.android.settingslib.net.DataUsageController.*; import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DataUsageInfoControllerTest { public class DataUsageInfoControllerTest {
private DataUsageInfoController mInfoController; private DataUsageInfoController mInfoController;
private DataUsageInfo info; private DataUsageInfo info;
private static final int NEGATIVE = -1;
private static final int ZERO = 0;
private static final int POSITIVE_SMALL = 1;
private static final int POSITIVE_LARGE = 5;
@Before @Before
public void setUp() { public void setUp() {
@@ -29,50 +30,50 @@ public class DataUsageInfoControllerTest {
@Test @Test
public void testLowUsageLowWarning_LimitUsed() { public void testLowUsageLowWarning_LimitUsed() {
info.warningLevel = 5; info.warningLevel = POSITIVE_SMALL;
info.limitLevel = 10; info.limitLevel = POSITIVE_LARGE;
info.usageLevel = 5; info.usageLevel = POSITIVE_SMALL;
assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
} }
@Test @Test
public void testLowUsageEqualWarning_LimitUsed() { public void testLowUsageEqualWarning_LimitUsed() {
info.warningLevel = 10; info.warningLevel = POSITIVE_LARGE;
info.limitLevel = 10; info.limitLevel = POSITIVE_LARGE;
info.usageLevel = 5; info.usageLevel = POSITIVE_SMALL;
assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
} }
@Test @Test
public void testNoLimitNoUsage_WarningUsed() { public void testNoLimitNoUsage_WarningUsed() {
info.warningLevel = 10; info.warningLevel = POSITIVE_LARGE;
info.limitLevel = 0; info.limitLevel = ZERO;
info.usageLevel = 0; info.usageLevel = ZERO;
assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
} }
@Test @Test
public void testNoLimitLowUsage_WarningUsed() { public void testNoLimitLowUsage_WarningUsed() {
info.warningLevel = 10; info.warningLevel = POSITIVE_LARGE;
info.limitLevel = 0; info.limitLevel = ZERO;
info.usageLevel = 5; info.usageLevel = POSITIVE_SMALL;
assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
} }
@Test @Test
public void testLowWarningNoLimit_UsageUsed() { public void testLowWarningNoLimit_UsageUsed() {
info.warningLevel = 5; info.warningLevel = POSITIVE_SMALL;
info.limitLevel = 0; info.limitLevel = ZERO;
info.usageLevel = 10; info.usageLevel = POSITIVE_LARGE;
assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
} }
@Test @Test
public void testLowWarningLowLimit_UsageUsed() { public void testLowWarningLowLimit_UsageUsed() {
info.warningLevel = 5; info.warningLevel = POSITIVE_SMALL;
info.limitLevel = 5; info.limitLevel = POSITIVE_SMALL;
info.usageLevel = 10; info.usageLevel = POSITIVE_LARGE;
assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel); assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
} }
private NetworkPolicy getDefaultNetworkPolicy() { private NetworkPolicy getDefaultNetworkPolicy() {
@@ -87,60 +88,56 @@ public class DataUsageInfoControllerTest {
@Test @Test
public void testNullArguments_NoError() { public void testNullArguments_NoError() {
try { mInfoController.updateDataLimit(null, null);
mInfoController.updateDataLimit(null, null); mInfoController.updateDataLimit(info, null);
mInfoController.updateDataLimit(info, null); mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
} catch (Exception e) {
fail("Update Data Limit should drop calls with null arguments");
}
} }
@Test @Test
public void testNegativeWarning_UpdatedToZero() { public void testNegativeWarning_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = -5; policy.warningBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.warningLevel); assertThat(info.warningLevel).isEqualTo(ZERO);
} }
@Test @Test
public void testWarningZero_UpdatedToZero() { public void testWarningZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = 0; policy.warningBytes = ZERO;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.warningLevel); assertThat(info.warningLevel).isEqualTo(ZERO);
} }
@Test @Test
public void testWarningPositive_UpdatedToWarning() { public void testWarningPositive_UpdatedToWarning() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = 5; policy.warningBytes = POSITIVE_SMALL;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(policy.warningBytes, info.warningLevel); assertThat(info.warningLevel).isEqualTo(policy.warningBytes);
} }
@Test @Test
public void testLimitNegative_UpdatedToZero() { public void testLimitNegative_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = -5; policy.limitBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.limitLevel); assertThat(info.limitLevel).isEqualTo(ZERO);
} }
@Test @Test
public void testLimitZero_UpdatedToZero() { public void testLimitZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = 0; policy.limitBytes = ZERO;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.limitLevel); assertThat(info.limitLevel).isEqualTo(ZERO);
} }
@Test @Test
public void testLimitPositive_UpdatedToLimit() { public void testLimitPositive_UpdatedToLimit() {
NetworkPolicy policy = getDefaultNetworkPolicy(); NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = 5; policy.limitBytes = POSITIVE_SMALL;
mInfoController.updateDataLimit(info, policy); mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(policy.limitBytes, info.limitLevel); assertThat(info.limitLevel).isEqualTo(policy.limitBytes);
} }
} }

View File

@@ -28,7 +28,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import static junit.framework.Assert.assertTrue; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -55,6 +55,6 @@ public class DataUsageSummaryTest {
@Test @Test
public void testMobileDataStatus() { public void testMobileDataStatus() {
boolean hasMobileData = DataUsageSummary.hasMobileData(mContext); boolean hasMobileData = DataUsageSummary.hasMobileData(mContext);
assertTrue(hasMobileData); assertThat(hasMobileData).isTrue();
} }
} }

View File

@@ -23,9 +23,8 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -33,10 +32,10 @@ public class ThreadUtilsTest {
@Test @Test
public void testMainThread() throws InterruptedException { public void testMainThread() throws InterruptedException {
assertTrue(ThreadUtils.isMainThread()); assertThat(ThreadUtils.isMainThread()).isTrue();
Thread background = new Thread(new Runnable() { Thread background = new Thread(new Runnable() {
public void run() { public void run() {
assertFalse(ThreadUtils.isMainThread()); assertThat(ThreadUtils.isMainThread()).isFalse();
} }
}); });
background.start(); background.start();

View File

@@ -29,7 +29,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertThat;
import java.util.Locale; import java.util.Locale;
@@ -64,19 +64,19 @@ public class RtlCompatibleViewPagerTest {
private void testRtlCompatibleInner(int currentItem) { private void testRtlCompatibleInner(int currentItem) {
// Set up the environment // Set up the environment
Locale.setDefault(mLocaleEn); Locale.setDefault(mLocaleEn);
assertEquals(View.LAYOUT_DIRECTION_LTR, assertThat(TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))
TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())); .isEqualTo(View.LAYOUT_DIRECTION_LTR);
mTestViewPager.setCurrentItem(currentItem); mTestViewPager.setCurrentItem(currentItem);
assertEquals(currentItem, mTestViewPager.getCurrentItem()); assertThat(mTestViewPager.getCurrentItem()).isEqualTo(currentItem);
// Simulate to change the language to RTL // Simulate to change the language to RTL
Parcelable savedInstance = mTestViewPager.onSaveInstanceState(); Parcelable savedInstance = mTestViewPager.onSaveInstanceState();
Locale.setDefault(mLocaleHe); Locale.setDefault(mLocaleHe);
assertEquals(View.LAYOUT_DIRECTION_RTL, assertThat(TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))
TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())); .isEqualTo(View.LAYOUT_DIRECTION_RTL);
mTestViewPager.onRestoreInstanceState(savedInstance); mTestViewPager.onRestoreInstanceState(savedInstance);
assertEquals(currentItem, mTestViewPager.getCurrentItem()); assertThat(mTestViewPager.getCurrentItem()).isEqualTo(currentItem);
} }