0

Migrate GURLJavaTest to javatests

native_java_unittests are going away, so these tests need to be migrated
to javatests. I had to add a helper to run the required native code for
some of the tests.

Bug: 1103344
Change-Id: Ia7afbe091eab4f214d0359d88ae2c330dc5cb609
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2405900
Reviewed-by: Yaron Friedman <yfriedman@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807047}
This commit is contained in:
Michael Thiessen
2020-09-15 15:56:59 +00:00
committed by Commit Bot
parent 90fcdb0aa0
commit 48a7379f05
6 changed files with 107 additions and 54 deletions

@ -1126,6 +1126,7 @@ android_library("chrome_test_java") {
"//third_party/android_deps:androidx_recyclerview_recyclerview_java", "//third_party/android_deps:androidx_recyclerview_recyclerview_java",
"//third_party/android_deps:androidx_test_runner_java", "//third_party/android_deps:androidx_test_runner_java",
"//third_party/android_deps:androidx_viewpager_viewpager_java", "//third_party/android_deps:androidx_viewpager_viewpager_java",
"//url:gurl_javatests",
"//url:origin_java", "//url:origin_java",
# TODO (bjoyce): Remove recyclerview_v7 when espresso tests are migrated # TODO (bjoyce): Remove recyclerview_v7 when espresso tests are migrated
@ -1894,6 +1895,7 @@ android_library("browser_java_test_support") {
"//third_party/android_deps:androidx_annotation_annotation_java", "//third_party/android_deps:androidx_annotation_annotation_java",
"//third_party/android_deps:protobuf_lite_runtime_java", "//third_party/android_deps:protobuf_lite_runtime_java",
"//third_party/junit", "//third_party/junit",
"//url:gurl_android_test_helper_java",
] ]
} }
@ -1916,6 +1918,7 @@ static_library("browser_test_support") {
"//components/query_tiles/test:test_support", "//components/query_tiles/test:test_support",
"//content/test:test_support", "//content/test:test_support",
"//net:test_support", "//net:test_support",
"//url:gurl_android_test_helper",
] ]
} }

