Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 5 | #include "content/web_test/renderer/blink_test_helpers.h" |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 6 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 7 | #include <string_view> |
| 8 | |
[email protected] | 0989186 | 2013-03-06 09:43:46 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 294e74f | 2013-04-29 14:50:54 | [diff] [blame] | 11 | #include "base/path_service.h" |
Daniel Cheng | 154c9406 | 2018-05-04 23:48:24 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 8b5f775 | 2013-08-29 22:40:50 | [diff] [blame] | 15 | #include "content/public/common/content_switches.h" |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 16 | #include "content/web_test/common/web_test_switches.h" |
| 17 | #include "content/web_test/renderer/test_preferences.h" |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 18 | #include "net/base/filename_util.h" |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 19 | #include "third_party/blink/public/common/web_preferences/web_preferences.h" |
danakj | 497fbc9 | 2020-04-08 20:45:35 | [diff] [blame] | 20 | #include "ui/display/display.h" |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 21 | |
Xiaohan Wang | 4b68d634 | 2022-01-15 17:26:01 | [diff] [blame] | 22 | #if BUILDFLAG(IS_MAC) |
Avi Drissman | d4f0708 | 2023-05-12 18:05:44 | [diff] [blame] | 23 | #include "base/apple/bundle_locations.h" |
Avi Drissman | eac566b0 | 2023-08-18 02:56:21 | [diff] [blame] | 24 | #include "base/apple/foundation_util.h" |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | using blink::WebURL; |
| 28 | |
| 29 | namespace { |
| 30 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 31 | constexpr std::string_view kFileScheme = "file:///"; |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 32 | |
Kent Tamura | 8dffeee | 2018-10-09 04:29:38 | [diff] [blame] | 33 | base::FilePath GetWebTestsFilePath() { |
| 34 | static base::FilePath path; |
| 35 | if (path.empty()) { |
| 36 | base::FilePath root_path; |
Ho Cheung | a18707a | 2023-10-18 15:30:40 | [diff] [blame] | 37 | bool success = |
| 38 | base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path); |
Kent Tamura | 8dffeee | 2018-10-09 04:29:38 | [diff] [blame] | 39 | CHECK(success); |
Kent Tamura | fde0e4e | 2018-11-26 08:27:00 | [diff] [blame] | 40 | path = root_path.Append(FILE_PATH_LITERAL("third_party/blink/web_tests/")); |
Kent Tamura | 8dffeee | 2018-10-09 04:29:38 | [diff] [blame] | 41 | } |
| 42 | return path; |
| 43 | } |
| 44 | |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 45 | base::FilePath GetExternalWPTFilePath() { |
| 46 | static base::FilePath path; |
| 47 | if (path.empty()) { |
| 48 | base::FilePath root_path; |
Ho Cheung | a18707a | 2023-10-18 15:30:40 | [diff] [blame] | 49 | bool success = |
| 50 | base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path); |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 51 | CHECK(success); |
| 52 | path = root_path.Append( |
| 53 | FILE_PATH_LITERAL("third_party/blink/web_tests/external/wpt")); |
| 54 | } |
| 55 | return path; |
| 56 | } |
| 57 | |
| 58 | // WPT tests use absolute path links such as |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 59 | // <script src="/resources/testharness.js">. |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 60 | // If we load the tests as local files (e.g. when we run |
| 61 | // `content_shell --run-web-tests manually for testing or debugging), such |
| 62 | // links don't work. This function fixes this issue by rewriting file: URLs |
| 63 | // which were produced from such links so that they point actual files under |
| 64 | // the WPT test directory. |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 65 | // |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 66 | // Note that this doesn't apply when the WPT tests are run by the python script. |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 67 | WebURL RewriteWPTAbsolutePath(std::string_view utf8_url) { |
Helmut Januschka | 731c62b | 2024-11-05 20:56:46 | [diff] [blame] | 68 | if (!utf8_url.starts_with(kFileScheme) || |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 69 | utf8_url.find("/web_tests/") != std::string::npos) { |
| 70 | return WebURL(GURL(utf8_url)); |
| 71 | } |
| 72 | |
Xiaohan Wang | 4b68d634 | 2022-01-15 17:26:01 | [diff] [blame] | 73 | #if BUILDFLAG(IS_WIN) |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 74 | // +3 for a drive letter, :, and /. |
Daniel Cheng | 154c9406 | 2018-05-04 23:48:24 | [diff] [blame] | 75 | static constexpr size_t kFileSchemeAndDriveLen = kFileScheme.size() + 3; |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 76 | if (utf8_url.size() <= kFileSchemeAndDriveLen) |
| 77 | return WebURL(); |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 78 | std::string_view path = utf8_url.substr(kFileSchemeAndDriveLen); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 79 | #else |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 80 | std::string_view path = utf8_url.substr(kFileScheme.size()); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 81 | #endif |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 82 | base::FilePath new_path = GetExternalWPTFilePath().AppendASCII(path); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 83 | return WebURL(net::FilePathToFileURL(new_path)); |
| 84 | } |
| 85 | |
| 86 | } // namespace |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 87 | |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 88 | namespace content { |
| 89 | |
danakj | 741848a | 2020-04-07 22:48:06 | [diff] [blame] | 90 | void ExportWebTestSpecificPreferences(const TestPreferences& from, |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 91 | blink::web_pref::WebPreferences* to) { |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 92 | to->javascript_can_access_clipboard = from.java_script_can_access_clipboard; |
Gyuyoung Kim | e3a02fd1 | 2020-09-18 09:21:44 | [diff] [blame] | 93 | to->editing_behavior = from.editing_behavior; |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 94 | to->default_font_size = from.default_font_size; |
| 95 | to->minimum_font_size = from.minimum_font_size; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 96 | to->default_encoding = from.default_text_encoding_name.Utf8().data(); |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 97 | to->javascript_enabled = from.java_script_enabled; |
| 98 | to->supports_multiple_windows = from.supports_multiple_windows; |
| 99 | to->loads_images_automatically = from.loads_images_automatically; |
| 100 | to->plugins_enabled = from.plugins_enabled; |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 101 | to->tabs_to_links = from.tabs_to_links; |
[email protected] | 375db13a | 2012-12-18 21:42:58 | [diff] [blame] | 102 | // experimentalCSSRegionsEnabled is deprecated and ignored. |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 103 | to->hyperlink_auditing_enabled = from.hyperlink_auditing_enabled; |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 104 | to->allow_running_insecure_content = from.allow_running_of_insecure_content; |
[email protected] | 1f70cfa4 | 2014-04-04 21:35:01 | [diff] [blame] | 105 | to->allow_file_access_from_file_urls = from.allow_file_access_from_file_urls; |
Kent Tamura | 4e5b27e | 2018-11-29 08:30:07 | [diff] [blame] | 106 | to->web_security_enabled = from.web_security_enabled; |
mkwst | 772ce814 | 2015-01-16 13:28:07 | [diff] [blame] | 107 | to->disable_reading_from_canvas = from.disable_reading_from_canvas; |
Kent Tamura | 4e5b27e | 2018-11-29 08:30:07 | [diff] [blame] | 108 | to->strict_mixed_content_checking = from.strict_mixed_content_checking; |
mkwst | 673a452f | 2015-01-10 14:41:50 | [diff] [blame] | 109 | to->strict_powerful_feature_restrictions = |
| 110 | from.strict_powerful_feature_restrictions; |
carlosk | d4c23ca | 2016-07-11 10:33:59 | [diff] [blame] | 111 | to->spatial_navigation_enabled = from.spatial_navigation_enabled; |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 112 | } |
| 113 | |
danakj | 4964f415 | 2020-04-02 18:55:07 | [diff] [blame] | 114 | static base::FilePath GetBuildDirectory() { |
Xiaohan Wang | 4b68d634 | 2022-01-15 17:26:01 | [diff] [blame] | 115 | #if BUILDFLAG(IS_MAC) |
Avi Drissman | eac566b0 | 2023-08-18 02:56:21 | [diff] [blame] | 116 | if (base::apple::AmIBundled()) { |
Robert Sesek | d4c827f | 2019-05-21 15:26:19 | [diff] [blame] | 117 | // If this is a bundled Content Shell.app, go up one from the outer bundle |
| 118 | // directory. |
Avi Drissman | d4f0708 | 2023-05-12 18:05:44 | [diff] [blame] | 119 | return base::apple::OuterBundlePath().DirName(); |
Robert Sesek | d4c827f | 2019-05-21 15:26:19 | [diff] [blame] | 120 | } |
| 121 | #endif |
| 122 | |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 123 | base::FilePath result; |
| 124 | bool success = base::PathService::Get(base::DIR_EXE, &result); |
| 125 | CHECK(success); |
| 126 | |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 127 | return result; |
| 128 | } |
| 129 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 130 | WebURL RewriteWebTestsURL(std::string_view utf8_url, bool is_wpt_mode) { |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 131 | if (is_wpt_mode) |
| 132 | return RewriteWPTAbsolutePath(utf8_url); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 133 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 134 | static constexpr std::string_view kGenPrefix = "file:///gen/"; |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 135 | |
| 136 | // Map "file:///gen/" to "file://<build directory>/gen/". |
Helmut Januschka | 731c62b | 2024-11-05 20:56:46 | [diff] [blame] | 137 | if (utf8_url.starts_with(kGenPrefix)) { |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 138 | base::FilePath gen_directory_path = |
| 139 | GetBuildDirectory().Append(FILE_PATH_LITERAL("gen/")); |
danakj | d65275f0 | 2020-04-22 16:32:03 | [diff] [blame] | 140 | std::string new_url("file://"); |
| 141 | new_url.append(gen_directory_path.AsUTF8Unsafe()); |
David Benjamin | 2eb24c24 | 2023-05-31 15:29:50 | [diff] [blame] | 142 | new_url.append(utf8_url.substr(kGenPrefix.size())); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 143 | return WebURL(GURL(new_url)); |
| 144 | } |
| 145 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 146 | static constexpr std::string_view kPrefix = "file:///tmp/web_tests/"; |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 147 | |
Helmut Januschka | 731c62b | 2024-11-05 20:56:46 | [diff] [blame] | 148 | if (!utf8_url.starts_with(kPrefix)) { |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 149 | return WebURL(GURL(utf8_url)); |
Helmut Januschka | 731c62b | 2024-11-05 20:56:46 | [diff] [blame] | 150 | } |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 151 | |
danakj | d65275f0 | 2020-04-22 16:32:03 | [diff] [blame] | 152 | std::string new_url("file://"); |
| 153 | new_url.append(GetWebTestsFilePath().AsUTF8Unsafe()); |
David Benjamin | 2eb24c24 | 2023-05-31 15:29:50 | [diff] [blame] | 154 | new_url.append(utf8_url.substr(kPrefix.size())); |
Nate Chapin | 98cf914 | 2018-05-01 20:45:15 | [diff] [blame] | 155 | return WebURL(GURL(new_url)); |
| 156 | } |
| 157 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 158 | WebURL RewriteFileURLToLocalResource(std::string_view resource) { |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 159 | return RewriteWebTestsURL(resource, /*is_wpt_mode=*/false); |
| 160 | } |
| 161 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 162 | bool IsWebPlatformTest(std::string_view test_url) { |
Xianzhu Wang | c338837 | 2022-09-01 17:25:43 | [diff] [blame] | 163 | // ://web-platform.test is a part of the http/https URL of a wpt test run by |
| 164 | // the python script. |
| 165 | return test_url.find("://web-platform.test") != std::string::npos || |
| 166 | // These are part of the file URL of a wpt test run manually with |
| 167 | // content_shell without a web server. |
| 168 | test_url.find("/external/wpt/") != std::string::npos || |
| 169 | test_url.find("/wpt_internal/") != std::string::npos; |
danakj | d65275f0 | 2020-04-22 16:32:03 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 172 | } // namespace content |