Commit a82b58b5 authored by Michael Thiessen's avatar Michael Thiessen Committed by Commit Bot

Introduce mechanism for junit tests to use GURLs

Introduces JUnitTestGURLs, which returns GURLs generated from serialized
GURLs for use in junit tests where the native library cannot be loaded.

This allows junit tests to do simple operations with GURLs and allow
unit testing of classes that use GURL without caring too much about more
complex things like formatting or comparing origins, etc.

The alternative to this is migrating the junit tests to instrumentation
tests, but this isn't always feasible.

For example, QualityEnforcerUnitTest is really difficult to re-write as
an instrumentation test because of how it mocks out Activity behaviour
and tests toasts.

Bug: 783819
Change-Id: I37cf3a869958bd30e7ae86d2b212fd19961c1d7e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2417063Reviewed-by: default avatarChris Palmer <palmer@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808347}
parent b7047f3c
......@@ -273,12 +273,22 @@ if (is_android) {
]
}
android_library("gurl_junit_test_support") {
testonly = true
sources = [ "android/test/java/src/org/chromium/url/JUnitTestGURLs.java" ]
deps = [ ":gurl_java" ]
}
android_library("gurl_javatests") {
testonly = true
sources = [ "android/javatests/src/org/chromium/url/GURLJavaTest.java" ]
sources = [
"android/javatests/src/org/chromium/url/GURLJavaTest.java",
"android/javatests/src/org/chromium/url/JUnitTestGURLsTest.java",
]
deps = [
":gurl_android_test_helper_java",
":gurl_java",
":gurl_junit_test_support",
"//base:base_java",
"//base:base_java_test_support",
"//base:jni_java",
......
......@@ -41,7 +41,7 @@ public class GURLJavaTest {
GURLJavaTestHelper.nativeInitializeICU();
}
private void deepAssertEquals(GURL expected, GURL actual) {
/* package */ static void deepAssertEquals(GURL expected, GURL actual) {
Assert.assertEquals(expected, actual);
Assert.assertEquals(expected.getScheme(), actual.getScheme());
Assert.assertEquals(expected.getUsername(), actual.getUsername());
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.url;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import androidx.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.chromium.base.Log;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Batch;
import java.util.Map;
/**
* Tests for JUnitTestGURLs.
*/
@RunWith(BaseJUnit4ClassRunner.class)
@Batch(Batch.UNIT_TESTS)
public class JUnitTestGURLsTest {
private static final String TAG = "JUnitTestGURLs";
@Mock
GURL.Natives mGURLMocks;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
private RuntimeException getErrorForGURL(GURL gurl) {
String serialized = gurl.serialize();
Assert.assertEquals(-1, serialized.indexOf(","));
serialized = serialized.replace(GURL.SERIALIZER_DELIMITER, ',');
return new RuntimeException("Please update the serialization in JUnitTestGURLs.java for "
+ gurl.getPossiblyInvalidSpec() + " to: '" + serialized + "'");
}
@SmallTest
@Test
public void testGURLEquivalence() throws Exception {
doThrow(new RuntimeException("Deserialization required re-initialization."))
.when(mGURLMocks)
.init(any(), any());
Exception exception = null;
for (Map.Entry<String, String> entry : JUnitTestGURLs.sGURLMap.entrySet()) {
GURL gurl = new GURL(entry.getKey());
try {
GURLJni.TEST_HOOKS.setInstanceForTesting(mGURLMocks);
GURL deserialized = JUnitTestGURLs.getGURL(entry.getKey());
GURLJni.TEST_HOOKS.setInstanceForTesting(null);
GURLJavaTest.deepAssertEquals(deserialized, gurl);
} catch (Exception e) {
GURLJni.TEST_HOOKS.setInstanceForTesting(null);
exception = getErrorForGURL(gurl);
Log.e(TAG, "Error: ", exception);
}
}
if (exception != null) throw exception;
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.url;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A Helper class for JUnit tests to be able to use GURLs without requiring native initialization.
* This should be used sparingly, when converting junit tests to Batched Instrumentation tests is
* not feasible.
*
* If any more complex GURL behaviour is tested, like comparing Origins, the test should be written
* as an Instrumentation test instead - you should never mock GURL.
*/
public class JUnitTestGURLs {
// In order to add a test URL:
// 1. Add the URL String as a constant here.
// 2. Add the constant to the map below, with a placeholder string for the GURL serialization.
// 3. Run JUnitTestGURLsTest (eg. './tools/autotest.py -C out/Debug JUnitTestGURLsTest').
// 4. Check logcat output or test exception for the correct serialization String, and place it
// in the map.
public static final String EXAMPLE_URL = "https://www.example.com";
public static final String URL_1 = "https://www.one.com";
public static final String URL_2 = "https://www.two.com";
// Map of URL string to GURL serialization.
/* package */ static final Map<String, String> sGURLMap;
static {
Map<String, String> map = new HashMap<>();
map.put(EXAMPLE_URL,
"82,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,0,-1,"
+ "false,false,https://www.example.com/");
map.put(URL_1,
"78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1,"
+ "false,false,https://www.one.com/");
map.put(URL_2,
"78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1,"
+ "false,false,https://www.two.com/");
sGURLMap = Collections.unmodifiableMap(map);
}
/**
* @return the GURL resulting from parsing the provided url. Must be registered in |sGURLMap|.
*/
public static GURL getGURL(String url) {
String serialized = sGURLMap.get(url);
if (serialized == null) {
throw new IllegalArgumentException("URL " + url + " not found");
}
serialized = serialized.replace(',', GURL.SERIALIZER_DELIMITER);
GURL gurl = GURL.deserialize(serialized);
// If you're here looking to use an empty GURL, just use GURL.emptyGURL() directly.
if (gurl.isEmpty()) {
throw new RuntimeException("Could not deserialize: " + serialized);
}
return gurl;
}
}
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