@ -222,15 +222,6 @@ test("url_unittests") {
"//url/mojom:test_url_mojom_gurl", "//url/mojom:test_url_mojom_gurl",
] ]
} }
if (is_android) {
sources += [ "android/gurl_android_unittest.cc" ]
deps += [
":gurl_android",
":gurl_java",
":gurl_javatests",
":native_j_unittests_jni_headers",
]
}
} }
test("url_perftests") { test("url_perftests") {
@ -258,17 +249,42 @@ fuzzer_test("gurl_fuzzer") {
} }
if (is_android) { if (is_android) {
android_library("gurl_javatests") { source_set("gurl_android_test_helper") {
testonly = true testonly = true
sources = [ sources = [ "android/gurl_java_test_helper.cc" ]
"android/native_java_unittests/src/org/chromium/url/GURLJavaTest.java", deps = [
":gurl_android",
":gurl_j_test_jni_headers",
":url",
"//base/test:test_support",
"//testing/gtest",
] ]
}
android_library("gurl_android_test_helper_java") {
testonly = true
sources =
[ "android/javatests/src/org/chromium/url/GURLJavaTestHelper.java" ]
deps = [ deps = [
":gurl_java", ":gurl_java",
":gurl_jni_headers",
"//base:base_java", "//base:base_java",
"//base:base_java_test_support", "//base:base_java_test_support",
"//base:jni_java", "//base:jni_java",
]
}
android_library("gurl_javatests") {
testonly = true
sources = [ "android/javatests/src/org/chromium/url/GURLJavaTest.java" ]
deps = [
":gurl_android_test_helper_java",
":gurl_java",
"//base:base_java",
"//base:base_java_test_support",
"//base:jni_java",
"//content/public/test/android:content_java_test_support",
"//third_party/android_deps:androidx_core_core_java",
"//third_party/android_deps:androidx_test_runner_java",
"//third_party/android_support_test_runner:rules_java", "//third_party/android_support_test_runner:rules_java",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/junit", "//third_party/junit",
@ -279,10 +295,9 @@ if (is_android) {
# See https://bugs.chromium.org/p/chromium/issues/detail?id=908819 for why we # See https://bugs.chromium.org/p/chromium/issues/detail?id=908819 for why we
# can't put 'java' in the name here. # can't put 'java' in the name here.
generate_jni("native_j_unittests_jni_headers") { generate_jni("gurl_j_test_jni_headers") {
testonly = true testonly = true
sources = [ sources =
"android/native_java_unittests/src/org/chromium/url/GURLJavaTest.java", [ "android/javatests/src/org/chromium/url/GURLJavaTestHelper.java" ]
]
} }
} }

@ -6,29 +6,21 @@
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/test/icu_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "url/android/gurl_android.h" #include "url/android/gurl_android.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/native_j_unittests_jni_headers/GURLJavaTest_jni.h" #include "url/gurl_j_test_jni_headers/GURLJavaTestHelper_jni.h"
using base::android::AttachCurrentThread; using base::android::AttachCurrentThread;
namespace url { namespace url {
class GURLAndroidTest : public ::testing::Test { static void JNI_GURLJavaTestHelper_InitializeICU(JNIEnv* env) {
public: base::test::InitializeICUForTesting();
GURLAndroidTest() }
: j_test_(Java_GURLJavaTest_Constructor(AttachCurrentThread())) {}
const base::android::ScopedJavaGlobalRef<jobject>& j_test() { static void JNI_GURLJavaTestHelper_TestGURLEquivalence(JNIEnv* env) {
return j_test_;
}
private:
base::android::ScopedJavaGlobalRef<jobject> j_test_;
};
TEST_F(GURLAndroidTest, TestGURLEquivalence) {
const char* cases[] = { const char* cases[] = {
// Common Standard URLs. // Common Standard URLs.
"https://www.google.com", "https://www.google.com",
@ -62,17 +54,14 @@ TEST_F(GURLAndroidTest, TestGURLEquivalence) {
// Invalid URLs. // Invalid URLs.
"foobar", "foobar",
}; };
JNIEnv* env = AttachCurrentThread();
for (const char* uri : cases) { for (const char* uri : cases) {
GURL gurl(uri); GURL gurl(uri);
base::android::ScopedJavaLocalRef<jobject> j_gurl = base::android::ScopedJavaLocalRef<jobject> j_gurl =
Java_GURLJavaTest_createGURL( Java_GURLJavaTestHelper_createGURL(
env, j_test(), base::android::ConvertUTF8ToJavaString(env, uri)); env, base::android::ConvertUTF8ToJavaString(env, uri));
std::unique_ptr<GURL> gurl2 = GURLAndroid::ToNativeGURL(env, j_gurl); std::unique_ptr<GURL> gurl2 = GURLAndroid::ToNativeGURL(env, j_gurl);
EXPECT_EQ(gurl, *gurl2); EXPECT_EQ(gurl, *gurl2);
} }
} }
JAVA_TESTS(GURLAndroidTest, j_test())
} // namespace url } // namespace url

@ -0,0 +1,3 @@
include_rules = [
"+content/public/test/android",
]

@ -7,12 +7,18 @@ package org.chromium.url;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import androidx.test.filters.SmallTest;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.annotations.CalledByNativeJavaTest; import org.chromium.base.test.util.Batch;
import org.chromium.content_public.browser.test.NativeLibraryTestUtils;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -21,18 +27,18 @@ import java.net.URISyntaxException;
* the logic is tested there. This test is primarily to make sure everything is plumbed through * the logic is tested there. This test is primarily to make sure everything is plumbed through
* correctly. * correctly.
*/ */
@RunWith(BaseJUnit4ClassRunner.class)
@Batch(Batch.UNIT_TESTS)
public class GURLJavaTest { public class GURLJavaTest {
@Mock @Mock
GURL.Natives mGURLMocks; GURL.Natives mGURLMocks;
@CalledByNative @Before
private GURLJavaTest() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
}
@CalledByNative NativeLibraryTestUtils.loadNativeLibraryNoBrowserProcess();
public GURL createGURL(String uri) { GURLJavaTestHelper.nativeInitializeICU();
return new GURL(uri);
} }
private void deepAssertEquals(GURL expected, GURL actual) { private void deepAssertEquals(GURL expected, GURL actual) {
@ -51,8 +57,15 @@ public class GURLJavaTest {
return Integer.toString(serialization.length()) + GURL.SERIALIZER_DELIMITER + serialization; return Integer.toString(serialization.length()) + GURL.SERIALIZER_DELIMITER + serialization;
} }
@SmallTest
@Test
public void testGURLEquivalence() {
GURLJavaTestHelper.nativeTestGURLEquivalence();
}
// Equivalent of GURLTest.Components // Equivalent of GURLTest.Components
@CalledByNativeJavaTest @SmallTest
@Test
@SuppressWarnings(value = "AuthLeak") @SuppressWarnings(value = "AuthLeak")
public void testComponents() { public void testComponents() {
GURL empty = new GURL(""); GURL empty = new GURL("");
@ -85,7 +98,8 @@ public class GURLJavaTest {
} }
// Equivalent of GURLTest.Empty // Equivalent of GURLTest.Empty
@CalledByNativeJavaTest @SmallTest
@Test
public void testEmpty() { public void testEmpty() {
GURLJni.TEST_HOOKS.setInstanceForTesting(mGURLMocks); GURLJni.TEST_HOOKS.setInstanceForTesting(mGURLMocks);
doThrow(new RuntimeException("Should not need to parse empty URL")) doThrow(new RuntimeException("Should not need to parse empty URL"))
@ -107,7 +121,8 @@ public class GURLJavaTest {
} }
// Test that GURL and URI return the correct Origin. // Test that GURL and URI return the correct Origin.
@CalledByNativeJavaTest @SmallTest
@Test
@SuppressWarnings(value = "AuthLeak") @SuppressWarnings(value = "AuthLeak")
public void testOrigin() throws URISyntaxException { public void testOrigin() throws URISyntaxException {
final String kExpectedOrigin1 = "http://google.com:21/"; final String kExpectedOrigin1 = "http://google.com:21/";
@ -122,12 +137,13 @@ public class GURLJavaTest {
Assert.assertEquals(kExpectedOrigin1, origin.getSpec()); Assert.assertEquals(kExpectedOrigin1, origin.getSpec());
} }
@CalledByNativeJavaTest @SmallTest
@Test
public void testWideInput() throws URISyntaxException { public void testWideInput() throws URISyntaxException {
final String kExpectedSpec = "http://xn--1xa.com/"; final String kExpectedSpec = "http://xn--1xa.com/";
GURL url = new GURL("http://\u03C0.com"); GURL url = new GURL("http://\u03C0.com");
Assert.assertEquals("http://xn--1xa.com/", url.getSpec()); Assert.assertEquals(kExpectedSpec, url.getSpec());
Assert.assertEquals("http", url.getScheme()); Assert.assertEquals("http", url.getScheme());
Assert.assertEquals("", url.getUsername()); Assert.assertEquals("", url.getUsername());
Assert.assertEquals("", url.getPassword()); Assert.assertEquals("", url.getPassword());
@ -138,7 +154,8 @@ public class GURLJavaTest {
Assert.assertEquals("", url.getRef()); Assert.assertEquals("", url.getRef());
} }
@CalledByNativeJavaTest @SmallTest
@Test
@SuppressWarnings(value = "AuthLeak") @SuppressWarnings(value = "AuthLeak")
public void testSerialization() { public void testSerialization() {
GURL cases[] = { GURL cases[] = {
@ -194,7 +211,8 @@ public class GURLJavaTest {
* Tests that we re-parse the URL from the spec, which must always be the last token in the * Tests that we re-parse the URL from the spec, which must always be the last token in the
* serialization, if the serialization version differs. * serialization, if the serialization version differs.
*/ */
@CalledByNativeJavaTest @SmallTest
@Test
public void testSerializationWithVersionSkew() { public void testSerializationWithVersionSkew() {
GURL url = new GURL("https://www.google.com"); GURL url = new GURL("https://www.google.com");
String serialization = (GURL.SERIALIZER_VERSION + 1) String serialization = (GURL.SERIALIZER_VERSION + 1)
@ -208,7 +226,8 @@ public class GURLJavaTest {
/** /**
* Tests that fields that aren't visible to java code are correctly serialized. * Tests that fields that aren't visible to java code are correctly serialized.
*/ */
@CalledByNativeJavaTest @SmallTest
@Test
public void testSerializationOfPrivateFields() { public void testSerializationOfPrivateFields() {
String serialization = GURL.SERIALIZER_VERSION String serialization = GURL.SERIALIZER_VERSION
+ ",true," + ",true,"
@ -226,7 +245,8 @@ public class GURLJavaTest {
/** /**
* Tests serialized GURL truncated by storage. * Tests serialized GURL truncated by storage.
*/ */
@CalledByNativeJavaTest @SmallTest
@Test
public void testTruncatedDeserialization() { public void testTruncatedDeserialization() {
String serialization = "123,1,true,1,2,3,4,5,6,7,8,9,10"; String serialization = "123,1,true,1,2,3,4,5,6,7,8,9,10";
serialization = serialization.replace(',', GURL.SERIALIZER_DELIMITER); serialization = serialization.replace(',', GURL.SERIALIZER_DELIMITER);
@ -237,7 +257,8 @@ public class GURLJavaTest {
/** /**
* Tests serialized GURL truncated by storage. * Tests serialized GURL truncated by storage.
*/ */
@CalledByNativeJavaTest @SmallTest
@Test
public void testCorruptedSerializations() { public void testCorruptedSerializations() {
String serialization = new GURL("https://www.google.ca").serialize(); String serialization = new GURL("https://www.google.ca").serialize();
// Replace the scheme length (5) with an extra delimiter. // Replace the scheme length (5) with an extra delimiter.

@ -0,0 +1,22 @@
// 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 org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
/**
* Helpers for GURLJavaTest that need to call into native code.
*/
@JNINamespace("url")
public class GURLJavaTestHelper {
@CalledByNative
public static GURL createGURL(String uri) {
return new GURL(uri);
}
public static native void nativeInitializeICU();
public static native void nativeTestGURLEquivalence();
}