blob: 0531591dd0c1287308a8e47334b0783246290e52 [file] [log] [blame]
[email protected]bf8feaa2011-07-12 00:17:361// Copyright (c) 2011 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 "chrome/test/test_navigation_observer.h"
6
[email protected]af44e7fb2011-07-29 18:32:327#include "chrome/test/base/ui_test_utils.h"
[email protected]350019f2011-08-02 04:18:338#include "content/browser/renderer_host/render_view_host_observer.h"
[email protected]0d6e9bd2011-10-18 04:29:169#include "content/public/browser/notification_types.h"
[email protected]bf8feaa2011-07-12 00:17:3610#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]350019f2011-08-02 04:18:3312// This class observes |rvh| and calls OnJsInjectionReady() of
13// |js_injection_ready_observer| when the time is right to inject
14// JavaScript into the page.
15class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver {
16 public:
17 RVHOSendJS(RenderViewHost* rvh,
18 JsInjectionReadyObserver* js_injection_ready_observer)
19 : RenderViewHostObserver(rvh),
20 js_injection_ready_observer_(js_injection_ready_observer) {
21 }
22
23 private:
24 // RenderViewHostObserver implementation.
25 virtual void RenderViewHostInitialized() OVERRIDE {
26 if (js_injection_ready_observer_)
27 js_injection_ready_observer_->OnJsInjectionReady(render_view_host());
28 }
29
30 JsInjectionReadyObserver* js_injection_ready_observer_;
31
32 DISALLOW_COPY_AND_ASSIGN(RVHOSendJS);
33};
34
[email protected]bf8feaa2011-07-12 00:17:3635TestNavigationObserver::JsInjectionReadyObserver::JsInjectionReadyObserver() {
36}
37
38TestNavigationObserver::JsInjectionReadyObserver::~JsInjectionReadyObserver() {
39}
40
41TestNavigationObserver::TestNavigationObserver(
[email protected]c01d41a2011-08-23 03:27:0142 const NotificationSource& source,
[email protected]bf8feaa2011-07-12 00:17:3643 TestNavigationObserver::JsInjectionReadyObserver*
44 js_injection_ready_observer,
45 int number_of_navigations)
46 : navigation_started_(false),
[email protected]bf8feaa2011-07-12 00:17:3647 navigations_completed_(0),
48 number_of_navigations_(number_of_navigations),
49 js_injection_ready_observer_(js_injection_ready_observer),
50 done_(false),
51 running_(false) {
[email protected]350019f2011-08-02 04:18:3352 // When we need to do javascript injection, register for RVH creation.
53 if (js_injection_ready_observer_) {
54 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED,
55 NotificationService::AllSources());
56 }
[email protected]c01d41a2011-08-23 03:27:0157 RegisterAsObserver(source);
[email protected]bf8feaa2011-07-12 00:17:3658}
59
[email protected]33a6c772011-07-12 05:12:4060TestNavigationObserver::~TestNavigationObserver() {
61}
62
63void TestNavigationObserver::WaitForObservation() {
64 if (!done_) {
65 EXPECT_FALSE(running_);
66 running_ = true;
67 ui_test_utils::RunMessageLoop();
68 }
69}
70
[email protected]bf8feaa2011-07-12 00:17:3671TestNavigationObserver::TestNavigationObserver(
72 TestNavigationObserver::JsInjectionReadyObserver*
73 js_injection_ready_observer,
74 int number_of_navigations)
75 : navigation_started_(false),
76 navigations_completed_(0),
77 number_of_navigations_(number_of_navigations),
78 js_injection_ready_observer_(js_injection_ready_observer),
79 done_(false),
80 running_(false) {
[email protected]350019f2011-08-02 04:18:3381 // When we need to do javascript injection, register for RVH creation.
82 if (js_injection_ready_observer_) {
83 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED,
84 NotificationService::AllSources());
85 }
[email protected]bf8feaa2011-07-12 00:17:3686}
87
[email protected]bf8feaa2011-07-12 00:17:3688void TestNavigationObserver::RegisterAsObserver(
[email protected]c01d41a2011-08-23 03:27:0189 const NotificationSource& source) {
[email protected]350019f2011-08-02 04:18:3390 // Register for events to know when we've finished loading the page and are
91 // ready to quit the current message loop to return control back to the
92 // waiting test.
[email protected]c01d41a2011-08-23 03:27:0193 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
94 registrar_.Add(this, content::NOTIFICATION_LOAD_START, source);
95 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, source);
[email protected]bf8feaa2011-07-12 00:17:3696}
97
[email protected]bf8feaa2011-07-12 00:17:3698void TestNavigationObserver::Observe(
99 int type, const NotificationSource& source,
100 const NotificationDetails& details) {
[email protected]33a6c772011-07-12 05:12:40101 switch (type) {
102 case content::NOTIFICATION_NAV_ENTRY_COMMITTED:
[email protected]33a6c772011-07-12 05:12:40103 case content::NOTIFICATION_LOAD_START:
104 navigation_started_ = true;
105 break;
106 case content::NOTIFICATION_LOAD_STOP:
107 if (navigation_started_ &&
108 ++navigations_completed_ == number_of_navigations_) {
109 navigation_started_ = false;
110 done_ = true;
111 if (running_)
112 MessageLoopForUI::current()->Quit();
113 }
114 break;
[email protected]350019f2011-08-02 04:18:33115 case content::NOTIFICATION_RENDER_VIEW_HOST_CREATED:
116 rvho_send_js_.reset(new RVHOSendJS(Source<RenderViewHost>(source).ptr(),
117 js_injection_ready_observer_));
118 break;
[email protected]33a6c772011-07-12 05:12:40119 default:
[email protected]39447952011-07-22 02:29:54120 NOTREACHED();
[email protected]bf8feaa2011-07-12 00:17:36121 }
122}