Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 5 | #include "content/public/browser/document_service.h" |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 6 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 9 | #include "base/run_loop.h" |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 10 | #include "content/browser/renderer_host/document_service_echo_impl.h" |
Fergal Daly | 3a62eb5 | 2019-10-31 23:43:42 | [diff] [blame] | 11 | #include "content/public/browser/back_forward_cache.h" |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 12 | #include "content/public/browser/render_frame_host.h" |
| 13 | #include "content/public/browser/web_contents.h" |
| 14 | #include "content/public/test/navigation_simulator.h" |
| 15 | #include "content/public/test/test_renderer_host.h" |
Ken Rockot | 9652fa6 | 2020-11-05 05:30:22 | [diff] [blame] | 16 | #include "content/test/echo.test-mojom.h" |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 17 | #include "content/test/test_render_frame_host.h" |
Mario Sanchez Prada | 9d38e35 | 2019-09-10 08:07:59 | [diff] [blame] | 18 | #include "mojo/public/cpp/bindings/remote.h" |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 19 | #include "url/gurl.h" |
| 20 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 21 | // Unit test for DocumentService in content/public/browser. |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 22 | |
| 23 | namespace content { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const char kFooOrigin[] = "https://foo.com"; |
| 28 | const char kBarOrigin[] = "https://bar.com"; |
| 29 | |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 30 | // Help functions to manipulate RenderFrameHosts. |
| 31 | |
| 32 | // Simulates navigation and returns the final RenderFrameHost. |
| 33 | RenderFrameHost* SimulateNavigation(RenderFrameHost* rfh, const GURL& url) { |
| 34 | auto navigation_simulator = |
| 35 | NavigationSimulator::CreateRendererInitiated(url, rfh); |
| 36 | navigation_simulator->Commit(); |
| 37 | return navigation_simulator->GetFinalRenderFrameHost(); |
| 38 | } |
| 39 | |
| 40 | RenderFrameHost* AddChildFrame(RenderFrameHost* rfh, const GURL& url) { |
| 41 | RenderFrameHost* child_rfh = RenderFrameHostTester::For(rfh)->AppendChild(""); |
| 42 | RenderFrameHostTester::For(child_rfh)->InitializeRenderFrameIfNeeded(); |
| 43 | return SimulateNavigation(child_rfh, url); |
| 44 | } |
| 45 | |
| 46 | void DetachFrame(RenderFrameHost* rfh) { |
| 47 | RenderFrameHostTester::For(rfh)->Detach(); |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 52 | class DocumentServiceTest : public RenderViewHostTestHarness { |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 53 | protected: |
| 54 | void SetUp() final { |
| 55 | RenderViewHostTestHarness::SetUp(); |
| 56 | Initialize(); |
| 57 | } |
| 58 | |
| 59 | void Initialize() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 60 | RenderFrameHost* main_rfh = web_contents()->GetPrimaryMainFrame(); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 61 | RenderFrameHostTester::For(main_rfh)->InitializeRenderFrameIfNeeded(); |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 62 | SimulateNavigation(main_rfh, GURL(kFooOrigin)); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 63 | } |
| 64 | |
danakj | c70aec1f | 2022-07-07 15:48:19 | [diff] [blame] | 65 | void CreateEchoImpl(RenderFrameHost& rfh) { |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 66 | DCHECK(!is_echo_impl_alive_); |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 67 | new DocumentServiceEchoImpl( |
Julie Jeongeun Kim | 5a66adb4 | 2021-08-11 07:14:10 | [diff] [blame] | 68 | rfh, echo_remote_.BindNewPipeAndPassReceiver(), |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 69 | base::BindOnce(&DocumentServiceTest::OnEchoImplDestructed, |
Julie Jeongeun Kim | 5a66adb4 | 2021-08-11 07:14:10 | [diff] [blame] | 70 | base::Unretained(this))); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 71 | is_echo_impl_alive_ = true; |
| 72 | } |
| 73 | |
| 74 | void OnEchoImplDestructed() { |
| 75 | DCHECK(is_echo_impl_alive_); |
| 76 | is_echo_impl_alive_ = false; |
| 77 | } |
| 78 | |
| 79 | void ResetConnection() { |
Mario Sanchez Prada | 9d38e35 | 2019-09-10 08:07:59 | [diff] [blame] | 80 | echo_remote_.reset(); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 81 | base::RunLoop().RunUntilIdle(); |
| 82 | } |
| 83 | |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 84 | RenderFrameHost* main_rfh() const { |
| 85 | return web_contents()->GetPrimaryMainFrame(); |
| 86 | } |
| 87 | |
Mario Sanchez Prada | 9d38e35 | 2019-09-10 08:07:59 | [diff] [blame] | 88 | mojo::Remote<mojom::Echo> echo_remote_; |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 89 | bool is_echo_impl_alive_ = false; |
| 90 | }; |
| 91 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 92 | TEST_F(DocumentServiceTest, ConnectionError) { |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 93 | CreateEchoImpl(*main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 94 | ResetConnection(); |
| 95 | EXPECT_FALSE(is_echo_impl_alive_); |
| 96 | } |
| 97 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 98 | TEST_F(DocumentServiceTest, RenderFrameDeleted) { |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 99 | // Needs to create a child frame so we can delete it using DetachFrame() |
| 100 | // because it is not allowed to detach the main frame. |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 101 | RenderFrameHost* child_rfh = AddChildFrame(main_rfh(), GURL(kBarOrigin)); |
danakj | c70aec1f | 2022-07-07 15:48:19 | [diff] [blame] | 102 | CreateEchoImpl(*child_rfh); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 103 | DetachFrame(child_rfh); |
| 104 | EXPECT_FALSE(is_echo_impl_alive_); |
| 105 | } |
| 106 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 107 | TEST_F(DocumentServiceTest, DidFinishNavigation) { |
Fergal Daly | 3a62eb5 | 2019-10-31 23:43:42 | [diff] [blame] | 108 | // When a page enters the BackForwardCache, the RenderFrameHost is not |
| 109 | // deleted. |
| 110 | web_contents()->GetController().GetBackForwardCache().DisableForTesting( |
Rakina Zata Amni | 30af706 | 2022-01-19 23:46:36 | [diff] [blame] | 111 | BackForwardCache::TEST_REQUIRES_NO_CACHING); |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 112 | CreateEchoImpl(*main_rfh()); |
| 113 | SimulateNavigation(main_rfh(), GURL(kBarOrigin)); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 114 | EXPECT_FALSE(is_echo_impl_alive_); |
| 115 | } |
| 116 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 117 | TEST_F(DocumentServiceTest, SameDocumentNavigation) { |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 118 | CreateEchoImpl(*main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 119 | |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 120 | RenderFrameHost* previous_main_rfh = main_rfh(); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 121 | // Must use the same origin to simulate same document navigation. |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 122 | auto navigation_simulator = NavigationSimulator::CreateRendererInitiated( |
| 123 | GURL(kFooOrigin), main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 124 | navigation_simulator->CommitSameDocument(); |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 125 | DCHECK_EQ(previous_main_rfh, navigation_simulator->GetFinalRenderFrameHost()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 126 | |
| 127 | EXPECT_TRUE(is_echo_impl_alive_); |
| 128 | } |
| 129 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 130 | TEST_F(DocumentServiceTest, FailedNavigation) { |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 131 | CreateEchoImpl(*main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 132 | |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 133 | auto navigation_simulator = NavigationSimulator::CreateRendererInitiated( |
| 134 | GURL(kFooOrigin), main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 135 | navigation_simulator->Fail(net::ERR_TIMED_OUT); |
| 136 | navigation_simulator->CommitErrorPage(); |
| 137 | |
| 138 | EXPECT_FALSE(is_echo_impl_alive_); |
| 139 | } |
| 140 | |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 141 | TEST_F(DocumentServiceTest, DeleteContents) { |
Sam Fortiner | fb39c1e | 2023-12-08 03:03:12 | [diff] [blame] | 142 | CreateEchoImpl(*main_rfh()); |
Xiaohan Wang | 15303d0c | 2017-10-06 05:22:44 | [diff] [blame] | 143 | DeleteContents(); |
| 144 | EXPECT_FALSE(is_echo_impl_alive_); |
| 145 | } |
| 146 | |
| 147 | } // namespace content |