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

content / browser / renderer_host / render_widget_host_view_child_frame_browsertest.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/viz/common/surfaces/surface_id.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/test_content_browser_client.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/features.h"
#include "ui/gfx/geometry/size.h"

namespace content {

namespace {

class RenderWidgetHostViewChildFrameBrowserTest : public ContentBrowserTest {
 public:
  RenderWidgetHostViewChildFrameBrowserTest() = default;

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

  void SetUpCommandLine(base::CommandLine* command_line) override {
    IsolateAllSitesForTesting(command_line);
  }

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

  // Tests that the FrameSinkId of each child frame has been updated by the
  // `blink::RemoteFrame`.
  void CheckFrameSinkId(RenderFrameHost* render_frame_host) {
    RenderWidgetHostViewBase* child_view =
        static_cast<RenderFrameHostImpl*>(render_frame_host)
            ->GetRenderWidgetHost()
            ->GetView();
    // Only interested in updated FrameSinkIds on child frames.
    if (!child_view || !child_view->IsRenderWidgetHostViewChildFrame())
      return;

    // Ensure that the received viz::FrameSinkId was correctly set on the child
    // frame.
    viz::FrameSinkId actual_frame_sink_id_ = child_view->GetFrameSinkId();
    EXPECT_EQ(expected_frame_sink_id_, actual_frame_sink_id_);

    // The viz::FrameSinkID will be replaced while the test blocks for
    // navigation. It should differ from the information stored in the child's
    // RenderWidgetHost.
    EXPECT_NE(base::checked_cast<uint32_t>(
                  child_view->GetRenderWidgetHost()->GetProcess()->GetID()),
              actual_frame_sink_id_.client_id());
    EXPECT_NE(base::checked_cast<uint32_t>(
                  child_view->GetRenderWidgetHost()->GetRoutingID()),
              actual_frame_sink_id_.sink_id());
  }

  void GiveItSomeTime() {
    base::RunLoop run_loop;
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout());
    run_loop.Run();
  }

  void set_expected_frame_sink_id(viz::FrameSinkId frame_sink_id) {
    expected_frame_sink_id_ = frame_sink_id;
  }

