[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 1 | // 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] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 7 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 8 | #include "content/browser/renderer_host/render_view_host_observer.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame^] | 9 | #include "content/public/browser/notification_types.h" |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 12 | // 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. |
| 15 | class 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] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 35 | TestNavigationObserver::JsInjectionReadyObserver::JsInjectionReadyObserver() { |
| 36 | } |
| 37 | |
| 38 | TestNavigationObserver::JsInjectionReadyObserver::~JsInjectionReadyObserver() { |
| 39 | } |
| 40 | |
| 41 | TestNavigationObserver::TestNavigationObserver( |
[email protected] | c01d41a | 2011-08-23 03:27:01 | [diff] [blame] | 42 | const NotificationSource& source, |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 43 | TestNavigationObserver::JsInjectionReadyObserver* |
| 44 | js_injection_ready_observer, |
| 45 | int number_of_navigations) |
| 46 | : navigation_started_(false), |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 47 | 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] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 52 | // 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] | c01d41a | 2011-08-23 03:27:01 | [diff] [blame] | 57 | RegisterAsObserver(source); |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 33a6c77 | 2011-07-12 05:12:40 | [diff] [blame] | 60 | TestNavigationObserver::~TestNavigationObserver() { |
| 61 | } |
| 62 | |
| 63 | void TestNavigationObserver::WaitForObservation() { |
| 64 | if (!done_) { |
| 65 | EXPECT_FALSE(running_); |
| 66 | running_ = true; |
| 67 | ui_test_utils::RunMessageLoop(); |
| 68 | } |
| 69 | } |
| 70 | |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 71 | TestNavigationObserver::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] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 81 | // 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] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 88 | void TestNavigationObserver::RegisterAsObserver( |
[email protected] | c01d41a | 2011-08-23 03:27:01 | [diff] [blame] | 89 | const NotificationSource& source) { |
[email protected] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 90 | // 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] | c01d41a | 2011-08-23 03:27:01 | [diff] [blame] | 93 | 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] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 98 | void TestNavigationObserver::Observe( |
| 99 | int type, const NotificationSource& source, |
| 100 | const NotificationDetails& details) { |
[email protected] | 33a6c77 | 2011-07-12 05:12:40 | [diff] [blame] | 101 | switch (type) { |
| 102 | case content::NOTIFICATION_NAV_ENTRY_COMMITTED: |
[email protected] | 33a6c77 | 2011-07-12 05:12:40 | [diff] [blame] | 103 | 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] | 350019f | 2011-08-02 04:18:33 | [diff] [blame] | 115 | 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] | 33a6c77 | 2011-07-12 05:12:40 | [diff] [blame] | 119 | default: |
[email protected] | 3944795 | 2011-07-22 02:29:54 | [diff] [blame] | 120 | NOTREACHED(); |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 121 | } |
| 122 | } |