1 // Copyright (c) 2012 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.
5 #include "content/public/test/test_navigation_observer.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/stl_util.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class TestNavigationObserver::TestWebContentsObserver
18 : public WebContentsObserver
{
20 TestWebContentsObserver(TestNavigationObserver
* parent
,
21 WebContents
* web_contents
)
22 : WebContentsObserver(web_contents
),
27 // WebContentsObserver:
28 void NavigationEntryCommitted(
29 const LoadCommittedDetails
& load_details
) override
{
30 parent_
->OnNavigationEntryCommitted(this, web_contents(), load_details
);
33 void DidAttachInterstitialPage() override
{
34 parent_
->OnDidAttachInterstitialPage(web_contents());
37 void WebContentsDestroyed() override
{
38 parent_
->OnWebContentsDestroyed(this, web_contents());
41 void DidStartLoading() override
{
42 parent_
->OnDidStartLoading(web_contents());
45 void DidStopLoading() override
{
46 parent_
->OnDidStopLoading(web_contents());
49 void DidStartProvisionalLoadForFrame(RenderFrameHost
* render_frame_host
,
50 const GURL
& validated_url
,
52 bool is_iframe_srcdoc
) override
{
53 parent_
->OnDidStartProvisionalLoadForFrame(
54 render_frame_host
, validated_url
, is_error_page
, is_iframe_srcdoc
);
57 void DidFailProvisionalLoad(
58 RenderFrameHost
* render_frame_host
,
59 const GURL
& validated_url
,
61 const base::string16
& error_description
) override
{
62 parent_
->OnDidFailProvisionalLoad(render_frame_host
, validated_url
,
63 error_code
, error_description
);
66 void DidCommitProvisionalLoadForFrame(
67 RenderFrameHost
* render_frame_host
,
69 ui::PageTransition transition_type
) override
{
70 parent_
->OnDidCommitProvisionalLoadForFrame(
71 render_frame_host
, url
, transition_type
);
74 TestNavigationObserver
* parent_
;
76 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver
);
79 TestNavigationObserver::TestNavigationObserver(
80 WebContents
* web_contents
,
81 int number_of_navigations
)
82 : navigation_started_(false),
83 navigations_completed_(0),
84 number_of_navigations_(number_of_navigations
),
85 message_loop_runner_(new MessageLoopRunner
),
86 web_contents_created_callback_(
88 &TestNavigationObserver::OnWebContentsCreated
,
89 base::Unretained(this))) {
91 RegisterAsObserver(web_contents
);
94 TestNavigationObserver::TestNavigationObserver(
95 WebContents
* web_contents
)
96 : navigation_started_(false),
97 navigations_completed_(0),
98 number_of_navigations_(1),
99 message_loop_runner_(new MessageLoopRunner
),
100 web_contents_created_callback_(
102 &TestNavigationObserver::OnWebContentsCreated
,
103 base::Unretained(this))) {
105 RegisterAsObserver(web_contents
);
108 TestNavigationObserver::~TestNavigationObserver() {
109 StopWatchingNewWebContents();
111 STLDeleteContainerPointers(web_contents_observers_
.begin(),
112 web_contents_observers_
.end());
115 void TestNavigationObserver::Wait() {
116 message_loop_runner_
->Run();
119 void TestNavigationObserver::StartWatchingNewWebContents() {
120 WebContentsImpl::FriendZone::AddCreatedCallbackForTesting(
121 web_contents_created_callback_
);
124 void TestNavigationObserver::StopWatchingNewWebContents() {
125 WebContentsImpl::FriendZone::RemoveCreatedCallbackForTesting(
126 web_contents_created_callback_
);
129 void TestNavigationObserver::RegisterAsObserver(WebContents
* web_contents
) {
130 web_contents_observers_
.insert(
131 new TestWebContentsObserver(this, web_contents
));
134 void TestNavigationObserver::OnWebContentsCreated(WebContents
* web_contents
) {
135 RegisterAsObserver(web_contents
);
138 void TestNavigationObserver::OnWebContentsDestroyed(
139 TestWebContentsObserver
* observer
,
140 WebContents
* web_contents
) {
141 web_contents_observers_
.erase(observer
);
145 void TestNavigationObserver::OnNavigationEntryCommitted(
146 TestWebContentsObserver
* observer
,
147 WebContents
* web_contents
,
148 const LoadCommittedDetails
& load_details
) {
149 navigation_started_
= true;
152 void TestNavigationObserver::OnDidAttachInterstitialPage(
153 WebContents
* web_contents
) {
154 // Going to an interstitial page does not trigger NavigationEntryCommitted,
155 // but has the same meaning for us here.
156 navigation_started_
= true;
159 void TestNavigationObserver::OnDidStartLoading(WebContents
* web_contents
) {
160 navigation_started_
= true;
163 void TestNavigationObserver::OnDidStopLoading(WebContents
* web_contents
) {
164 if (!navigation_started_
)
167 ++navigations_completed_
;
168 if (navigations_completed_
== number_of_navigations_
) {
169 navigation_started_
= false;
170 message_loop_runner_
->Quit();
174 void TestNavigationObserver::OnDidStartProvisionalLoadForFrame(
175 RenderFrameHost
* render_frame_host
,
176 const GURL
& validated_url
,
178 bool is_iframe_srcdoc
) {
179 last_navigation_succeeded_
= false;
182 void TestNavigationObserver::OnDidFailProvisionalLoad(
183 RenderFrameHost
* render_frame_host
,
184 const GURL
& validated_url
,
186 const base::string16
& error_description
) {
187 last_navigation_url_
= validated_url
;
188 last_navigation_succeeded_
= false;
191 void TestNavigationObserver::OnDidCommitProvisionalLoadForFrame(
192 RenderFrameHost
* render_frame_host
,
194 ui::PageTransition transition_type
) {
195 last_navigation_url_
= url
;
196 last_navigation_succeeded_
= true;
199 } // namespace content