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

content / test / test_web_contents.cc [blame]

// Copyright 2012 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/test/test_web_contents.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/no_destructor.h"
#include "content/browser/browser_url_handler_impl.h"
#include "content/browser/display_cutout/display_cutout_host_impl.h"
#include "content/browser/preloading/preload_pipeline_info.h"
#include "content/browser/preloading/prerender/prerender_host.h"
#include "content/browser/preloading/prerender/prerender_host_registry.h"
#include "content/browser/renderer_host/cross_process_frame_connector.h"
#include "content/browser/renderer_host/debug_urls.h"
#include "content/browser/renderer_host/navigation_entry_impl.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/renderer_host/navigator.h"
#include "content/browser/renderer_host/render_frame_proxy_host.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/common/render_message_filter.mojom.h"
#include "content/public/common/referrer_type_converters.h"
#include "content/public/common/url_utils.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/prerender_test_util.h"
#include "content/test/navigation_simulator_impl.h"
#include "content/test/test_render_view_host.h"
#include "mojo/public/cpp/bindings/clone_traits.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/page_state/page_state.h"
#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
#include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom-shared.h"
#include "ui/base/page_transition_types.h"

namespace content {

namespace {

RenderProcessHostFactory* GetMockProcessFactory() {
  static base::NoDestructor<MockRenderProcessHostFactory> factory;
  return factory.get();
}

}  // namespace

TestWebContents::TestWebContents(BrowserContext* browser_context)
    : WebContentsImpl(browser_context),
      delegate_view_override_(nullptr),
      web_preferences_changed_counter_(nullptr),
      pause_subresource_loading_called_(false),
      audio_group_id_(base::UnguessableToken::Create()),
      is_page_frozen_(false) {
  if (!RenderProcessHostImpl::get_render_process_host_factory_for_testing()) {
    // Most unit tests should prefer to create a generic MockRenderProcessHost
    // (instead of a real RenderProcessHostImpl).  Tests that need to use a
    // specific, custom RenderProcessHostFactory should set it before creating
    // the first TestWebContents.
    RenderProcessHostImpl::set_render_process_host_factory_for_testing(
        GetMockProcessFactory());
  }
}

std::unique_ptr<TestWebContents> TestWebContents::Create(
    BrowserContext* browser_context,
    scoped_refptr<SiteInstance> instance) {
  std::unique_ptr<TestWebContents> test_web_contents(
      new TestWebContents(browser_context));
  test_web_contents->Init(CreateParams(browser_context, std::move(instance)),
                          blink::FramePolicy());
  return test_web_contents;
}

TestWebContents* TestWebContents::Create(const CreateParams& params) {
  TestWebContents* test_web_contents =
      new TestWebContents(params.browser_context);
  test_web_contents->Init(params, blink::FramePolicy());
  return test_web_contents;
}

TestWebContents::~TestWebContents() = default;

const TestRenderFrameHost* TestWebContents::GetPrimaryMainFrame() const {
  const auto* const instance = WebContentsImpl::GetPrimaryMainFrame();
  DCHECK(instance->IsTestRenderFrameHost())
      << "You may want to instantiate RenderViewHostTestEnabler.";
  return static_cast<const TestRenderFrameHost*>(instance);
}

TestRenderFrameHost* TestWebContents::GetPrimaryMainFrame() {
  return const_cast<TestRenderFrameHost*>(
      std::as_const(*this).GetPrimaryMainFrame());
}

TestRenderViewHost* TestWebContents::GetRenderViewHost() {
  auto* instance = WebContentsImpl::GetRenderViewHost();
  DCHECK(instance->IsTestRenderViewHost())
      << "You may want to instantiate RenderViewHostTestEnabler.";
  return static_cast<TestRenderViewHost*>(instance);
}

TestRenderFrameHost* TestWebContents::GetSpeculativePrimaryMainFrame() {
  return static_cast<TestRenderFrameHost*>(
      GetPrimaryFrameTree().root()->render_manager()->speculative_frame_host());
}

int TestWebContents::DownloadImage(const GURL& url,
                                   bool is_favicon,
                                   const gfx::Size& preferred_size,
                                   uint32_t max_bitmap_size,
                                   bool bypass_cache,
                                   ImageDownloadCallback callback) {
  static int g_next_image_download_id = 0;
  ++g_next_image_download_id;
  pending_image_downloads_[url].emplace_back(g_next_image_download_id,
                                             std::move(callback));
  return g_next_image_download_id;
}

const GURL& TestWebContents::GetLastCommittedURL() {
  if (last_committed_url_.is_valid()) {
    return last_committed_url_;
  }
  return WebContentsImpl::GetLastCommittedURL();
}

const std::u16string& TestWebContents::GetTitle() {
  if (title_)
    return title_.value();

  return WebContentsImpl::GetTitle();
}

void TestWebContents::SetTabSwitchStartTime(base::TimeTicks start_time,
                                            bool destination_is_loaded) {
  tab_switch_start_time_ = start_time;
  WebContentsImpl::SetTabSwitchStartTime(start_time, destination_is_loaded);
}

const std::string& TestWebContents::GetSaveFrameHeaders() {
  return save_frame_headers_;
}

const std::u16string& TestWebContents::GetSuggestedFileName() {
  return suggested_filename_;
}

bool TestWebContents::HasPendingDownloadImage(const GURL& url) {
  return !pending_image_downloads_[url].empty();
}

void TestWebContents::OnWebPreferencesChanged() {
  WebContentsImpl::OnWebPreferencesChanged();
  if (web_preferences_changed_counter_)
    ++*web_preferences_changed_counter_;
}

void TestWebContents::SetBackForwardCacheSupported(bool supported) {
  back_forward_cache_supported_ = supported;
}

bool TestWebContents::IsPageFrozen() {
  return is_page_frozen_;
}

bool TestWebContents::TestDidDownloadImage(
    const GURL& url,
    int http_status_code,
    const std::vector<SkBitmap>& bitmaps,
    const std::vector<gfx::Size>& original_bitmap_sizes) {
  if (!HasPendingDownloadImage(url))
    return false;
  int id = pending_image_downloads_[url].front().first;
  ImageDownloadCallback callback =
      std::move(pending_image_downloads_[url].front().second);
  pending_image_downloads_[url].pop_front();
  WebContentsImpl::OnDidDownloadImage(/*rfh=*/nullptr, std::move(callback), id,
                                      url, http_status_code, bitmaps,
                                      original_bitmap_sizes);
  return true;
}

void TestWebContents::TestSetFaviconURL(
    const std::vector<blink::mojom::FaviconURLPtr>& favicon_urls) {
  GetPrimaryPage().set_favicon_urls(mojo::Clone(favicon_urls));
}

void TestWebContents::TestUpdateFaviconURL(
    const std::vector<blink::mojom::FaviconURLPtr>& favicon_urls) {
  GetPrimaryMainFrame()->UpdateFaviconURL(mojo::Clone(favicon_urls));
}

void TestWebContents::SetLastCommittedURL(const GURL& url) {
  last_committed_url_ = url;
}

void TestWebContents::SetTitle(const std::u16string& title) {
  title_ = title;
}

void TestWebContents::SetMainFrameMimeType(const std::string& mime_type) {
  GetPrimaryPage().SetContentsMimeType(mime_type);
}

void TestWebContents::SetMainFrameSize(const gfx::Size& frame_size) {
  GetPrimaryMainFrame()->FrameSizeChanged(frame_size);
}

const std::string& TestWebContents::GetContentsMimeType() {
  return GetPrimaryPage().GetContentsMimeType();
}

void TestWebContents::SetIsCurrentlyAudible(bool audible) {
  audio_stream_monitor()->set_is_currently_audible_for_testing(audible);
  OnAudioStateChanged();
}

void TestWebContents::TestDidReceiveMouseDownEvent() {
  blink::WebMouseEvent event;
  event.SetType(blink::WebInputEvent::Type::kMouseDown);
  // Use the first RenderWidgetHost from the frame tree to make sure that the
  // interaction doesn't get ignored.
  DCHECK(primary_frame_tree_.Nodes().begin() !=
         primary_frame_tree_.Nodes().end());
  RenderWidgetHostImpl* render_widget_host =
      (*primary_frame_tree_.Nodes().begin())
          ->current_frame_host()
          ->GetRenderWidgetHost();
  DidReceiveInputEvent(render_widget_host, event);
}

void TestWebContents::TestDidFinishLoad(const GURL& url) {
  OnDidFinishLoad(primary_frame_tree_.root()->current_frame_host(), url);
}

void TestWebContents::TestDidFailLoadWithError(const GURL& url,
                                               int error_code) {
  GetPrimaryMainFrame()->DidFailLoadWithError(url, error_code);
}

void TestWebContents::TestDidFirstVisuallyNonEmptyPaint() {
  OnFirstVisuallyNonEmptyPaint(GetPrimaryPage());
}

bool TestWebContents::CrossProcessNavigationPending() {
  // If we don't have a speculative RenderFrameHost then it means we did not
  // change SiteInstances so we must be in the same process.
  if (GetRenderManager()->speculative_render_frame_host_ == nullptr)
    return false;

  auto* current_instance =
      GetRenderManager()->current_frame_host()->GetSiteInstance();
  auto* speculative_instance =
      GetRenderManager()->speculative_frame_host()->GetSiteInstance();
  if (current_instance == speculative_instance)
    return false;
  return current_instance->GetProcess() != speculative_instance->GetProcess();
}

bool TestWebContents::CreateRenderViewForRenderManager(
    RenderViewHost* render_view_host,
    const std::optional<blink::FrameToken>& opener_frame_token,
    RenderFrameProxyHost* proxy_host) {
  const auto proxy_routing_id =
      proxy_host ? proxy_host->GetRoutingID() : MSG_ROUTING_NONE;
  // This will go to a TestRenderViewHost.
  static_cast<RenderViewHostImpl*>(render_view_host)
      ->CreateRenderView(opener_frame_token, proxy_routing_id, false);
  return true;
}

std::unique_ptr<WebContents> TestWebContents::Clone() {
  std::unique_ptr<WebContentsImpl> contents =
      Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
  contents->GetController().CopyStateFrom(&GetController(), true);
  return contents;
}

void TestWebContents::NavigateAndCommit(const GURL& url,
                                        ui::PageTransition transition) {
  std::unique_ptr<NavigationSimulator> navigation =
      NavigationSimulator::CreateBrowserInitiated(url, this);
  // TODO(clamy): Browser-initiated navigations should not have a transition of
  // type ui::PAGE_TRANSITION_LINK however several tests expect this. They
  // should be rewritten to simulate renderer-initiated navigations in these
  // cases. Once that's done, the transtion can be set to
  // ui::PAGE_TRANSITION_TYPED which makes more sense in this context.
  // ui::PAGE_TRANSITION_TYPED is the default value for transition
  navigation->SetTransition(transition);
  navigation->Commit();
}

void TestWebContents::NavigateAndFail(const GURL& url, int error_code) {
  std::unique_ptr<NavigationSimulator> navigation =
      NavigationSimulator::CreateBrowserInitiated(url, this);
  navigation->Fail(error_code);
}

void TestWebContents::TestSetIsLoading(bool value) {
  if (value) {
    DidStartLoading(GetPrimaryMainFrame()->frame_tree_node());
    LoadingStateChanged(LoadingState::LOADING_UI_REQUESTED);
  } else {
    for (FrameTreeNode* node : primary_frame_tree_.Nodes()) {
      RenderFrameHostImpl* current_frame_host =
          node->render_manager()->current_frame_host();
      DCHECK(current_frame_host);
      current_frame_host->ResetLoadingState();

      RenderFrameHostImpl* speculative_frame_host =
          node->render_manager()->speculative_frame_host();
      if (speculative_frame_host)
        speculative_frame_host->ResetLoadingState();
      node->ResetNavigationRequest(
          NavigationDiscardReason::kExplicitCancellation);
    }
  }
}

void TestWebContents::CommitPendingNavigation() {
  NavigationEntry* entry = GetController().GetPendingEntry();
  DCHECK(entry);

  auto navigation = NavigationSimulator::CreateFromPending(GetController());
  navigation->Commit();
}

RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
  if (delegate_view_override_)
    return delegate_view_override_;
  return WebContentsImpl::GetDelegateView();
}

