File: render_view_context_menu_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1111 lines) | stat: -rw-r--r-- 45,602 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>
#include <variant>
#include <vector>

#include "base/command_line.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom-shared.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/launchservices_utils_mac.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/custom_handlers/protocol_handler.h"
#include "components/custom_handlers/protocol_handler_registry.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/scoped_privacy_sandbox_attestations.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view_delegate.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
#include "ui/gfx/geometry/point_f.h"
#include "url/gurl.h"
#include "url/origin.h"

using testing::AllOf;
using testing::AnyOfArray;
using testing::Contains;
using testing::Ge;
using testing::IsSupersetOf;
using testing::Le;
using testing::Not;

namespace {

class ContextMenuUiTest : public InteractiveBrowserTest {
 public:
  ContextMenuUiTest() = default;

  ContextMenuUiTest(const ContextMenuUiTest&) = delete;
  ContextMenuUiTest& operator=(const ContextMenuUiTest&) = delete;

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
  }
};

// This is a regression test for https://crbug.com/1257907.  It tests using
// "Open link in new tab" context menu item in a subframe, to follow a link
// that should stay in the same SiteInstance (e.g. "about:blank", or "data:"
// URL).  This test is somewhat similar to ChromeNavigationBrowserTest's
// ContextMenuNavigationToInvalidUrl testcase, but 1) uses a subframe, and 2)
// more accurately simulates what the product code does.
//
// The test is compiled out on Mac, because RenderViewContextMenuMacCocoa::Show
// requires running a nested message loop - this would undesirably yield control
// over the next steps to the OS.
#if !BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(ContextMenuUiTest,
                       ContextMenuNavigationToAboutBlankUrlInSubframe) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // Navigate to a page with a cross-site subframe.
  GURL start_url = embedded_test_server()->GetURL(
      "start.com", "/frame_tree/page_with_two_frames_remote_and_local.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), start_url));
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  content::RenderFrameHost* main_frame = web_contents->GetPrimaryMainFrame();
  content::RenderFrameHost* subframe = content::ChildFrameAt(main_frame, 0);
  ASSERT_NE(main_frame->GetLastCommittedOrigin(),
            subframe->GetLastCommittedOrigin());
  ASSERT_EQ("bar.com", subframe->GetLastCommittedOrigin().host());

  // Prepare ContextMenuParams that correspond to a link to an about:blank URL
  // in the cross-site subframe.  This preparation to some extent
  // duplicates/replicates the code in RenderFrameHostImpl::ShowContextMenu.
  //
  // Note that the repro steps in https://crbug.com/1257907 resulted in a
  // navigation to about:blank#blocked because of how a navigation to
  // javascript: URL gets rewritten by RenderProcessHost::FilterURL calls.
  // Directly navigating to an about:blank URL is just as good for replicating a
  // discrepancy between `source_site_instance` and `initiator_origin` (without
  // relying on implementation details of FilterURL).
  GURL link_url("about:blank#blah");
  content::ContextMenuParams params;
  params.link_url = link_url;
  params.is_editable = false;
  params.media_type = blink::mojom::ContextMenuDataMediaType::kNone;
  params.page_url = main_frame->GetLastCommittedURL();
  params.frame_url = subframe->GetLastCommittedURL();
  content::RenderProcessHost* process = subframe->GetProcess();
  process->FilterURL(true, &params.link_url);
  process->FilterURL(true, &params.src_url);

  // Simulate opening a context menu.
  //
  // Note that we can't use TestRenderViewContextMenu (like some other tests),
  // because this wouldn't exercise the product code responsible for the
  // https://crbug.com/1257907 bug (it wouldn't go through
  // ChromeWebContentsViewDelegateViews::ShowContextMenu).
  std::unique_ptr<content::WebContentsViewDelegate> view_delegate =
      CreateWebContentsViewDelegate(web_contents);
  view_delegate->ShowContextMenu(*subframe, params);

  // Simulate using the context menu to "Open link in new tab".
  content::WebContents* new_web_contents = nullptr;
  {
    ui_test_utils::TabAddedWaiter tab_add(browser());
    view_delegate->ExecuteCommandForTesting(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
                                            0);
    tab_add.Wait();
    int index_of_new_tab = browser()->tab_strip_model()->count() - 1;
    new_web_contents =
        browser()->tab_strip_model()->GetWebContentsAt(index_of_new_tab);
  }

  // Verify that the load succeeded and was associated with the right
  // SiteInstance.
  EXPECT_TRUE(WaitForLoadStop(new_web_contents));
  EXPECT_EQ(link_url, new_web_contents->GetLastCommittedURL());
  EXPECT_EQ(new_web_contents->GetPrimaryMainFrame()->GetSiteInstance(),
            subframe->GetSiteInstance());
}
#endif  // !BUILDFLAG(IS_MAC)

using CheckCommandsCallback =
    base::OnceCallback<void(const std::vector<int>&, const std::vector<int>&)>;

