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