void TestWebContents::SetOpener(WebContents* opener) {
  primary_frame_tree_.root()->SetOpener(
      static_cast<WebContentsImpl*>(opener)->GetPrimaryFrameTree().root());
}

void TestWebContents::SetIsCrashed(base::TerminationStatus status,
                                   int error_code) {
  SetPrimaryMainFrameProcessStatus(status, error_code);
}

void TestWebContents::AddPendingContents(
    std::unique_ptr<WebContentsImpl> contents,
    const GURL& target_url) {
  // This is normally only done in WebContentsImpl::CreateNewWindow.
  GlobalRoutingID key(
      contents->GetRenderViewHost()->GetProcess()->GetID(),
      contents->GetRenderViewHost()->GetWidget()->GetRoutingID());
  AddWebContentsDestructionObserver(contents.get());
  pending_contents_[key] = CreatedWindow(std::move(contents), target_url);
}

FrameTree* TestWebContents::CreateNewWindow(
    RenderFrameHostImpl* opener,
    const mojom::CreateNewWindowParams& params,
    bool is_new_browsing_instance,
    bool has_user_gesture,
    SessionStorageNamespace* session_storage_namespace) {
  return nullptr;
}

RenderWidgetHostImpl* TestWebContents::CreateNewPopupWidget(
    base::SafeRef<SiteInstanceGroup> site_instance_group,
    int32_t route_id,
    mojo::PendingAssociatedReceiver<blink::mojom::PopupWidgetHost>
        blink_popup_widget_host,
    mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
    mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
  return nullptr;
}

