blob: 0f82a9fa6da30fd6dfecb1a5e0bc20d4337b02fd [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]55915a72012-12-18 11:55:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj89f47082020-09-02 17:53:435#include "content/web_test/renderer/blink_test_helpers.h"
[email protected]55915a72012-12-18 11:55:256
Md Hasibul Hasan8d445152024-04-11 07:38:507#include <string_view>
8
[email protected]09891862013-03-06 09:43:469#include "base/command_line.h"
thestigb7aad54f2014-09-05 18:25:3910#include "base/files/file_util.h"
[email protected]294e74f2013-04-29 14:50:5411#include "base/path_service.h"
Daniel Cheng154c94062018-05-04 23:48:2412#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0013#include "base/strings/utf_string_conversions.h"
avi66a07722015-12-25 23:38:1214#include "build/build_config.h"
[email protected]8b5f7752013-08-29 22:40:5015#include "content/public/common/content_switches.h"
danakj89f47082020-09-02 17:53:4316#include "content/web_test/common/web_test_switches.h"
17#include "content/web_test/renderer/test_preferences.h"
Nate Chapin98cf9142018-05-01 20:45:1518#include "net/base/filename_util.h"
Gyuyoung Kim1ac4ca782020-09-11 03:32:5119#include "third_party/blink/public/common/web_preferences/web_preferences.h"
danakj497fbc92020-04-08 20:45:3520#include "ui/display/display.h"
Nate Chapin98cf9142018-05-01 20:45:1521
Xiaohan Wang4b68d6342022-01-15 17:26:0122#if BUILDFLAG(IS_MAC)
Avi Drissmand4f07082023-05-12 18:05:4423#include "base/apple/bundle_locations.h"
Avi Drissmaneac566b02023-08-18 02:56:2124#include "base/apple/foundation_util.h"
Nate Chapin98cf9142018-05-01 20:45:1525#endif
26
27using blink::WebURL;
28
29namespace {
30
Md Hasibul Hasan8d445152024-04-11 07:38:5031constexpr std::string_view kFileScheme = "file:///";
Xianzhu Wangc3388372022-09-01 17:25:4332
Kent Tamura8dffeee2018-10-09 04:29:3833base::FilePath GetWebTestsFilePath() {
34 static base::FilePath path;
35 if (path.empty()) {
36 base::FilePath root_path;
Ho Cheunga18707a2023-10-18 15:30:4037 bool success =
38 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path);
Kent Tamura8dffeee2018-10-09 04:29:3839 CHECK(success);
Kent Tamurafde0e4e2018-11-26 08:27:0040 path = root_path.Append(FILE_PATH_LITERAL("third_party/blink/web_tests/"));
Kent Tamura8dffeee2018-10-09 04:29:3841 }
42 return path;
43}
44
Xianzhu Wangc3388372022-09-01 17:25:4345base::FilePath GetExternalWPTFilePath() {
46 static base::FilePath path;
47 if (path.empty()) {
48 base::FilePath root_path;
Ho Cheunga18707a2023-10-18 15:30:4049 bool success =
50 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path);
Xianzhu Wangc3388372022-09-01 17:25:4351 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 Chapin98cf9142018-05-01 20:45:1559// <script src="/resources/testharness.js">.
Xianzhu Wangc3388372022-09-01 17:25:4360// 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 Chapin98cf9142018-05-01 20:45:1565//
Xianzhu Wangc3388372022-09-01 17:25:4366// Note that this doesn't apply when the WPT tests are run by the python script.
Md Hasibul Hasan8d445152024-04-11 07:38:5067WebURL RewriteWPTAbsolutePath(std::string_view utf8_url) {
Helmut Januschka731c62b2024-11-05 20:56:4668 if (!utf8_url.starts_with(kFileScheme) ||
Xianzhu Wangc3388372022-09-01 17:25:4369 utf8_url.find("/web_tests/") != std::string::npos) {
70 return WebURL(GURL(utf8_url));
71 }
72
Xiaohan Wang4b68d6342022-01-15 17:26:0173#if BUILDFLAG(IS_WIN)
Nate Chapin98cf9142018-05-01 20:45:1574 // +3 for a drive letter, :, and /.
Daniel Cheng154c94062018-05-04 23:48:2475 static constexpr size_t kFileSchemeAndDriveLen = kFileScheme.size() + 3;
Nate Chapin98cf9142018-05-01 20:45:1576 if (utf8_url.size() <= kFileSchemeAndDriveLen)
77 return WebURL();
Md Hasibul Hasan8d445152024-04-11 07:38:5078 std::string_view path = utf8_url.substr(kFileSchemeAndDriveLen);
Nate Chapin98cf9142018-05-01 20:45:1579#else
Md Hasibul Hasan8d445152024-04-11 07:38:5080 std::string_view path = utf8_url.substr(kFileScheme.size());
Nate Chapin98cf9142018-05-01 20:45:1581#endif
Xianzhu Wangc3388372022-09-01 17:25:4382 base::FilePath new_path = GetExternalWPTFilePath().AppendASCII(path);
Nate Chapin98cf9142018-05-01 20:45:1583 return WebURL(net::FilePathToFileURL(new_path));
84}
85
86} // namespace
[email protected]55915a72012-12-18 11:55:2587
[email protected]55915a72012-12-18 11:55:2588namespace content {
89
danakj741848a2020-04-07 22:48:0690void ExportWebTestSpecificPreferences(const TestPreferences& from,
Gyuyoung Kim1ac4ca782020-09-11 03:32:5191 blink::web_pref::WebPreferences* to) {
[email protected]1f70cfa42014-04-04 21:35:0192 to->javascript_can_access_clipboard = from.java_script_can_access_clipboard;
Gyuyoung Kime3a02fd12020-09-18 09:21:4493 to->editing_behavior = from.editing_behavior;
[email protected]1f70cfa42014-04-04 21:35:0194 to->default_font_size = from.default_font_size;
95 to->minimum_font_size = from.minimum_font_size;
Blink Reformat1c4d759e2017-04-09 16:34:5496 to->default_encoding = from.default_text_encoding_name.Utf8().data();
[email protected]1f70cfa42014-04-04 21:35:0197 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]1f70cfa42014-04-04 21:35:01101 to->tabs_to_links = from.tabs_to_links;
[email protected]375db13a2012-12-18 21:42:58102 // experimentalCSSRegionsEnabled is deprecated and ignored.
[email protected]1f70cfa42014-04-04 21:35:01103 to->hyperlink_auditing_enabled = from.hyperlink_auditing_enabled;
[email protected]1f70cfa42014-04-04 21:35:01104 to->allow_running_insecure_content = from.allow_running_of_insecure_content;
[email protected]1f70cfa42014-04-04 21:35:01105 to->allow_file_access_from_file_urls = from.allow_file_access_from_file_urls;
Kent Tamura4e5b27e2018-11-29 08:30:07106 to->web_security_enabled = from.web_security_enabled;
mkwst772ce8142015-01-16 13:28:07107 to->disable_reading_from_canvas = from.disable_reading_from_canvas;
Kent Tamura4e5b27e2018-11-29 08:30:07108 to->strict_mixed_content_checking = from.strict_mixed_content_checking;
mkwst673a452f2015-01-10 14:41:50109 to->strict_powerful_feature_restrictions =
110 from.strict_powerful_feature_restrictions;
carloskd4c23ca2016-07-11 10:33:59111 to->spatial_navigation_enabled = from.spatial_navigation_enabled;
[email protected]55915a72012-12-18 11:55:25112}
113
danakj4964f4152020-04-02 18:55:07114static base::FilePath GetBuildDirectory() {
Xiaohan Wang4b68d6342022-01-15 17:26:01115#if BUILDFLAG(IS_MAC)
Avi Drissmaneac566b02023-08-18 02:56:21116 if (base::apple::AmIBundled()) {
Robert Sesekd4c827f2019-05-21 15:26:19117 // If this is a bundled Content Shell.app, go up one from the outer bundle
118 // directory.
Avi Drissmand4f07082023-05-12 18:05:44119 return base::apple::OuterBundlePath().DirName();
Robert Sesekd4c827f2019-05-21 15:26:19120 }
121#endif
122
Nate Chapin98cf9142018-05-01 20:45:15123 base::FilePath result;
124 bool success = base::PathService::Get(base::DIR_EXE, &result);
125 CHECK(success);
126
Nate Chapin98cf9142018-05-01 20:45:15127 return result;
128}
129
Md Hasibul Hasan8d445152024-04-11 07:38:50130WebURL RewriteWebTestsURL(std::string_view utf8_url, bool is_wpt_mode) {
Xianzhu Wangc3388372022-09-01 17:25:43131 if (is_wpt_mode)
132 return RewriteWPTAbsolutePath(utf8_url);
Nate Chapin98cf9142018-05-01 20:45:15133
Md Hasibul Hasan8d445152024-04-11 07:38:50134 static constexpr std::string_view kGenPrefix = "file:///gen/";
Nate Chapin98cf9142018-05-01 20:45:15135
136 // Map "file:///gen/" to "file://<build directory>/gen/".
Helmut Januschka731c62b2024-11-05 20:56:46137 if (utf8_url.starts_with(kGenPrefix)) {
Nate Chapin98cf9142018-05-01 20:45:15138 base::FilePath gen_directory_path =
139 GetBuildDirectory().Append(FILE_PATH_LITERAL("gen/"));
danakjd65275f02020-04-22 16:32:03140 std::string new_url("file://");
141 new_url.append(gen_directory_path.AsUTF8Unsafe());
David Benjamin2eb24c242023-05-31 15:29:50142 new_url.append(utf8_url.substr(kGenPrefix.size()));
Nate Chapin98cf9142018-05-01 20:45:15143 return WebURL(GURL(new_url));
144 }
145
Md Hasibul Hasan8d445152024-04-11 07:38:50146 static constexpr std::string_view kPrefix = "file:///tmp/web_tests/";
Nate Chapin98cf9142018-05-01 20:45:15147
Helmut Januschka731c62b2024-11-05 20:56:46148 if (!utf8_url.starts_with(kPrefix)) {
Nate Chapin98cf9142018-05-01 20:45:15149 return WebURL(GURL(utf8_url));
Helmut Januschka731c62b2024-11-05 20:56:46150 }
Nate Chapin98cf9142018-05-01 20:45:15151
danakjd65275f02020-04-22 16:32:03152 std::string new_url("file://");
153 new_url.append(GetWebTestsFilePath().AsUTF8Unsafe());
David Benjamin2eb24c242023-05-31 15:29:50154 new_url.append(utf8_url.substr(kPrefix.size()));
Nate Chapin98cf9142018-05-01 20:45:15155 return WebURL(GURL(new_url));
156}
157
Md Hasibul Hasan8d445152024-04-11 07:38:50158WebURL RewriteFileURLToLocalResource(std::string_view resource) {
Xianzhu Wangc3388372022-09-01 17:25:43159 return RewriteWebTestsURL(resource, /*is_wpt_mode=*/false);
160}
161
Md Hasibul Hasan8d445152024-04-11 07:38:50162bool IsWebPlatformTest(std::string_view test_url) {
Xianzhu Wangc3388372022-09-01 17:25:43163 // ://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;
danakjd65275f02020-04-22 16:32:03170}
171
[email protected]55915a72012-12-18 11:55:25172} // namespace content