struct FencedFrameContextMenuTestCase {
  // Commands to be verified that are enabled initially and disabled after
  // network revocation.
  std::vector<int> command_ids;

  // URL that the target frame will be navigated to.
  std::string relative_url;

  // Either the target HTML element id or click coordinate.
  std::variant<std::string, gfx::PointF> click_target;

  // Invoked before network revocation.
  CheckCommandsCallback callback_before_revocation;

  // Invoked when the menu is opened after network revocation.
  CheckCommandsCallback callback_after_revocation;

  // If true, the test case sets up a nested iframe inside a fenced frame. Else
  // there is a single fenced frame.
  bool is_in_nested_iframe = false;
};

// TODO(crbug.com/375048798): Once Kombucha framework supports querying elements
// inside iframe and fenced frame, convert the context menu tests to use
// Kombucha framework.
class ContextMenuFencedFrameTest : public ContextMenuUiTest {
 public:
  ContextMenuFencedFrameTest() = default;
  ~ContextMenuFencedFrameTest() override = default;
  ContextMenuFencedFrameTest(const ContextMenuFencedFrameTest&) = delete;
  ContextMenuFencedFrameTest& operator=(const ContextMenuFencedFrameTest&) =
      delete;

  // Defines the skeleton of set up method.
  void SetUpOnMainThread() override {
    ContextMenuUiTest::SetUpOnMainThread();

    embedded_https_test_server().AddDefaultHandlers(GetChromeTestDataDir());
    // Add content/test/data for cross_site_iframe_factory.html.
    embedded_https_test_server().ServeFilesFromSourceDirectory(
        "content/test/data");
    embedded_https_test_server().SetSSLConfig(
        net::EmbeddedTestServer::CERT_TEST_NAMES);

    override_registration_ =
        web_app::OsIntegrationTestOverrideImpl::OverrideForTesting();
  }

  void RunTest(FencedFrameContextMenuTestCase& test_case) {
    ASSERT_TRUE(embedded_https_test_server().Start());

    TestCommandsDisabled(test_case);
    TestCommandsBlockedFromExecuting(test_case);
  }

