Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1078)

Side by Side Diff: content/test/test_navigation_observer.cc

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: darin feedback Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "content/public/browser/notification_service.h" 10 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_types.h" 11 #include "content/public/browser/notification_types.h"
11 #include "content/public/browser/render_view_host_observer.h" 12 #include "content/public/browser/render_view_host_observer.h"
12 #include "content/public/test/js_injection_ready_observer.h" 13 #include "content/public/test/js_injection_ready_observer.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace content { 16 namespace content {
16 17
18 namespace {
19
20 // Allow some pending tasks and up to |num_deferrals| generations to complete.
21 static void DeferredQuitRunLoop(
22 const base::WeakPtr<base::RunLoop>& run_loop,
23 int num_quit_deferrals) {
24 if (num_quit_deferrals <= 0) {
25 if (run_loop) run_loop->Quit();
jar (doing other things) 2012/06/23 02:43:19 (as commented in other file): When run_loop is nul
jbates 2012/06/25 20:04:57 Done.
26 } else {
27 MessageLoop::current()->PostTask(FROM_HERE,
28 base::Bind(&DeferredQuitRunLoop, run_loop, num_quit_deferrals - 1));
29 }
30 }
31
32 } // namespace
33
17 // This class observes |render_view_host| and calls OnJsInjectionReady() of 34 // This class observes |render_view_host| and calls OnJsInjectionReady() of
18 // |js_injection_ready_observer| when the time is right to inject JavaScript 35 // |js_injection_ready_observer| when the time is right to inject JavaScript
19 // into the page. 36 // into the page.
20 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { 37 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver {
21 public: 38 public:
22 RVHOSendJS(RenderViewHost* render_view_host, 39 RVHOSendJS(RenderViewHost* render_view_host,
23 JsInjectionReadyObserver* js_injection_ready_observer) 40 JsInjectionReadyObserver* js_injection_ready_observer)
24 : RenderViewHostObserver(render_view_host), 41 : RenderViewHostObserver(render_view_host),
25 js_injection_ready_observer_(js_injection_ready_observer) { 42 js_injection_ready_observer_(js_injection_ready_observer) {
26 } 43 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 return; 94 return;
78 95
79 EXPECT_FALSE(running_); 96 EXPECT_FALSE(running_);
80 running_ = true; 97 running_ = true;
81 done_callback_ = done_callback; 98 done_callback_ = done_callback;
82 wait_loop_callback.Run(); 99 wait_loop_callback.Run();
83 EXPECT_TRUE(done_); 100 EXPECT_TRUE(done_);
84 } 101 }
85 102
86 void TestNavigationObserver::Wait() { 103 void TestNavigationObserver::Wait() {
104 base::RunLoop run_loop;
105 // Number of times to repost QuitNow task to allow pending tasks to complete:
106 const int num_quit_deferrals = 5;
jar (doing other things) 2012/06/23 02:43:19 Is this a hack number guess, or is this meaningful
jbates 2012/06/25 20:04:57 It's approaching being meaningful. 20 worked but m
jar (doing other things) 2012/06/25 21:19:43 The larger number (20) should have no measurable i
jbates 2012/06/27 02:06:37 Docs done. Went with 10, because some perf tests m
87 WaitForObservation( 107 WaitForObservation(
88 base::Bind(&MessageLoop::Run, 108 base::Bind(&base::RunLoop::Run, run_loop.AsWeakPtr()),
89 base::Unretained(MessageLoop::current())), 109 base::Bind(&DeferredQuitRunLoop, run_loop.AsWeakPtr(),
90 base::Bind(&MessageLoop::Quit, 110 num_quit_deferrals));
91 base::Unretained(MessageLoop::current())));
92 } 111 }
93 112
94 TestNavigationObserver::TestNavigationObserver( 113 TestNavigationObserver::TestNavigationObserver(
95 JsInjectionReadyObserver* js_injection_ready_observer, 114 JsInjectionReadyObserver* js_injection_ready_observer,
96 int number_of_navigations) 115 int number_of_navigations)
97 : navigation_started_(false), 116 : navigation_started_(false),
98 navigations_completed_(0), 117 navigations_completed_(0),
99 number_of_navigations_(number_of_navigations), 118 number_of_navigations_(number_of_navigations),
100 js_injection_ready_observer_(js_injection_ready_observer), 119 js_injection_ready_observer_(js_injection_ready_observer),
101 done_(false), 120 done_(false),
(...skipping 22 matching lines...) Expand all
124 switch (type) { 143 switch (type) {
125 case NOTIFICATION_NAV_ENTRY_COMMITTED: 144 case NOTIFICATION_NAV_ENTRY_COMMITTED:
126 case NOTIFICATION_LOAD_START: 145 case NOTIFICATION_LOAD_START:
127 navigation_started_ = true; 146 navigation_started_ = true;
128 break; 147 break;
129 case NOTIFICATION_LOAD_STOP: 148 case NOTIFICATION_LOAD_STOP:
130 if (navigation_started_ && 149 if (navigation_started_ &&
131 ++navigations_completed_ == number_of_navigations_) { 150 ++navigations_completed_ == number_of_navigations_) {
132 navigation_started_ = false; 151 navigation_started_ = false;
133 done_ = true; 152 done_ = true;
134 if (running_) 153 if (running_) {
154 running_ = false;
135 done_callback_.Run(); 155 done_callback_.Run();
156 }
136 } 157 }
137 break; 158 break;
138 case NOTIFICATION_RENDER_VIEW_HOST_CREATED: 159 case NOTIFICATION_RENDER_VIEW_HOST_CREATED:
139 rvho_send_js_.reset(new RVHOSendJS( 160 rvho_send_js_.reset(new RVHOSendJS(
140 Source<RenderViewHost>(source).ptr(), 161 Source<RenderViewHost>(source).ptr(),
141 js_injection_ready_observer_)); 162 js_injection_ready_observer_));
142 break; 163 break;
143 default: 164 default:
144 NOTREACHED(); 165 NOTREACHED();
145 } 166 }
146 } 167 }
147 168
148 } // namespace content 169 } // namespace content
OLDNEW
« content/browser/browser_main_loop.cc ('K') | « content/browser/browser_main_loop.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698