blob: caff0375684e625e2504d5a26396bfad6fb7aece [file] [log] [blame]
Khushal Sagar91b544222024-03-12 17:36:591// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/test/scoped_feature_list.h"
6#include "components/viz/host/host_frame_sink_manager.h"
7#include "content/browser/compositor/surface_utils.h"
8#include "content/browser/renderer_host/frame_tree_node.h"
9#include "content/public/test/browser_test.h"
10#include "content/public/test/browser_test_utils.h"
11#include "content/public/test/content_browser_test.h"
12#include "content/shell/browser/shell.h"
13#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
14#include "net/dns/mock_host_resolver.h"
15#include "net/test/embedded_test_server/default_handlers.h"
16#include "third_party/blink/public/common/features.h"
17
18namespace content {
19
20class ViewTransitionBrowserTest : public ContentBrowserTest {
21 public:
22 class TestCondition : public CommitDeferringCondition {
23 public:
24 TestCondition(NavigationRequest& request, base::RunLoop* run_loop)
25 : CommitDeferringCondition(request), run_loop_(run_loop) {}
26 ~TestCondition() override = default;
27
28 Result WillCommitNavigation(base::OnceClosure resume) override {
29 GetUIThreadTaskRunner()->PostTask(FROM_HERE, run_loop_->QuitClosure());
30 return Result::kDefer;
31 }
32
33 private:
34 raw_ptr<base::RunLoop> run_loop_;
35 };
36
37 ViewTransitionBrowserTest() {
38 feature_list_.InitWithFeatures(
39 /*enabled_features=*/{blink::features::kViewTransitionOnNavigation},
40 /*disabled_features=*/{});
41 }
42
43 void SetUpOnMainThread() override {
44 ContentBrowserTest::SetUpOnMainThread();
45 host_resolver()->AddRule("*", "127.0.0.1");
46 embedded_test_server()->ServeFilesFromSourceDirectory(
47 GetTestDataFilePath());
48 net::test_server::RegisterDefaultHandlers(embedded_test_server());
49 ASSERT_TRUE(embedded_test_server()->Start());
50 }
51
52 void WaitForConditionsDone(NavigationRequest* request) {
53 // Inject a condition to know when the VT response has been received but
54 // before the NavigationRequest is notified.
55 run_loop_ = std::make_unique<base::RunLoop>();
56 request->RegisterCommitDeferringConditionForTesting(
57 std::make_unique<TestCondition>(*request, run_loop_.get()));
58 run_loop_->Run();
59 }
60
61 private:
62 base::test::ScopedFeatureList feature_list_;
63 std::unique_ptr<base::RunLoop> run_loop_;
64};
65
66IN_PROC_BROWSER_TEST_F(ViewTransitionBrowserTest,
67 NavigationCancelledAfterScreenshot) {
68 // Start with a page which has an opt-in for VT.
69 GURL test_url(
70 embedded_test_server()->GetURL("/view_transitions/basic-vt-opt-in.html"));
71 ASSERT_TRUE(NavigateToURL(shell()->web_contents(), test_url));
72
73 TestNavigationManager navigation_manager(shell()->web_contents(), test_url);
74 ASSERT_TRUE(
75 ExecJs(shell()->web_contents(), "location.href = location.href;"));
76
77 // Wait for response and resume. The navigation should be blocked by the view
78 // transition condition.
79 ASSERT_TRUE(navigation_manager.WaitForResponse());
80 navigation_manager.ResumeNavigation();
81
82 auto* navigation_request =
83 NavigationRequest::From(navigation_manager.GetNavigationHandle());
84 ASSERT_TRUE(navigation_request);
85 ASSERT_TRUE(
86 navigation_request->IsCommitDeferringConditionDeferredForTesting());
87 ASSERT_FALSE(navigation_request->commit_params().view_transition_state);
88
89 WaitForConditionsDone(navigation_request);
90 ASSERT_TRUE(navigation_request->commit_params().view_transition_state);
91
92 mojo::ScopedAllowSyncCallForTesting allow_sync;
93
94 ASSERT_TRUE(
95 GetHostFrameSinkManager()->HasUnclaimedViewTransitionResourcesForTest());
96
97 shell()->web_contents()->Stop();
98 ASSERT_FALSE(navigation_manager.was_committed());
99 ASSERT_FALSE(
100 GetHostFrameSinkManager()->HasUnclaimedViewTransitionResourcesForTest());
101}
102
103IN_PROC_BROWSER_TEST_F(ViewTransitionBrowserTest,
104 OwnershipTransferredToNewRenderer) {
105 // Start with a page which has an opt-in for VT.
106 GURL test_url(
107 embedded_test_server()->GetURL("/view_transitions/basic-vt-opt-in.html"));
108 ASSERT_TRUE(NavigateToURL(shell()->web_contents(), test_url));
109
110 TestNavigationManager navigation_manager(shell()->web_contents(), test_url);
111 ASSERT_TRUE(
112 ExecJs(shell()->web_contents(), "location.href = location.href;"));
113 ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
114 ASSERT_TRUE(static_cast<RenderWidgetHostViewBase*>(
115 shell()->web_contents()->GetRenderWidgetHostView())
116 ->HasViewTransitionResourcesForTesting());
117}
118
119} // namespace content