test_navigation_observer.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
5#include "content/public/test/test_navigation_observer.h"
6
7#include "base/bind.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"
13
14namespace content {
15
16class TestNavigationObserver::TestWebContentsObserver
17    : public WebContentsObserver {
18 public:
19  TestWebContentsObserver(TestNavigationObserver* parent,
20                          WebContents* web_contents)
21      : WebContentsObserver(web_contents),
22        parent_(parent) {
23  }
24
25 private:
26  // WebContentsObserver:
27  virtual void NavigationEntryCommitted(
28      const LoadCommittedDetails& load_details) OVERRIDE {
29    parent_->OnNavigationEntryCommitted(this, web_contents(), load_details);
30  }
31
32  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
33    parent_->OnWebContentsDestroyed(this, web_contents);
34  }
35
36  virtual void DidStartLoading(RenderViewHost* render_view_host) OVERRIDE {
37    parent_->OnDidStartLoading(web_contents());
38  }
39
40  virtual void DidStopLoading(RenderViewHost* render_view_host) OVERRIDE {
41    parent_->OnDidStopLoading(web_contents());
42  }
43
44  TestNavigationObserver* parent_;
45
46  DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver);
47};
48
49TestNavigationObserver::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_(
57          base::Bind(
58              &TestNavigationObserver::OnWebContentsCreated,
59              base::Unretained(this))) {
60  if (web_contents)
61    RegisterAsObserver(web_contents);
62}
63
64TestNavigationObserver::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_(
71          base::Bind(
72              &TestNavigationObserver::OnWebContentsCreated,
73              base::Unretained(this))) {
74  if (web_contents)
75    RegisterAsObserver(web_contents);
76}
77
78TestNavigationObserver::~TestNavigationObserver() {
79  StopWatchingNewWebContents();
80
81  STLDeleteContainerPointers(web_contents_observers_.begin(),
82                             web_contents_observers_.end());
83}
84
85void TestNavigationObserver::Wait() {
86  message_loop_runner_->Run();
87}
88
89void TestNavigationObserver::StartWatchingNewWebContents() {
90  WebContents::AddCreatedCallback(web_contents_created_callback_);
91}
92
93void TestNavigationObserver::StopWatchingNewWebContents() {
94  WebContents::RemoveCreatedCallback(web_contents_created_callback_);
95}
96
97void TestNavigationObserver::RegisterAsObserver(WebContents* web_contents) {
98  web_contents_observers_.insert(
99      new TestWebContentsObserver(this, web_contents));
100}
101
102void TestNavigationObserver::OnWebContentsCreated(WebContents* web_contents) {
103  RegisterAsObserver(web_contents);
104}
105
106void TestNavigationObserver::OnWebContentsDestroyed(
107    TestWebContentsObserver* observer,
108    WebContents* web_contents) {
109  web_contents_observers_.erase(observer);
110  delete observer;
111}
112
113void TestNavigationObserver::OnNavigationEntryCommitted(
114    TestWebContentsObserver* observer,
115    WebContents* web_contents,
116    const LoadCommittedDetails& load_details) {
117  navigation_started_ = true;
118}
119
120void TestNavigationObserver::OnDidStartLoading(WebContents* web_contents) {
121  navigation_started_ = true;
122}
123
124void TestNavigationObserver::OnDidStopLoading(WebContents* web_contents) {
125  if (!navigation_started_)
126    return;
127
128  ++navigations_completed_;
129  if (navigations_completed_ == number_of_navigations_) {
130    navigation_started_ = false;
131    message_loop_runner_->Quit();
132  }
133}
134
135}  // namespace content
136