void TestWebContents::ShowCreatedWindow(
    RenderFrameHostImpl* opener,
    int route_id,
    WindowOpenDisposition disposition,
    const blink::mojom::WindowFeatures& window_features,
    bool user_gesture) {}

void TestWebContents::ShowCreatedWidget(int process_id,
                                        int route_id,
                                        const gfx::Rect& initial_rect,
                                        const gfx::Rect& initial_anchor_rect) {}

void TestWebContents::SaveFrameWithHeaders(
    const GURL& url,
    const Referrer& referrer,
    const std::string& headers,
    const std::u16string& suggested_filename,
    RenderFrameHost* rfh,
    bool is_subresource) {
  save_frame_headers_ = headers;
  suggested_filename_ = suggested_filename;
}

bool TestWebContents::GetPauseSubresourceLoadingCalled() {
  return pause_subresource_loading_called_;
}

void TestWebContents::ResetPauseSubresourceLoadingCalled() {
  pause_subresource_loading_called_ = false;
}

void TestWebContents::SetLastActiveTimeTicks(
    base::TimeTicks last_active_time_ticks) {
  last_active_time_ticks_ = last_active_time_ticks;
}

void TestWebContents::SetLastActiveTime(base::Time last_active_time) {
  last_active_time_ = last_active_time;
}