  // Test that commands are disabled in context menu when fenced frame untrusted
  // network is revoked.
  void TestCommandsDisabled(FencedFrameContextMenuTestCase& test_case) {
    // Set up the frames.
    GURL url(
        embedded_https_test_server().GetURL("a.test", test_case.relative_url));
    content::RenderFrameHost* fenced_frame_rfh =
        test_case.is_in_nested_iframe ? CreateFencedFrameWithNestedIframe(url)
                                      : CreateFencedFrame(url);

    // To avoid flakiness and ensure fenced_frame_rfh is ready for hit testing.
    content::WaitForHitTestData(fenced_frame_rfh);

    // Get the nested iframe if there is one, else it is a nullptr.
    content::RenderFrameHost* nested_iframe_rfh =
        content::ChildFrameAt(fenced_frame_rfh, 0);
    if (test_case.is_in_nested_iframe) {
      ASSERT_TRUE(nested_iframe_rfh);
      ASSERT_EQ(nested_iframe_rfh->GetLastCommittedURL(), url);
      content::WaitForHitTestData(nested_iframe_rfh);
    }

    content::RenderFrameHost* target_frame =
        test_case.is_in_nested_iframe ? nested_iframe_rfh : fenced_frame_rfh;

    // Get the coordinate of the click target with respect to the target frame.
    gfx::PointF target =
        std::visit(absl::Overload(
                       [&target_frame = std::as_const(target_frame)](
                           std::string target_id) {
                         return GetCenterCoordinatesOfElementWithId(
                             target_frame, target_id);
                       },
                       [](gfx::PointF target_point) { return target_point; }),
                   test_case.click_target);

    if (test_case.is_in_nested_iframe) {
      // Because the mouse event is forwarded to the `RenderWidgetHost` of the
      // fenced frame, when the element is inside the nested iframe, it needs to
      // be offset by the top left coordinates of the nested iframe relative to
      // the fenced frame.
      const gfx::PointF iframe_offset =
          content::test::GetTopLeftCoordinatesOfElementWithId(fenced_frame_rfh,
                                                              "child-0");
      target.Offset(iframe_offset.x(), iframe_offset.y());
    }

    // Open a context menu by right clicking on the target.
    ContextMenuWaiter menu_observer;
    content::test::SimulateClickInFencedFrameTree(
        target_frame, blink::WebMouseEvent::Button::kRight, target);

    // Wait for context menu to be visible.
    menu_observer.WaitForMenuOpenAndClose();

    // All commands should be present and enabled in the context menu.
    EXPECT_THAT(menu_observer.GetCapturedCommandIds(),
                IsSupersetOf(test_case.command_ids));
    EXPECT_THAT(menu_observer.GetCapturedEnabledCommandIds(),
                IsSupersetOf(test_case.command_ids));

    if (test_case.callback_before_revocation) {
      std::move(test_case.callback_before_revocation)
          .Run(menu_observer.GetCapturedCommandIds(),
               menu_observer.GetCapturedEnabledCommandIds());
    }

    // Disable fenced frame untrusted network access.
    ASSERT_TRUE(ExecJs(fenced_frame_rfh, R"(
      (async () => {
        return window.fence.disableUntrustedNetwork();
      })();
    )"));

    // Open the context menu again.
    ContextMenuWaiter menu_observer_after_revocation;
    content::test::SimulateClickInFencedFrameTree(
        target_frame, blink::WebMouseEvent::Button::kRight, target);

    // Wait for context menu to be visible.
    menu_observer_after_revocation.WaitForMenuOpenAndClose();

    // All commands should be disabled in the context menu after fenced frame
    // has untrusted network access revoked.
    EXPECT_THAT(menu_observer_after_revocation.GetCapturedCommandIds(),
                IsSupersetOf(test_case.command_ids));
    EXPECT_THAT(menu_observer_after_revocation.GetCapturedEnabledCommandIds(),
                Not(Contains(AnyOfArray(test_case.command_ids))));

    if (test_case.callback_after_revocation) {
      std::move(test_case.callback_after_revocation)
          .Run(menu_observer_after_revocation.GetCapturedCommandIds(),
               menu_observer_after_revocation.GetCapturedEnabledCommandIds());
    }
  }

  // Test that commands are blocked from executing when fenced frame untrusted
  // network is revoked.
  // TODO(crbug.com/394523687): Verify no navigation takes place if navigation
  // commands are blocked.
  void TestCommandsBlockedFromExecuting(
      FencedFrameContextMenuTestCase& test_case) {
    for (int command_id : test_case.command_ids) {
      // Set up the frames.
      GURL url(embedded_https_test_server().GetURL("a.test",
                                                   test_case.relative_url));
      content::RenderFrameHost* fenced_frame_rfh =
          test_case.is_in_nested_iframe ? CreateFencedFrameWithNestedIframe(url)
                                        : CreateFencedFrame(url);

      // To avoid flakiness and ensure fenced_frame_rfh is ready for hit
      // testing.
      content::WaitForHitTestData(fenced_frame_rfh);

      // Get the nested iframe if there is one, else it is a nullptr.
      content::RenderFrameHost* nested_iframe_rfh =
          content::ChildFrameAt(fenced_frame_rfh, 0);
      if (test_case.is_in_nested_iframe) {
        ASSERT_TRUE(nested_iframe_rfh);
        ASSERT_EQ(nested_iframe_rfh->GetLastCommittedURL(), url);
        content::WaitForHitTestData(nested_iframe_rfh);
      }

      content::RenderFrameHost* target_frame =
          test_case.is_in_nested_iframe ? nested_iframe_rfh : fenced_frame_rfh;

      // Get the coordinate of the click target with respect to the target
      // frame.
      gfx::PointF target = std::visit(
          absl::Overload(
              [&target_frame =
                   std::as_const(target_frame)](std::string target_id) {
                return GetCenterCoordinatesOfElementWithId(target_frame,
                                                           target_id);
              },
              [](gfx::PointF target_point) { return target_point; }),
          test_case.click_target);

      if (test_case.is_in_nested_iframe) {
        // Because the mouse event is forwarded to the `RenderWidgetHost` of the
        // fenced frame, when the element is inside the nested iframe, it needs
        // to be offset by the top left coordinates of the nested iframe
        // relative to the fenced frame.
        const gfx::PointF iframe_offset =
            content::test::GetTopLeftCoordinatesOfElementWithId(
                fenced_frame_rfh, "child-0");
        target.Offset(iframe_offset.x(), iframe_offset.y());
      }

      // Create a callback that will be invoked before command execution.
      auto before_execute = base::BindLambdaForTesting([&fenced_frame_rfh]() {
        // Disable fenced frame untrusted network access.
        ASSERT_TRUE(ExecJs(fenced_frame_rfh, R"(
          (async () => {
            return window.fence.disableUntrustedNetwork();
          })();
        )"));
      });

      // Set up the observer for the console warning.
      content::WebContentsConsoleObserver console_observer(
          browser()->tab_strip_model()->GetActiveWebContents());
      console_observer.SetPattern("*Context menu command is not executed*");

      // Open a context menu by right clicking on the target.
      ContextMenuWaiter menu_observer(command_id, before_execute);
      content::test::SimulateClickInFencedFrameTree(
          target_frame, blink::WebMouseEvent::Button::kRight, target);

      // Wait for context menu and the command to start execution.
      menu_observer.WaitForMenuOpenAndClose();

      // The command should still be enabled.
      EXPECT_THAT(menu_observer.GetCapturedCommandIds(), Contains(command_id));
      EXPECT_THAT(menu_observer.GetCapturedEnabledCommandIds(),
                  Contains(command_id));

      // The command should not be executed because the fenced frame untrusted
      // network has been revoked.
      EXPECT_EQ(menu_observer.IsCommandExecuted(), false)
          << "Command " << command_id
          << " is executed, however it should be blocked since fenced frame "
             "untrusted network is revoked.";

      ASSERT_TRUE(console_observer.Wait());
      EXPECT_EQ(console_observer.messages().size(), 1u);
    }
  }

  // Create a fenced frame which is navigated to `url`.
  content::RenderFrameHost* CreateFencedFrame(const GURL& url) {
    // Navigate fenced frame to a page with an anchor element.
    GURL main_url(embedded_https_test_server().GetURL(
        "a.test",
        base::StringPrintf("/cross_site_iframe_factory.html?a.test(%s{fenced})",
                           url.spec().c_str())));
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));

    // Get fenced frame render frame host.
    std::vector<content::RenderFrameHost*> child_frames =
        fenced_frame_test_helper().GetChildFencedFrameHosts(
            primary_main_frame_host());
    EXPECT_EQ(child_frames.size(), 1u);
    content::RenderFrameHost* fenced_frame_rfh = child_frames[0];
    EXPECT_EQ(fenced_frame_rfh->GetLastCommittedURL(), url);

    return fenced_frame_rfh;
  }

