Content-shell support for the SetRPHRegistrationMode webdriver command

ChromeDriver implements the SetRPHRegistrationMode but the testing bots
use the content-shell instead of a Chrome instance. Hence, we need to
implement the support for this command in the testdriver-vendor.js via
the TestRunner APIs.

Bug: 1359103
Change-Id: Id0fd360ef487b6437e1fe5a104840500802cad12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5205730
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Javier Fernandez <jfernandez@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1261541}
diff --git a/content/web_test/BUILD.gn b/content/web_test/BUILD.gn
index 38e5fd5..24facc4 100644
--- a/content/web_test/BUILD.gn
+++ b/content/web_test/BUILD.gn
@@ -177,6 +177,8 @@
     "//build:chromeos_buildflags",
     "//cc/base",
     "//cc/paint",
+    "//components/custom_handlers:custom_handlers",
+    "//components/custom_handlers:test_support",
     "//components/download/content/factory",
     "//components/download/public/background_service:public",
     "//components/download/public/common:public",
diff --git a/content/web_test/browser/DEPS b/content/web_test/browser/DEPS
index dc5db24..69c5c57 100644
--- a/content/web_test/browser/DEPS
+++ b/content/web_test/browser/DEPS
@@ -1,5 +1,6 @@
 include_rules = [
   "+components/content_settings/core/common",
+  "+components/custom_handlers",
   "+components/download",
   "+components/keyed_service/core",
   "+components/network_session_configurator/common",
diff --git a/content/web_test/browser/web_test_control_host.cc b/content/web_test/browser/web_test_control_host.cc
index fb75851..18ef5355 100644
--- a/content/web_test/browser/web_test_control_host.cc
+++ b/content/web_test/browser/web_test_control_host.cc
@@ -37,6 +37,8 @@
 #include "base/time/time.h"
 #include "build/build_config.h"
 #include "cc/paint/skia_paint_canvas.h"
+#include "components/custom_handlers/protocol_handler_registry.h"
+#include "components/custom_handlers/simple_protocol_handler_registry_factory.h"
 #include "content/browser/aggregation_service/aggregation_service.h"
 #include "content/browser/attribution_reporting/attribution_manager.h"
 #include "content/browser/renderer_host/frame_tree.h"
@@ -1829,6 +1831,30 @@
   lcpp_hint_ = *hint.get();
 }
 
+void WebTestControlHost::SetRegisterProtocolHandlerMode(
+    mojom::WebTestControlHost::AutoResponseMode mode) {
+  custom_handlers::ProtocolHandlerRegistry* registry =
+      custom_handlers::SimpleProtocolHandlerRegistryFactory::
+          GetForBrowserContext(web_contents()->GetBrowserContext(), true);
+  CHECK(registry);
+
+  switch (mode) {
+    case WebTestControlHost::AutoResponseMode::kNone:
+      registry->SetRphRegistrationMode(
+          custom_handlers::RphRegistrationMode::kNone);
+      return;
+    case WebTestControlHost::AutoResponseMode::kAutoAccept:
+      registry->SetRphRegistrationMode(
+          custom_handlers::RphRegistrationMode::kAutoAccept);
+      return;
+    case WebTestControlHost::AutoResponseMode::kAutoReject:
+      registry->SetRphRegistrationMode(
+          custom_handlers::RphRegistrationMode::kAutoReject);
+      return;
+  }
+  NOTREACHED_NORETURN();
+}
+
 void WebTestControlHost::GoToOffset(int offset) {
   main_window_->GoBackOrForward(offset);
 }
diff --git a/content/web_test/browser/web_test_control_host.h b/content/web_test/browser/web_test_control_host.h
index a2a6f12..d28dfa870 100644
--- a/content/web_test/browser/web_test_control_host.h
+++ b/content/web_test/browser/web_test_control_host.h
@@ -267,6 +267,10 @@
   void SetLCPPNavigationHint(
       blink::mojom::LCPCriticalPathPredictorNavigationTimeHintPtr hint)
       override;
+  // Sets the Protocol Handler Registry in automation mode to avoid the
+  // permission prompt in tests.
+  void SetRegisterProtocolHandlerMode(
+      mojom::WebTestControlHost::AutoResponseMode mode) override;
 
   void DiscardMainWindow();
   void FlushInputAndStartTest(WeakDocumentPtr rfh);
diff --git a/content/web_test/common/web_test.mojom b/content/web_test/common/web_test.mojom
index aee2075..118419e 100644
--- a/content/web_test/common/web_test.mojom
+++ b/content/web_test/common/web_test.mojom
@@ -336,6 +336,20 @@
   // Set Accept Languages via RendererPreferences on the WebContents.
   SetAcceptLanguages(string accept_languages);
 
