| OLD | NEW |
| (Empty) |
| 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 | |
| 7 #include "chrome/test/base/ui_test_utils.h" | |
| 8 #include "content/public/browser/notification_service.h" | |
| 9 #include "content/public/browser/notification_types.h" | |
| 10 #include "content/public/browser/render_view_host_observer.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 // This class observes |rvh| and calls OnJsInjectionReady() of | |
| 14 // |js_injection_ready_observer| when the time is right to inject | |
| 15 // JavaScript into the page. | |
| 16 class TestNavigationObserver::RVHOSendJS | |
| 17 : public content::RenderViewHostObserver { | |
| 18 public: | |
| 19 RVHOSendJS(RenderViewHost* rvh, | |
| 20 JsInjectionReadyObserver* js_injection_ready_observer) | |
| 21 : content::RenderViewHostObserver(rvh), | |
| 22 js_injection_ready_observer_(js_injection_ready_observer) { | |
| 23 } | |
| 24 | |
| 25 private: | |
| 26 // content::RenderViewHostObserver implementation. | |
| 27 virtual void RenderViewHostInitialized() OVERRIDE { | |
| 28 if (js_injection_ready_observer_) | |
| 29 js_injection_ready_observer_->OnJsInjectionReady(render_view_host()); | |
| 30 } | |
| 31 | |
| 32 JsInjectionReadyObserver* js_injection_ready_observer_; | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(RVHOSendJS); | |
| 35 }; | |
| 36 | |
| 37 TestNavigationObserver::JsInjectionReadyObserver::JsInjectionReadyObserver() { | |
| 38 } | |
| 39 | |
| 40 TestNavigationObserver::JsInjectionReadyObserver::~JsInjectionReadyObserver() { | |
| 41 } | |
| 42 | |
| 43 TestNavigationObserver::TestNavigationObserver( | |
| 44 const content::NotificationSource& source, | |
| 45 TestNavigationObserver::JsInjectionReadyObserver* | |
| 46 js_injection_ready_observer, | |
| 47 int number_of_navigations) | |
| 48 : navigation_started_(false), | |
| 49 navigations_completed_(0), | |
| 50 number_of_navigations_(number_of_navigations), | |
| 51 js_injection_ready_observer_(js_injection_ready_observer), | |
| 52 done_(false), | |
| 53 running_(false) { | |
| 54 // When we need to do javascript injection, register for RVH creation. | |
| 55 if (js_injection_ready_observer_) { | |
| 56 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, | |
| 57 content::NotificationService::AllSources()); | |
| 58 } | |
| 59 RegisterAsObserver(source); | |
| 60 } | |
| 61 | |
| 62 TestNavigationObserver::~TestNavigationObserver() { | |
| 63 } | |
| 64 | |
| 65 void TestNavigationObserver::WaitForObservation() { | |
| 66 if (!done_) { | |
| 67 EXPECT_FALSE(running_); | |
| 68 running_ = true; | |
| 69 ui_test_utils::RunMessageLoop(); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 TestNavigationObserver::TestNavigationObserver( | |
| 74 TestNavigationObserver::JsInjectionReadyObserver* | |
| 75 js_injection_ready_observer, | |
| 76 int number_of_navigations) | |
| 77 : navigation_started_(false), | |
| 78 navigations_completed_(0), | |
| 79 number_of_navigations_(number_of_navigations), | |
| 80 js_injection_ready_observer_(js_injection_ready_observer), | |
| 81 done_(false), | |
| 82 running_(false) { | |
| 83 // When we need to do javascript injection, register for RVH creation. | |
| 84 if (js_injection_ready_observer_) { | |
| 85 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, | |
| 86 content::NotificationService::AllSources()); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void TestNavigationObserver::RegisterAsObserver( | |
| 91 const content::NotificationSource& source) { | |
| 92 // Register for events to know when we've finished loading the page and are | |
| 93 // ready to quit the current message loop to return control back to the | |
| 94 // waiting test. | |
| 95 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source); | |
| 96 registrar_.Add(this, content::NOTIFICATION_LOAD_START, source); | |
| 97 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, source); | |
| 98 } | |
| 99 | |
| 100 void TestNavigationObserver::Observe( | |
| 101 int type, const content::NotificationSource& source, | |
| 102 const content::NotificationDetails& details) { | |
| 103 switch (type) { | |
| 104 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: | |
| 105 case content::NOTIFICATION_LOAD_START: | |
| 106 navigation_started_ = true; | |
| 107 break; | |
| 108 case content::NOTIFICATION_LOAD_STOP: | |
| 109 if (navigation_started_ && | |
| 110 ++navigations_completed_ == number_of_navigations_) { | |
| 111 navigation_started_ = false; | |
| 112 done_ = true; | |
| 113 if (running_) | |
| 114 MessageLoopForUI::current()->Quit(); | |
| 115 } | |
| 116 break; | |
| 117 case content::NOTIFICATION_RENDER_VIEW_HOST_CREATED: | |
| 118 rvho_send_js_.reset(new RVHOSendJS( | |
| 119 content::Source<RenderViewHost>(source).ptr(), | |
| 120 js_injection_ready_observer_)); | |
| 121 break; | |
| 122 default: | |
| 123 NOTREACHED(); | |
| 124 } | |
| 125 } | |
| OLD | NEW |