Commit bc3e2f93 authored by yolandyan's avatar yolandyan Committed by Commit Bot

Convert components/ java tests to JUnit4

For more on JUnit4 migration, please check src/testing/android/docs/junit4.md

Bug: 640116
Change-Id: Ied8a563ee6833b76beeaa0b4141aa6e0e74a2abd
Reviewed-on: https://chromium-review.googlesource.com/567627Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488657}
parent f3265df8
...@@ -10,9 +10,14 @@ import android.content.Context; ...@@ -10,9 +10,14 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.MinAndroidSdkLevel; import org.chromium.base.test.util.MinAndroidSdkLevel;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -20,9 +25,10 @@ import java.util.concurrent.TimeUnit; ...@@ -20,9 +25,10 @@ import java.util.concurrent.TimeUnit;
/** /**
* Tests for {@link BackgroundTaskSchedulerJobService}. * Tests for {@link BackgroundTaskSchedulerJobService}.
*/ */
@RunWith(BaseJUnit4ClassRunner.class)
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1) @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1)
public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCase { public class BackgroundTaskSchedulerJobServiceTest {
private static class TestBackgroundTask implements BackgroundTask { private static class TestBackgroundTask implements BackgroundTask {
@Override @Override
public boolean onStartTask( public boolean onStartTask(
...@@ -39,19 +45,21 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa ...@@ -39,19 +45,21 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa
public void reschedule(Context context) {} public void reschedule(Context context) {}
} }
@Test
@SmallTest @SmallTest
public void testOneOffTaskInfoWithDeadlineConversion() { public void testOneOffTaskInfoWithDeadlineConversion() {
TaskInfo oneOffTask = TaskInfo.createOneOffTask(TaskIds.TEST, TestBackgroundTask.class, TaskInfo oneOffTask = TaskInfo.createOneOffTask(TaskIds.TEST, TestBackgroundTask.class,
TimeUnit.MINUTES.toMillis(200)) TimeUnit.MINUTES.toMillis(200))
.build(); .build();
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), oneOffTask); InstrumentationRegistry.getInstrumentation().getTargetContext(), oneOffTask);
assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); Assert.assertEquals(oneOffTask.getTaskId(), jobInfo.getId());
assertFalse(jobInfo.isPeriodic()); Assert.assertFalse(jobInfo.isPeriodic());
assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(), Assert.assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(),
jobInfo.getMaxExecutionDelayMillis()); jobInfo.getMaxExecutionDelayMillis());
} }
@Test
@SmallTest @SmallTest
public void testOneOffTaskInfoWithWindowConversion() { public void testOneOffTaskInfoWithWindowConversion() {
TaskInfo oneOffTask = TaskInfo oneOffTask =
...@@ -59,27 +67,30 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa ...@@ -59,27 +67,30 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa
TimeUnit.MINUTES.toMillis(100), TimeUnit.MINUTES.toMillis(200)) TimeUnit.MINUTES.toMillis(100), TimeUnit.MINUTES.toMillis(200))
.build(); .build();
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), oneOffTask); InstrumentationRegistry.getInstrumentation().getTargetContext(), oneOffTask);
assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); Assert.assertEquals(oneOffTask.getTaskId(), jobInfo.getId());
assertFalse(jobInfo.isPeriodic()); Assert.assertFalse(jobInfo.isPeriodic());
assertEquals( Assert.assertEquals(
oneOffTask.getOneOffInfo().getWindowStartTimeMs(), jobInfo.getMinLatencyMillis()); oneOffTask.getOneOffInfo().getWindowStartTimeMs(), jobInfo.getMinLatencyMillis());
assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(), Assert.assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(),
jobInfo.getMaxExecutionDelayMillis()); jobInfo.getMaxExecutionDelayMillis());
} }
@Test
@SmallTest @SmallTest
public void testPeriodicTaskInfoWithoutFlexConversion() { public void testPeriodicTaskInfoWithoutFlexConversion() {
TaskInfo periodicTask = TaskInfo.createPeriodicTask(TaskIds.TEST, TestBackgroundTask.class, TaskInfo periodicTask = TaskInfo.createPeriodicTask(TaskIds.TEST, TestBackgroundTask.class,
TimeUnit.MINUTES.toMillis(200)) TimeUnit.MINUTES.toMillis(200))
.build(); .build();
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), periodicTask); InstrumentationRegistry.getInstrumentation().getTargetContext(), periodicTask);
assertEquals(periodicTask.getTaskId(), jobInfo.getId()); Assert.assertEquals(periodicTask.getTaskId(), jobInfo.getId());
assertTrue(jobInfo.isPeriodic()); Assert.assertTrue(jobInfo.isPeriodic());
assertEquals(periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.getIntervalMillis()); Assert.assertEquals(
periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.getIntervalMillis());
} }
@Test
@SmallTest @SmallTest
public void testPeriodicTaskInfoWithFlexConversion() { public void testPeriodicTaskInfoWithFlexConversion() {
TaskInfo periodicTask = TaskInfo periodicTask =
...@@ -87,15 +98,18 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa ...@@ -87,15 +98,18 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa
TimeUnit.MINUTES.toMillis(200), TimeUnit.MINUTES.toMillis(50)) TimeUnit.MINUTES.toMillis(200), TimeUnit.MINUTES.toMillis(50))
.build(); .build();
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), periodicTask); InstrumentationRegistry.getInstrumentation().getTargetContext(), periodicTask);
assertEquals(periodicTask.getTaskId(), jobInfo.getId()); Assert.assertEquals(periodicTask.getTaskId(), jobInfo.getId());
assertTrue(jobInfo.isPeriodic()); Assert.assertTrue(jobInfo.isPeriodic());
assertEquals(periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.getIntervalMillis()); Assert.assertEquals(
periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.getIntervalMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
assertEquals(periodicTask.getPeriodicInfo().getFlexMs(), jobInfo.getFlexMillis()); Assert.assertEquals(
periodicTask.getPeriodicInfo().getFlexMs(), jobInfo.getFlexMillis());
} }
} }
@Test
@SmallTest @SmallTest
public void testTaskInfoWithExtras() { public void testTaskInfoWithExtras() {
Bundle taskExtras = new Bundle(); Bundle taskExtras = new Bundle();
...@@ -107,34 +121,36 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa ...@@ -107,34 +121,36 @@ public class BackgroundTaskSchedulerJobServiceTest extends InstrumentationTestCa
.setExtras(taskExtras) .setExtras(taskExtras)
.build(); .build();
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), oneOffTask); InstrumentationRegistry.getInstrumentation().getTargetContext(), oneOffTask);
assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); Assert.assertEquals(oneOffTask.getTaskId(), jobInfo.getId());
PersistableBundle jobExtras = jobInfo.getExtras(); PersistableBundle jobExtras = jobInfo.getExtras();
PersistableBundle persistableBundle = jobExtras.getPersistableBundle( PersistableBundle persistableBundle = jobExtras.getPersistableBundle(
BackgroundTaskSchedulerJobService.BACKGROUND_TASK_EXTRAS_KEY); BackgroundTaskSchedulerJobService.BACKGROUND_TASK_EXTRAS_KEY);
assertEquals(taskExtras.keySet().size(), persistableBundle.keySet().size()); Assert.assertEquals(taskExtras.keySet().size(), persistableBundle.keySet().size());
assertEquals(taskExtras.getString("foo"), persistableBundle.getString("foo")); Assert.assertEquals(taskExtras.getString("foo"), persistableBundle.getString("foo"));
assertEquals(taskExtras.getBoolean("bools"), persistableBundle.getBoolean("bools")); Assert.assertEquals(taskExtras.getBoolean("bools"), persistableBundle.getBoolean("bools"));
assertEquals(taskExtras.getLong("longs"), persistableBundle.getLong("longs")); Assert.assertEquals(taskExtras.getLong("longs"), persistableBundle.getLong("longs"));
} }
@Test
@SmallTest @SmallTest
public void testTaskInfoWithManyConstraints() { public void testTaskInfoWithManyConstraints() {
TaskInfo.Builder taskBuilder = TaskInfo.createOneOffTask( TaskInfo.Builder taskBuilder = TaskInfo.createOneOffTask(
TaskIds.TEST, TestBackgroundTask.class, TimeUnit.MINUTES.toMillis(200)); TaskIds.TEST, TestBackgroundTask.class, TimeUnit.MINUTES.toMillis(200));
JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), taskBuilder.setIsPersisted(true).build()); InstrumentationRegistry.getInstrumentation().getTargetContext(),
assertTrue(jobInfo.isPersisted()); taskBuilder.setIsPersisted(true).build());
Assert.assertTrue(jobInfo.isPersisted());
jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), InstrumentationRegistry.getInstrumentation().getTargetContext(),
taskBuilder.setRequiredNetworkType(TaskInfo.NETWORK_TYPE_UNMETERED).build()); taskBuilder.setRequiredNetworkType(TaskInfo.NETWORK_TYPE_UNMETERED).build());
assertEquals(JobInfo.NETWORK_TYPE_UNMETERED, jobInfo.getNetworkType()); Assert.assertEquals(JobInfo.NETWORK_TYPE_UNMETERED, jobInfo.getNetworkType());
jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo(
getInstrumentation().getTargetContext(), InstrumentationRegistry.getInstrumentation().getTargetContext(),
taskBuilder.setRequiresCharging(true).build()); taskBuilder.setRequiresCharging(true).build());
assertTrue(jobInfo.isRequireCharging()); Assert.assertTrue(jobInfo.isRequireCharging());
} }
} }
...@@ -9,8 +9,12 @@ import android.os.Build; ...@@ -9,8 +9,12 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.MinAndroidSdkLevel; import org.chromium.base.test.util.MinAndroidSdkLevel;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -20,9 +24,11 @@ import java.util.Set; ...@@ -20,9 +24,11 @@ import java.util.Set;
/** /**
* Tests for {@link BundleToPersistableBundleConverter}. * Tests for {@link BundleToPersistableBundleConverter}.
*/ */
@RunWith(BaseJUnit4ClassRunner.class)
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1) @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1)
public class BundleToPersistableBundleConverterTest extends InstrumentationTestCase { public class BundleToPersistableBundleConverterTest {
@Test
@SmallTest @SmallTest
public void testAllValidConversions() { public void testAllValidConversions() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
...@@ -41,19 +47,21 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC ...@@ -41,19 +47,21 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC
BundleToPersistableBundleConverter.convert(bundle); BundleToPersistableBundleConverter.convert(bundle);
PersistableBundle pBundle = result.getPersistableBundle(); PersistableBundle pBundle = result.getPersistableBundle();
assertFalse(result.hasErrors()); Assert.assertFalse(result.hasErrors());
assertEquals(bundle.getString("s"), pBundle.getString("s")); Assert.assertEquals(bundle.getString("s"), pBundle.getString("s"));
assertTrue(Arrays.equals(bundle.getStringArray("sa"), pBundle.getStringArray("sa"))); Assert.assertTrue(Arrays.equals(bundle.getStringArray("sa"), pBundle.getStringArray("sa")));
assertEquals(bundle.getBoolean("b"), pBundle.getBoolean("b")); Assert.assertEquals(bundle.getBoolean("b"), pBundle.getBoolean("b"));
assertTrue(Arrays.equals(bundle.getBooleanArray("ba"), pBundle.getBooleanArray("ba"))); Assert.assertTrue(
assertEquals(bundle.getInt("i"), pBundle.getInt("i")); Arrays.equals(bundle.getBooleanArray("ba"), pBundle.getBooleanArray("ba")));
assertTrue(Arrays.equals(bundle.getIntArray("ia"), pBundle.getIntArray("ia"))); Assert.assertEquals(bundle.getInt("i"), pBundle.getInt("i"));
assertEquals(bundle.getLong("l"), pBundle.getLong("l")); Assert.assertTrue(Arrays.equals(bundle.getIntArray("ia"), pBundle.getIntArray("ia")));
assertTrue(Arrays.equals(bundle.getLongArray("la"), pBundle.getLongArray("la"))); Assert.assertEquals(bundle.getLong("l"), pBundle.getLong("l"));
assertEquals(bundle.getDouble("d"), pBundle.getDouble("d")); Assert.assertTrue(Arrays.equals(bundle.getLongArray("la"), pBundle.getLongArray("la")));
assertTrue(Arrays.equals(bundle.getDoubleArray("da"), pBundle.getDoubleArray("da"))); Assert.assertEquals(bundle.getDouble("d"), pBundle.getDouble("d"), 0);
Assert.assertTrue(Arrays.equals(bundle.getDoubleArray("da"), pBundle.getDoubleArray("da")));
} }
@Test
@SmallTest @SmallTest
public void testSomeBadConversions() { public void testSomeBadConversions() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
...@@ -68,15 +76,16 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC ...@@ -68,15 +76,16 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC
BundleToPersistableBundleConverter.Result result = BundleToPersistableBundleConverter.Result result =
BundleToPersistableBundleConverter.convert(bundle); BundleToPersistableBundleConverter.convert(bundle);
assertTrue(result.hasErrors()); Assert.assertTrue(result.hasErrors());
Set<String> failedKeys = result.getFailedKeys(); Set<String> failedKeys = result.getFailedKeys();
assertEquals(3, failedKeys.size()); Assert.assertEquals(3, failedKeys.size());
assertTrue(failedKeys.contains("byte")); Assert.assertTrue(failedKeys.contains("byte"));
assertTrue(failedKeys.contains("float")); Assert.assertTrue(failedKeys.contains("float"));
assertTrue(failedKeys.contains("arrayList")); Assert.assertTrue(failedKeys.contains("arrayList"));
assertEquals(bundle.getString("s"), result.getPersistableBundle().getString("s")); Assert.assertEquals(bundle.getString("s"), result.getPersistableBundle().getString("s"));
} }
@Test
@SmallTest @SmallTest
public void testNullValue() { public void testNullValue() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
...@@ -87,9 +96,12 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC ...@@ -87,9 +96,12 @@ public class BundleToPersistableBundleConverterTest extends InstrumentationTestC
BundleToPersistableBundleConverter.Result result = BundleToPersistableBundleConverter.Result result =
BundleToPersistableBundleConverter.convert(bundle); BundleToPersistableBundleConverter.convert(bundle);
assertFalse(result.hasErrors()); Assert.assertFalse(result.hasErrors());
assertEquals(bundle.getString("foo"), result.getPersistableBundle().getString("foo")); Assert.assertEquals(
assertEquals(bundle.getString("bar"), result.getPersistableBundle().getString("bar")); bundle.getString("foo"), result.getPersistableBundle().getString("foo"));
assertEquals(bundle.getString("qux"), result.getPersistableBundle().getString("qux")); Assert.assertEquals(
bundle.getString("bar"), result.getPersistableBundle().getString("bar"));
Assert.assertEquals(
bundle.getString("qux"), result.getPersistableBundle().getString("qux"));
} }
} }
...@@ -4,11 +4,18 @@ ...@@ -4,11 +4,18 @@
package org.chromium.components.crash.browser; package org.chromium.components.crash.browser;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.TestFileUtil; import org.chromium.base.test.util.TestFileUtil;
...@@ -19,21 +26,21 @@ import java.io.IOException; ...@@ -19,21 +26,21 @@ import java.io.IOException;
/** /**
* Unittests for {@link CrashDumpManager}. * Unittests for {@link CrashDumpManager}.
*/ */
public class CrashDumpManagerTest extends InstrumentationTestCase { @RunWith(BaseJUnit4ClassRunner.class)
public class CrashDumpManagerTest {
File mTempDir; File mTempDir;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); ContextUtils.initApplicationContextForTests(InstrumentationRegistry.getInstrumentation()
ContextUtils.initApplicationContextForTests( .getTargetContext()
getInstrumentation().getTargetContext().getApplicationContext()); .getApplicationContext());
mTempDir = ContextUtils.getApplicationContext().getCacheDir(); mTempDir = ContextUtils.getApplicationContext().getCacheDir();
assert mTempDir.exists(); assert mTempDir.exists();
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
File[] files = mTempDir.listFiles(); File[] files = mTempDir.listFiles();
if (files == null) return; if (files == null) return;
...@@ -42,16 +49,18 @@ public class CrashDumpManagerTest extends InstrumentationTestCase { ...@@ -42,16 +49,18 @@ public class CrashDumpManagerTest extends InstrumentationTestCase {
} }
} }
@Test
@SmallTest @SmallTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
@DisabledTest // Flaky, crbug.com/725379. @DisabledTest // Flaky, crbug.com/725379.
public void testUploadMinidump_NoCallback() throws IOException { public void testUploadMinidump_NoCallback() throws IOException {
File minidump = new File(mTempDir, "mini.dmp"); File minidump = new File(mTempDir, "mini.dmp");
assertTrue(minidump.createNewFile()); Assert.assertTrue(minidump.createNewFile());
CrashDumpManager.tryToUploadMinidump(minidump.getAbsolutePath()); CrashDumpManager.tryToUploadMinidump(minidump.getAbsolutePath());
} }
@Test
@SmallTest @SmallTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
@DisabledTest // Flaky, crbug.com/725379. @DisabledTest // Flaky, crbug.com/725379.
...@@ -59,7 +68,7 @@ public class CrashDumpManagerTest extends InstrumentationTestCase { ...@@ -59,7 +68,7 @@ public class CrashDumpManagerTest extends InstrumentationTestCase {
registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() { registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() {
@Override @Override
public void tryToUploadMinidump(File minidump) { public void tryToUploadMinidump(File minidump) {
fail("The callback should not be called when the minidump path is null."); Assert.fail("The callback should not be called when the minidump path is null.");
} }
}); });
...@@ -68,12 +77,14 @@ public class CrashDumpManagerTest extends InstrumentationTestCase { ...@@ -68,12 +77,14 @@ public class CrashDumpManagerTest extends InstrumentationTestCase {
// @SmallTest // @SmallTest
// @Feature({"Android-AppBase"}) // @Feature({"Android-AppBase"})
@Test
@DisabledTest // Flaky, crbug.com/725379. @DisabledTest // Flaky, crbug.com/725379.
public void testUploadMinidump_FileDoesntExist() { public void testUploadMinidump_FileDoesntExist() {
registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() { registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() {
@Override @Override
public void tryToUploadMinidump(File minidump) { public void tryToUploadMinidump(File minidump) {
fail("The callback should not be called when the minidump file doesn't exist."); Assert.fail(
"The callback should not be called when the minidump file doesn't exist.");
} }
}); });
...@@ -81,34 +92,37 @@ public class CrashDumpManagerTest extends InstrumentationTestCase { ...@@ -81,34 +92,37 @@ public class CrashDumpManagerTest extends InstrumentationTestCase {
mTempDir.getAbsolutePath() + "/some/file/that/doesnt/exist"); mTempDir.getAbsolutePath() + "/some/file/that/doesnt/exist");
} }
@Test
@SmallTest @SmallTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
@DisabledTest // Flaky, crbug.com/725379. @DisabledTest // Flaky, crbug.com/725379.
public void testUploadMinidump_MinidumpPathIsDirectory() throws IOException { public void testUploadMinidump_MinidumpPathIsDirectory() throws IOException {
File directory = new File(mTempDir, "a_directory"); File directory = new File(mTempDir, "a_directory");
assertTrue(directory.mkdir()); Assert.assertTrue(directory.mkdir());
registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() { registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() {
@Override @Override
public void tryToUploadMinidump(File minidump) { public void tryToUploadMinidump(File minidump) {
fail("The callback should not be called when the minidump path is not a file."); Assert.fail(
"The callback should not be called when the minidump path is not a file.");
} }
}); });
CrashDumpManager.tryToUploadMinidump(directory.getAbsolutePath()); CrashDumpManager.tryToUploadMinidump(directory.getAbsolutePath());
} }
@Test
@SmallTest @SmallTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
@DisabledTest // Flaky, crbug.com/725379. @DisabledTest // Flaky, crbug.com/725379.
public void testUploadMinidump_MinidumpPathIsValid() throws IOException { public void testUploadMinidump_MinidumpPathIsValid() throws IOException {
final File minidump = new File(mTempDir, "mini.dmp"); final File minidump = new File(mTempDir, "mini.dmp");
assertTrue(minidump.createNewFile()); Assert.assertTrue(minidump.createNewFile());
registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() { registerUploadCallback(new CrashDumpManager.UploadMinidumpCallback() {
@Override @Override
public void tryToUploadMinidump(File actualMinidump) { public void tryToUploadMinidump(File actualMinidump) {
assertEquals("Should call the callback with the correct filename.", minidump, Assert.assertEquals("Should call the callback with the correct filename.", minidump,
actualMinidump); actualMinidump);
} }
}); });
......
...@@ -731,6 +731,7 @@ android_library("cronet_javatests") { ...@@ -731,6 +731,7 @@ android_library("cronet_javatests") {
"//net/android:net_java", "//net/android:net_java",
"//net/android:net_java_test_support", "//net/android:net_java_test_support",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/junit",
] ]
run_findbugs_override = true run_findbugs_override = true
......
...@@ -239,7 +239,7 @@ public class CronetHttpURLConnection extends HttpURLConnection { ...@@ -239,7 +239,7 @@ public class CronetHttpURLConnection extends HttpURLConnection {
if (superFixedContentLengthLong != -1) { if (superFixedContentLengthLong != -1) {
contentLength = superFixedContentLengthLong; contentLength = superFixedContentLengthLong;
} }
} catch (Exception e) { } catch (NoSuchFieldException | IllegalAccessException e) {
// Ignored. // Ignored.
} }
return contentLength; return contentLength;
......
...@@ -5,8 +5,12 @@ ...@@ -5,8 +5,12 @@
package org.chromium.net; package org.chromium.net;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.AndroidTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.net.impl.UrlResponseInfoImpl; import org.chromium.net.impl.UrlResponseInfoImpl;
...@@ -18,10 +22,12 @@ import java.util.Map; ...@@ -18,10 +22,12 @@ import java.util.Map;
/** /**
* Tests for {@link UrlResponseInfo}. * Tests for {@link UrlResponseInfo}.
*/ */
public class UrlResponseInfoTest extends AndroidTestCase { @RunWith(BaseJUnit4ClassRunner.class)
public class UrlResponseInfoTest {
/** /**
* Test for public API of {@link UrlResponseInfo}. * Test for public API of {@link UrlResponseInfo}.
*/ */
@Test
@SmallTest @SmallTest
@Feature({"Cronet"}) @Feature({"Cronet"})
public void testPublicAPI() throws Exception { public void testPublicAPI() throws Exception {
...@@ -39,29 +45,29 @@ public class UrlResponseInfoTest extends AndroidTestCase { ...@@ -39,29 +45,29 @@ public class UrlResponseInfoTest extends AndroidTestCase {
final UrlResponseInfo info = new UrlResponseInfoImpl(urlChain, httpStatusCode, final UrlResponseInfo info = new UrlResponseInfoImpl(urlChain, httpStatusCode,
httpStatusText, allHeadersList, wasCached, negotiatedProtocol, proxyServer); httpStatusText, allHeadersList, wasCached, negotiatedProtocol, proxyServer);
assertEquals(info.getUrlChain(), urlChain); Assert.assertEquals(info.getUrlChain(), urlChain);
try { try {
info.getUrlChain().add("example.com"); info.getUrlChain().add("example.com");
fail("getUrlChain() returned modifyable list."); Assert.fail("getUrlChain() returned modifyable list.");
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
// Expected. // Expected.
} }
assertEquals(info.getHttpStatusCode(), httpStatusCode); Assert.assertEquals(info.getHttpStatusCode(), httpStatusCode);
assertEquals(info.getHttpStatusText(), httpStatusText); Assert.assertEquals(info.getHttpStatusText(), httpStatusText);
assertEquals(info.getAllHeadersAsList(), allHeadersList); Assert.assertEquals(info.getAllHeadersAsList(), allHeadersList);
try { try {
info.getAllHeadersAsList().add( info.getAllHeadersAsList().add(
new AbstractMap.SimpleImmutableEntry<String, String>("X", "Y")); new AbstractMap.SimpleImmutableEntry<String, String>("X", "Y"));
fail("getAllHeadersAsList() returned modifyable list."); Assert.fail("getAllHeadersAsList() returned modifyable list.");
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
// Expected. // Expected.
} }
assertEquals(info.getAllHeaders().size(), allHeadersList.size()); Assert.assertEquals(info.getAllHeaders().size(), allHeadersList.size());
assertEquals(info.getAllHeaders().get(allHeadersList.get(0).getKey()).size(), 1); Assert.assertEquals(info.getAllHeaders().get(allHeadersList.get(0).getKey()).size(), 1);
assertEquals(info.getAllHeaders().get(allHeadersList.get(0).getKey()).get(0), Assert.assertEquals(info.getAllHeaders().get(allHeadersList.get(0).getKey()).get(0),
allHeadersList.get(0).getValue()); allHeadersList.get(0).getValue());
assertEquals(info.wasCached(), wasCached); Assert.assertEquals(info.wasCached(), wasCached);
assertEquals(info.getNegotiatedProtocol(), negotiatedProtocol); Assert.assertEquals(info.getNegotiatedProtocol(), negotiatedProtocol);
assertEquals(info.getProxyServer(), proxyServer); Assert.assertEquals(info.getProxyServer(), proxyServer);
} }
} }
\ No newline at end of file
...@@ -235,6 +235,7 @@ if (is_android) { ...@@ -235,6 +235,7 @@ if (is_android) {
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/cacheinvalidation:cacheinvalidation_javalib", "//third_party/cacheinvalidation:cacheinvalidation_javalib",
"//third_party/cacheinvalidation:cacheinvalidation_proto_java", "//third_party/cacheinvalidation:cacheinvalidation_proto_java",
"//third_party/junit",
] ]
java_files = [ java_files = [
"android/javatests/src/org/chromium/components/invalidation/InvalidationClientServiceTest.java", "android/javatests/src/org/chromium/components/invalidation/InvalidationClientServiceTest.java",
......
...@@ -40,6 +40,7 @@ android_library("javatests") { ...@@ -40,6 +40,7 @@ android_library("javatests") {
"//base:base_java", "//base:base_java",
"//base:base_java_test_support", "//base:base_java_test_support",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/junit",
] ]
java_files = [ "javatests/src/org/chromium/components/signin/test/AccountManagerFacadeTest.java" ] java_files = [ "javatests/src/org/chromium/components/signin/test/AccountManagerFacadeTest.java" ]
......
...@@ -6,10 +6,17 @@ package org.chromium.components.signin.test; ...@@ -6,10 +6,17 @@ package org.chromium.components.signin.test;
import android.accounts.Account; import android.accounts.Account;
import android.content.Context; import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.components.signin.AccountManagerFacade; import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.test.util.AccountHolder; import org.chromium.components.signin.test.util.AccountHolder;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate; import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
...@@ -18,45 +25,45 @@ import org.chromium.components.signin.test.util.SimpleFuture; ...@@ -18,45 +25,45 @@ import org.chromium.components.signin.test.util.SimpleFuture;
/** /**
* Test class for {@link AccountManagerFacade}. * Test class for {@link AccountManagerFacade}.
*/ */
public class AccountManagerFacadeTest extends InstrumentationTestCase { @RunWith(BaseJUnit4ClassRunner.class)
public class AccountManagerFacadeTest {
private FakeAccountManagerDelegate mDelegate; private FakeAccountManagerDelegate mDelegate;
private AccountManagerFacade mHelper; private AccountManagerFacade mHelper;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Context context = getInstrumentation().getTargetContext();
mDelegate = new FakeAccountManagerDelegate(context); mDelegate = new FakeAccountManagerDelegate(context);
AccountManagerFacade.overrideAccountManagerFacadeForTests(context, mDelegate); AccountManagerFacade.overrideAccountManagerFacadeForTests(context, mDelegate);
mHelper = AccountManagerFacade.get(); mHelper = AccountManagerFacade.get();
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() throws Exception {
AccountManagerFacade.resetAccountManagerFacadeForTests(); AccountManagerFacade.resetAccountManagerFacadeForTests();
super.tearDown();
} }
@Test
@SmallTest @SmallTest
public void testCanonicalAccount() throws InterruptedException { public void testCanonicalAccount() throws InterruptedException {
addTestAccount("test@gmail.com", "password"); addTestAccount("test@gmail.com", "password");
assertTrue(hasAccountForName("test@gmail.com")); Assert.assertTrue(hasAccountForName("test@gmail.com"));
assertTrue(hasAccountForName("Test@gmail.com")); Assert.assertTrue(hasAccountForName("Test@gmail.com"));
assertTrue(hasAccountForName("te.st@gmail.com")); Assert.assertTrue(hasAccountForName("te.st@gmail.com"));
} }
// If this test starts flaking, please re-open crbug.com/568636 and make sure there is some sort // If this test starts flaking, please re-open crbug.com/568636 and make sure there is some sort
// of stack trace or error message in that bug BEFORE disabling the test. // of stack trace or error message in that bug BEFORE disabling the test.
@Test
@SmallTest @SmallTest
public void testNonCanonicalAccount() throws InterruptedException { public void testNonCanonicalAccount() throws InterruptedException {
addTestAccount("test.me@gmail.com", "password"); addTestAccount("test.me@gmail.com", "password");
assertTrue(hasAccountForName("test.me@gmail.com")); Assert.assertTrue(hasAccountForName("test.me@gmail.com"));
assertTrue(hasAccountForName("testme@gmail.com")); Assert.assertTrue(hasAccountForName("testme@gmail.com"));
assertTrue(hasAccountForName("Testme@gmail.com")); Assert.assertTrue(hasAccountForName("Testme@gmail.com"));
assertTrue(hasAccountForName("te.st.me@gmail.com")); Assert.assertTrue(hasAccountForName("te.st.me@gmail.com"));
} }
private Account addTestAccount(String accountName, String password) { private Account addTestAccount(String accountName, String password) {
......
...@@ -46,6 +46,7 @@ android_library("sync_javatests") { ...@@ -46,6 +46,7 @@ android_library("sync_javatests") {
"//third_party/cacheinvalidation:cacheinvalidation_javalib", "//third_party/cacheinvalidation:cacheinvalidation_javalib",
"//third_party/cacheinvalidation:cacheinvalidation_proto_java", "//third_party/cacheinvalidation:cacheinvalidation_proto_java",
"//third_party/jsr-305:jsr_305_javalib", "//third_party/jsr-305:jsr_305_javalib",
"//third_party/junit",
] ]
java_files = [ java_files = [
"javatests/src/org/chromium/components/sync/AndroidSyncSettingsTest.java", "javatests/src/org/chromium/components/sync/AndroidSyncSettingsTest.java",
......
...@@ -5,21 +5,28 @@ ...@@ -5,21 +5,28 @@
package org.chromium.components.sync.notifier; package org.chromium.components.sync.notifier;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import java.util.Arrays; import java.util.Arrays;
/** Tests for the {@link InvalidationClientNameProvider} */ /** Tests for the {@link InvalidationClientNameProvider} */
public class InvalidationClientNameProviderTest extends InstrumentationTestCase { @RunWith(BaseJUnit4ClassRunner.class)
public class InvalidationClientNameProviderTest {
private InvalidationClientNameProvider mProvider; private InvalidationClientNameProvider mProvider;
@Override @Before
protected void setUp() { public void setUp() {
mProvider = new InvalidationClientNameProvider(); mProvider = new InvalidationClientNameProvider();
} }
@Test
@SmallTest @SmallTest
@Feature({"Sync"}) @Feature({"Sync"})
public void testFallbackClientId() { public void testFallbackClientId() {
...@@ -29,7 +36,7 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase ...@@ -29,7 +36,7 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase
byte[] id2 = mProvider.getInvalidatorClientName(); byte[] id2 = mProvider.getInvalidatorClientName();
// We expect the returned IDs to be consistent in every call. // We expect the returned IDs to be consistent in every call.
assertTrue("Expected returned IDs to be consistent", Arrays.equals(id1, id2)); Assert.assertTrue("Expected returned IDs to be consistent", Arrays.equals(id1, id2));
// Even if initialize the generator late, the ID will remain consistent. // Even if initialize the generator late, the ID will remain consistent.
registerHardCodedGenerator(mProvider); registerHardCodedGenerator(mProvider);
...@@ -39,10 +46,11 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase ...@@ -39,10 +46,11 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase
// getInvalidatorClientName() and never change afterwards. We test this anyway to make sure // getInvalidatorClientName() and never change afterwards. We test this anyway to make sure
// nothing will blow up if someone accidentally violates that constraint.) // nothing will blow up if someone accidentally violates that constraint.)
byte[] id3 = mProvider.getInvalidatorClientName(); byte[] id3 = mProvider.getInvalidatorClientName();
assertTrue("Changing generators should not affect returned ID consistency", Assert.assertTrue("Changing generators should not affect returned ID consistency",
Arrays.equals(id2, id3)); Arrays.equals(id2, id3));
} }
@Test
@SmallTest @SmallTest
@Feature({"Sync"}) @Feature({"Sync"})
public void testPreRegisteredGenerator() { public void testPreRegisteredGenerator() {
...@@ -52,7 +60,7 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase ...@@ -52,7 +60,7 @@ public class InvalidationClientNameProviderTest extends InstrumentationTestCase
byte[] id2 = mProvider.getInvalidatorClientName(); byte[] id2 = mProvider.getInvalidatorClientName();
// Expect that consistent IDs are maintained when using a custom generator, too. // Expect that consistent IDs are maintained when using a custom generator, too.
assertTrue("Custom generators should return consistent IDs", Arrays.equals(id, id2)); Assert.assertTrue("Custom generators should return consistent IDs", Arrays.equals(id, id2));
} }
private static void registerHardCodedGenerator(InvalidationClientNameProvider provider) { private static void registerHardCodedGenerator(InvalidationClientNameProvider provider) {
......
...@@ -6,11 +6,15 @@ package org.chromium.components.sync.notifier; ...@@ -6,11 +6,15 @@ package org.chromium.components.sync.notifier;
import android.accounts.Account; import android.accounts.Account;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import com.google.ipc.invalidation.external.client.types.ObjectId; import com.google.ipc.invalidation.external.client.types.ObjectId;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.CollectionUtil; import org.chromium.base.CollectionUtil;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
...@@ -22,8 +26,10 @@ import java.util.Set; ...@@ -22,8 +26,10 @@ import java.util.Set;
* *
* @author dsmyers@google.com (Daniel Myers) * @author dsmyers@google.com (Daniel Myers)
*/ */
@RunWith(BaseJUnit4ClassRunner.class)
@RetryOnFailure @RetryOnFailure
public class InvalidationPreferencesTest extends InstrumentationTestCase { public class InvalidationPreferencesTest {
@Test
@SmallTest @SmallTest
@Feature({"Sync"}) @Feature({"Sync"})
public void testReadMissingData() { public void testReadMissingData() {
...@@ -31,12 +37,13 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase { ...@@ -31,12 +37,13 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase {
* Test plan: read saved state from empty preferences. Verify that null is returned. * Test plan: read saved state from empty preferences. Verify that null is returned.
*/ */
InvalidationPreferences invPreferences = new InvalidationPreferences(); InvalidationPreferences invPreferences = new InvalidationPreferences();
assertNull(invPreferences.getSavedSyncedAccount()); Assert.assertNull(invPreferences.getSavedSyncedAccount());
assertNull(invPreferences.getSavedSyncedTypes()); Assert.assertNull(invPreferences.getSavedSyncedTypes());
assertNull(invPreferences.getSavedObjectIds()); Assert.assertNull(invPreferences.getSavedObjectIds());
assertNull(invPreferences.getInternalNotificationClientState()); Assert.assertNull(invPreferences.getInternalNotificationClientState());
} }
@Test
@SmallTest @SmallTest
@Feature({"Sync"}) @Feature({"Sync"})
public void testReadWriteAndReadData() { public void testReadWriteAndReadData() {
...@@ -61,16 +68,16 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase { ...@@ -61,16 +68,16 @@ public class InvalidationPreferencesTest extends InstrumentationTestCase {
invPreferences.setInternalNotificationClientState(editContext, internalClientState); invPreferences.setInternalNotificationClientState(editContext, internalClientState);
// Nothing should yet have been written. // Nothing should yet have been written.
assertNull(invPreferences.getSavedSyncedAccount()); Assert.assertNull(invPreferences.getSavedSyncedAccount());
assertNull(invPreferences.getSavedSyncedTypes()); Assert.assertNull(invPreferences.getSavedSyncedTypes());
assertNull(invPreferences.getSavedObjectIds()); Assert.assertNull(invPreferences.getSavedObjectIds());
// Write the new data and verify that they are correctly read back. // Write the new data and verify that they are correctly read back.
invPreferences.commit(editContext); invPreferences.commit(editContext);
assertEquals(account, invPreferences.getSavedSyncedAccount()); Assert.assertEquals(account, invPreferences.getSavedSyncedAccount());
assertEquals(syncTypes, invPreferences.getSavedSyncedTypes()); Assert.assertEquals(syncTypes, invPreferences.getSavedSyncedTypes());
assertEquals(objectIds, invPreferences.getSavedObjectIds()); Assert.assertEquals(objectIds, invPreferences.getSavedObjectIds());
assertTrue(Arrays.equals( Assert.assertTrue(Arrays.equals(
internalClientState, invPreferences.getInternalNotificationClientState())); internalClientState, invPreferences.getInternalNotificationClientState()));
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment