xref: /aosp_15_r20/external/cronet/url/android/gurl_java_test_helper.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "url/j_test_jni_headers/GURLJavaTestHelper_jni.h"
13 
14 using base::android::AttachCurrentThread;
15 
16 namespace url {
17 
JNI_GURLJavaTestHelper_InitializeICU(JNIEnv * env)18 static void JNI_GURLJavaTestHelper_InitializeICU(JNIEnv* env) {
19   base::test::InitializeICUForTesting();
20 }
21 
JNI_GURLJavaTestHelper_TestGURLEquivalence(JNIEnv * env)22 static void JNI_GURLJavaTestHelper_TestGURLEquivalence(JNIEnv* env) {
23   const char* cases[] = {
24       // Common Standard URLs.
25       "https://www.google.com",
26       "https://www.google.com/",
27       "https://www.google.com/maps.htm",
28       "https://www.google.com/maps/",
29       "https://www.google.com/index.html",
30       "https://www.google.com/index.html?q=maps",
31       "https://www.google.com/index.html#maps/",
32       "https://foo:[email protected]/maps.htm",
33       "https://www.google.com/maps/au/index.html",
34       "https://www.google.com/maps/au/north",
35       "https://www.google.com/maps/au/north/",
36       "https://www.google.com/maps/au/index.html?q=maps#fragment/",
37       "http://www.google.com:8000/maps/au/index.html?q=maps#fragment/",
38       "https://www.google.com/maps/au/north/?q=maps#fragment",
39       "https://www.google.com/maps/au/north?q=maps#fragment",
40       // Less common standard URLs.
41       "filesystem:http://www.google.com/temporary/bar.html?baz=22",
42       "file:///temporary/bar.html?baz=22",
43       "ftp://foo/test/index.html",
44       "gopher://foo/test/index.html",
45       "ws://foo/test/index.html",
46       // Non-standard,
47       "chrome://foo/bar.html",
48       "httpa://foo/test/index.html",
49       "blob:https://foo.bar/test/index.html",
50       "about:blank",
51       "data:foobar",
52       "scheme:opaque_data",
53       // Invalid URLs.
54       "foobar",
55   };
56   for (const char* uri : cases) {
57     GURL gurl(uri);
58     base::android::ScopedJavaLocalRef<jobject> j_gurl =
59         Java_GURLJavaTestHelper_createGURL(
60             env, base::android::ConvertUTF8ToJavaString(env, uri));
61     GURL gurl2 = GURLAndroid::ToNativeGURL(env, j_gurl);
62     if (gurl != gurl2) {
63       std::stringstream ss;
64       ss << "GURL not equivalent: " << gurl << ", " << gurl2;
65       env->ThrowNew(env->FindClass("java/lang/AssertionError"),
66                     ss.str().data());
67       return;
68     }
69   }
70 }
71 
72 }  // namespace url
73