Clean up RewriteWebTestsURL() for web tests

1. The conditions for is_wpt_tests are changed to reflect the current
  status
  - no more /external/csswg-test/
  - add /wpt_internal/
  - remove /harness-tests/wpt/, and move the only test into
    wpt_internal/harness-tests

2. Rename RewriteAbsolutePathInCsswgTest() to RewriteWPTAbsolutePath()
   to support running WPT tests manually with content_shell without a
   web server, for testing or debugging.
   The mapped location is changed to external/wpt which is the root
   of file path of WPT tests (e.g. /common/utils.js to
   ...web_tests/external/wpt/common/utils.js).
   Previously the rewriting worked only if we have the same file
   under web_tests/ as web_tests/external/wpt/.

3. Remove the file://// rewriting rule which is not used by any web
   tests.

Change-Id: Ib6c5760b9170a04c315d978619a60883b33dd488
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3847034
Reviewed-by: Mike West <[email protected]>
Commit-Queue: Xianzhu Wang <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1042175}
diff --git a/content/web_test/renderer/blink_test_helpers.cc b/content/web_test/renderer/blink_test_helpers.cc
index b53d4e2..fdc2983 100644
--- a/content/web_test/renderer/blink_test_helpers.cc
+++ b/content/web_test/renderer/blink_test_helpers.cc
@@ -27,6 +27,8 @@
 
 namespace {
 
+constexpr base::StringPiece kFileScheme = "file:///";
+
 base::FilePath GetWebTestsFilePath() {
   static base::FilePath path;
   if (path.empty()) {
@@ -38,20 +40,33 @@
   return path;
 }
 
-// Tests in csswg-test use absolute path links such as
+base::FilePath GetExternalWPTFilePath() {
+  static base::FilePath path;
+  if (path.empty()) {
+    base::FilePath root_path;
+    bool success = base::PathService::Get(base::DIR_SOURCE_ROOT, &root_path);
+    CHECK(success);
+    path = root_path.Append(
+        FILE_PATH_LITERAL("third_party/blink/web_tests/external/wpt"));
+  }
+  return path;
+}
+
+// WPT tests use absolute path links such as
 //   <script src="/resources/testharness.js">.
-// Because we load the tests as local files, such links don't work.
-// This function fixes this issue by rewriting file: URLs which were produced
-// from such links so that they point actual files in web_tests/resources/.
+// If we load the tests as local files (e.g. when we run
+// `content_shell --run-web-tests manually for testing or debugging), such
+// links don't work. This function fixes this issue by rewriting file: URLs
+// which were produced from such links so that they point actual files under
+// the WPT test directory.
 //
-// Note that this isn't applied to external/wpt because tests in external/wpt
-// are accessed via http.
-WebURL RewriteAbsolutePathInCsswgTest(base::StringPiece utf8_url) {
-  static constexpr base::StringPiece kFileScheme = "file:///";
-  if (!base::StartsWith(utf8_url, kFileScheme, base::CompareCase::SENSITIVE))
-    return WebURL();
-  if (utf8_url.find("/web_tests/") != std::string::npos)
-    return WebURL();
+// Note that this doesn't apply when the WPT tests are run by the python script.
+WebURL RewriteWPTAbsolutePath(base::StringPiece utf8_url) {
+  if (!base::StartsWith(utf8_url, kFileScheme, base::CompareCase::SENSITIVE) ||
+      utf8_url.find("/web_tests/") != std::string::npos) {
+    return WebURL(GURL(utf8_url));
+  }
+
 #if BUILDFLAG(IS_WIN)
   // +3 for a drive letter, :, and /.
   static constexpr size_t kFileSchemeAndDriveLen = kFileScheme.size() + 3;
@@ -61,7 +76,7 @@
 #else
   base::StringPiece path = utf8_url.substr(kFileScheme.size());
 #endif
-  base::FilePath new_path = GetWebTestsFilePath().AppendASCII(path);
+  base::FilePath new_path = GetExternalWPTFilePath().AppendASCII(path);
   return WebURL(net::FilePathToFileURL(new_path));
 }
 
@@ -110,12 +125,8 @@
 }
 
 WebURL RewriteWebTestsURL(base::StringPiece utf8_url, bool is_wpt_mode) {
-  if (is_wpt_mode) {
-    WebURL rewritten_url = RewriteAbsolutePathInCsswgTest(utf8_url);
-    if (!rewritten_url.IsEmpty())
-      return rewritten_url;
-    return WebURL(GURL(utf8_url));
-  }
+  if (is_wpt_mode)
+    return RewriteWPTAbsolutePath(utf8_url);
 
   static constexpr base::StringPiece kGenPrefix = "file:///gen/";
 
@@ -141,15 +152,17 @@
 }
 
 WebURL RewriteFileURLToLocalResource(base::StringPiece resource) {
-  // Some web tests use file://// which we resolve as a UNC path. Normalize
-  // them to just file:///.
-  std::string result(resource);
-  static const size_t kFileLen = sizeof("file:///") - 1;
-  while (base::StartsWith(base::ToLowerASCII(result), "file:////",
-                          base::CompareCase::SENSITIVE)) {
-    result = result.substr(0, kFileLen) + result.substr(kFileLen + 1);
-  }
-  return RewriteWebTestsURL(result, /*is_wpt_mode=*/false);
+  return RewriteWebTestsURL(resource, /*is_wpt_mode=*/false);
+}
+
+bool IsWebPlatformTest(base::StringPiece test_url) {
+  // ://web-platform.test is a part of the http/https URL of a wpt test run by
+  // the python script.
+  return test_url.find("://web-platform.test") != std::string::npos ||
+         // These are part of the file URL of a wpt test run manually with
+         // content_shell without a web server.
+         test_url.find("/external/wpt/") != std::string::npos ||
+         test_url.find("/wpt_internal/") != std::string::npos;
 }
 
 }  // namespace content