 private:
  viz::FrameSinkId expected_frame_sink_id_;
};

// Tests that the screen is properly reflected for RWHVChildFrame.
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest, Screen) {
  GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
                            ->GetPrimaryFrameTree()
                            .root();

  // Load cross-site page into iframe.
  GURL cross_site_url(
      embedded_test_server()->GetURL("foo.com", "/title2.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));

  int main_frame_screen_width =
      EvalJs(shell()->web_contents()->GetPrimaryMainFrame(),
             "window.screen.width")
          .ExtractInt();
  EXPECT_NE(main_frame_screen_width, 0);

  shell()->web_contents()->GetPrimaryMainFrame()->ForEachRenderFrameHost(
      [&](RenderFrameHost* frame_host) {
        EXPECT_EQ(main_frame_screen_width,
                  EvalJs(frame_host, "window.screen.width"));
      });
}

// Auto-resize is only implemented for Ash and GuestViews. So we need to inject
// an implementation that actually resizes the top level widget.
class AutoResizeWebContentsDelegate : public WebContentsDelegate {
  void ResizeDueToAutoResize(WebContents* web_contents,
                             const gfx::Size& new_size) override {
    RenderWidgetHostView* view =
        web_contents->GetTopLevelRenderWidgetHostView();
    view->SetSize(new_size);
  }
};

// Test that the |visible_viewport_size| from the top frame is propagated to all
// local roots (aka RenderWidgets):
// a) Initially upon adding the child frame (we create this scenario by
// navigating a child on b.com to c.com, where we already have a RenderProcess
// active for c.com).
// b) When resizing the top level widget.
// c) When auto-resize is enabled for the top level main frame and the renderer
// resizes the top level widget.
// d) When auto-resize is enabled for the nested main frame and the renderer
// resizes the nested widget.
// TODO(b/40945321): Flaky on Fuchsia and Linux.
#if BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
#define MAYBE_VisualPropertiesPropagation_VisibleViewportSize \
  DISABLED_VisualPropertiesPropagation_VisibleViewportSize
#else
#define MAYBE_VisualPropertiesPropagation_VisibleViewportSize \
  VisualPropertiesPropagation_VisibleViewportSize
#endif
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest,
                       MAYBE_VisualPropertiesPropagation_VisibleViewportSize) {
  GURL main_url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b,c,about:blank)"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents());
  FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
  RenderWidgetHostView* root_view =
      root->current_frame_host()->GetRenderWidgetHost()->GetView();

  // We attach an inner WebContents which contains b.com and c.com subframes,
  // too.
  auto* nested_contents = static_cast<WebContentsImpl*>(
      CreateAndAttachInnerContents(root->child_at(2)->current_frame_host()));
  EXPECT_TRUE(NavigateToURL(
      nested_contents, embedded_test_server()->GetURL(
                           "a.com", "/cross_site_iframe_factory.html?a(b,c)")));
  FrameTreeNode* nested_root = nested_contents->GetPrimaryFrameTree().root();
  RenderWidgetHostView* nested_root_view =
      nested_root->current_frame_host()->GetRenderWidgetHost()->GetView();

  // Watch processes for a.com and c.com, as we will throw away b.com when we
  // navigate it below.
  auto* root_rwh = root->current_frame_host()->GetRenderWidgetHost();
  auto* child_rwh =
      root->child_at(1)->current_frame_host()->GetRenderWidgetHost();
  ASSERT_NE(root_rwh->GetProcess(), child_rwh->GetProcess());

  auto* nested_root_rwh =
      nested_root->current_frame_host()->GetRenderWidgetHost();
  auto* nested_child_rwh =
      nested_root->child_at(1)->current_frame_host()->GetRenderWidgetHost();
  ASSERT_NE(nested_root_rwh->GetProcess(), nested_child_rwh->GetProcess());

  const gfx::Size initial_size = root_view->GetVisibleViewportSize();
  ASSERT_FALSE(initial_size.IsEmpty());

  gfx::Size nested_initial_size = nested_root_view->GetVisibleViewportSize();
  while (nested_initial_size.IsEmpty()) {
    // CrossProcessFrameConnector for `nested_child_rwh` must receive a
    // SetRectInParentView() IPC before it has a viewport size. Run tasks until
    // that IPC arrives.
    base::RunLoop().RunUntilIdle();
    nested_initial_size = nested_root_view->GetVisibleViewportSize();
  }
  ASSERT_NE(initial_size, nested_initial_size);

  // We should see the top level widget's size in the visible_viewport_size
  // in both local roots. When a child local root is added in the parent
  // renderer, the value is propagated down through the browser to the child
  // renderer's RenderWidget.
  //
  // This property is not directly visible in the renderer, so we can only
  // check that the value is sent to the appropriate RenderWidget.
  {
    GURL cross_site_url(
        embedded_test_server()->GetURL("c.com", "/title2.html"));
    EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));

    // Wait to see the size sent to the child RenderWidget.
    while (true) {
      std::optional<blink::VisualProperties> properties =
          child_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == initial_size)
        break;
      base::RunLoop().RunUntilIdle();
    }
  }

  // Same check as above but for a nested WebContents.
  {
    GURL cross_site_url(
        embedded_test_server()->GetURL("c.com", "/title2.html"));
    EXPECT_TRUE(
        NavigateToURLFromRenderer(nested_root->child_at(0), cross_site_url));

    // Wait to see the size sent to the child RenderWidget.
    while (true) {
      std::optional<blink::VisualProperties> properties =
          nested_child_rwh->LastComputedVisualProperties();
      if (properties &&
          properties->visible_viewport_size == nested_initial_size)
        break;
      base::RunLoop().RunUntilIdle();
    }
  }

