blob: b44dcea60ceabee27518c616bcbef9c706e92d41 [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
[email protected]0989186e2013-03-06 09:43:467#include "base/command_line.h"
thestigb7aad54f2014-09-05 18:25:398#include "base/files/file_util.h"
[email protected]294e74f2013-04-29 14:50:549#include "base/path_service.h"
Daniel Cheng154c94062018-05-04 23:48:2410#include "base/strings/string_piece.h"
11#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0012#include "base/strings/utf_string_conversions.h"
avi66a07722015-12-25 23:38:1213#include "build/build_config.h"
[email protected]8b5f7752013-08-29 22:40:5014#include "content/public/common/content_switches.h"
danakj89f47082020-09-02 17:53:4315#include "content/web_test/common/web_test_switches.h"
16#include "content/web_test/renderer/test_preferences.h"
Nate Chapin98cf9142018-05-01 20:45:1517#include "net/base/filename_util.h"
Gyuyoung Kim1ac4ca782020-09-11 03:32:5118#include "third_party/blink/public/common/web_preferences/web_preferences.h"
danakj497fbc92020-04-08 20:45:3519#include "ui/display/display.h"
Nate Chapin98cf9142018-05-01 20:45:1520
Xiaohan Wang4b68d6342022-01-15 17:26:0121#if BUILDFLAG(IS_MAC)
Avi Drissmand4f07082023-05-12 18:05:4422#include "base/apple/bundle_locations.h"
Avi Drissmaneac566b02023-08-18 02:56:2123#include "base/apple/foundation_util.h"
Nate Chapin98cf9142018-05-01 20:45:1524#endif
25
26using blink::WebURL;
27
28namespace {
29
Xianzhu Wangc3388372022-09-01 17:25:4330constexpr base::StringPiece kFileScheme = "file:///";
31
Kent Tamura8dffeee2018-10-09 04:29:3832base::FilePath GetWebTestsFilePath() {
33 static base::FilePath path;
34 if (path.empty()) {
35 base::FilePath root_path;
Ho Cheunga18707a2023-10-18 15:30:4036 bool success =
37 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path);
Kent Tamura8dffeee2018-10-09 04:29:3838 CHECK(success);
Kent Tamurafde0e4e2018-11-26 08:27:0039 path = root_path.Append(FILE_PATH_LITERAL("third_party/blink/web_tests/"));
Kent Tamura8dffeee2018-10-09 04:29:3840 }
41 return path;
42}
43
Xianzhu Wangc3388372022-09-01 17:25:4344base::FilePath GetExternalWPTFilePath() {
45 static base::FilePath path;
46 if (path.empty()) {
47 base::FilePath root_path;
Ho Cheunga18707a2023-10-18 15:30:4048 bool success =
49 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root_path);
Xianzhu Wangc3388372022-09-01 17:25:4350 CHECK(success);
51 path = root_path.Append(
52 FILE_PATH_LITERAL("third_party/blink/web_tests/external/wpt"));
53 }
54 return path;
55}
56
57// WPT tests use absolute path links such as
Nate Chapin98cf9142018-05-01 20:45:1558// <script src="/resources/testharness.js">.
Xianzhu Wangc3388372022-09-01 17:25:4359// If we load the tests as local files (e.g. when we run
60// `content_shell --run-web-tests manually for testing or debugging), such
61// links don't work. This function fixes this issue by rewriting file: URLs
62// which were produced from such links so that they point actual files under
63// the WPT test directory.
Nate Chapin98cf9142018-05-01 20:45:1564//
Xianzhu Wangc3388372022-09-01 17:25:4365// Note that this doesn't apply when the WPT tests are run by the python script.
66WebURL RewriteWPTAbsolutePath(base::StringPiece utf8_url) {
67 if (!base::StartsWith(utf8_url, kFileScheme, base::CompareCase::SENSITIVE) ||
68 utf8_url.find("/web_tests/") != std::string::npos) {
69 return WebURL(GURL(utf8_url));
70 }
71
Xiaohan Wang4b68d6342022-01-15 17:26:0172#if BUILDFLAG(IS_WIN)
Nate Chapin98cf9142018-05-01 20:45:1573 // +3 for a drive letter, :, and /.
Daniel Cheng154c94062018-05-04 23:48:2474 static constexpr size_t kFileSchemeAndDriveLen = kFileScheme.size() + 3;
Nate Chapin98cf9142018-05-01 20:45:1575 if (utf8_url.size() <= kFileSchemeAndDriveLen)
76 return WebURL();
danakjd65275f02020-04-22 16:32:0377 base::StringPiece path = utf8_url.substr(kFileSchemeAndDriveLen);
Nate Chapin98cf9142018-05-01 20:45:1578#else
danakjd65275f02020-04-22 16:32:0379 base::StringPiece path = utf8_url.substr(kFileScheme.size());
Nate Chapin98cf9142018-05-01 20:45:1580#endif
Xianzhu Wangc3388372022-09-01 17:25:4381 base::FilePath new_path = GetExternalWPTFilePath().AppendASCII(path);
Nate Chapin98cf9142018-05-01 20:45:1582 return WebURL(net::FilePathToFileURL(new_path));
83}
84
85} // namespace
[email protected]55915a72012-12-18 11:55:2586
[email protected]55915a72012-12-18 11:55:2587namespace content {
88
danakj741848a2020-04-07 22:48:0689void ExportWebTestSpecificPreferences(const TestPreferences& from,
Gyuyoung Kim1ac4ca782020-09-11 03:32:5190 blink::web_pref::WebPreferences* to) {
[email protected]1f70cfa42014-04-04 21:35:0191 to->javascript_can_access_clipboard = from.java_script_can_access_clipboard;
Gyuyoung Kime3a02fd12020-09-18 09:21:4492 to->editing_behavior = from.editing_behavior;
[email protected]1f70cfa42014-04-04 21:35:0193 to->default_font_size = from.default_font_size;
94 to->minimum_font_size = from.minimum_font_size;
Blink Reformat1c4d759e2017-04-09 16:34:5495 to->default_encoding = from.default_text_encoding_name.Utf8().data();
[email protected]1f70cfa42014-04-04 21:35:0196 to->javascript_enabled = from.java_script_enabled;
97 to->supports_multiple_windows = from.supports_multiple_windows;
98 to->loads_images_automatically = from.loads_images_automatically;
99 to->plugins_enabled = from.plugins_enabled;
[email protected]1f70cfa42014-04-04 21:35:01100 to->tabs_to_links = from.tabs_to_links;
[email protected]375db13a2012-12-18 21:42:58101 // experimentalCSSRegionsEnabled is deprecated and ignored.
[email protected]1f70cfa42014-04-04 21:35:01102 to->hyperlink_auditing_enabled = from.hyperlink_auditing_enabled;
[email protected]1f70cfa42014-04-04 21:35:01103 to->allow_running_insecure_content = from.allow_running_of_insecure_content;
[email protected]1f70cfa42014-04-04 21:35:01104 to->allow_file_access_from_file_urls = from.allow_file_access_from_file_urls;
Kent Tamura4e5b27e2018-11-29 08:30:07105 to->web_security_enabled = from.web_security_enabled;
mkwst772ce8142015-01-16 13:28:07106 to->disable_reading_from_canvas = from.disable_reading_from_canvas;
Kent Tamura4e5b27e2018-11-29 08:30:07107 to->strict_mixed_content_checking = from.strict_mixed_content_checking;
mkwst673a452f2015-01-10 14:41:50108 to->strict_powerful_feature_restrictions =
109 from.strict_powerful_feature_restrictions;
carloskd4c23ca2016-07-11 10:33:59110 to->spatial_navigation_enabled = from.spatial_navigation_enabled;
[email protected]55915a72012-12-18 11:55:25111}
112
danakj4964f4152020-04-02 18:55:07113static base::FilePath GetBuildDirectory() {
Xiaohan Wang4b68d6342022-01-15 17:26:01114#if BUILDFLAG(IS_MAC)
Avi Drissmaneac566b02023-08-18 02:56:21115 if (base::apple::AmIBundled()) {
Robert Sesekd4c827f2019-05-21 15:26:19116 // If this is a bundled Content Shell.app, go up one from the outer bundle
117 // directory.
Avi Drissmand4f07082023-05-12 18:05:44118 return base::apple::OuterBundlePath().DirName();
Robert Sesekd4c827f2019-05-21 15:26:19119 }
120#endif
121
Nate Chapin98cf9142018-05-01 20:45:15122 base::FilePath result;
123 bool success = base::PathService::Get(base::DIR_EXE, &result);
124 CHECK(success);
125
Nate Chapin98cf9142018-05-01 20:45:15126 return result;
127}
128
danakjd65275f02020-04-22 16:32:03129WebURL RewriteWebTestsURL(base::StringPiece utf8_url, bool is_wpt_mode) {
Xianzhu Wangc3388372022-09-01 17:25:43130 if (is_wpt_mode)
131 return RewriteWPTAbsolutePath(utf8_url);
Nate Chapin98cf9142018-05-01 20:45:15132
Daniel Cheng154c94062018-05-04 23:48:24133 static constexpr base::StringPiece kGenPrefix = "file:///gen/";
Nate Chapin98cf9142018-05-01 20:45:15134
135 // Map "file:///gen/" to "file://<build directory>/gen/".
Daniel Cheng154c94062018-05-04 23:48:24136 if (base::StartsWith(utf8_url, kGenPrefix, base::CompareCase::SENSITIVE)) {
Nate Chapin98cf9142018-05-01 20:45:15137 base::FilePath gen_directory_path =
138 GetBuildDirectory().Append(FILE_PATH_LITERAL("gen/"));
danakjd65275f02020-04-22 16:32:03139 std::string new_url("file://");
140 new_url.append(gen_directory_path.AsUTF8Unsafe());
David Benjamin2eb24c242023-05-31 15:29:50141 new_url.append(utf8_url.substr(kGenPrefix.size()));
Nate Chapin98cf9142018-05-01 20:45:15142 return WebURL(GURL(new_url));
143 }
144
Kent Tamuraa73680b82018-11-27 09:33:11145 static constexpr base::StringPiece kPrefix = "file:///tmp/web_tests/";
Nate Chapin98cf9142018-05-01 20:45:15146
Daniel Cheng154c94062018-05-04 23:48:24147 if (!base::StartsWith(utf8_url, kPrefix, base::CompareCase::SENSITIVE))
Nate Chapin98cf9142018-05-01 20:45:15148 return WebURL(GURL(utf8_url));
149
danakjd65275f02020-04-22 16:32:03150 std::string new_url("file://");
151 new_url.append(GetWebTestsFilePath().AsUTF8Unsafe());
David Benjamin2eb24c242023-05-31 15:29:50152 new_url.append(utf8_url.substr(kPrefix.size()));
Nate Chapin98cf9142018-05-01 20:45:15153 return WebURL(GURL(new_url));
154}
155
danakjd65275f02020-04-22 16:32:03156WebURL RewriteFileURLToLocalResource(base::StringPiece resource) {
Xianzhu Wangc3388372022-09-01 17:25:43157 return RewriteWebTestsURL(resource, /*is_wpt_mode=*/false);
158}
159
160bool IsWebPlatformTest(base::StringPiece test_url) {
161 // ://web-platform.test is a part of the http/https URL of a wpt test run by
162 // the python script.
163 return test_url.find("://web-platform.test") != std::string::npos ||
164 // These are part of the file URL of a wpt test run manually with
165 // content_shell without a web server.
166 test_url.find("/external/wpt/") != std::string::npos ||
167 test_url.find("/wpt_internal/") != std::string::npos;
danakjd65275f02020-04-22 16:32:03168}
169
[email protected]55915a72012-12-18 11:55:25170} // namespace content