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
,
62 bool was_ignored_by_handler
) override
{
63 parent_
->OnDidFailProvisionalLoad(render_frame_host
, validated_url
,
64 error_code
, error_description
);
67 void DidCommitProvisionalLoadForFrame(
68 RenderFrameHost
* render_frame_host
,
70 ui::PageTransition transition_type
) override
{
71 parent_
->OnDidCommitProvisionalLoadForFrame(
72 render_frame_host
, url
, transition_type
);
75 TestNavigationObserver
* parent_
;
77 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver
);
80 TestNavigationObserver::TestNavigationObserver(
81 WebContents
* web_contents
,
82 int number_of_navigations
)
83 : navigation_started_(false),
84 navigations_completed_(0),
85 number_of_navigations_(number_of_navigations
),
86 message_loop_runner_(new MessageLoopRunner
),
87 web_contents_created_callback_(
89 &TestNavigationObserver::OnWebContentsCreated
,
90 base::Unretained(this))) {
92 RegisterAsObserver(web_contents
);
95 TestNavigationObserver::TestNavigationObserver(
96 WebContents
* web_contents
)
97 : navigation_started_(false),
98 navigations_completed_(0),
99 number_of_navigations_(1),
100 message_loop_runner_(new MessageLoopRunner
),
101 web_contents_created_callback_(
103 &TestNavigationObserver::OnWebContentsCreated
,
104 base::Unretained(this))) {
106 RegisterAsObserver(web_contents
);
109 TestNavigationObserver::~TestNavigationObserver() {
110 StopWatchingNewWebContents();
112 STLDeleteContainerPointers(web_contents_observers_
.begin(),
113 web_contents_observers_
.end());
116 void TestNavigationObserver::Wait() {
117 message_loop_runner_
->Run();
120 void TestNavigationObserver::StartWatchingNewWebContents() {
121 WebContentsImpl::FriendZone::AddCreatedCallbackForTesting(
122 web_contents_created_callback_
);
125 void TestNavigationObserver::StopWatchingNewWebContents() {
126 WebContentsImpl::FriendZone::RemoveCreatedCallbackForTesting(
127 web_contents_created_callback_
);
130 void TestNavigationObserver::RegisterAsObserver(WebContents
* web_contents
) {
131 web_contents_observers_
.insert(
132 new TestWebContentsObserver(this, web_contents
));
135 void TestNavigationObserver::OnWebContentsCreated(WebContents
* web_contents
) {
136 RegisterAsObserver(web_contents
);
139 void TestNavigationObserver::OnWebContentsDestroyed(
140 TestWebContentsObserver
* observer
,
141 WebContents
* web_contents
) {
142 web_contents_observers_
.erase(observer
);
146 void TestNavigationObserver::OnNavigationEntryCommitted(
147 TestWebContentsObserver
* observer
,
148 WebContents
* web_contents
,
149 const LoadCommittedDetails
& load_details
) {
150 navigation_started_
= true;
153 void TestNavigationObserver::OnDidAttachInterstitialPage(
154 WebContents
* web_contents
) {
155 // Going to an interstitial page does not trigger NavigationEntryCommitted,
156 // but has the same meaning for us here.
157 navigation_started_
= true;
160 void TestNavigationObserver::OnDidStartLoading(WebContents
* web_contents
) {
161 navigation_started_
= true;
164 void TestNavigationObserver::OnDidStopLoading(WebContents
* web_contents
) {
165 if (!navigation_started_
)
168 ++navigations_completed_
;
169 if (navigations_completed_
== number_of_navigations_
) {
170 navigation_started_
= false;
171 message_loop_runner_
->Quit();
175 void TestNavigationObserver::OnDidStartProvisionalLoadForFrame(
176 RenderFrameHost
* render_frame_host
,
177 const GURL
& validated_url
,
179 bool is_iframe_srcdoc
) {
180 last_navigation_succeeded_
= false;
183 void TestNavigationObserver::OnDidFailProvisionalLoad(
184 RenderFrameHost
* render_frame_host
,
185 const GURL
& validated_url
,
187 const base::string16
& error_description
) {
188 last_navigation_url_
= validated_url
;
189 last_navigation_succeeded_
= false;
192 void TestNavigationObserver::OnDidCommitProvisionalLoadForFrame(
193 RenderFrameHost
* render_frame_host
,
195 ui::PageTransition transition_type
) {
196 last_navigation_url_
= url
;
197 last_navigation_succeeded_
= true;
200 } // namespace content