blob: 38c8aacd199aab74a864520915134727e01e95cf [file] [log] [blame]
Xiaohan Wang15303d0c2017-10-06 05:22:441// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Daniel Cheng9fb887ff2021-10-01 20:27:385#include "content/public/browser/document_service.h"
Xiaohan Wang15303d0c2017-10-06 05:22:446
Sebastien Marchandf8cbfab2019-01-25 16:02:307#include "base/bind.h"
Keishi Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
Xiaohan Wang15303d0c2017-10-06 05:22:449#include "base/run_loop.h"
Daniel Cheng9fb887ff2021-10-01 20:27:3810#include "content/browser/renderer_host/document_service_echo_impl.h"
Fergal Daly3a62eb52019-10-31 23:43:4211#include "content/public/browser/back_forward_cache.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4412#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 Rockot9652fa62020-11-05 05:30:2216#include "content/test/echo.test-mojom.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4417#include "content/test/test_render_frame_host.h"
Mario Sanchez Prada9d38e352019-09-10 08:07:5918#include "mojo/public/cpp/bindings/remote.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4419#include "url/gurl.h"
20
Daniel Cheng9fb887ff2021-10-01 20:27:3821// Unit test for DocumentService in content/public/browser.
Xiaohan Wang15303d0c2017-10-06 05:22:4422
23namespace content {
24
25namespace {
26
27const char kFooOrigin[] = "https://foo.com";
28const char kBarOrigin[] = "https://bar.com";
29
Xiaohan Wang15303d0c2017-10-06 05:22:4430// Help functions to manipulate RenderFrameHosts.
31
32// Simulates navigation and returns the final RenderFrameHost.
33RenderFrameHost* 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
40RenderFrameHost* 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
46void DetachFrame(RenderFrameHost* rfh) {
47 RenderFrameHostTester::For(rfh)->Detach();
48}
49
50} // namespace
51
Daniel Cheng9fb887ff2021-10-01 20:27:3852class DocumentServiceTest : public RenderViewHostTestHarness {
Xiaohan Wang15303d0c2017-10-06 05:22:4453 protected:
54 void SetUp() final {
55 RenderViewHostTestHarness::SetUp();
56 Initialize();
57 }
58
59 void Initialize() {
60 RenderFrameHost* main_rfh = web_contents()->GetMainFrame();
61 RenderFrameHostTester::For(main_rfh)->InitializeRenderFrameIfNeeded();
62 main_rfh_ = SimulateNavigation(main_rfh, GURL(kFooOrigin));
63 }
64
65 void CreateEchoImpl(RenderFrameHost* rfh) {
66 DCHECK(!is_echo_impl_alive_);
Daniel Cheng9fb887ff2021-10-01 20:27:3867 new DocumentServiceEchoImpl(
Julie Jeongeun Kim5a66adb42021-08-11 07:14:1068 rfh, echo_remote_.BindNewPipeAndPassReceiver(),
Daniel Cheng9fb887ff2021-10-01 20:27:3869 base::BindOnce(&DocumentServiceTest::OnEchoImplDestructed,
Julie Jeongeun Kim5a66adb42021-08-11 07:14:1070 base::Unretained(this)));
Xiaohan Wang15303d0c2017-10-06 05:22:4471 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 Prada9d38e352019-09-10 08:07:5980 echo_remote_.reset();
Xiaohan Wang15303d0c2017-10-06 05:22:4481 base::RunLoop().RunUntilIdle();
82 }
83
Keishi Hattori0e45c022021-11-27 09:25:5284 raw_ptr<RenderFrameHost> main_rfh_ = nullptr;
Mario Sanchez Prada9d38e352019-09-10 08:07:5985 mojo::Remote<mojom::Echo> echo_remote_;
Xiaohan Wang15303d0c2017-10-06 05:22:4486 bool is_echo_impl_alive_ = false;
87};
88
Daniel Cheng9fb887ff2021-10-01 20:27:3889TEST_F(DocumentServiceTest, ConnectionError) {
Xiaohan Wang15303d0c2017-10-06 05:22:4490 CreateEchoImpl(main_rfh_);
91 ResetConnection();
92 EXPECT_FALSE(is_echo_impl_alive_);
93}
94
Daniel Cheng9fb887ff2021-10-01 20:27:3895TEST_F(DocumentServiceTest, RenderFrameDeleted) {
Xiaohan Wang15303d0c2017-10-06 05:22:4496 // Needs to create a child frame so we can delete it using DetachFrame()
97 // because it is not allowed to detach the main frame.
98 RenderFrameHost* child_rfh = AddChildFrame(main_rfh_, GURL(kBarOrigin));
99 CreateEchoImpl(child_rfh);
100 DetachFrame(child_rfh);
101 EXPECT_FALSE(is_echo_impl_alive_);
102}
103
Daniel Cheng9fb887ff2021-10-01 20:27:38104TEST_F(DocumentServiceTest, DidFinishNavigation) {
Fergal Daly3a62eb52019-10-31 23:43:42105 // When a page enters the BackForwardCache, the RenderFrameHost is not
106 // deleted.
107 web_contents()->GetController().GetBackForwardCache().DisableForTesting(
Rakina Zata Amni30af7062022-01-19 23:46:36108 BackForwardCache::TEST_REQUIRES_NO_CACHING);
Xiaohan Wang15303d0c2017-10-06 05:22:44109 CreateEchoImpl(main_rfh_);
110 SimulateNavigation(main_rfh_, GURL(kBarOrigin));
111 EXPECT_FALSE(is_echo_impl_alive_);
112}
113
Daniel Cheng9fb887ff2021-10-01 20:27:38114TEST_F(DocumentServiceTest, SameDocumentNavigation) {
Xiaohan Wang15303d0c2017-10-06 05:22:44115 CreateEchoImpl(main_rfh_);
116
117 // Must use the same origin to simulate same document navigation.
118 auto navigation_simulator =
119 NavigationSimulator::CreateRendererInitiated(GURL(kFooOrigin), main_rfh_);
120 navigation_simulator->CommitSameDocument();
121 DCHECK_EQ(main_rfh_, navigation_simulator->GetFinalRenderFrameHost());
122
123 EXPECT_TRUE(is_echo_impl_alive_);
124}
125
Daniel Cheng9fb887ff2021-10-01 20:27:38126TEST_F(DocumentServiceTest, FailedNavigation) {
Xiaohan Wang15303d0c2017-10-06 05:22:44127 CreateEchoImpl(main_rfh_);
128
129 auto navigation_simulator =
130 NavigationSimulator::CreateRendererInitiated(GURL(kFooOrigin), main_rfh_);
131 navigation_simulator->Fail(net::ERR_TIMED_OUT);
132 navigation_simulator->CommitErrorPage();
133
134 EXPECT_FALSE(is_echo_impl_alive_);
135}
136
Daniel Cheng9fb887ff2021-10-01 20:27:38137TEST_F(DocumentServiceTest, DeleteContents) {
Xiaohan Wang15303d0c2017-10-06 05:22:44138 CreateEchoImpl(main_rfh_);
139 DeleteContents();
140 EXPECT_FALSE(is_echo_impl_alive_);
141}
142
143} // namespace content