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).
LOCAL_STATIC_JAVA_LIBRARIES := \
platform-system-robolectric
platform-system-robolectric \
truth-prebuilt
LOCAL_JAVA_LIBRARIES := \
junit4-target \

View File

@@ -29,7 +29,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static org.junit.Assert.assertTrue;
import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -55,6 +55,6 @@ public class MetricsFeatureProviderTest {
MetricsFeatureProvider feature2 =
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.FragmentController;
import static org.junit.Assert.assertTrue;
import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -109,15 +109,15 @@ public class LifecycleTest {
TestActivity activity = ac.get();
ac.start();
assertTrue(activity.mActObserver.mOnStartObserved);
assertThat(activity.mActObserver.mOnStartObserved).isTrue();
ac.resume();
assertTrue(activity.mActObserver.mOnResumeObserved);
assertThat(activity.mActObserver.mOnResumeObserved).isTrue();
ac.pause();
assertTrue(activity.mActObserver.mOnPauseObserved);
assertThat(activity.mActObserver.mOnPauseObserved).isTrue();
ac.stop();
assertTrue(activity.mActObserver.mOnStopObserved);
assertThat(activity.mActObserver.mOnStopObserved).isTrue();
ac.destroy();
assertTrue(activity.mActObserver.mOnDestroyObserved);
assertThat(activity.mActObserver.mOnDestroyObserved).isTrue();
}
@Test
@@ -128,12 +128,12 @@ public class LifecycleTest {
fragmentController.attach().create().start().resume().pause().stop().destroy();
assertTrue(fragment.mFragObserver.mOnAttachObserved);
assertTrue(fragment.mFragObserver.mOnAttachHasContext);
assertTrue(fragment.mFragObserver.mOnStartObserved);
assertTrue(fragment.mFragObserver.mOnResumeObserved);
assertTrue(fragment.mFragObserver.mOnPauseObserved);
assertTrue(fragment.mFragObserver.mOnStopObserved);
assertTrue(fragment.mFragObserver.mOnDestroyObserved);
assertThat(fragment.mFragObserver.mOnAttachObserved).isTrue();
assertThat(fragment.mFragObserver.mOnAttachHasContext).isTrue();
assertThat(fragment.mFragObserver.mOnStartObserved).isTrue();
assertThat(fragment.mFragObserver.mOnResumeObserved).isTrue();
assertThat(fragment.mFragObserver.mOnPauseObserved).isTrue();
assertThat(fragment.mFragObserver.mOnStopObserved).isTrue();
assertThat(fragment.mFragObserver.mOnDestroyObserved).isTrue();
}
}

View File

@@ -3,23 +3,24 @@ package com.android.settings.datausage;
import android.net.NetworkPolicy;
import android.net.NetworkTemplate;
import com.android.settings.TestConfig;
import junit.framework.Assert;
import com.android.settingslib.net.DataUsageController.DataUsageInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static com.android.settingslib.net.DataUsageController.*;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DataUsageInfoControllerTest {
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
public void setUp() {
@@ -29,50 +30,50 @@ public class DataUsageInfoControllerTest {
@Test
public void testLowUsageLowWarning_LimitUsed() {
info.warningLevel = 5;
info.limitLevel = 10;
info.usageLevel = 5;
assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel);
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = POSITIVE_LARGE;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
}
@Test
public void testLowUsageEqualWarning_LimitUsed() {
info.warningLevel = 10;
info.limitLevel = 10;
info.usageLevel = 5;
assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel);
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = POSITIVE_LARGE;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
}
@Test
public void testNoLimitNoUsage_WarningUsed() {
info.warningLevel = 10;
info.limitLevel = 0;
info.usageLevel = 0;
assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel);
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = ZERO;
info.usageLevel = ZERO;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
}
@Test
public void testNoLimitLowUsage_WarningUsed() {
info.warningLevel = 10;
info.limitLevel = 0;
info.usageLevel = 5;
assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel);
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = ZERO;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
}
@Test
public void testLowWarningNoLimit_UsageUsed() {
info.warningLevel = 5;
info.limitLevel = 0;
info.usageLevel = 10;
assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel);
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = ZERO;
info.usageLevel = POSITIVE_LARGE;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
}
@Test
public void testLowWarningLowLimit_UsageUsed() {
info.warningLevel = 5;
info.limitLevel = 5;
info.usageLevel = 10;
assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel);
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = POSITIVE_SMALL;
info.usageLevel = POSITIVE_LARGE;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
}
private NetworkPolicy getDefaultNetworkPolicy() {
@@ -87,60 +88,56 @@ public class DataUsageInfoControllerTest {
@Test
public void testNullArguments_NoError() {
try {
mInfoController.updateDataLimit(null, null);
mInfoController.updateDataLimit(info, null);
mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
} catch (Exception e) {
fail("Update Data Limit should drop calls with null arguments");
}
mInfoController.updateDataLimit(null, null);
mInfoController.updateDataLimit(info, null);
mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
}
@Test
public void testNegativeWarning_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = -5;
policy.warningBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.warningLevel);
assertThat(info.warningLevel).isEqualTo(ZERO);
}
@Test
public void testWarningZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = 0;
policy.warningBytes = ZERO;
mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.warningLevel);
assertThat(info.warningLevel).isEqualTo(ZERO);
}
@Test
public void testWarningPositive_UpdatedToWarning() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = 5;
policy.warningBytes = POSITIVE_SMALL;
mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(policy.warningBytes, info.warningLevel);
assertThat(info.warningLevel).isEqualTo(policy.warningBytes);
}
@Test
public void testLimitNegative_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = -5;
policy.limitBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.limitLevel);
assertThat(info.limitLevel).isEqualTo(ZERO);
}
@Test
public void testLimitZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = 0;
policy.limitBytes = ZERO;
mInfoController.updateDataLimit(info, policy);
Assert.assertEquals(0, info.limitLevel);
assertThat(info.limitLevel).isEqualTo(ZERO);
}
@Test
public void testLimitPositive_UpdatedToLimit() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = 5;
policy.limitBytes = POSITIVE_SMALL;
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.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.Mockito.when;
@@ -55,6 +55,6 @@ public class DataUsageSummaryTest {
@Test
public void testMobileDataStatus() {
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.annotation.Config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static com.google.common.truth.Truth.assertThat;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -33,10 +32,10 @@ public class ThreadUtilsTest {
@Test
public void testMainThread() throws InterruptedException {
assertTrue(ThreadUtils.isMainThread());
assertThat(ThreadUtils.isMainThread()).isTrue();
Thread background = new Thread(new Runnable() {
public void run() {
assertFalse(ThreadUtils.isMainThread());
assertThat(ThreadUtils.isMainThread()).isFalse();
}
});
background.start();

View File

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