void TestWebContents::TestIncrementUsbActiveFrameCount() {
  IncrementUsbActiveFrameCount();
}

void TestWebContents::TestDecrementUsbActiveFrameCount() {
  DecrementUsbActiveFrameCount();
}

void TestWebContents::TestIncrementHidActiveFrameCount() {
  IncrementHidActiveFrameCount();
}

void TestWebContents::TestDecrementHidActiveFrameCount() {
  DecrementHidActiveFrameCount();
}

void TestWebContents::TestIncrementSerialActiveFrameCount() {
  IncrementSerialActiveFrameCount();
}

void TestWebContents::TestDecrementSerialActiveFrameCount() {
  DecrementSerialActiveFrameCount();
}

void TestWebContents::TestIncrementBluetoothConnectedDeviceCount() {
  IncrementBluetoothConnectedDeviceCount();
}

void TestWebContents::TestDecrementBluetoothConnectedDeviceCount() {
  DecrementBluetoothConnectedDeviceCount();
}

base::UnguessableToken TestWebContents::GetAudioGroupId() {
  return audio_group_id_;
}

void TestWebContents::SetPageFrozen(bool frozen) {
  is_page_frozen_ = frozen;
}

bool TestWebContents::IsBackForwardCacheSupported() {
  return back_forward_cache_supported_;
}

const std::optional<blink::mojom::PictureInPictureWindowOptions>&
TestWebContents::GetPictureInPictureOptions() const {
  if (picture_in_picture_options_.has_value()) {
    return picture_in_picture_options_;
  }
  return WebContentsImpl::GetPictureInPictureOptions();
}

FrameTreeNodeId TestWebContents::AddPrerender(const GURL& url) {
  DCHECK(!base::FeatureList::IsEnabled(
      blink::features::kPrerender2MemoryControls));

  TestRenderFrameHost* rfhi = GetPrimaryMainFrame();
  return GetPrerenderHostRegistry()->CreateAndStartHost(PrerenderAttributes(
      url, PreloadingTriggerType::kSpeculationRule,
      /*embedder_histogram_suffix=*/"",
      blink::mojom::SpeculationTargetHint::kNoHint, Referrer(),
      blink::mojom::SpeculationEagerness::kEager,
      /*no_vary_search_expected=*/std::nullopt, rfhi->GetLastCommittedOrigin(),
      rfhi->GetProcess()->GetID(), GetWeakPtr(), rfhi->GetFrameToken(),
      rfhi->GetFrameTreeNodeId(), rfhi->GetPageUkmSourceId(),
      ui::PAGE_TRANSITION_LINK,
      /*should_warm_up_compositor=*/false,
      /*url_match_predicate=*/{},
      /*prerender_navigation_handle_callback=*/{},
      base::MakeRefCounted<PreloadPipelineInfo>()));
}

