| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/test_navigation_observer.h" | 5 #include "content/public/test/test_navigation_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| 11 #include "content/public/browser/notification_types.h" | 11 #include "content/public/browser/notification_types.h" |
| 12 #include "content/public/browser/render_view_host_observer.h" | |
| 13 #include "content/public/test/js_injection_ready_observer.h" | |
| 14 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 14 |
| 17 namespace content { | 15 namespace content { |
| 18 | 16 |
| 19 // This class observes |render_view_host| and calls OnJsInjectionReady() of | |
| 20 // |js_injection_ready_observer| when the time is right to inject JavaScript | |
| 21 // into the page. | |
| 22 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { | |
| 23 public: | |
| 24 RVHOSendJS(RenderViewHost* render_view_host, | |
| 25 JsInjectionReadyObserver* js_injection_ready_observer) | |
| 26 : RenderViewHostObserver(render_view_host), | |
| 27 js_injection_ready_observer_(js_injection_ready_observer) { | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 // RenderViewHostObserver implementation. | |
| 32 virtual void RenderViewHostInitialized() OVERRIDE { | |
| 33 if (js_injection_ready_observer_) | |
| 34 js_injection_ready_observer_->OnJsInjectionReady(render_view_host()); | |
| 35 } | |
| 36 | |
| 37 JsInjectionReadyObserver* js_injection_ready_observer_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(RVHOSendJS); | |
| 40 }; | |
| 41 | |
| 42 TestNavigationObserver::TestNavigationObserver( | 17 TestNavigationObserver::TestNavigationObserver( |
| 43 const NotificationSource& source, | 18 const NotificationSource& source, |
| 44 JsInjectionReadyObserver* js_injection_ready_observer, | |
| 45 int number_of_navigations) | 19 int number_of_navigations) |
| 46 : navigation_started_(false), | 20 : navigation_started_(false), |
| 47 navigations_completed_(0), | 21 navigations_completed_(0), |
| 48 number_of_navigations_(number_of_navigations), | 22 number_of_navigations_(number_of_navigations), |
| 49 js_injection_ready_observer_(js_injection_ready_observer), | |
| 50 done_(false), | 23 done_(false), |
| 51 running_(false) { | 24 running_(false) { |
| 52 // When javascript injection is requested, register for RenderViewHost | |
| 53 // creation. | |
| 54 if (js_injection_ready_observer_) { | |
| 55 registrar_.Add(this, NOTIFICATION_RENDER_VIEW_HOST_CREATED, | |
| 56 NotificationService::AllSources()); | |
| 57 } | |
| 58 RegisterAsObserver(source); | 25 RegisterAsObserver(source); |
| 59 } | 26 } |
| 60 | 27 |
| 61 TestNavigationObserver::TestNavigationObserver( | 28 TestNavigationObserver::TestNavigationObserver( |
| 62 const NotificationSource& source) | 29 const NotificationSource& source) |
| 63 : navigation_started_(false), | 30 : navigation_started_(false), |
| 64 navigations_completed_(0), | 31 navigations_completed_(0), |
| 65 number_of_navigations_(1), | 32 number_of_navigations_(1), |
| 66 js_injection_ready_observer_(NULL), | |
| 67 done_(false), | 33 done_(false), |
| 68 running_(false) { | 34 running_(false) { |
| 69 RegisterAsObserver(source); | 35 RegisterAsObserver(source); |
| 70 } | 36 } |
| 71 | 37 |
| 72 TestNavigationObserver::~TestNavigationObserver() { | 38 TestNavigationObserver::~TestNavigationObserver() { |
| 73 } | 39 } |
| 74 | 40 |
| 75 void TestNavigationObserver::WaitForObservation( | 41 void TestNavigationObserver::WaitForObservation( |
| 76 const base::Closure& wait_loop_callback, | 42 const base::Closure& wait_loop_callback, |
| 77 const base::Closure& done_callback) { | 43 const base::Closure& done_callback) { |
| 78 if (done_) | 44 if (done_) |
| 79 return; | 45 return; |
| 80 | 46 |
| 81 EXPECT_FALSE(running_); | 47 EXPECT_FALSE(running_); |
| 82 running_ = true; | 48 running_ = true; |
| 83 done_callback_ = done_callback; | 49 done_callback_ = done_callback; |
| 84 wait_loop_callback.Run(); | 50 wait_loop_callback.Run(); |
| 85 EXPECT_TRUE(done_); | 51 EXPECT_TRUE(done_); |
| 86 } | 52 } |
| 87 | 53 |
| 88 void TestNavigationObserver::Wait() { | 54 void TestNavigationObserver::Wait() { |
| 89 base::RunLoop run_loop; | 55 base::RunLoop run_loop; |
| 90 WaitForObservation( | 56 WaitForObservation( |
| 91 base::Bind(&base::RunLoop::Run, base::Unretained(&run_loop)), | 57 base::Bind(&base::RunLoop::Run, base::Unretained(&run_loop)), |
| 92 GetQuitTaskForRunLoop(&run_loop)); | 58 GetQuitTaskForRunLoop(&run_loop)); |
| 93 } | 59 } |
| 94 | 60 |
| 95 TestNavigationObserver::TestNavigationObserver( | 61 TestNavigationObserver::TestNavigationObserver( |
| 96 JsInjectionReadyObserver* js_injection_ready_observer, | |
| 97 int number_of_navigations) | 62 int number_of_navigations) |
| 98 : navigation_started_(false), | 63 : navigation_started_(false), |
| 99 navigations_completed_(0), | 64 navigations_completed_(0), |
| 100 number_of_navigations_(number_of_navigations), | 65 number_of_navigations_(number_of_navigations), |
| 101 js_injection_ready_observer_(js_injection_ready_observer), | |
| 102 done_(false), | 66 done_(false), |
| 103 running_(false) { | 67 running_(false) { |
| 104 // When javascript injection is requested, register for RenderViewHost | |
| 105 // creation. | |
| 106 if (js_injection_ready_observer_) { | |
| 107 registrar_.Add(this, NOTIFICATION_RENDER_VIEW_HOST_CREATED, | |
| 108 NotificationService::AllSources()); | |
| 109 } | |
| 110 } | 68 } |
| 111 | 69 |
| 112 void TestNavigationObserver::RegisterAsObserver( | 70 void TestNavigationObserver::RegisterAsObserver( |
| 113 const NotificationSource& source) { | 71 const NotificationSource& source) { |
| 114 // Register for events to know when we've finished loading the page and are | 72 // Register for events to know when we've finished loading the page and are |
| 115 // ready to quit the current message loop to return control back to the | 73 // ready to quit the current message loop to return control back to the |
| 116 // waiting test. | 74 // waiting test. |
| 117 registrar_.Add(this, NOTIFICATION_NAV_ENTRY_COMMITTED, source); | 75 registrar_.Add(this, NOTIFICATION_NAV_ENTRY_COMMITTED, source); |
| 118 registrar_.Add(this, NOTIFICATION_LOAD_START, source); | 76 registrar_.Add(this, NOTIFICATION_LOAD_START, source); |
| 119 registrar_.Add(this, NOTIFICATION_LOAD_STOP, source); | 77 registrar_.Add(this, NOTIFICATION_LOAD_STOP, source); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 131 if (navigation_started_ && | 89 if (navigation_started_ && |
| 132 ++navigations_completed_ == number_of_navigations_) { | 90 ++navigations_completed_ == number_of_navigations_) { |
| 133 navigation_started_ = false; | 91 navigation_started_ = false; |
| 134 done_ = true; | 92 done_ = true; |
| 135 if (running_) { | 93 if (running_) { |
| 136 running_ = false; | 94 running_ = false; |
| 137 done_callback_.Run(); | 95 done_callback_.Run(); |
| 138 } | 96 } |
| 139 } | 97 } |
| 140 break; | 98 break; |
| 141 case NOTIFICATION_RENDER_VIEW_HOST_CREATED: | |
| 142 rvho_send_js_.reset(new RVHOSendJS( | |
| 143 Source<RenderViewHost>(source).ptr(), | |
| 144 js_injection_ready_observer_)); | |
| 145 break; | |
| 146 default: | 99 default: |
| 147 NOTREACHED(); | 100 NOTREACHED(); |
| 148 } | 101 } |
| 149 } | 102 } |
| 150 | 103 |
| 151 } // namespace content | 104 } // namespace content |
| OLD | NEW |