// This part of the test does not work well on Android, for a few reasons:
// 1. RenderWidgetHostViewAndroid can not be resized, the Java objects need to
// be resized somehow through ui::ViewAndroid.
// 2. AutoResize on Android does not size to the min/max bounds specified, it
// ends up ignoring them and sizing to the screen (I think).
// Luckily this test is verifying interactions and behaviour of
// RenderWidgetHostImpl - RenderWidget - `blink::RemoteFrame` -
// CrossProcessFrameConnector, and this isn't Android-specific code.
// For iOS, RenderWidgetHostViewIOS can't be resized, either.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

  // Resize the top level widget to cause its |visible_viewport_size| to be
  // changed. The change should propagate down to the child RenderWidget.
  {
    const gfx::Size resize_to(initial_size.width() - 10,
                              initial_size.height() - 10);

    root_view->SetSize(resize_to);

    // Wait to see both RenderWidgets receive the message.
    while (true) {
      std::optional<blink::VisualProperties> properties =
          root_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }
    while (true) {
      std::optional<blink::VisualProperties> properties =
          child_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }
  }

  // Same check as above but resizing the nested WebContents' main frame
  // instead.
  // Resize the top level widget to cause its |visible_viewport_size| to be
  // changed. The change should propagate down to the child RenderWidget.
  {
    const gfx::Size resize_to(nested_initial_size.width() - 10,
                              nested_initial_size.height() - 10);

    EXPECT_TRUE(
        ExecJs(root->current_frame_host(),
               JsReplace("document.getElementById('child-2').width = '$1px';"
                         "document.getElementById('child-2').height = '$2px';",
                         resize_to.width(), resize_to.height())));

    // Wait to see both RenderWidgets receive the message.
    while (true) {
      std::optional<blink::VisualProperties> properties =
          nested_root_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }
    while (true) {
      std::optional<blink::VisualProperties> properties =
          nested_child_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }
  }

  // Informs the top-level frame it can auto-resize. It will shrink down to its
  // minimum window size based on the empty content we've loaded, which is
  // 100x100.
  //
  // When the renderer resizes, thanks to our AutoResizeWebContentsDelegate
  // the top level widget will resize. That size will be reflected in the
  // visible_viewport_size which is sent back through the top level RenderWidget
  // to propagte down to child local roots.
  //
  // This property is not directly visible in the renderer, so we can only
  // check that the value is sent to both RenderWidgets.
  {
    const gfx::Size auto_resize_to(105, 100);

    // Replace the WebContentsDelegate so that we can use the auto-resize
    // changes to adjust the size of the top widget.
    WebContentsDelegate* old_delegate = shell()->web_contents()->GetDelegate();
    AutoResizeWebContentsDelegate resize_delegate;
    shell()->web_contents()->SetDelegate(&resize_delegate);

    root_view->EnableAutoResize(auto_resize_to, auto_resize_to);

    // Wait for the renderer side to resize itself and the RenderWidget
    // waterfall to pass the new |visible_viewport_size| down.
    while (true) {
      std::optional<blink::VisualProperties> properties =
          root_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == auto_resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }
    while (true) {
      std::optional<blink::VisualProperties> properties =
          child_rwh->LastComputedVisualProperties();
      if (properties && properties->visible_viewport_size == auto_resize_to)
        break;
      base::RunLoop().RunUntilIdle();
    }

    shell()->web_contents()->SetDelegate(old_delegate);
  }

  // TODO(danakj): We'd like to run the same check as above but tell the main
  // frame of the nested WebContents that it can auto-resize. However this seems
  // to get through to the main frame's RenderWidget and propagate correctly but
  // no size change occurs in the renderer.
#endif
}

