| 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" | 12 #include "content/public/browser/render_view_host_observer.h" |
| 13 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/js_injection_ready_observer.h" | 14 #include "content/public/test/js_injection_ready_observer.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 namespace { | |
| 19 | |
| 20 // Allow some pending tasks up to |num_deferrals| generations to complete. | |
| 21 void DeferredQuitRunLoop(const base::Closure& quit_task, | |
| 22 int num_quit_deferrals) { | |
| 23 if (num_quit_deferrals <= 0) { | |
| 24 quit_task.Run(); | |
| 25 } else { | |
| 26 MessageLoop::current()->PostTask(FROM_HERE, | |
| 27 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 // This class observes |render_view_host| and calls OnJsInjectionReady() of | 19 // This class observes |render_view_host| and calls OnJsInjectionReady() of |
| 34 // |js_injection_ready_observer| when the time is right to inject JavaScript | 20 // |js_injection_ready_observer| when the time is right to inject JavaScript |
| 35 // into the page. | 21 // into the page. |
| 36 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { | 22 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { |
| 37 public: | 23 public: |
| 38 RVHOSendJS(RenderViewHost* render_view_host, | 24 RVHOSendJS(RenderViewHost* render_view_host, |
| 39 JsInjectionReadyObserver* js_injection_ready_observer) | 25 JsInjectionReadyObserver* js_injection_ready_observer) |
| 40 : RenderViewHostObserver(render_view_host), | 26 : RenderViewHostObserver(render_view_host), |
| 41 js_injection_ready_observer_(js_injection_ready_observer) { | 27 js_injection_ready_observer_(js_injection_ready_observer) { |
| 42 } | 28 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 80 |
| 95 EXPECT_FALSE(running_); | 81 EXPECT_FALSE(running_); |
| 96 running_ = true; | 82 running_ = true; |
| 97 done_callback_ = done_callback; | 83 done_callback_ = done_callback; |
| 98 wait_loop_callback.Run(); | 84 wait_loop_callback.Run(); |
| 99 EXPECT_TRUE(done_); | 85 EXPECT_TRUE(done_); |
| 100 } | 86 } |
| 101 | 87 |
| 102 void TestNavigationObserver::Wait() { | 88 void TestNavigationObserver::Wait() { |
| 103 base::RunLoop run_loop; | 89 base::RunLoop run_loop; |
| 104 // Number of times to repost Quit task to allow pending tasks to complete. See | |
| 105 // kNumQuitDeferrals in ui_test_utils.cc for explanation. | |
| 106 const int num_quit_deferrals = 10; | |
| 107 WaitForObservation( | 90 WaitForObservation( |
| 108 base::Bind(&base::RunLoop::Run, base::Unretained(&run_loop)), | 91 base::Bind(&base::RunLoop::Run, base::Unretained(&run_loop)), |
| 109 base::Bind(&DeferredQuitRunLoop, run_loop.QuitClosure(), | 92 content::GetQuitTaskForRunLoop(&run_loop)); |
| 110 num_quit_deferrals)); | |
| 111 } | 93 } |
| 112 | 94 |
| 113 TestNavigationObserver::TestNavigationObserver( | 95 TestNavigationObserver::TestNavigationObserver( |
| 114 JsInjectionReadyObserver* js_injection_ready_observer, | 96 JsInjectionReadyObserver* js_injection_ready_observer, |
| 115 int number_of_navigations) | 97 int number_of_navigations) |
| 116 : navigation_started_(false), | 98 : navigation_started_(false), |
| 117 navigations_completed_(0), | 99 navigations_completed_(0), |
| 118 number_of_navigations_(number_of_navigations), | 100 number_of_navigations_(number_of_navigations), |
| 119 js_injection_ready_observer_(js_injection_ready_observer), | 101 js_injection_ready_observer_(js_injection_ready_observer), |
| 120 done_(false), | 102 done_(false), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 rvho_send_js_.reset(new RVHOSendJS( | 142 rvho_send_js_.reset(new RVHOSendJS( |
| 161 Source<RenderViewHost>(source).ptr(), | 143 Source<RenderViewHost>(source).ptr(), |
| 162 js_injection_ready_observer_)); | 144 js_injection_ready_observer_)); |
| 163 break; | 145 break; |
| 164 default: | 146 default: |
| 165 NOTREACHED(); | 147 NOTREACHED(); |
| 166 } | 148 } |
| 167 } | 149 } |
| 168 | 150 |
| 169 } // namespace content | 151 } // namespace content |
| OLD | NEW |