blob: 9a7d170a7cfb72ae30a706481ce07cd7aaffa58f [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"
Xiaohan Wang15303d0c2017-10-06 05:22:448#include "base/run_loop.h"
Daniel Cheng9fb887ff2021-10-01 20:27:389#include "content/browser/renderer_host/document_service_echo_impl.h"
Fergal Daly3a62eb52019-10-31 23:43:4210#include "content/public/browser/back_forward_cache.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4411#include "content/public/browser/render_frame_host.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/test/navigation_simulator.h"
14#include "content/public/test/test_renderer_host.h"
Ken Rockot9652fa62020-11-05 05:30:2215#include "content/test/echo.test-mojom.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4416#include "content/test/test_render_frame_host.h"
Mario Sanchez Prada9d38e352019-09-10 08:07:5917#include "mojo/public/cpp/bindings/remote.h"
Xiaohan Wang15303d0c2017-10-06 05:22:4418#include "url/gurl.h"
19
Daniel Cheng9fb887ff2021-10-01 20:27:3820// Unit test for DocumentService in content/public/browser.
Xiaohan Wang15303d0c2017-10-06 05:22:4421
22namespace content {
23
24namespace {
25
26const char kFooOrigin[] = "https://foo.com";
27const char kBarOrigin[] = "https://bar.com";
28
Xiaohan Wang15303d0c2017-10-06 05:22:4429// Help functions to manipulate RenderFrameHosts.
30
31// Simulates navigation and returns the final RenderFrameHost.
32RenderFrameHost* SimulateNavigation(RenderFrameHost* rfh, const GURL& url) {
33 auto navigation_simulator =
34 NavigationSimulator::CreateRendererInitiated(url, rfh);
35 navigation_simulator->Commit();
36 return navigation_simulator->GetFinalRenderFrameHost();
37}
38
39RenderFrameHost* AddChildFrame(RenderFrameHost* rfh, const GURL& url) {
40 RenderFrameHost* child_rfh = RenderFrameHostTester::For(rfh)->AppendChild("");
41 RenderFrameHostTester::For(child_rfh)->InitializeRenderFrameIfNeeded();
42 return SimulateNavigation(child_rfh, url);
43}
44
45void DetachFrame(RenderFrameHost* rfh) {
46 RenderFrameHostTester::For(rfh)->Detach();
47}
48
49} // namespace
50
Daniel Cheng9fb887ff2021-10-01 20:27:3851class DocumentServiceTest : public RenderViewHostTestHarness {
Xiaohan Wang15303d0c2017-10-06 05:22:4452 protected:
53 void SetUp() final {
54 RenderViewHostTestHarness::SetUp();
55 Initialize();
56 }
57
58 void Initialize() {
59 RenderFrameHost* main_rfh = web_contents()->GetMainFrame();
60 RenderFrameHostTester::For(main_rfh)->InitializeRenderFrameIfNeeded();
61 main_rfh_ = SimulateNavigation(main_rfh, GURL(kFooOrigin));
62 }
63
64 void CreateEchoImpl(RenderFrameHost* rfh) {
65 DCHECK(!is_echo_impl_alive_);
Daniel Cheng9fb887ff2021-10-01 20:27:3866 new DocumentServiceEchoImpl(
Julie Jeongeun Kim5a66adb42021-08-11 07:14:1067 rfh, echo_remote_.BindNewPipeAndPassReceiver(),
Daniel Cheng9fb887ff2021-10-01 20:27:3868 base::BindOnce(&DocumentServiceTest::OnEchoImplDestructed,
Julie Jeongeun Kim5a66adb42021-08-11 07:14:1069 base::Unretained(this)));
Xiaohan Wang15303d0c2017-10-06 05:22:4470 is_echo_impl_alive_ = true;
71 }
72
73 void OnEchoImplDestructed() {
74 DCHECK(is_echo_impl_alive_);
75 is_echo_impl_alive_ = false;
76 }
77
78 void ResetConnection() {
Mario Sanchez Prada9d38e352019-09-10 08:07:5979 echo_remote_.reset();
Xiaohan Wang15303d0c2017-10-06 05:22:4480 base::RunLoop().RunUntilIdle();
81 }
82
83 RenderFrameHost* main_rfh_ = nullptr;
Mario Sanchez Prada9d38e352019-09-10 08:07:5984 mojo::Remote<mojom::Echo> echo_remote_;
Xiaohan Wang15303d0c2017-10-06 05:22:4485 bool is_echo_impl_alive_ = false;
86};
87
Daniel Cheng9fb887ff2021-10-01 20:27:3888TEST_F(DocumentServiceTest, ConnectionError) {
Xiaohan Wang15303d0c2017-10-06 05:22:4489 CreateEchoImpl(main_rfh_);
90 ResetConnection();
91 EXPECT_FALSE(is_echo_impl_alive_);
92}
93
Daniel Cheng9fb887ff2021-10-01 20:27:3894TEST_F(DocumentServiceTest, RenderFrameDeleted) {
Xiaohan Wang15303d0c2017-10-06 05:22:4495 // Needs to create a child frame so we can delete it using DetachFrame()
96 // because it is not allowed to detach the main frame.
97 RenderFrameHost* child_rfh = AddChildFrame(main_rfh_, GURL(kBarOrigin));
98 CreateEchoImpl(child_rfh);
99 DetachFrame(child_rfh);
100 EXPECT_FALSE(is_echo_impl_alive_);
101}
102
Daniel Cheng9fb887ff2021-10-01 20:27:38103TEST_F(DocumentServiceTest, DidFinishNavigation) {
Fergal Daly3a62eb52019-10-31 23:43:42104 // When a page enters the BackForwardCache, the RenderFrameHost is not
105 // deleted.
106 web_contents()->GetController().GetBackForwardCache().DisableForTesting(
107 BackForwardCache::TEST_ASSUMES_NO_CACHING);
Xiaohan Wang15303d0c2017-10-06 05:22:44108 CreateEchoImpl(main_rfh_);
109 SimulateNavigation(main_rfh_, GURL(kBarOrigin));
110 EXPECT_FALSE(is_echo_impl_alive_);
111}
112
Daniel Cheng9fb887ff2021-10-01 20:27:38113TEST_F(DocumentServiceTest, SameDocumentNavigation) {
Xiaohan Wang15303d0c2017-10-06 05:22:44114 CreateEchoImpl(main_rfh_);
115
116 // Must use the same origin to simulate same document navigation.
117 auto navigation_simulator =
118 NavigationSimulator::CreateRendererInitiated(GURL(kFooOrigin), main_rfh_);
119 navigation_simulator->CommitSameDocument();
120 DCHECK_EQ(main_rfh_, navigation_simulator->GetFinalRenderFrameHost());
121
122 EXPECT_TRUE(is_echo_impl_alive_);
123}
124
Daniel Cheng9fb887ff2021-10-01 20:27:38125TEST_F(DocumentServiceTest, FailedNavigation) {
Xiaohan Wang15303d0c2017-10-06 05:22:44126 CreateEchoImpl(main_rfh_);
127
128 auto navigation_simulator =
129 NavigationSimulator::CreateRendererInitiated(GURL(kFooOrigin), main_rfh_);
130 navigation_simulator->Fail(net::ERR_TIMED_OUT);
131 navigation_simulator->CommitErrorPage();
132
133 EXPECT_FALSE(is_echo_impl_alive_);
134}
135
Daniel Cheng9fb887ff2021-10-01 20:27:38136TEST_F(DocumentServiceTest, DeleteContents) {
Xiaohan Wang15303d0c2017-10-06 05:22:44137 CreateEchoImpl(main_rfh_);
138 DeleteContents();
139 EXPECT_FALSE(is_echo_impl_alive_);
140}
141
142} // namespace content