// Validate that OOPIFs receive presentation feedbacks.
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest,
                       PresentationFeedback) {
  base::HistogramTester histogram_tester;
  GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
                            ->GetPrimaryFrameTree()
                            .root();
  // Load cross-site page into iframe.
  GURL cross_site_url(
      embedded_test_server()->GetURL("foo.com", "/title2.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));

  const auto trigger_subframe_tab_switch = [&root]() -> bool {
    auto* child_rwh_impl =
        root->child_at(0)->current_frame_host()->GetRenderWidgetHost();
    // Hide the frame and make it visible again, to force it to record the
    // tab-switch time, which is generated from presentation-feedback.
    child_rwh_impl->WasHidden();
    child_rwh_impl->WasShown(
        blink::mojom::RecordContentToVisibleTimeRequest::New(
            base::TimeTicks::Now(),
            /*destination_is_loaded=*/true,
            /*show_reason_tab_switching=*/true,
            /*show_reason_bfcache_restore=*/false,
            /*show_reason_unfold=*/false));
    // Force the child to submit a new frame.
    return ExecJs(root->child_at(0)->current_frame_host(),
                  "document.write('Force a new frame.');");
  };
  ASSERT_TRUE(trigger_subframe_tab_switch());

  // Ensure the loop starts in the right state.
  ASSERT_TRUE(
      histogram_tester.GetAllSamples("Browser.Tabs.TotalSwitchDuration3")
          .empty());

  // Once the tab switch completes the PresentationFeedback should cause a
  // single TotalSwitchDuration3 histogram to be logged.
  bool got_incomplete_tab_switch = false;
  const base::TimeTicks start_time = base::TimeTicks::Now();
  while (histogram_tester.GetAllSamples("Browser.Tabs.TotalSwitchDuration3")
             .empty()) {
    ASSERT_LT(base::TimeTicks::Now() - start_time,
              TestTimeouts::action_timeout())
        << "Timed out waiting for Browser.Tabs.TotalSwitchDuration3.";
    FetchHistogramsFromChildProcesses();
    GiveItSomeTime();

    // Work around a race condition while loading the cross-site iframe.
    //
    // The NavigateToURLFromRenderer call above replaces the
    // LocalFrame/LocalFrameView in renderer process A with a
    // RemoteFrame/RemoteFrameView proxy for the frame which is now hosted in
    // renderer process B. During initialization the RemoteFrameView sends a
    // series of VisibilityChanged messages to the browser process, which cause
    // CrossProcessFrameConnector to call WasHidden and then WasShown on
    // `child_rwh_impl`. Depending on the timing these might arrive during the
    // NavigateToURLFromRenderer call or the ExecJS call above, both of which
    // pump the message loop. If CrossProcessFrameConnector calls WasHidden
    // after the WasShown call above, it will cancel the simulated tab switch.
    // This causes ContentToVisibleTimeReporter to log
    // TotalIncompleteSwitchDuration3, which is not based on
    // PresentationFeedback, instead of TotalSwitchDuration3. See
    // crbug.com/1288560 for more details.
    //
    // The race condition can only cause a single incomplete tab switch, so
    // only check for this once. If the second simulated tab switch is also
    // cancelled something else is wrong, so the loop will time out and fail
    // the test.
    //
    // TODO(crbug.com/40817022): Remove this once the race condition is
    // fixed.
    if (!got_incomplete_tab_switch &&
        !histogram_tester
             .GetAllSamples("Browser.Tabs.TotalIncompleteSwitchDuration3")
             .empty()) {
      LOG(ERROR) << "Incomplete tab switch - try again.";
      got_incomplete_tab_switch = true;
      ASSERT_TRUE(trigger_subframe_tab_switch());
    }
  }
}

// Auto-resize is only implemented for Ash and GuestViews. So we need to inject
// an implementation that actually resizes the top level widget.
class DisplayModeControllingWebContentsDelegate : public WebContentsDelegate {
 public:
  blink::mojom::DisplayMode GetDisplayMode(
      const WebContents* web_contents) override {
    return mode_;
  }

  void set_display_mode(blink::mojom::DisplayMode mode) { mode_ = mode; }

 private:
  // The is the default value throughout the browser and renderer.
  blink::mojom::DisplayMode mode_ = blink::mojom::DisplayMode::kBrowser;
};

// TODO(crbug.com/40121997): Unlike most VisualProperties, the DisplayMode does
// not propagate down the tree of RenderWidgets, but is sent independently to
// each RenderWidget.
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest,
                       VisualPropertiesPropagation_DisplayMode) {
  GURL main_url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b(a))"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents());

  DisplayModeControllingWebContentsDelegate display_mode_delegate;
  shell()->web_contents()->SetDelegate(&display_mode_delegate);

  // Main frame.
  FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
  RenderWidgetHostImpl* root_widget =
      root->current_frame_host()->GetRenderWidgetHost();
  // Out-of-process frame.
  FrameTreeNode* oopchild = root->child_at(0);
  RenderWidgetHostImpl* oopchild_widget =
      oopchild->current_frame_host()->GetRenderWidgetHost();
  // In-process frame.
  FrameTreeNode* ipchild = oopchild->child_at(0);
  RenderWidgetHostImpl* ipchild_widget =
      ipchild->current_frame_host()->GetRenderWidgetHost();
  EXPECT_NE(root_widget, ipchild_widget);

  // Check all frames for the initial value.
  EXPECT_EQ(
      true,
      EvalJs(root, "window.matchMedia('(display-mode: browser)').matches"));
  EXPECT_EQ(
      true,
      EvalJs(oopchild, "window.matchMedia('(display-mode: browser)').matches"));
  EXPECT_EQ(
      true,
      EvalJs(ipchild, "window.matchMedia('(display-mode: browser)').matches"));

  // The display mode changes.
  display_mode_delegate.set_display_mode(
      blink::mojom::DisplayMode::kStandalone);
  // Each RenderWidgetHost would need to hear about that by having
  // SynchronizeVisualProperties() called. It's not clear what triggers that but
  // the place that changes the DisplayMode would be responsible.
  //
  // We ignore the pending ack to ensure this IPC is sent immediately.
  EXPECT_TRUE(root_widget->SynchronizeVisualPropertiesIgnoringPendingAck());
  EXPECT_TRUE(oopchild_widget->SynchronizeVisualPropertiesIgnoringPendingAck());
  EXPECT_TRUE(ipchild_widget->SynchronizeVisualPropertiesIgnoringPendingAck());

  // Check all frames for the changed value.
  EXPECT_EQ(
      true,
      EvalJsAfterLifecycleUpdate(
          root, "", "window.matchMedia('(display-mode: standalone)').matches"));
  EXPECT_EQ(true,
            EvalJsAfterLifecycleUpdate(
                oopchild, "",
                "window.matchMedia('(display-mode: standalone)').matches"));
  EXPECT_EQ(true,
            EvalJsAfterLifecycleUpdate(
                ipchild, "",
                "window.matchMedia('(display-mode: standalone)').matches"));

  // Navigate a frame to b.com, which we already have a process for.
  GURL same_site_url(embedded_test_server()->GetURL("b.com", "/title2.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), same_site_url));

  // The navigated frame sees the correct (non-default) value.
  EXPECT_EQ(true,
            EvalJs(root->child_at(0),
                   "window.matchMedia('(display-mode: standalone)').matches"));

  // Navigate the frame to c.com, which we don't have a process for.
  GURL cross_site_url(embedded_test_server()->GetURL("c.com", "/title2.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));

  // The navigated frame sees the correct (non-default) value.
  EXPECT_EQ(true,
            EvalJs(root->child_at(0),
                   "window.matchMedia('(display-mode: standalone)').matches"));
}

// Validate that the root widget's viewport segments are correctly propagated
// via the SynchronizeVisualProperties cascade.
// Flaky on Mac, Linux and Android (http://crbug/1089994).
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
    BUILDFLAG(IS_ANDROID)
#define MAYBE_VisualPropertiesPropagation_RootViewportSegments \
  DISABLED_VisualPropertiesPropagation_RootViewportSegments
#else
#define MAYBE_VisualPropertiesPropagation_RootViewportSegments \
  VisualPropertiesPropagation_RootViewportSegments
#endif
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest,
                       MAYBE_VisualPropertiesPropagation_RootViewportSegments) {
  GURL main_url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b(c),a)"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents());

  // Main frame/root view.
  FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
  RenderWidgetHostImpl* root_widget =
      root->current_frame_host()->GetRenderWidgetHost();
  RenderWidgetHostViewBase* root_view = root_widget->GetView();
  // Out-of-process child frame.
  FrameTreeNode* oopchild = root->child_at(0);
  // Out-of-process descendant frame (child of the first oop-iframe).
  FrameTreeNode* oopdescendant = oopchild->child_at(0);

  const gfx::Size root_view_size = root_view->GetVisibleViewportSize();
  const int kDisplayFeatureLength = 10;
  DisplayFeature emulated_display_feature{
      DisplayFeature::Orientation::kVertical,
      /* offset */ root_view_size.width() / 2 - kDisplayFeatureLength / 2,
      /* mask_length */ kDisplayFeatureLength};
  std::vector<gfx::Rect> expected_segments;
  expected_segments.emplace_back(0, 0, emulated_display_feature.offset,
                                 root_view_size.height());
  const int second_segment_offset =
      emulated_display_feature.offset + emulated_display_feature.mask_length;
  expected_segments.emplace_back(second_segment_offset, 0,
                                 root_view_size.width() - second_segment_offset,
                                 root_view_size.height());

  std::optional<blink::VisualProperties> properties =
      oopchild->current_frame_host()
          ->GetRenderWidgetHost()
          ->LastComputedVisualProperties();
  EXPECT_TRUE(properties);
  EXPECT_TRUE(properties->local_surface_id);
  viz::LocalSurfaceId oopchild_initial_lsid =
      properties->local_surface_id.value();

  properties = oopdescendant->current_frame_host()
                   ->GetRenderWidgetHost()
                   ->LastComputedVisualProperties();
  EXPECT_TRUE(properties);
  EXPECT_TRUE(properties->local_surface_id);
  viz::LocalSurfaceId oopdescendant_initial_lsid =
      properties->local_surface_id.value();

  {
    // Watch for visual properties changes, first to the child oop-iframe, then
    // to the descendant (at which point we're done and can validate the
    // values).
    root_view->SetDisplayFeatureForTesting(&emulated_display_feature);
    root_widget->SynchronizeVisualProperties();

    while (true) {
      properties = oopchild->current_frame_host()
                       ->GetRenderWidgetHost()
                       ->LastComputedVisualProperties();
      if (properties && properties->local_surface_id &&
          oopchild_initial_lsid < properties->local_surface_id) {
        EXPECT_EQ(properties->root_widget_viewport_segments, expected_segments);
        break;
      }
      base::RunLoop().RunUntilIdle();
    }
    while (true) {
      properties = oopdescendant->current_frame_host()
                       ->GetRenderWidgetHost()
                       ->LastComputedVisualProperties();
      if (properties && properties->local_surface_id &&
          oopdescendant_initial_lsid < properties->local_surface_id) {
        EXPECT_EQ(properties->root_widget_viewport_segments, expected_segments);
        break;
      }
      base::RunLoop().RunUntilIdle();
    }
  }

  {
    // Creating a new local frame root (navigating a.com to c.com) should also
    // propagate the property to the new local frame root. Note that we're
    // re-using the existing RenderProcessHost from c.com (aka
    // |oopdescendant_rph|).
    GURL new_frame_url(embedded_test_server()->GetURL("c.com", "/title2.html"));
    EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(1), new_frame_url));

    while (true) {
      properties = oopdescendant->current_frame_host()
                       ->GetRenderWidgetHost()
                       ->LastComputedVisualProperties();
      // This check is needed, since we'll get an IPC originating from
      // RenderWidgetHostImpl immediately after the frame is added with the
      // incorrect value (the segments are cascaded from the parent renderer
      // when the frame is added in that process). So we need to wait for
      // the outgoing VisualProperties triggered from the parent renderer
      // and comes in via the CrossProcessFrameConnector, which can happen
      // after NavigateToURLFromRenderer completes.
      if (properties &&
          properties->root_widget_viewport_segments == expected_segments) {
        break;
      }
      base::RunLoop().RunUntilIdle();
    }
  }
}

IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewChildFrameBrowserTest,
                       SetTextDirection) {
  GURL main_url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(a,b)"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents());

  // Main frame.
  FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
  RenderWidgetHostImpl* root_widget =
      root->current_frame_host()->GetRenderWidgetHost();
  ASSERT_TRUE(ExecJs(root->current_frame_host(),
                     "var elem = document.createElement('input'); "
                     "elem.id = 'mainframe_input_id';"
                     "document.body.appendChild(elem);"
                     "document.getElementById('mainframe_input_id').focus();"));
  root_widget->UpdateTextDirection(base::i18n::RIGHT_TO_LEFT);
  root_widget->NotifyTextDirection();
  EXPECT_EQ("rtl", EvalJs(root->current_frame_host(),
                          "document.getElementById('mainframe_input_id').dir"));

  // In-process frame.
  FrameTreeNode* ipchild = root->child_at(0);
  RenderWidgetHostImpl* ipchild_widget =
      ipchild->current_frame_host()->GetRenderWidgetHost();
  ASSERT_TRUE(ExecJs(ipchild->current_frame_host(),
                     "var elem = document.createElement('input'); "
                     "elem.id = 'ipchild_input_id';"
                     "document.body.appendChild(elem);"
                     "document.getElementById('ipchild_input_id').focus();"));
  ipchild_widget->UpdateTextDirection(base::i18n::LEFT_TO_RIGHT);
  ipchild_widget->NotifyTextDirection();
  EXPECT_EQ("ltr", EvalJs(ipchild->current_frame_host(),
                          "document.getElementById('ipchild_input_id').dir"));

  // Out-of-process frame.
  FrameTreeNode* oopchild = root->child_at(1);
  RenderWidgetHostImpl* oopchild_widget =
      oopchild->current_frame_host()->GetRenderWidgetHost();
  ASSERT_TRUE(ExecJs(oopchild->current_frame_host(),
                     "var elem = document.createElement('input'); "
                     "elem.id = 'oop_input_id';"
                     "document.body.appendChild(elem);"
                     "document.getElementById('oop_input_id').focus();"));
  oopchild_widget->UpdateTextDirection(base::i18n::RIGHT_TO_LEFT);
  oopchild_widget->NotifyTextDirection();
  EXPECT_EQ("rtl", EvalJs(oopchild->current_frame_host(),
                          "document.getElementById('oop_input_id').dir"));

  // In case of UNKNOWN_DIRECTION, old value of direction is maintained.
  oopchild_widget->UpdateTextDirection(base::i18n::UNKNOWN_DIRECTION);
  oopchild_widget->NotifyTextDirection();
  EXPECT_EQ("rtl", EvalJs(oopchild->current_frame_host(),
                          "document.getElementById('oop_input_id').dir"));
}

}  // namespace

}  // namespace content