1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/feature_list.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/test/base/ui_test_utils.h"
9 #include "chrome/test/payments/payment_request_platform_browsertest_base.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/common/content_features.h"
13 #include "content/public/test/browser_test.h"
14 #include "net/dns/mock_host_resolver.h"
17 class CapabilityDelegationBrowserTest
18 : public payments::PaymentRequestPlatformBrowserTestBase {
20 CapabilityDelegationBrowserTest() {
21 feature_list_.InitAndEnableFeature(
22 features::kCapabilityDelegationPaymentRequest);
25 ~CapabilityDelegationBrowserTest() override = default;
27 void SetUpOnMainThread() override {
28 host_resolver()->AddRule("*", "127.0.0.1");
29 ASSERT_TRUE(https_server()->InitializeAndListen());
30 content::SetupCrossSiteRedirector(https_server());
31 https_server()->ServeFilesFromSourceDirectory(
32 "chrome/test/data/capability_delegation");
33 https_server()->StartAcceptingConnections();
37 base::test::ScopedFeatureList feature_list_;
39 DISALLOW_COPY_AND_ASSIGN(CapabilityDelegationBrowserTest);
42 IN_PROC_BROWSER_TEST_F(CapabilityDelegationBrowserTest, PaymentRequest) {
43 // Navigate the top frame.
45 https_server()->GetURL("a.com", "/payment_request_delegation.html"));
46 ui_test_utils::NavigateToURL(browser(), main_url);
48 // Navigate the sub-frame cross-site.
49 content::WebContents* active_web_contents =
50 browser()->tab_strip_model()->GetActiveWebContents();
52 https_server()->GetURL("b.com", "/payment_request_delegation_sub.html"));
54 NavigateIframeToURL(active_web_contents, "iframe", cross_site_url));
56 // Confirm that the subframe is cross-process depending on the process
58 content::RenderFrameHost* frame_host =
59 ChildFrameAt(active_web_contents->GetMainFrame(), 0);
60 ASSERT_TRUE(frame_host);
61 EXPECT_EQ(cross_site_url, frame_host->GetLastCommittedURL());
62 auto* main_instance = active_web_contents->GetMainFrame()->GetSiteInstance();
63 auto* subframe_instance = frame_host->GetSiteInstance();
64 if (main_instance->RequiresDedicatedProcess()) {
65 // Subframe is cross process because it can't be place in the main frame's
67 EXPECT_TRUE(frame_host->IsCrossProcessSubframe());
69 // The main frame does not require a dedicated process so the subframe will
70 // be placed in the same process as the main frame.
71 EXPECT_FALSE(frame_host->IsCrossProcessSubframe());
72 EXPECT_FALSE(subframe_instance->RequiresDedicatedProcess());
73 EXPECT_EQ(content::AreDefaultSiteInstancesEnabled(),
74 main_instance == subframe_instance);
77 // Without either user activation or payment request token, PaymentRequest
78 // dialog is not allowed.
79 EXPECT_EQ("NotAllowedError",
80 content::EvalJs(active_web_contents, "sendRequestToSubframe(false)",
81 content::EXECUTE_SCRIPT_NO_USER_GESTURE));
83 // Without user activation but with the delegation option, PaymentRequest
84 // dialog is not allowed.
85 EXPECT_EQ("NotAllowedError",
86 content::EvalJs(active_web_contents, "sendRequestToSubframe(true)",
87 content::EXECUTE_SCRIPT_NO_USER_GESTURE));
89 // With user activation but without the delegation option, PaymentRequest
90 // dialog is not allowed.
91 EXPECT_EQ("NotAllowedError", content::EvalJs(active_web_contents,
92 "sendRequestToSubframe(false)"));
94 // With both user activation and the delegation option, PaymentRequest dialog
95 // is shown and then successfully aborted by the script.
96 EXPECT_EQ("AbortError", content::EvalJs(active_web_contents,
97 "sendRequestToSubframe(true)"));