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