| 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 "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/browser/notification_types.h" | 10 #include "content/public/browser/notification_types.h" |
| 11 #include "content/public/browser/render_view_host_observer.h" | 11 #include "content/public/browser/render_view_host_observer.h" |
| 12 #include "content/public/test/js_injection_ready_observer.h" | 12 #include "content/public/test/js_injection_ready_observer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 19 // Allow some pending tasks and up to |num_deferrals| generations to complete. |
| 20 static void DeferQuitWithID(const MessageLoop::RunID& run_id, |
| 21 int num_deferrals) { |
| 22 if (num_deferrals <= 0) { |
| 23 MessageLoop::current()->QuitNowWithID(run_id); |
| 24 } else { |
| 25 MessageLoop::current()->PostTask( |
| 26 FROM_HERE, base::Bind(&DeferQuitWithID, run_id, num_deferrals - 1)); |
| 27 } |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 17 // This class observes |render_view_host| and calls OnJsInjectionReady() of | 32 // This class observes |render_view_host| and calls OnJsInjectionReady() of |
| 18 // |js_injection_ready_observer| when the time is right to inject JavaScript | 33 // |js_injection_ready_observer| when the time is right to inject JavaScript |
| 19 // into the page. | 34 // into the page. |
| 20 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { | 35 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { |
| 21 public: | 36 public: |
| 22 RVHOSendJS(RenderViewHost* render_view_host, | 37 RVHOSendJS(RenderViewHost* render_view_host, |
| 23 JsInjectionReadyObserver* js_injection_ready_observer) | 38 JsInjectionReadyObserver* js_injection_ready_observer) |
| 24 : RenderViewHostObserver(render_view_host), | 39 : RenderViewHostObserver(render_view_host), |
| 25 js_injection_ready_observer_(js_injection_ready_observer) { | 40 js_injection_ready_observer_(js_injection_ready_observer) { |
| 26 } | 41 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return; | 92 return; |
| 78 | 93 |
| 79 EXPECT_FALSE(running_); | 94 EXPECT_FALSE(running_); |
| 80 running_ = true; | 95 running_ = true; |
| 81 done_callback_ = done_callback; | 96 done_callback_ = done_callback; |
| 82 wait_loop_callback.Run(); | 97 wait_loop_callback.Run(); |
| 83 EXPECT_TRUE(done_); | 98 EXPECT_TRUE(done_); |
| 84 } | 99 } |
| 85 | 100 |
| 86 void TestNavigationObserver::Wait() { | 101 void TestNavigationObserver::Wait() { |
| 102 MessageLoop::RunID run_id = MessageLoop::current()->GetRunID(); |
| 103 // Number of times to repost QuitNow task to allow pending tasks to complete: |
| 104 const int num_quit_deferrals = 20; |
| 87 WaitForObservation( | 105 WaitForObservation( |
| 88 base::Bind(&MessageLoop::Run, | 106 base::Bind(&MessageLoop::RunWithID, |
| 89 base::Unretained(MessageLoop::current())), | 107 base::Unretained(MessageLoop::current()), run_id), |
| 90 base::Bind(&MessageLoop::Quit, | 108 base::Bind(&DeferQuitWithID, run_id, num_quit_deferrals)); |
| 91 base::Unretained(MessageLoop::current()))); | |
| 92 } | 109 } |
| 93 | 110 |
| 94 TestNavigationObserver::TestNavigationObserver( | 111 TestNavigationObserver::TestNavigationObserver( |
| 95 JsInjectionReadyObserver* js_injection_ready_observer, | 112 JsInjectionReadyObserver* js_injection_ready_observer, |
| 96 int number_of_navigations) | 113 int number_of_navigations) |
| 97 : navigation_started_(false), | 114 : navigation_started_(false), |
| 98 navigations_completed_(0), | 115 navigations_completed_(0), |
| 99 number_of_navigations_(number_of_navigations), | 116 number_of_navigations_(number_of_navigations), |
| 100 js_injection_ready_observer_(js_injection_ready_observer), | 117 js_injection_ready_observer_(js_injection_ready_observer), |
| 101 done_(false), | 118 done_(false), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 124 switch (type) { | 141 switch (type) { |
| 125 case NOTIFICATION_NAV_ENTRY_COMMITTED: | 142 case NOTIFICATION_NAV_ENTRY_COMMITTED: |
| 126 case NOTIFICATION_LOAD_START: | 143 case NOTIFICATION_LOAD_START: |
| 127 navigation_started_ = true; | 144 navigation_started_ = true; |
| 128 break; | 145 break; |
| 129 case NOTIFICATION_LOAD_STOP: | 146 case NOTIFICATION_LOAD_STOP: |
| 130 if (navigation_started_ && | 147 if (navigation_started_ && |
| 131 ++navigations_completed_ == number_of_navigations_) { | 148 ++navigations_completed_ == number_of_navigations_) { |
| 132 navigation_started_ = false; | 149 navigation_started_ = false; |
| 133 done_ = true; | 150 done_ = true; |
| 134 if (running_) | 151 if (running_) { |
| 152 running_ = false; |
| 135 done_callback_.Run(); | 153 done_callback_.Run(); |
| 154 } |
| 136 } | 155 } |
| 137 break; | 156 break; |
| 138 case NOTIFICATION_RENDER_VIEW_HOST_CREATED: | 157 case NOTIFICATION_RENDER_VIEW_HOST_CREATED: |
| 139 rvho_send_js_.reset(new RVHOSendJS( | 158 rvho_send_js_.reset(new RVHOSendJS( |
| 140 Source<RenderViewHost>(source).ptr(), | 159 Source<RenderViewHost>(source).ptr(), |
| 141 js_injection_ready_observer_)); | 160 js_injection_ready_observer_)); |
| 142 break; | 161 break; |
| 143 default: | 162 default: |
| 144 NOTREACHED(); | 163 NOTREACHED(); |
| 145 } | 164 } |
| 146 } | 165 } |
| 147 | 166 |
| 148 } // namespace content | 167 } // namespace content |
| OLD | NEW |