  // Create a fenced frame with a nested iframe. The nested iframe is navigated
  // to `url`.
  content::RenderFrameHost* CreateFencedFrameWithNestedIframe(const GURL& url) {
    // The page has a fenced frame with a nested iframe inside.
    GURL main_url(embedded_https_test_server().GetURL(
        "a.test",
        base::StringPrintf(
            "/cross_site_iframe_factory.html?a.test(a.test{fenced}(%s))",
            url.spec().c_str())));
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));

    // Get fenced frame render frame host.
    std::vector<content::RenderFrameHost*> child_frames =
        fenced_frame_test_helper().GetChildFencedFrameHosts(
            primary_main_frame_host());
    EXPECT_EQ(child_frames.size(), 1u);
    content::RenderFrameHost* fenced_frame_rfh = child_frames[0];

    return fenced_frame_rfh;
  }

  content::RenderFrameHost* primary_main_frame_host() {
    return browser()
        ->tab_strip_model()
        ->GetActiveWebContents()
        ->GetPrimaryMainFrame();
  }

  void InstallTestWebApp(const GURL& start_url) {
    auto web_app_info =
        web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
    web_app_info->scope = start_url;
    web_app_info->title = u"Test app";
    web_app_info->description = u"Test description";
    web_app_info->user_display_mode =
        web_app::mojom::UserDisplayMode::kStandalone;

    web_app::test::InstallWebApp(browser()->profile(), std::move(web_app_info));
  }

  void CleanupWebApps() {
    web_app::test::UninstallAllWebApps(browser()->profile());
    override_registration_.reset();
  }

  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_test_helper_;
  }

 private:
  content::test::FencedFrameTestHelper fenced_frame_test_helper_;
  // OS integration is needed to be able to launch web applications. This
  // override ensures OS integration doesn't leave any traces.
  std::unique_ptr<web_app::OsIntegrationTestOverrideImpl::BlockingRegistration>
      override_registration_;
};

// Check which commands are present after opening the context menu for a
// fencedframe.
// TODO(crbug.com/40273673): Enable the test.
#if BUILDFLAG(IS_MAC)
#define MAYBE_MenuContentsVerification_Fencedframe \
  DISABLED_MenuContentsVerification_Fencedframe
#else
#define MAYBE_MenuContentsVerification_Fencedframe \
  MenuContentsVerification_Fencedframe
