File: web_navigation_apitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (803 lines) | stat: -rw-r--r-- 30,657 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
// 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 <list>
#include <set>

#include "base/command_line.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_browser_main.h"
#include "chrome/browser/download/download_browsertest_utils.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ssl/https_upgrades_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle_registry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/no_renderer_crashes_assertion.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/switches.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/chrome_debug_urls.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/common/switches.h"
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"

using content::WebContents;

namespace extensions {

namespace {

// Waits for a WC to be created. Once it starts loading |delay_url| (after at
// least the first navigation has committed), it delays the load, executes
// |script| in the last committed RVH and resumes the load when a URL ending in
// |until_url_suffix| commits. This class expects |script| to trigger the load
// of an URL ending in |until_url_suffix|.
class DelayLoadStartAndExecuteJavascript : public TabStripModelObserver,
                                           public content::WebContentsObserver {
 public:
  DelayLoadStartAndExecuteJavascript(Browser* browser,
                                     const GURL& delay_url,
                                     const std::string& script,
                                     const std::string& until_url_suffix)
      : content::WebContentsObserver(),
        delay_url_(delay_url),
        until_url_suffix_(until_url_suffix),
        script_(script) {
    browser->tab_strip_model()->AddObserver(this);
  }

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

  ~DelayLoadStartAndExecuteJavascript() override = default;

  // TabStripModelObserver:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override {
    if (change.type() != TabStripModelChange::kInserted) {
      return;
    }

    content::WebContentsObserver::Observe(
        change.GetInsert()->contents[0].contents);
    tab_strip_model->RemoveObserver(this);

    throttle_inserter_ =
        std::make_unique<content::TestNavigationThrottleInserter>(
            web_contents(),
            base::BindRepeating(
                &DelayLoadStartAndExecuteJavascript::InsertThrottle,
                base::Unretained(this)));
  }

  void InsertThrottle(content::NavigationThrottleRegistry& registry) {
    auto& navigation_handle = registry.GetNavigationHandle();
    if (navigation_handle.GetURL() != delay_url_ || !render_frame_host_) {
      return;
    }

    auto throttle =
        std::make_unique<WillStartRequestObserverThrottle>(registry);
    throttle_ = throttle->AsWeakPtr();
    registry.AddThrottle(std::move(throttle));

    if (has_user_gesture_) {
      render_frame_host_->ExecuteJavaScriptWithUserGestureForTests(
          base::UTF8ToUTF16(script_), base::NullCallback(),
          content::ISOLATED_WORLD_ID_GLOBAL);
    } else {
      render_frame_host_->ExecuteJavaScriptForTests(
          base::UTF8ToUTF16(script_), base::NullCallback(),
          content::ISOLATED_WORLD_ID_GLOBAL);
    }
    script_was_executed_ = true;
  }

  // WebContentsObserver:
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override {
    if (!navigation_handle->HasCommitted() ||
        navigation_handle->IsErrorPage()) {
      return;
    }

    if (script_was_executed_ &&
        base::EndsWith(navigation_handle->GetURL().spec(), until_url_suffix_,
                       base::CompareCase::SENSITIVE)) {
      content::WebContentsObserver::Observe(nullptr);
      if (throttle_) {
        throttle_->Unblock();
      }
    }

    if (navigation_handle->IsInMainFrame()) {
      render_frame_host_ = navigation_handle->GetRenderFrameHost();
    }
  }

  void set_has_user_gesture(bool has_user_gesture) {
    has_user_gesture_ = has_user_gesture;
  }

 private:
  class WillStartRequestObserverThrottle : public content::NavigationThrottle {
   public:
    explicit WillStartRequestObserverThrottle(
        content::NavigationThrottleRegistry& registry)
        : NavigationThrottle(registry) {}
    ~WillStartRequestObserverThrottle() override = default;

    const char* GetNameForLogging() override {
      return "WillStartRequestObserverThrottle";
    }

    void Unblock() {
      DCHECK(throttled_);
      Resume();
    }

    base::WeakPtr<WillStartRequestObserverThrottle> AsWeakPtr() {
      return weak_ptr_factory_.GetWeakPtr();
    }

   private:
    NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
      throttled_ = true;
      return NavigationThrottle::DEFER;
    }

    bool throttled_ = false;

    base::WeakPtrFactory<WillStartRequestObserverThrottle> weak_ptr_factory_{
        this};
  };

  base::WeakPtr<WillStartRequestObserverThrottle> throttle_;