+  // Enumeration of possible values for automated operation modes in APIs
+  enum AutoResponseMode {
+    kNone = 0,
+    kAutoAccept = 1,
+    kAutoReject = 2,
+  };
+
+  // This method implements the Set RPH Registration Mode WebDriver extension
+  // command.
+  // https://html.spec.whatwg.org/#set-rph-registration-mode
+  // Emulates the ChromeDrivers's SetRPHRegistrationMode method, implemented
+  // via CDP
+  SetRegisterProtocolHandlerMode(AutoResponseMode mode);
+
   // Enable Auto Resize mode. This will cause a layout of the content and
   // the new size will be determined by the renderer after it dispatches
   // the resize event.
diff --git a/content/web_test/renderer/test_runner.cc b/content/web_test/renderer/test_runner.cc
index d3966e7a..8f18365 100644
--- a/content/web_test/renderer/test_runner.cc
+++ b/content/web_test/renderer/test_runner.cc
@@ -370,6 +370,7 @@
                                    v8::Local<v8::Function> callback);
   void SetWillSendRequestClearHeader(const std::string& header);
   void SetWillSendRequestClearReferrer();
+  void SetRphRegistrationMode(gin::Arguments* args);
   void SimulateBrowserWindowFocus(bool value);
   void NavigateSecondaryWindow(const std::string& url);
   void InspectSecondaryWindow();
@@ -773,6 +774,8 @@
       .SetMethod("setPrintingForFrame",
                  &TestRunnerBindings::SetPrintingForFrame)
       .SetMethod("setPrintingSize", &TestRunnerBindings::SetPrintingSize)
+      .SetMethod("setRphRegistrationMode",
+                 &TestRunnerBindings::SetRphRegistrationMode)
       .SetMethod("setScrollbarPolicy", &TestRunnerBindings::NotImplemented)
       .SetMethod("setShouldGeneratePixelResults",
                  &TestRunnerBindings::SetShouldGeneratePixelResults)
@@ -2408,6 +2411,36 @@
   frame_->GetWebTestControlHostRemote()->GoToOffset(offset);
 }
 
+void TestRunnerBindings::SetRphRegistrationMode(gin::Arguments* args) {
+  if (!frame_) {
+    return;
+  }
+
+  if (args->Length() != 1) {
+    args->ThrowTypeError("setRphRegistrationMode expects 1 argument");
+    return;
+  }
+
+  std::string arg;
+  if (!args->GetNext(&arg)) {
+    args->ThrowError();
+    return;
+  }
+
+  auto mode = mojom::WebTestControlHost::AutoResponseMode::kNone;
+  if (arg == "autoAccept") {
+    mode = mojom::WebTestControlHost::AutoResponseMode::kAutoAccept;
+  } else if (arg == "autoReject") {
+    mode = mojom::WebTestControlHost::AutoResponseMode::kAutoReject;
+  } else if (arg != "none") {
+    args->ThrowTypeError(
+        "setRphRegistrationMode called with an invalid 'mode' argument");
+    return;
+  }
+
+  frame_->GetWebTestControlHostRemote()->SetRegisterProtocolHandlerMode(mode);
+}
+
 void TestRunnerBindings::NotImplemented(const gin::Arguments& args) {}
 
 // This class helps track active main windows and when the `blink::WebView` is
diff --git a/third_party/blink/web_tests/ChromeTestExpectations b/third_party/blink/web_tests/ChromeTestExpectations
index 50f8d8e..d048504f 100644
--- a/third_party/blink/web_tests/ChromeTestExpectations
+++ b/third_party/blink/web_tests/ChromeTestExpectations
@@ -877,6 +877,11 @@
 crbug.com/1485918 external/wpt/html/webappapis/scripting/events/event-handler-attributes-body-window.html [ Failure ]
 crbug.com/1485918 external/wpt/html/webappapis/scripting/events/event-handler-attributes-frameset-window.html [ Failure ]
 crbug.com/1485918 external/wpt/html/webappapis/scripting/events/event-handler-attributes-windowless-body.html [ Failure ]
+crbug.com/1485918 external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https.html [ Skip Timeout ]
+crbug.com/1485918 external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https.html [ Skip Timeout ]
+crbug.com/1485918 external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https.html [ Skip Timeout ]
+crbug.com/1485918 external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https.html [ Skip Timeout ]
+crbug.com/1485918 external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https.html [ Skip Timeout ]
 crbug.com/1485918 external/wpt/intersection-observer/root-margin.html [ Failure ]
 crbug.com/1485918 external/wpt/intersection-observer/v2/blur-filter.html [ Failure ]
 crbug.com/1485918 external/wpt/intersection-observer/v2/drop-shadow-filter-vertical-rl.html [ Failure ]
@@ -941,3 +946,8 @@
 crbug.com/1485918 external/wpt/webxr/dom-overlay/nested_fullscreen.https.html [ Failure Timeout ] # Timeout for headless shell
 crbug.com/1485918 external/wpt/window-management/multi-screen-window-open-fullscreen.tentative.https.html [ Failure ]
 crbug.com/1485918 external/wpt/workers/shared-worker-partitioned-cookies.tentative.https.html [ Failure Timeout ]
