• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 package org.chromium.url;
6 
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 
11 /**
12  * A Helper class for JUnit tests to be able to use GURLs without requiring native initialization.
13  * This should be used sparingly, when converting junit tests to Batched Instrumentation tests is
14  * not feasible.
15  *
16  * If any more complex GURL behaviour is tested, like comparing Origins, the test should be written
17  * as an Instrumentation test instead - you should never mock GURL.
18  */
19 public class JUnitTestGURLs {
20     // In order to add a test URL:
21     // 1. Add the URL String as a constant here.
22     // 2. Add the constant to the map below, with a placeholder string for the GURL serialization.
23     // 3. Run JUnitTestGURLsTest (eg. './tools/autotest.py -C out/Debug JUnitTestGURLsTest').
24     // 4. Check logcat output or test exception for the correct serialization String, and place it
25     //    in the map.
26     public static final String EXAMPLE_URL = "https://www.example.com/";
27     public static final String HTTP_URL = "http://www.example.com/";
28     public static final String URL_1 = "https://www.one.com/";
29     public static final String URL_1_NUMERAL = "https://www.1.com/";
30     public static final String URL_1_WITH_PATH = "https://www.one.com/some_path.html";
31     public static final String URL_2 = "https://www.two.com/";
32     public static final String URL_3 = "https://www.three.com/";
33     public static final String MAPS_URL = "https://maps.google.com/";
34     public static final String SEARCH_URL = "https://www.google.com/search?q=test";
35     public static final String SEARCH_2_URL = "https://www.google.com/search?q=query";
36     public static final String INITIAL_URL = "https://initial.com";
37     public static final String SPECULATED_URL = "https://speculated.com";
38     public static final String NTP_URL = "chrome://newtab/";
39     public static final String NTP_NATIVE_URL = "chrome-native://newtab/";
40     public static final String DOM_DISILLER_URL = "chrome-distiller://url";
41     public static final String RED_1 = "https://www.red.com/page1";
42     public static final String RED_2 = "https://www.red.com/page2";
43     public static final String RED_3 = "https://www.red.com/page3";
44     public static final String BLUE_1 = "https://www.blue.com/page1";
45     public static final String BLUE_2 = "https://www.blue.com/page2";
46     public static final String BLUE_3 = "https://www.blue.com/page3";
47     public static final String AMP_URL =
48             "https://www.google.com/amp/www.nyt.com/ampthml/blogs.html";
49     public static final String AMP_CACHE_URL =
50             "https://www.google.com/amp/s/www.nyt.com/ampthml/blogs.html";
51     public static final String TEXT_FRAGMENT_URL = "https://www.example.com/#:~:text=selector";
52     public static final String MULTI_TEXT_FRAGMENT_URL =
53             "https://www.example.com/#:~:text=selector1&text=selector2&text=selector3";
54     public static final String INVALID_URL = "http://0x100.0/";
55     public static final String GOOGLE_URL = "http://www.google.com/";
56     public static final String GOOGLE_URL_DOGS = "http://www.google.com/dogs";
57     public static final String GOOGLE_URL_DOGS_FUN = "http://www.google.com/dogs-are-fun";
58     public static final String GOOGLE_URL_DOG = "http://www.google.com/dog";
59     public static final String GOOGLE_URL_CAT = "http://www.google.com/cat";
60     public static final String GOOGLE_URL_PIG = "http://www.google.com/pig";
61     public static final String ABOUT_BLANK = "about:blank";
62     public static final String CHROME_ABOUT = "chrome://about";
63 
64     // Map of URL string to GURL serialization.
65     /* package */ static final Map<String, String> sGURLMap;
66     static {
67         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/")68         map.put(EXAMPLE_URL,
69                 "82,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,0,-1,"
70                         + "false,false,https://www.example.com/");
map.put(HTTP_URL, "81,1,true,0,4,0,-1,0,-1,7,15,0,-1,22,1,0,-1,0,-1," + "false,false,http://www.example.com/")71         map.put(HTTP_URL,
72                 "81,1,true,0,4,0,-1,0,-1,7,15,0,-1,22,1,0,-1,0,-1,"
73                         + "false,false,http://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/")74         map.put(URL_1,
75                 "78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1,"
76                         + "false,false,https://www.one.com/");
map.put(URL_1_NUMERAL, "75,1,true,0,5,0,-1,0,-1,8,9,0,-1,17,1,0,-1,0,-1," + "false,false,https://www.1.com/")77         map.put(URL_1_NUMERAL,
78                 "75,1,true,0,5,0,-1,0,-1,8,9,0,-1,17,1,0,-1,0,-1,"
79                         + "false,false,https://www.1.com/");
map.put(URL_1_WITH_PATH, "93,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,15,0,-1,0,-1," + "false,false,https://www.one.com/some_path.html")80         map.put(URL_1_WITH_PATH,
81                 "93,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,15,0,-1,0,-1,"
82                         + "false,false,https://www.one.com/some_path.html");
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/")83         map.put(URL_2,
84                 "78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1,"
85                         + "false,false,https://www.two.com/");
map.put(URL_3, "80,1,true,0,5,0,-1,0,-1,8,13,0,-1,21,1,0,-1,0,-1,false,false,https://www.three.com/")86         map.put(URL_3,
87                 "80,1,true,0,5,0,-1,0,-1,8,13,0,-1,21,1,0,-1,0,-1,false,false,https://www.three.com/");
map.put(RED_1, "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1," + "false,false,https://www.red.com/page1")88         map.put(RED_1,
89                 "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1,"
90                         + "false,false,https://www.red.com/page1");
map.put(RED_2, "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1," + "false,false,https://www.red.com/page2")91         map.put(RED_2,
92                 "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1,"
93                         + "false,false,https://www.red.com/page2");
map.put(RED_3, "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1," + "false,false,https://www.red.com/page3")94         map.put(RED_3,
95                 "83,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,6,0,-1,0,-1,"
96                         + "false,false,https://www.red.com/page3");
map.put(BLUE_1, "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1," + "false,false,https://www.blue.com/page1")97         map.put(BLUE_1,
98                 "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1,"
99                         + "false,false,https://www.blue.com/page1");
map.put(BLUE_2, "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1," + "false,false,https://www.blue.com/page2")100         map.put(BLUE_2,
101                 "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1,"
102                         + "false,false,https://www.blue.com/page2");
map.put(BLUE_3, "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1," + "false,false,https://www.blue.com/page3")103         map.put(BLUE_3,
104                 "84,1,true,0,5,0,-1,0,-1,8,12,0,-1,20,6,0,-1,0,-1,"
105                         + "false,false,https://www.blue.com/page3");
map.put(SEARCH_URL, "94,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,7,30,6,0,-1," + "false,false,https://www.google.com/search?q=test")106         map.put(SEARCH_URL,
107                 "94,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,7,30,6,0,-1,"
108                         + "false,false,https://www.google.com/search?q=test");
map.put(SEARCH_2_URL, "95,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,7,30,7,0,-1," + "false,false,https://www.google.com/search?q=query")109         map.put(SEARCH_2_URL,
110                 "95,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,7,30,7,0,-1,"
111                         + "false,false,https://www.google.com/search?q=query");
map.put(INITIAL_URL, "78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1," + "false,false,https://initial.com/")112         map.put(INITIAL_URL,
113                 "78,1,true,0,5,0,-1,0,-1,8,11,0,-1,19,1,0,-1,0,-1,"
114                         + "false,false,https://initial.com/");
map.put(SPECULATED_URL, "81,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,1,0,-1,0,-1," + "false,false,https://speculated.com/")115         map.put(SPECULATED_URL,
116                 "81,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,1,0,-1,0,-1,"
117                         + "false,false,https://speculated.com/");
map.put(NTP_URL, "73,1,true,0,6,0,-1,0,-1,9,6,0,-1,15,1,0,-1,0,-1," + "false,false,chrome://newtab/")118         map.put(NTP_URL,
119                 "73,1,true,0,6,0,-1,0,-1,9,6,0,-1,15,1,0,-1,0,-1,"
120                         + "false,false,chrome://newtab/");
map.put(NTP_NATIVE_URL, "82,1,true,0,13,0,-1,0,-1,16,6,0,-1,22,1,0,-1,0,-1,false,false," + "chrome-native://newtab/")121         map.put(NTP_NATIVE_URL,
122                 "82,1,true,0,13,0,-1,0,-1,16,6,0,-1,22,1,0,-1,0,-1,false,false,"
123                         + "chrome-native://newtab/");
map.put(DOM_DISILLER_URL, "82,1,true,0,16,0,-1,0,-1,19,3,0,-1,22,1,0,-1,0,-1,false,false," + "chrome-distiller://url/")124         map.put(DOM_DISILLER_URL,
125                 "82,1,true,0,16,0,-1,0,-1,19,3,0,-1,22,1,0,-1,0,-1,false,false,"
126                         + "chrome-distiller://url/");
map.put(MAPS_URL, "82,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,0,-1,false,false,https://maps.google.com/")127         map.put(MAPS_URL,
128                 "82,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,0,-1,false,false,https://maps.google.com/");
map.put(AMP_URL, "116,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,35,0,-1,0,-1,false,false,https://www.google.com/amp/www.nyt.com/ampthml/blogs.html")129         map.put(AMP_URL,
130                 "116,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,35,0,-1,0,-1,false,false,https://www.google.com/amp/www.nyt.com/ampthml/blogs.html");
map.put(AMP_CACHE_URL, "118,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,37,0,-1,0,-1,false,false,https://www.google.com/amp/s/www.nyt.com/ampthml/blogs.html")131         map.put(AMP_CACHE_URL,
132                 "118,1,true,0,5,0,-1,0,-1,8,14,0,-1,22,37,0,-1,0,-1,false,false,https://www.google.com/amp/s/www.nyt.com/ampthml/blogs.html");
map.put(TEXT_FRAGMENT_URL, "100,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,25,16,false,false,https://www.example.com/#:~:text=selector")133         map.put(TEXT_FRAGMENT_URL,
134                 "100,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,25,16,false,false,https://www.example.com/#:~:text=selector");
map.put(MULTI_TEXT_FRAGMENT_URL, "131,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,25,47,false,false,https://www.example.com/#:~:text=selector1&text=selector2&text=selector3")135         map.put(MULTI_TEXT_FRAGMENT_URL,
136                 "131,1,true,0,5,0,-1,0,-1,8,15,0,-1,23,1,0,-1,25,47,false,false,https://www.example.com/#:~:text=selector1&text=selector2&text=selector3");
map.put(INVALID_URL, "73,1,false,0,4,0,-1,0,-1,7,7,0,-1,14,1,0,-1,0,-1,false,false,http://0x100.0/")137         map.put(INVALID_URL,
138                 "73,1,false,0,4,0,-1,0,-1,7,7,0,-1,14,1,0,-1,0,-1,false,false,http://0x100.0/");
map.put(GOOGLE_URL, "80,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,1,0,-1,0,-1,false,false,http://www.google.com/")139         map.put(GOOGLE_URL,
140                 "80,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,1,0,-1,0,-1,false,false,http://www.google.com/");
map.put(GOOGLE_URL_DOGS, "84,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,5,0,-1,0,-1,false,false,http://www.google.com/dogs")141         map.put(GOOGLE_URL_DOGS,
142                 "84,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,5,0,-1,0,-1,false,false,http://www.google.com/dogs");
map.put(GOOGLE_URL_DOGS_FUN, "93,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,13,0,-1,0,-1,false,false,http://www.google.com/dogs-are-fun")143         map.put(GOOGLE_URL_DOGS_FUN,
144                 "93,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,13,0,-1,0,-1,false,false,http://www.google.com/dogs-are-fun");
map.put(GOOGLE_URL_DOG, "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/dog")145         map.put(GOOGLE_URL_DOG,
146                 "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/dog");
map.put(GOOGLE_URL_CAT, "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/cat")147         map.put(GOOGLE_URL_CAT,
148                 "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/cat");
map.put(GOOGLE_URL_PIG, "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/pig")149         map.put(GOOGLE_URL_PIG,
150                 "83,1,true,0,4,0,-1,0,-1,7,14,0,-1,21,4,0,-1,0,-1,false,false,http://www.google.com/pig");
map.put(ABOUT_BLANK, "68,1,true,0,5,0,-1,0,-1,0,-1,0,-1,6,5,0,-1,0,-1,false,false,about:blank")151         map.put(ABOUT_BLANK,
152                 "68,1,true,0,5,0,-1,0,-1,0,-1,0,-1,6,5,0,-1,0,-1,false,false,about:blank");
map.put(CHROME_ABOUT, "72,1,true,0,6,0,-1,0,-1,9,5,0,-1,14,1,0,-1,0,-1,false,false,chrome://about/")153         map.put(CHROME_ABOUT,
154                 "72,1,true,0,6,0,-1,0,-1,9,5,0,-1,14,1,0,-1,0,-1,false,false,chrome://about/");
155         sGURLMap = Collections.unmodifiableMap(map);
156     }
157 
158     /**
159      * @return the GURL resulting from parsing the provided url. Must be registered in |sGURLMap|.
160      */
getGURL(String url)161     public static GURL getGURL(String url) {
162         String serialized = sGURLMap.get(url);
163         if (serialized == null) {
164             throw new IllegalArgumentException("URL " + url + " not found");
165         }
166         serialized = serialized.replace(',', GURL.SERIALIZER_DELIMITER);
167         GURL gurl = GURL.deserialize(serialized);
168         // If you're here looking to use an empty GURL, just use GURL.emptyGURL() directly.
169         if (gurl.isEmpty()) {
170             throw new RuntimeException("Could not deserialize: " + serialized);
171         }
172         return gurl;
173     }
174 }
175