#endif
IN_PROC_BROWSER_TEST_F(ContextMenuFencedFrameTest,
                       MAYBE_MenuContentsVerification_Fencedframe) {
  ASSERT_TRUE(embedded_test_server()->Start());

  auto initial_url = embedded_test_server()->GetURL("/empty.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  // Load a fenced frame.
  GURL fenced_frame_url(
      embedded_test_server()->GetURL("/fenced_frames/title1.html"));
  auto* fenced_frame_rfh = fenced_frame_test_helper().CreateFencedFrame(
      primary_main_frame_host(), fenced_frame_url);

  // To avoid a flakiness and ensure fenced_frame_rfh is ready for hit testing.
  content::WaitForHitTestData(fenced_frame_rfh);

  // Open a context menu.
  ContextMenuWaiter menu_observer;
  blink::WebMouseEvent mouse_event(
      blink::WebInputEvent::Type::kMouseDown,
      blink::WebInputEvent::kNoModifiers,
      blink::WebInputEvent::GetStaticTimeStampForTests());
  mouse_event.button = blink::WebMouseEvent::Button::kRight;
  // These coordinates are relative to the fenced frame's widget since we're
  // forwarding this event directly to the widget.
  mouse_event.SetPositionInWidget(50, 50);

  auto* fenced_frame_widget = fenced_frame_rfh->GetRenderWidgetHost();
  fenced_frame_widget->ForwardMouseEvent(mouse_event);
  mouse_event.SetType(blink::WebInputEvent::Type::kMouseUp);
  fenced_frame_widget->ForwardMouseEvent(mouse_event);

  // Wait for context menu to be visible.
  menu_observer.WaitForMenuOpenAndClose();
  EXPECT_THAT(menu_observer.GetCapturedCommandIds(),
              IsSupersetOf({IDC_BACK, IDC_FORWARD, IDC_RELOAD, IDC_VIEW_SOURCE,
                            IDC_SAVE_PAGE, IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE,
                            IDC_CONTENT_CONTEXT_RELOADFRAME,
                            IDC_CONTENT_CONTEXT_INSPECTELEMENT}));
}

// Check that all fenced frame untrusted network status gated commands are
// disabled if the context menu is inside a fenced frame that has revoked
// untrusted network.
IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    FencedFrameNetworkStatusGatedCommandsDisabledAfterNetworkCutoff) {
  ASSERT_TRUE(embedded_https_test_server().Start());

  // Set up the fenced frame.
  content::RenderFrameHost* fenced_frame_rfh =
      CreateFencedFrame(embedded_https_test_server().GetURL(
          "a.test", "/fenced_frames/title1.html"));

  // Create a context menu for the fenced frame.
  TestRenderViewContextMenu menu(*fenced_frame_rfh,
                                 content::ContextMenuParams());

  // Disable fenced frame untrusted network access.
  ASSERT_TRUE(ExecJs(fenced_frame_rfh, R"(
      (async () => {
        return window.fence.disableUntrustedNetwork();
      })();
    )"));

  auto is_command_id_enabled = [&menu](int command_id) {
    return menu.IsCommandIdEnabled(command_id);
  };

  // Check that the commands that are gated on fenced frame untrusted
  // network status should all be disabled.
  //
  // NOTE: This only checks that the command is disabled. It does not check
  // whether the command is in the context menu. For example, when the context
  // menu opens upon an anchor element, commands that operate on images are not
  // in the menu. However, the `RenderViewContextMenu::IsCommandIdEnabled()`
  // check is independent of whether the command exists in the menu. So it is
  // fine to check it without checking the existence of the command.
  ASSERT_THAT(TestRenderViewContextMenu::
                  GetFencedFrameUntrustedNetworkStatusGatedCommands(),
              testing::Each(testing::ResultOf(is_command_id_enabled,
                                              testing::IsFalse())));
}

// Check that all fenced frame untrusted network status gated commands are
// not allowed to execute if the context menu is inside a fenced frame that has
// revoked untrusted network.
IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    FencedFrameNetworkStatusGatedCommandsBlockedAfterNetworkCutoff) {
  ASSERT_TRUE(embedded_https_test_server().Start());

  // Set up the fenced frame.
  content::RenderFrameHost* fenced_frame_rfh =
      CreateFencedFrame(embedded_https_test_server().GetURL(
          "a.test", "/fenced_frames/title1.html"));

  // Create a context menu for the fenced frame.
  TestRenderViewContextMenu menu(*fenced_frame_rfh,
                                 content::ContextMenuParams());

  // Disable fenced frame untrusted network access.
  ASSERT_TRUE(ExecJs(fenced_frame_rfh, R"(
      (async () => {
        return window.fence.disableUntrustedNetwork();
      })();
    )"));

  auto is_command_executed = [&menu](int command_id) {
    CommandExecutionObserver observer(&menu, command_id);
    menu.ExecuteCommand(command_id, 0);
    return observer.IsCommandExecuted();
  };

  // Check that the commands that are gated on fenced frame untrusted network
  // status should not be allowed to execute.
  ASSERT_THAT(TestRenderViewContextMenu::
                  GetFencedFrameUntrustedNetworkStatusGatedCommands(),
              testing::Each(testing::ResultOf(is_command_executed,
                                              testing::Optional(false))));
}

// Demonstrate the URL can be changed by context menu event listener. Note this
// test does not revoke fenced frame untrusted network. So the command proceeds
// to execute. `TestCommandsBlockedFromExecuting()` covers the case where
// untrusted network is revoked and the command is blocked from executing.
IN_PROC_BROWSER_TEST_F(ContextMenuFencedFrameTest,
                       OnContextMenuListenerAttack) {
  ASSERT_TRUE(embedded_https_test_server().Start());

  // Set up the fenced frame.
  content::RenderFrameHost* fenced_frame_rfh =
      CreateFencedFrame(embedded_https_test_server().GetURL(
          "a.test", "/fenced_frames/context_menu_listener.html"));

  // To avoid flakiness and ensure fenced_frame_rfh is ready for hit
  // testing.
  content::WaitForHitTestData(fenced_frame_rfh);

  // Get the coordinate of the anchor element.
  const gfx::PointF target =
      GetCenterCoordinatesOfElementWithId(fenced_frame_rfh, "anchor");

  // Verify the URL before the listener is invoked.
  ASSERT_EQ(content::EvalJs(fenced_frame_rfh,
                            "document.getElementById('anchor').href")
                .ExtractString(),
            "https://example.com/");

  // Open a context menu by right clicking on the target. The anchor element
  // has a context menu listener which appends the cross-site data to the
  // anchor element's href URL.
  ContextMenuWaiter menu_observer(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
  content::test::SimulateClickInFencedFrameTree(
      fenced_frame_rfh, blink::WebMouseEvent::Button::kRight, target);

  // Wait for context menu and the command to start execution.
  menu_observer.WaitForMenuOpenAndClose();

  // The command should be enabled since the untrusted network is not disabled.
  ASSERT_FALSE(fenced_frame_rfh->IsUntrustedNetworkDisabled());
  EXPECT_THAT(menu_observer.GetCapturedCommandIds(),
              Contains(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
  EXPECT_THAT(menu_observer.GetCapturedEnabledCommandIds(),
              Contains(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));

  // The URL has been changed.
  GURL altered_url("https://example.com#cross-site-data");
  ASSERT_EQ(menu_observer.params().link_url, altered_url);

  // With the untrusted network enabled, the command proceeds to execute.
  EXPECT_EQ(menu_observer.IsCommandExecuted(), true);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    CommonOpenLinkCommandsDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
                      IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW,
                      IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    CommonOpenLinkCommandsDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
                      IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW,
                      IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenLinkInWebAppDisabledInFencedFrameAfterNetworkCutoff) {
  // Install the URL as a web App.
  InstallTestWebApp(GURL("https://www.google.com/"));

  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP},
      .relative_url = "/fenced_frames/web_app.html",
      .click_target = "anchor",
      .is_in_nested_iframe = false};

  RunTest(test_case);
  CleanupWebApps();
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenLinkInWebAppDisabledInNestedIframeAfterNetworkCutoff) {
  // Install the URL as a web App.
  InstallTestWebApp(GURL("https://www.google.com/"));

  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP},
      .relative_url = "/fenced_frames/web_app.html",
      .click_target = "anchor",
      .is_in_nested_iframe = true};

  RunTest(test_case);
  CleanupWebApps();
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenImageInNewTabDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB},
      .relative_url = "/test_visual.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenImageInNewTabDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB},
      .relative_url = "/test_visual.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenAudioInNewTabDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENAVNEWTAB},
      .relative_url = "/accessibility/html/audio.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenAudioInNewTabDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENAVNEWTAB},
      .relative_url = "/accessibility/html/audio.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenVideoInNewTabDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENAVNEWTAB},
      .relative_url = "/media/video-player-autoplay.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameTest,
    OpenVideoInNewTabDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENAVNEWTAB},
      .relative_url = "/media/video-player-autoplay.html",
      .click_target = gfx::PointF(15, 15),
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

