1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/test/icu_test_util.h"
10 #include "url/android/gurl_android.h"
11 #include "url/gurl.h"
12
13 // Must come after all headers that specialize FromJniType() / ToJniType().
14 #include "url/j_test_jni_headers/GURLJavaTestHelper_jni.h"
15
16 using base::android::AttachCurrentThread;
17
18 namespace url {
19
JNI_GURLJavaTestHelper_InitializeICU(JNIEnv * env)20 static void JNI_GURLJavaTestHelper_InitializeICU(JNIEnv* env) {
21 base::test::InitializeICUForTesting();
22 }
23
JNI_GURLJavaTestHelper_TestGURLEquivalence(JNIEnv * env)24 static void JNI_GURLJavaTestHelper_TestGURLEquivalence(JNIEnv* env) {
25 const char* cases[] = {
26 // Common Standard URLs.
27 "https://www.google.com",
28 "https://www.google.com/",
29 "https://www.google.com/maps.htm",
30 "https://www.google.com/maps/",
31 "https://www.google.com/index.html",
32 "https://www.google.com/index.html?q=maps",
33 "https://www.google.com/index.html#maps/",
34 "https://foo:bar@www.google.com/maps.htm",
35 "https://www.google.com/maps/au/index.html",
36 "https://www.google.com/maps/au/north",
37 "https://www.google.com/maps/au/north/",
38 "https://www.google.com/maps/au/index.html?q=maps#fragment/",
39 "http://www.google.com:8000/maps/au/index.html?q=maps#fragment/",
40 "https://www.google.com/maps/au/north/?q=maps#fragment",
41 "https://www.google.com/maps/au/north?q=maps#fragment",
42 // Less common standard URLs.
43 "filesystem:http://www.google.com/temporary/bar.html?baz=22",
44 "file:///temporary/bar.html?baz=22",
45 "ftp://foo/test/index.html",
46 "gopher://foo/test/index.html",
47 "ws://foo/test/index.html",
48 // Non-standard,
49 "chrome://foo/bar.html",
50 "httpa://foo/test/index.html",
51 "blob:https://foo.bar/test/index.html",
52 "about:blank",
53 "data:foobar",
54 "scheme:opaque_data",
55 // Invalid URLs.
56 "foobar",
57 };
58 for (const char* uri : cases) {
59 GURL gurl(uri);
60 base::android::ScopedJavaLocalRef<jobject> j_gurl =
61 Java_GURLJavaTestHelper_createGURL(
62 env, base::android::ConvertUTF8ToJavaString(env, uri));
63 GURL gurl2 = GURLAndroid::ToNativeGURL(env, j_gurl);
64 if (gurl != gurl2) {
65 std::stringstream ss;
66 ss << "GURL not equivalent: " << gurl << ", " << gurl2;
67 env->ThrowNew(env->FindClass("java/lang/AssertionError"),
68 ss.str().data());
69 return;
70 }
71 }
72 }
73
74 } // namespace url
75