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/public/browser/web_contents_observer.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class TestNavigationObserver::TestWebContentsObserver
17 : public WebContentsObserver {
19 TestWebContentsObserver(TestNavigationObserver* parent,
20 WebContents* web_contents)
21 : WebContentsObserver(web_contents),
26 // WebContentsObserver:
27 virtual void NavigationEntryCommitted(
28 const LoadCommittedDetails& load_details) OVERRIDE {
29 parent_->OnNavigationEntryCommitted(this, web_contents(), load_details);
32 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
33 parent_->OnWebContentsDestroyed(this, web_contents);
36 virtual void DidStartLoading(RenderViewHost* render_view_host) OVERRIDE {
37 parent_->OnDidStartLoading(web_contents());
40 virtual void DidStopLoading(RenderViewHost* render_view_host) OVERRIDE {
41 parent_->OnDidStopLoading(web_contents());
44 TestNavigationObserver* parent_;
46 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver);
49 TestNavigationObserver::TestNavigationObserver(
50 WebContents* web_contents,
51 int number_of_navigations)
52 : navigation_started_(false),
53 navigations_completed_(0),
54 number_of_navigations_(number_of_navigations),
55 message_loop_runner_(new MessageLoopRunner),
56 web_contents_created_callback_(
58 &TestNavigationObserver::OnWebContentsCreated,
59 base::Unretained(this))) {
61 RegisterAsObserver(web_contents);
64 TestNavigationObserver::TestNavigationObserver(
65 WebContents* web_contents)
66 : navigation_started_(false),
67 navigations_completed_(0),
68 number_of_navigations_(1),
69 message_loop_runner_(new MessageLoopRunner),
70 web_contents_created_callback_(
72 &TestNavigationObserver::OnWebContentsCreated,
73 base::Unretained(this))) {
75 RegisterAsObserver(web_contents);
78 TestNavigationObserver::~TestNavigationObserver() {
79 StopWatchingNewWebContents();
81 STLDeleteContainerPointers(web_contents_observers_.begin(),
82 web_contents_observers_.end());
85 void TestNavigationObserver::Wait() {
86 message_loop_runner_->Run();
89 void TestNavigationObserver::StartWatchingNewWebContents() {
90 WebContents::AddCreatedCallback(web_contents_created_callback_);
93 void TestNavigationObserver::StopWatchingNewWebContents() {
94 WebContents::RemoveCreatedCallback(web_contents_created_callback_);
97 void TestNavigationObserver::RegisterAsObserver(WebContents* web_contents) {
98 web_contents_observers_.insert(
99 new TestWebContentsObserver(this, web_contents));
102 void TestNavigationObserver::OnWebContentsCreated(WebContents* web_contents) {
103 RegisterAsObserver(web_contents);
106 void TestNavigationObserver::OnWebContentsDestroyed(
107 TestWebContentsObserver* observer,
108 WebContents* web_contents) {
109 web_contents_observers_.erase(observer);
113 void TestNavigationObserver::OnNavigationEntryCommitted(
114 TestWebContentsObserver* observer,
115 WebContents* web_contents,
116 const LoadCommittedDetails& load_details) {
117 navigation_started_ = true;
120 void TestNavigationObserver::OnDidStartLoading(WebContents* web_contents) {
121 navigation_started_ = true;
124 void TestNavigationObserver::OnDidStopLoading(WebContents* web_contents) {
125 if (!navigation_started_)
128 ++navigations_completed_;
129 if (navigations_completed_ == number_of_navigations_) {
130 navigation_started_ = false;
131 message_loop_runner_->Quit();
135 } // namespace content