// "Open Link in Profile" functionality is not available on ChromeOS where there
// is only one profile.
#if !BUILDFLAG(IS_CHROMEOS)
class ContextMenuFencedFrameMutilpleProfilesTest
    : public ContextMenuFencedFrameTest {
 public:
  ContextMenuFencedFrameMutilpleProfilesTest() = default;
  ~ContextMenuFencedFrameMutilpleProfilesTest() override = default;

  void SetUpOnMainThread() override {
    ContextMenuFencedFrameTest::SetUpOnMainThread();

    ProfileManager* profile_manager = g_browser_process->profile_manager();
    Profile& secondary_profile = profiles::testing::CreateProfileSync(
        profile_manager, profile_manager->GenerateNextProfileDirectoryPath());
    CreateBrowser(&secondary_profile);
  }

  CheckCommandsCallback GetCallbackBeforeRevocation() const {
    return base::BindLambdaForTesting(
        [](const std::vector<int>& captured_commands,
           const std::vector<int>& enabled_commands) {
          ASSERT_THAT(captured_commands,
                      Not(Contains(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE)));
          // "Open Link as User ..." should be present and enabled in the
          // context menu.
          EXPECT_THAT(captured_commands,
                      Contains(AllOf(Ge(IDC_OPEN_LINK_IN_PROFILE_FIRST),
                                     Le(IDC_OPEN_LINK_IN_PROFILE_LAST))));
          EXPECT_THAT(enabled_commands,
                      Contains(AllOf(Ge(IDC_OPEN_LINK_IN_PROFILE_FIRST),
                                     Le(IDC_OPEN_LINK_IN_PROFILE_LAST))));
        });
  }

  CheckCommandsCallback GetCallbackAfterRevocation() const {
    return base::BindLambdaForTesting(
        [](const std::vector<int>& captured_commands,
           const std::vector<int>& enabled_commands) {
          ASSERT_THAT(captured_commands,
                      Not(Contains(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE)));
          // "Open Link as User ..." should be present and disabled in the
          // context menu.
          EXPECT_THAT(captured_commands,
                      Contains(AllOf(Ge(IDC_OPEN_LINK_IN_PROFILE_FIRST),
                                     Le(IDC_OPEN_LINK_IN_PROFILE_LAST))));
          EXPECT_THAT(enabled_commands,
                      Not(Contains(AllOf(Ge(IDC_OPEN_LINK_IN_PROFILE_FIRST),
                                         Le(IDC_OPEN_LINK_IN_PROFILE_LAST)))));
        });
  }
};

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameMutilpleProfilesTest,
    OpenLinkInProfileDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .callback_before_revocation = GetCallbackBeforeRevocation(),
      .callback_after_revocation = GetCallbackAfterRevocation(),
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(
    ContextMenuFencedFrameMutilpleProfilesTest,
    OpenLinkInProfileDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .callback_before_revocation = GetCallbackBeforeRevocation(),
      .callback_after_revocation = GetCallbackAfterRevocation(),
      .is_in_nested_iframe = true};

  RunTest(test_case);
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