TestRenderFrameHost* TestWebContents::AddPrerenderAndCommitNavigation(
    const GURL& url) {
  FrameTreeNodeId host_id = AddPrerender(url);
  DCHECK(host_id);

  PrerenderHost* host =
      GetPrerenderHostRegistry()->FindNonReservedHostById(host_id);
  DCHECK(host);
  {
    std::unique_ptr<NavigationSimulatorImpl> navigation =
        NavigationSimulatorImpl::CreateFromPendingInFrame(
            FrameTreeNode::GloballyFindByID(host->frame_tree_node_id()));
    navigation->Commit();
  }
  return static_cast<TestRenderFrameHost*>(host->GetPrerenderedMainFrameHost());
}

std::unique_ptr<NavigationSimulator>
TestWebContents::AddPrerenderAndStartNavigation(const GURL& url) {
  FrameTreeNodeId host_id = AddPrerender(url);
  DCHECK(host_id);

  PrerenderHost* host =
      GetPrerenderHostRegistry()->FindNonReservedHostById(host_id);
  DCHECK(host);

  return NavigationSimulatorImpl::CreateFromPendingInFrame(
      FrameTreeNode::GloballyFindByID(host->frame_tree_node_id()));
}

void TestWebContents::ActivatePrerenderedPage(const GURL& url) {
  // Make sure the page for `url` has been prerendered.
  PrerenderHostRegistry* registry = GetPrerenderHostRegistry();
  PrerenderHost* prerender_host = registry->FindHostByUrlForTesting(url);
  DCHECK(prerender_host);
  FrameTreeNodeId prerender_host_id = prerender_host->frame_tree_node_id();

  // Activate the prerendered page.
  test::PrerenderHostObserver prerender_host_observer(*this, prerender_host_id);
  std::unique_ptr<NavigationSimulatorImpl> navigation =
      NavigationSimulatorImpl::CreateRendererInitiated(url,
                                                       GetPrimaryMainFrame());
  navigation->SetReferrer(blink::mojom::Referrer::New(
      GetPrimaryMainFrame()->GetLastCommittedURL(),
      network::mojom::ReferrerPolicy::kStrictOriginWhenCrossOrigin));
  navigation->Commit();
  prerender_host_observer.WaitForDestroyed();

  DCHECK_EQ(GetPrimaryMainFrame()->GetLastCommittedURL(), url);

  DCHECK(prerender_host_observer.was_activated());
  DCHECK(!registry->HasReservedHost());
}

void TestWebContents::ActivatePrerenderedPageFromAddressBar(const GURL& url) {
  // Make sure the page for `url` has been prerendered.
  PrerenderHostRegistry* registry = GetPrerenderHostRegistry();
  PrerenderHost* prerender_host = registry->FindHostByUrlForTesting(url);
  DCHECK(prerender_host);
  FrameTreeNodeId prerender_host_id = prerender_host->frame_tree_node_id();

  // Activate the prerendered page by navigation initiated by the address bar.
  test::PrerenderHostObserver prerender_host_observer(*this, prerender_host_id);
  std::unique_ptr<NavigationSimulatorImpl> navigation =
      NavigationSimulatorImpl::CreateBrowserInitiated(url, this);
  navigation->SetTransition(ui::PageTransitionFromInt(
      ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
  navigation->Commit();
  prerender_host_observer.WaitForDestroyed();

  DCHECK_EQ(GetPrimaryMainFrame()->GetLastCommittedURL(), url);

  DCHECK(prerender_host_observer.was_activated());
  DCHECK(!registry->HasReservedHost());
}

base::TimeTicks TestWebContents::GetTabSwitchStartTime() {
  return tab_switch_start_time_;
}

void TestWebContents::SetPictureInPictureOptions(
    std::optional<blink::mojom::PictureInPictureWindowOptions> options) {
  picture_in_picture_options_ = options;
}

void TestWebContents::SetOverscrollNavigationEnabled(bool enabled) {
  overscroll_enabled_ = enabled;
}

bool TestWebContents::GetOverscrollNavigationEnabled() {
  return overscroll_enabled_;
}

void TestWebContents::SetSafeAreaInsetsHost(
    std::unique_ptr<SafeAreaInsetsHost> safe_area_insets_host) {
  safe_area_insets_host_ = std::move(safe_area_insets_host);
}

void TestWebContents::GetMediaCaptureRawDeviceIdsOpened(
    blink::mojom::MediaStreamType type,
    base::OnceCallback<void(std::vector<std::string>)> callback) {
  CHECK(media_capture_raw_device_ids_opened_.contains(type));
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback),
                                media_capture_raw_device_ids_opened_.at(type)));
}

void TestWebContents::SetMediaCaptureRawDeviceIdsOpened(
    blink::mojom::MediaStreamType type,
    std::vector<std::string> ids) {
  media_capture_raw_device_ids_opened_[type] = std::move(ids);
}

}  // namespace content