  GURL delay_url_;
  std::string until_url_suffix_;
  std::string script_;
  bool has_user_gesture_ = false;
  bool script_was_executed_ = false;
  raw_ptr<content::RenderFrameHost, AcrossTasksDanglingUntriaged>
      render_frame_host_ = nullptr;
  std::unique_ptr<content::TestNavigationThrottleInserter> throttle_inserter_;
};

// Handles requests for URLs with paths of "/test*" sent to the test server, so
// tests request a URL that receives a non-error response.
std::unique_ptr<net::test_server::HttpResponse> HandleTestRequest(
    const net::test_server::HttpRequest& request) {
  if (!base::StartsWith(request.relative_url, "/test",
                        base::CompareCase::SENSITIVE)) {
    return nullptr;
  }
  std::unique_ptr<net::test_server::BasicHttpResponse> response(
      new net::test_server::BasicHttpResponse());
  response->set_content("This space intentionally left blank.");
  return std::move(response);
}

}  // namespace

class WebNavigationApiTest : public ExtensionApiTest {
 public:
  explicit WebNavigationApiTest(ContextType context_type = ContextType::kNone)
      : ExtensionApiTest(context_type) {
    embedded_test_server()->RegisterRequestHandler(
        base::BindRepeating(&HandleTestRequest));
  }
  ~WebNavigationApiTest() override = default;
  WebNavigationApiTest(const WebNavigationApiTest&) = delete;
  WebNavigationApiTest& operator=(const WebNavigationApiTest&) = delete;

  void SetUpInProcessBrowserTestFixture() override {
    ExtensionApiTest::SetUpInProcessBrowserTestFixture();

    FrameNavigationState::set_allow_extension_scheme(true);

    host_resolver()->AddRule("*", "127.0.0.1");
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ExtensionApiTest::SetUpCommandLine(command_line);
    // Some builders are flaky due to slower loading interacting
    // with deferred commits.
    command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
  }

  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }
};

class WebNavigationApiBackForwardCacheTest : public WebNavigationApiTest {
 public:
  WebNavigationApiBackForwardCacheTest() {
    feature_list_.InitWithFeaturesAndParameters(
        content::GetBasicBackForwardCacheFeatureForTesting(
            {{features::kBackForwardCache, {}}}),
        content::GetDefaultDisabledBackForwardCacheFeaturesForTesting());
  }
  ~WebNavigationApiBackForwardCacheTest() override = default;

 private:
  base::test::ScopedFeatureList feature_list_;
};

using ContextType = extensions::browser_test_util::ContextType;

class WebNavigationApiTestWithContextType
    : public WebNavigationApiTest,
      public testing::WithParamInterface<ContextType> {
 public:
  WebNavigationApiTestWithContextType() : WebNavigationApiTest(GetParam()) {}
  ~WebNavigationApiTestWithContextType() override = default;
  WebNavigationApiTestWithContextType(
      const WebNavigationApiTestWithContextType&) = delete;
  WebNavigationApiTestWithContextType& operator=(
      const WebNavigationApiTestWithContextType&) = delete;

 protected:
  [[nodiscard]] bool RunTest(const char* name,
                             bool allow_in_incognito = false) {
    return RunExtensionTest(name, {},
                            {.allow_in_incognito = allow_in_incognito});
  }
};

class WebNavigationApiPrerenderTestWithContextType
    : public WebNavigationApiTest,
      public testing::WithParamInterface<ContextType> {
 public:
  WebNavigationApiPrerenderTestWithContextType()
      : WebNavigationApiTest(GetParam()) {}
  ~WebNavigationApiPrerenderTestWithContextType() override = default;
  WebNavigationApiPrerenderTestWithContextType(
      const WebNavigationApiPrerenderTestWithContextType&) = delete;
  WebNavigationApiPrerenderTestWithContextType& operator=(
      const WebNavigationApiPrerenderTestWithContextType&) = delete;

 private:
  content::test::ScopedPrerenderFeatureList prerender_feature_list_;
};

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, Api) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/api")) << message_;
}

// TODO(crbug.com/40858121): Flakily timing out.
IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, DISABLED_GetFrame) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/getFrame")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiPrerenderTestWithContextType, GetFrame) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/getFrame")) << message_;
}

IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, GetFrameIncognito) {
  // TODO(crbug.com/40937027): Convert test to use HTTPS and then remove.
  ScopedAllowHttpForHostnamesForTesting allow_http({"a.com"},
                                                   profile()->GetPrefs());
  ASSERT_TRUE(StartEmbeddedTestServer());

  GURL url = embedded_test_server()->GetURL("a.com", "/empty.html");

  Browser* incognito_browser = OpenURLOffTheRecord(browser()->profile(), url);
  ASSERT_TRUE(incognito_browser);

  // Now that we have a OTR browser, run the extension test.
  ASSERT_TRUE(RunExtensionTest("webnavigation/getFrameIncognito", {},
                               {.allow_in_incognito = true}))
      << message_;
}

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         WebNavigationApiTestWithContextType,
                         testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         WebNavigationApiTestWithContextType,
                         testing::Values(ContextType::kServiceWorker));
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         WebNavigationApiPrerenderTestWithContextType,
                         testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         WebNavigationApiPrerenderTestWithContextType,
                         testing::Values(ContextType::kServiceWorker));

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, ClientRedirect) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/clientRedirect")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, ServerRedirect) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/serverRedirect")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, FormSubmission) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/formSubmission")) << message_;
}

class WebNavigationApiPrerenderTestWithServiceWorker
    : public WebNavigationApiTest {
 public:
  WebNavigationApiPrerenderTestWithServiceWorker()
      // This test uses chrome.tabs.executeScript, which is not available in
      // MV3 or later. See crbug.com/332328868.
      : WebNavigationApiTest(ContextType::kServiceWorkerMV2) {}
  ~WebNavigationApiPrerenderTestWithServiceWorker() override = default;
  WebNavigationApiPrerenderTestWithServiceWorker(
      const WebNavigationApiPrerenderTestWithServiceWorker&) = delete;
  WebNavigationApiPrerenderTestWithServiceWorker& operator=(
      const WebNavigationApiPrerenderTestWithServiceWorker&) = delete;
};

// Tests that prerender events emit the correct events in the expected order.
IN_PROC_BROWSER_TEST_F(WebNavigationApiPrerenderTestWithServiceWorker,
                       Prerendering) {
  // TODO(crbug.com/40248833): Use https in the test and remove this allowlist
  // entry.
  ScopedAllowHttpForHostnamesForTesting scoped_allow_http(
      {"a.test"}, browser()->profile()->GetPrefs());

  ASSERT_TRUE(StartEmbeddedTestServer());
  EXPECT_TRUE(RunExtensionTest("webnavigation/prerendering")) << message_;
}

// TODO(crbug.com/40791797):
// WebNavigationApiTestWithContextType.Download test is flaky.
#if BUILDFLAG(IS_WIN)
#define MAYBE_Download DISABLED_Download
#else
#define MAYBE_Download Download
#endif
IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, MAYBE_Download) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  content::DownloadManager* download_manager =
      browser()->profile()->GetDownloadManager();
  content::DownloadTestObserverTerminal observer(
      download_manager, 1,
      content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
  bool result = RunTest("webnavigation/download");
  observer.WaitForFinished();
  ASSERT_TRUE(result) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType,
                       ServerRedirectSingleProcess) {
  // TODO(crbug.com/40248833): Use https in the test and remove these allowlist
  // entries.
  ScopedAllowHttpForHostnamesForTesting scoped_allow_http(
      {"www.a.com", "www.b.com"}, browser()->profile()->GetPrefs());

  ASSERT_TRUE(StartEmbeddedTestServer());

  // Set max renderers to 1 to force running out of processes.
  content::RenderProcessHost::SetMaxRendererProcessCount(1);

  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/serverRedirectSingleProcess"))
      << message_;

  WebContents* tab = GetWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(tab));

  ResultCatcher catcher;
  GURL url(
      base::StringPrintf("http://www.a.com:%u/extensions/api_test/"
                         "webnavigation/serverRedirectSingleProcess/a.html",
                         embedded_test_server()->port()));

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  url = GURL(base::StringPrintf(
      "http://www.b.com:%u/server-redirect?http://www.b.com:%u/test",
      embedded_test_server()->port(), embedded_test_server()->port()));

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, ForwardBack) {
  ASSERT_TRUE(RunTest("webnavigation/forwardBack")) << message_;
}

// TODO(crbug.com/40221198): Flaky on several platforms.
IN_PROC_BROWSER_TEST_F(WebNavigationApiBackForwardCacheTest,
                       DISABLED_ForwardBack) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/backForwardCache")) << message_;
}