class ContextMenuFencedFrameProtocolHandlerTest
    : public ContextMenuFencedFrameTest {
 public:
  ContextMenuFencedFrameProtocolHandlerTest() = default;
  ~ContextMenuFencedFrameProtocolHandlerTest() override = default;

  void SetUpOnMainThread() override {
    ContextMenuFencedFrameTest::SetUpOnMainThread();

#if BUILDFLAG(IS_MAC)
    ASSERT_TRUE(test::RegisterAppWithLaunchServices());
#endif

    // Add a protocol handler.
    std::string protocol{"web+search"};
    AddProtocolHandler(protocol, GURL("https://www.google.com/%s"));
    custom_handlers::ProtocolHandlerRegistry* registry =
        ProtocolHandlerRegistryFactory::GetForBrowserContext(
            browser()->profile());
    ASSERT_EQ(1u, registry->GetHandlersFor(protocol).size());
  }

  void AddProtocolHandler(const std::string& protocol, const GURL& url) {
    custom_handlers::ProtocolHandler handler =
        custom_handlers::ProtocolHandler::CreateProtocolHandler(protocol, url);
    custom_handlers::ProtocolHandlerRegistry* registry =
        ProtocolHandlerRegistryFactory::GetForBrowserContext(
            browser()->profile());
    // Fake that this registration is happening on profile startup. Otherwise
    // it'll try to register with the OS, which causes DCHECKs on Windows when
    // running as admin on Windows 7.
    registry->SetIsLoading(true);
    registry->OnAcceptRegisterProtocolHandler(handler);
    registry->SetIsLoading(true);
    ASSERT_TRUE(registry->IsHandledProtocol(protocol));
  }
};

IN_PROC_BROWSER_TEST_F(ContextMenuFencedFrameProtocolHandlerTest,
                       OpenLinkWithDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKWITH},
      .relative_url = "/fenced_frames/protocol_handler.html",
      .click_target = "anchor",
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(ContextMenuFencedFrameProtocolHandlerTest,
                       OpenLinkWithDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKWITH},
      .relative_url = "/fenced_frames/protocol_handler.html",
      .click_target = "anchor",
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

class ContextMenuLinkPreviewFencedFrameTest
    : public ContextMenuFencedFrameTest {
 public:
  ContextMenuLinkPreviewFencedFrameTest() {
    scoped_feature_list_.InitAndEnableFeature(blink::features::kLinkPreview);
  }

  ~ContextMenuLinkPreviewFencedFrameTest() override = default;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(ContextMenuLinkPreviewFencedFrameTest,
                       LinkPreviewDisabledInFencedFrameAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKPREVIEW},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .is_in_nested_iframe = false};

  RunTest(test_case);
}

IN_PROC_BROWSER_TEST_F(ContextMenuLinkPreviewFencedFrameTest,
                       LinkPreviewDisabledInNestedIframeAfterNetworkCutoff) {
  FencedFrameContextMenuTestCase test_case = {
      .command_ids = {IDC_CONTENT_CONTEXT_OPENLINKPREVIEW},
      .relative_url = "/download-anchor-same-origin.html",
      .click_target = "anchor",
      .is_in_nested_iframe = true};

  RunTest(test_case);
}

class InterestGroupContentBrowserClient : public ChromeContentBrowserClient {
 public:
  InterestGroupContentBrowserClient() = default;
  InterestGroupContentBrowserClient(const InterestGroupContentBrowserClient&) =
      delete;
  InterestGroupContentBrowserClient& operator=(
      const InterestGroupContentBrowserClient&) = delete;