+crbug.com/1485918 wpt_internal/css/css-pseudo/spelling-error-color-003.html [ Failure ]
+crbug.com/1485918 wpt_internal/dom/abort/abort-signal-memory-tests.https.any.html [ Failure ]
+crbug.com/1485918 wpt_internal/dom/abort/abort-signal-memory-tests.https.any.worker.html [ Failure ]
+crbug.com/1485918 wpt_internal/geolocation-api/disconnected-frame-permission-denied.https.html [ Timeout ]
+crbug.com/1485918 wpt_internal/serial/serial_requestPort-mojoServiceUnavailable.https.window.html [ Timeout ]
diff --git a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt b/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt
deleted file mode 100644
index 428a0b6ed..0000000
--- a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-Harness Error. harness_status.status = 1 , harness_status.message = Error: unimplemented
-Harness: the test ran to completion.
-
diff --git a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt b/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt
deleted file mode 100644
index 428a0b6ed..0000000
--- a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-Harness Error. harness_status.status = 1 , harness_status.message = Error: unimplemented
-Harness: the test ran to completion.
-
diff --git a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt b/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt
deleted file mode 100644
index 428a0b6ed..0000000
--- a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-Harness Error. harness_status.status = 1 , harness_status.message = Error: unimplemented
-Harness: the test ran to completion.
-
diff --git a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt b/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt
deleted file mode 100644
index 428a0b6ed..0000000
--- a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-Harness Error. harness_status.status = 1 , harness_status.message = Error: unimplemented
-Harness: the test ran to completion.
-
diff --git a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt b/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt
deleted file mode 100644
index 428a0b6ed..0000000
--- a/third_party/blink/web_tests/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-Harness Error. harness_status.status = 1 , harness_status.message = Error: unimplemented
-Harness: the test ran to completion.
-
diff --git a/third_party/blink/web_tests/external/wpt/resources/testdriver.js b/third_party/blink/web_tests/external/wpt/resources/testdriver.js
index 20140b2..3d3ed31 100644
--- a/third_party/blink/web_tests/external/wpt/resources/testdriver.js
+++ b/third_party/blink/web_tests/external/wpt/resources/testdriver.js
@@ -1153,10 +1153,6 @@
             throw new Error("set_spc_transaction_mode() is not implemented by testdriver-vendor.js");
         },
 
-        set_rph_registration_mode: function(mode, context=null) {
-            return Promise.reject(new Error("unimplemented"));
-        },
-
         async cancel_fedcm_dialog(context=null) {
             throw new Error("cancel_fedcm_dialog() is not implemented by testdriver-vendor.js");
         },
diff --git a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt b/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt
deleted file mode 100644
index 5b37deb..0000000
--- a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment-nosw.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-All subtests passed and are omitted for brevity.
-See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
-Harness: the test ran to completion.
diff --git a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt b/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt
deleted file mode 100644
index 5b37deb..0000000
--- a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-fragment.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-All subtests passed and are omitted for brevity.
-See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
-Harness: the test ran to completion.
diff --git a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt b/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt
deleted file mode 100644
index 5b37deb..0000000
--- a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-path.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-All subtests passed and are omitted for brevity.
-See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
-Harness: the test ran to completion.
diff --git a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt b/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt
deleted file mode 100644
index 5b37deb..0000000
--- a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query-nosw.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-All subtests passed and are omitted for brevity.
-See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
-Harness: the test ran to completion.
diff --git a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt b/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt
deleted file mode 100644
index 5b37deb..0000000
--- a/third_party/blink/web_tests/platform/linux-chrome/external/wpt/html/webappapis/system-state-and-capabilities/the-navigator-object/protocol-handler-query.https-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-All subtests passed and are omitted for brevity.
-See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
-Harness: the test ran to completion.
diff --git a/third_party/blink/web_tests/resources/testdriver-vendor.js b/third_party/blink/web_tests/resources/testdriver-vendor.js
index 020b89d..46567b0 100644
--- a/third_party/blink/web_tests/resources/testdriver-vendor.js
+++ b/third_party/blink/web_tests/resources/testdriver-vendor.js
@@ -532,6 +532,10 @@
       return {'x': window.screenX, 'y': window.screenY, 'width': window.outerWidth, 'height': window.outerHeight};
   }
 
+  window.test_driver_internal.set_rph_registration_mode = async function (mode, context) {
+      window.testRunner.setRphRegistrationMode(mode);
+  };
+
   window.test_driver_internal.get_fedcm_dialog_type = async function() {
     return internals.getFedCmDialogType();
   }
diff --git a/third_party/blink/web_tests/resources/testdriver.js b/third_party/blink/web_tests/resources/testdriver.js
index 20140b2..3d3ed31 100644
--- a/third_party/blink/web_tests/resources/testdriver.js
+++ b/third_party/blink/web_tests/resources/testdriver.js
@@ -1153,10 +1153,6 @@
             throw new Error("set_spc_transaction_mode() is not implemented by testdriver-vendor.js");
         },
 
-        set_rph_registration_mode: function(mode, context=null) {
-            return Promise.reject(new Error("unimplemented"));
-        },
-
         async cancel_fedcm_dialog(context=null) {
             throw new Error("cancel_fedcm_dialog() is not implemented by testdriver-vendor.js");
         },