#if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
// https://crbug.com/1223028
#define MAYBE_IFrame DISABLED_IFrame
#else
#define MAYBE_IFrame IFrame
#endif
IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, MAYBE_IFrame) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/iframe")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, SrcDoc) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/srcdoc")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, OpenTab) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/openTab")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, ReferenceFragment) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/referenceFragment")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, SimpleLoad) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/simpleLoad")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, Failures) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/failures")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, FilteredTest) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/filtered")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, UserAction) {
  content::IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
  ASSERT_TRUE(StartEmbeddedTestServer());

  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/userAction")) << message_;

  WebContents* tab = GetWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(tab));

  ResultCatcher catcher;

  const extensions::Extension* extension =
      extension_registry()->enabled_extensions().GetByID(
          last_loaded_extension_id());
  GURL url = extension->ResolveExtensionURL(
      "a.html?" + base::NumberToString(embedded_test_server()->port()));

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  // This corresponds to "Open link in new tab".
  content::ContextMenuParams params;
  params.is_editable = false;
  params.media_type = blink::mojom::ContextMenuDataMediaType::kNone;
  params.page_url = url;
  params.frame_url = url;
  params.frame_origin = url::Origin::Create(params.frame_url);
  params.link_url = extension->ResolveExtensionURL("b.html");

  // Get the child frame, which will be the one associated with the context
  // menu.
  content::RenderFrameHost* child_frame = ChildFrameAt(tab, 0);
  ASSERT_TRUE(child_frame);

  TestRenderViewContextMenu menu(*child_frame, params);
  menu.Init();
  menu.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, 0);

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, RequestOpenTab) {
  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/requestOpenTab")) << message_;

  WebContents* tab = GetWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(tab));

  ResultCatcher catcher;

  const extensions::Extension* extension =
      extension_registry()->enabled_extensions().GetByID(
          last_loaded_extension_id());
  GURL url = extension->ResolveExtensionURL("a.html");

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  // There's a link on a.html. Middle-click on it to open it in a new tab.
  blink::WebMouseEvent mouse_event(
      blink::WebInputEvent::Type::kMouseDown,
      blink::WebInputEvent::kNoModifiers,
      blink::WebInputEvent::GetStaticTimeStampForTests());
  mouse_event.button = blink::WebMouseEvent::Button::kMiddle;
  mouse_event.SetPositionInWidget(7, 7);
  mouse_event.click_count = 1;
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);
  mouse_event.SetType(blink::WebInputEvent::Type::kMouseUp);
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, TargetBlank) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/targetBlank")) << message_;

  WebContents* tab = GetWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(tab));

  ResultCatcher catcher;

  GURL url = embedded_test_server()->GetURL(
      "/extensions/api_test/webnavigation/targetBlank/a.html");

  NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
  ui_test_utils::NavigateToURL(&params);

  // There's a link with target=_blank on a.html. Click on it to open it in a
  // new tab.
  blink::WebMouseEvent mouse_event(
      blink::WebInputEvent::Type::kMouseDown,
      blink::WebInputEvent::kNoModifiers,
      blink::WebInputEvent::GetStaticTimeStampForTests());
  mouse_event.button = blink::WebMouseEvent::Button::kLeft;
  mouse_event.SetPositionInWidget(7, 7);
  mouse_event.click_count = 1;
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);
  mouse_event.SetType(blink::WebInputEvent::Type::kMouseUp);
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType,
                       TargetBlankIncognito) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/targetBlank", {},
                               {.allow_in_incognito = true}))
      << message_;

  ResultCatcher catcher;

  GURL url = embedded_test_server()->GetURL(
      "/extensions/api_test/webnavigation/targetBlank/a.html");

  Browser* otr_browser = OpenURLOffTheRecord(browser()->profile(), url);
  WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
  content::SimulateEndOfPaintHoldingOnPrimaryMainFrame(tab);

  // There's a link with target=_blank on a.html. Click on it to open it in a
  // new tab.
  blink::WebMouseEvent mouse_event(
      blink::WebInputEvent::Type::kMouseDown,
      blink::WebInputEvent::kNoModifiers,
      blink::WebInputEvent::GetStaticTimeStampForTests());
  mouse_event.button = blink::WebMouseEvent::Button::kLeft;
  mouse_event.SetPositionInWidget(7, 7);
  mouse_event.click_count = 1;
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);
  mouse_event.SetType(blink::WebInputEvent::Type::kMouseUp);
  tab->GetPrimaryMainFrame()
      ->GetRenderViewHost()
      ->GetWidget()
      ->ForwardMouseEvent(mouse_event);

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, History) {
  ASSERT_TRUE(RunExtensionTest("webnavigation/history")) << message_;
}

IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, CrossProcess) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  LoadExtension(test_data_dir_.AppendASCII("webnavigation").AppendASCII("app"));

  // See crossProcess/d.html.
  DelayLoadStartAndExecuteJavascript call_script(
      browser(), embedded_test_server()->GetURL("/test1"), "navigate2()",
      "empty.html");

  DelayLoadStartAndExecuteJavascript call_script_user_gesture(
      browser(), embedded_test_server()->GetURL("/test2"), "navigate2()",
      "empty.html");
  call_script_user_gesture.set_has_user_gesture(true);

  ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcess")) << message_;
}

// crbug.com/708139.
IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, DISABLED_CrossProcessFragment) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // See crossProcessFragment/f.html.
  DelayLoadStartAndExecuteJavascript call_script3(
      browser(), embedded_test_server()->GetURL("/test3"), "updateFragment()",
      base::StringPrintf("f.html?%u#foo", embedded_test_server()->port()));

  // See crossProcessFragment/g.html.
  DelayLoadStartAndExecuteJavascript call_script4(
      browser(), embedded_test_server()->GetURL("/test4"), "updateFragment()",
      base::StringPrintf("g.html?%u#foo", embedded_test_server()->port()));

  ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcessFragment"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType,
                       CrossProcessHistory) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // See crossProcessHistory/e.html.
  DelayLoadStartAndExecuteJavascript call_script2(
      browser(), embedded_test_server()->GetURL("/test2"), "updateHistory()",
      "empty.html");

  // See crossProcessHistory/h.html.
  DelayLoadStartAndExecuteJavascript call_script5(
      browser(), embedded_test_server()->GetURL("/test5"), "updateHistory()",
      "empty.html");

  // See crossProcessHistory/i.html.
  DelayLoadStartAndExecuteJavascript call_script6(
      browser(), embedded_test_server()->GetURL("/test6"), "updateHistory()",
      "empty.html");

  ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcessHistory"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType,
                       CrossProcessIframe) {
  content::IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcessIframe")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, PendingDeletion) {
  content::IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/pendingDeletion")) << message_;
}

IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, Crash) {
  // TODO(crbug.com/40248833): Use https in the test and remove this allowlist
  // entry.
  ScopedAllowHttpForHostnamesForTesting scoped_allow_http(
      {"www.a.com"}, browser()->profile()->GetPrefs());

  content::ScopedAllowRendererCrashes scoped_allow_renderer_crashes;
  ASSERT_TRUE(StartEmbeddedTestServer());

  // Wait for the extension to set itself up and return control to us.
  ASSERT_TRUE(RunExtensionTest("webnavigation/crash")) << message_;

  WebContents* tab = GetWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(tab));

  ResultCatcher catcher;

  GURL url(embedded_test_server()->GetURL(
      "www.a.com", "/extensions/api_test/webnavigation/crash/a.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  content::RenderProcessHostWatcher process_watcher(
      tab, content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
  ASSERT_TRUE(
      ui_test_utils::NavigateToURL(browser(), GURL(blink::kChromeUICrashURL)));
  process_watcher.Wait();

  url = GURL(embedded_test_server()->GetURL(
      "www.a.com", "/extensions/api_test/webnavigation/crash/b.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

#if BUILDFLAG(IS_MAC)
// TODO(crbug.com/40187463): Re-enable this test.
#define MAYBE_Xslt DISABLED_Xslt
#else
#define MAYBE_Xslt Xslt
#endif
IN_PROC_BROWSER_TEST_P(WebNavigationApiTestWithContextType, MAYBE_Xslt) {
  content::IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/xslt")) << message_;
}

class WebNavigationApiFencedFrameTest : public WebNavigationApiTest {
 protected:
  WebNavigationApiFencedFrameTest() {
    feature_list_.InitWithFeaturesAndParameters(
        /*enabled_features=*/{{blink::features::kFencedFrames, {}},
                              {blink::features::kFencedFramesAPIChanges, {}},
                              {blink::features::kFencedFramesDefaultMode, {}},
                              {features::kPrivacySandboxAdsAPIsOverride, {}}},
        /*disabled_features=*/{features::kSpareRendererForSitePerProcess});
    // Fenced frames are only allowed in a secure context.
    UseHttpsTestServer();
  }
  ~WebNavigationApiFencedFrameTest() override = default;

 private:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(WebNavigationApiFencedFrameTest, Load) {
  ASSERT_TRUE(StartEmbeddedTestServer());
  ASSERT_TRUE(RunExtensionTest("webnavigation/fencedFrames")) << message_;
}
}  // namespace extensions