  // ChromeContentBrowserClient overrides:
  // This is needed so that the interest group related APIs can run without
  // failing with the result AuctionResult::kSellerRejected.
  bool IsInterestGroupAPIAllowed(content::BrowserContext* browser_context,
                                 content::RenderFrameHost* render_frame_host,
                                 content::InterestGroupApiOperation operation,
                                 const url::Origin& top_frame_origin,
                                 const url::Origin& api_origin) override {
    return true;
  }
};

// TODO(crbug.com/40285326): This fails with the field trial testing config.
class ContextMenuFencedFrameTestNoTestingConfig
    : public ContextMenuFencedFrameTest {
 public:
  ContextMenuFencedFrameTestNoTestingConfig() = default;
  ~ContextMenuFencedFrameTestNoTestingConfig() override {
    content::SetBrowserClientForTesting(original_client_);
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ContextMenuFencedFrameTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch("disable-field-trial-config");
  }

  // Defines the skeleton of set up method.
  void SetUpOnMainThread() override {
    ContextMenuFencedFrameTest::SetUpOnMainThread();

    content_browser_client_ =
        std::make_unique<InterestGroupContentBrowserClient>();
    original_client_ =
        content::SetBrowserClientForTesting(content_browser_client_.get());
  }

 private:
  std::unique_ptr<InterestGroupContentBrowserClient> content_browser_client_;
  raw_ptr<content::ContentBrowserClient> original_client_;
};

// Test that automatic beacons are sent after clicking "Open Link in New Tab"
// from a contextual menu inside of a fenced frame.
IN_PROC_BROWSER_TEST_F(ContextMenuFencedFrameTestNoTestingConfig,
                       AutomaticBeaconSentAfterContextMenuNavigation) {
  privacy_sandbox::ScopedPrivacySandboxAttestations scoped_attestations(
      privacy_sandbox::PrivacySandboxAttestations::CreateForTesting());
  // Mark all Privacy Sandbox APIs as attested since the test case is testing
  // behaviors not related to attestations.
  privacy_sandbox::PrivacySandboxAttestations::GetInstance()
      ->SetAllPrivacySandboxAttestedForTesting(true);

  constexpr char kReportingURL[] = "/_report_event_server.html";
  constexpr char kBeaconMessage[] = "this is the message";

  // In order to check events reported over the network, we register an HTTP
  // response interceptor for each successful reportEvent request we expect.
  net::test_server::ControllableHttpResponse response(
      &embedded_https_test_server(), kReportingURL);

  ASSERT_TRUE(embedded_https_test_server().Start());

  auto initial_url =
      embedded_https_test_server().GetURL("a.test", "/empty.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  // Load a fenced frame.
  GURL fenced_frame_url(embedded_https_test_server().GetURL(
      "a.test", "/fenced_frames/title1.html"));
  GURL new_tab_url(
      embedded_https_test_server().GetURL("a.test", "/title2.html"));
  EXPECT_TRUE(ExecJs(primary_main_frame_host(),
                     "var fenced_frame = document.createElement('fencedframe');"
                     "fenced_frame.id = 'fenced_frame';"
                     "document.body.appendChild(fenced_frame);"));
  content::RenderFrameHost* fenced_frame_rfh =
      fenced_frame_test_helper().GetMostRecentlyAddedFencedFrame(
          primary_main_frame_host());
  content::TestFrameNavigationObserver observer(fenced_frame_rfh);
  fenced_frame_test_helper().NavigateFencedFrameUsingFledge(
      primary_main_frame_host(), fenced_frame_url, "fenced_frame");
  observer.Wait();

  // Embedder-initiated fenced frame navigation uses a new browsing instance.
  // Fenced frame RenderFrameHost is a new one after navigation, so we need
  // to retrieve it.
  fenced_frame_rfh = fenced_frame_test_helper().GetMostRecentlyAddedFencedFrame(
      primary_main_frame_host());

  // Set the automatic beacon
  EXPECT_TRUE(ExecJs(
      fenced_frame_rfh,
      content::JsReplace(R"(
      window.fence.setReportEventDataForAutomaticBeacons({
        eventType: $1,
        eventData: $2,
        destination: ['seller', 'buyer']
      });
    )",
                         "reserved.top_navigation_commit", kBeaconMessage)));

  // This simulates the conditions when right clicking on a link.
  content::ContextMenuParams params;
  params.is_editable = false;
  params.media_type = blink::mojom::ContextMenuDataMediaType::kNone;
  params.page_url = fenced_frame_url;
  params.link_url = new_tab_url;

  ui_test_utils::TabAddedWaiter tab_add(browser());

  // Open the contextual menu and click "Open Link in New Tab".
  TestRenderViewContextMenu menu(*fenced_frame_rfh, params);
  menu.Init();
  menu.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, 0);

  // Verify the automatic beacon was sent and has the correct data.
  response.WaitForRequest();
  EXPECT_EQ(response.http_request()->content, kBeaconMessage);
}

}  // namespace