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

Side by Side Diff: chrome/test/base/ui_test_utils.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 "chrome/test/base/ui_test_utils.h" 5 #include "chrome/test/base/ui_test_utils.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 using content::NavigationController; 84 using content::NavigationController;
85 using content::NavigationEntry; 85 using content::NavigationEntry;
86 using content::OpenURLParams; 86 using content::OpenURLParams;
87 using content::RenderViewHost; 87 using content::RenderViewHost;
88 using content::RenderWidgetHost; 88 using content::RenderWidgetHost;
89 using content::Referrer; 89 using content::Referrer;
90 using content::WebContents; 90 using content::WebContents;
91 91
92 static const int kDefaultWsPort = 8880; 92 static const int kDefaultWsPort = 8880;
93 93
94 // Number of times to repost a QuitNow task so that the MessageLoop finishes up
95 // pending tasks and tasks posted by those pending tasks without risking the
96 // potential hang behavior of MessageLoop::QuitWhenIdle.
97 static const int kNumQuitDeferrals = 5;
98
94 namespace ui_test_utils { 99 namespace ui_test_utils {
95 100
96 namespace { 101 namespace {
97 102
98 class DOMOperationObserver : public content::NotificationObserver, 103 class DOMOperationObserver : public content::NotificationObserver,
99 public content::WebContentsObserver { 104 public content::WebContentsObserver {
100 public: 105 public:
101 explicit DOMOperationObserver(RenderViewHost* render_view_host) 106 explicit DOMOperationObserver(RenderViewHost* render_view_host)
102 : content::WebContentsObserver( 107 : content::WebContentsObserver(
103 WebContents::FromRenderViewHost(render_view_host)), 108 WebContents::FromRenderViewHost(render_view_host)),
104 did_respond_(false) { 109 did_respond_(false) {
105 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE, 110 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
106 content::Source<RenderViewHost>(render_view_host)); 111 content::Source<RenderViewHost>(render_view_host));
107 ui_test_utils::RunMessageLoop(); 112 message_loop_runner_ = new MessageLoopRunner;
113 message_loop_runner_->Run();
108 } 114 }
109 115
110 virtual void Observe(int type, 116 virtual void Observe(int type,
111 const content::NotificationSource& source, 117 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE { 118 const content::NotificationDetails& details) OVERRIDE {
113 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE); 119 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
114 content::Details<DomOperationNotificationDetails> dom_op_details(details); 120 content::Details<DomOperationNotificationDetails> dom_op_details(details);
115 response_ = dom_op_details->json; 121 response_ = dom_op_details->json;
116 did_respond_ = true; 122 did_respond_ = true;
117 MessageLoopForUI::current()->Quit(); 123 message_loop_runner_->QuitNow();
118 } 124 }
119 125
120 // Overridden from content::WebContentsObserver: 126 // Overridden from content::WebContentsObserver:
121 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE { 127 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE {
122 MessageLoopForUI::current()->Quit(); 128 message_loop_runner_->QuitNow();
123 } 129 }
124 130
125 bool GetResponse(std::string* response) WARN_UNUSED_RESULT { 131 bool GetResponse(std::string* response) WARN_UNUSED_RESULT {
126 *response = response_; 132 *response = response_;
127 return did_respond_; 133 return did_respond_;
128 } 134 }
129 135
130 private: 136 private:
131 content::NotificationRegistrar registrar_; 137 content::NotificationRegistrar registrar_;
132 std::string response_; 138 std::string response_;
133 bool did_respond_; 139 bool did_respond_;
140 scoped_refptr<MessageLoopRunner> message_loop_runner_;
134 141
135 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); 142 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
136 }; 143 };
137 144
138 class FindInPageNotificationObserver : public content::NotificationObserver { 145 class FindInPageNotificationObserver : public content::NotificationObserver {
139 public: 146 public:
140 explicit FindInPageNotificationObserver(TabContents* parent_tab) 147 explicit FindInPageNotificationObserver(TabContents* parent_tab)
141 : parent_tab_(parent_tab), 148 : parent_tab_(parent_tab),
142 active_match_ordinal_(-1), 149 active_match_ordinal_(-1),
143 number_of_matches_(0) { 150 number_of_matches_(0) {
144 current_find_request_id_ = 151 current_find_request_id_ =
145 parent_tab->find_tab_helper()->current_find_request_id(); 152 parent_tab->find_tab_helper()->current_find_request_id();
146 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, 153 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
147 content::Source<WebContents>(parent_tab_->web_contents())); 154 content::Source<WebContents>(parent_tab_->web_contents()));
148 ui_test_utils::RunMessageLoop(); 155 message_loop_runner_ = new MessageLoopRunner;
156 message_loop_runner_->Run();
149 } 157 }
150 158
151 int active_match_ordinal() const { return active_match_ordinal_; } 159 int active_match_ordinal() const { return active_match_ordinal_; }
152 160
153 int number_of_matches() const { return number_of_matches_; } 161 int number_of_matches() const { return number_of_matches_; }
154 162
155 virtual void Observe(int type, const content::NotificationSource& source, 163 virtual void Observe(int type, const content::NotificationSource& source,
156 const content::NotificationDetails& details) { 164 const content::NotificationDetails& details) {
157 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) { 165 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
158 content::Details<FindNotificationDetails> find_details(details); 166 content::Details<FindNotificationDetails> find_details(details);
159 if (find_details->request_id() == current_find_request_id_) { 167 if (find_details->request_id() == current_find_request_id_) {
160 // We get multiple responses and one of those will contain the ordinal. 168 // We get multiple responses and one of those will contain the ordinal.
161 // This message comes to us before the final update is sent. 169 // This message comes to us before the final update is sent.
162 if (find_details->active_match_ordinal() > -1) 170 if (find_details->active_match_ordinal() > -1)
163 active_match_ordinal_ = find_details->active_match_ordinal(); 171 active_match_ordinal_ = find_details->active_match_ordinal();
164 if (find_details->final_update()) { 172 if (find_details->final_update()) {
165 number_of_matches_ = find_details->number_of_matches(); 173 number_of_matches_ = find_details->number_of_matches();
166 MessageLoopForUI::current()->Quit(); 174 message_loop_runner_->QuitNow();
167 } else { 175 } else {
168 DVLOG(1) << "Ignoring, since we only care about the final message"; 176 DVLOG(1) << "Ignoring, since we only care about the final message";
169 } 177 }
170 } 178 }
171 } else { 179 } else {
172 NOTREACHED(); 180 NOTREACHED();
173 } 181 }
174 } 182 }
175 183
176 private: 184 private:
177 content::NotificationRegistrar registrar_; 185 content::NotificationRegistrar registrar_;
178 TabContents* parent_tab_; 186 TabContents* parent_tab_;
179 // We will at some point (before final update) be notified of the ordinal and 187 // We will at some point (before final update) be notified of the ordinal and
180 // we need to preserve it so we can send it later. 188 // we need to preserve it so we can send it later.
181 int active_match_ordinal_; 189 int active_match_ordinal_;
182 int number_of_matches_; 190 int number_of_matches_;
183 // The id of the current find request, obtained from WebContents. Allows us 191 // The id of the current find request, obtained from WebContents. Allows us
184 // to monitor when the search completes. 192 // to monitor when the search completes.
185 int current_find_request_id_; 193 int current_find_request_id_;
194 scoped_refptr<MessageLoopRunner> message_loop_runner_;
186 195
187 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 196 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
188 }; 197 };
189 198
190 class InProcessJavaScriptExecutionController 199 class InProcessJavaScriptExecutionController
191 : public base::RefCounted<InProcessJavaScriptExecutionController>, 200 : public base::RefCounted<InProcessJavaScriptExecutionController>,
192 public JavaScriptExecutionController { 201 public JavaScriptExecutionController {
193 public: 202 public:
194 explicit InProcessJavaScriptExecutionController( 203 explicit InProcessJavaScriptExecutionController(
195 RenderViewHost* render_view_host) 204 RenderViewHost* render_view_host)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); 263 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
255 result->reset(reader.ReadToValue(json)); 264 result->reset(reader.ReadToValue(json));
256 if (!result->get()) { 265 if (!result->get()) {
257 DLOG(ERROR) << reader.GetErrorMessage(); 266 DLOG(ERROR) << reader.GetErrorMessage();
258 return false; 267 return false;
259 } 268 }
260 269
261 return true; 270 return true;
262 } 271 }
263 272
264 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id) { 273 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id,
265 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 274 const base::Closure& quit_task) {
275 MessageLoop::current()->PostTask(FROM_HERE,
276 MessageLoop::QuitWhenIdleClosure());
266 RunMessageLoop(); 277 RunMessageLoop();
267 content::BrowserThread::PostTask(thread_id, FROM_HERE, 278 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task);
268 MessageLoop::QuitClosure());
269 } 279 }
270 280
271 } // namespace 281 } // namespace
272 282
273 void RunMessageLoop() { 283 void RunMessageLoop() {
274 MessageLoop* loop = MessageLoop::current(); 284 base::RunLoop run_loop;
275 MessageLoopForUI* ui_loop = 285 RunRunLoop(run_loop.AsWeakPtr());
276 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ? 286 }
277 MessageLoopForUI::current() : NULL; 287
278 MessageLoop::ScopedNestableTaskAllower allow(loop); 288 void RunRunLoop(const base::WeakPtr<base::RunLoop>& run_loop) {
jar (doing other things) 2012/06/23 02:43:19 nit: I didn't like the RunRun... prefix. Perhaps
jbates 2012/06/25 20:04:57 Renamed to RunThisRunLoop. I tried the helper clas
279 if (ui_loop) { 289 CHECK(run_loop);
280 #if defined(USE_AURA) 290 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
281 ui_loop->Run(); 291 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
282 #elif defined(TOOLKIT_VIEWS) 292 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
283 views::AcceleratorHandler handler; 293 views::AcceleratorHandler handler;
284 ui_loop->RunWithDispatcher(&handler); 294 run_loop->set_dispatcher(&handler);
285 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
286 ui_loop->RunWithDispatcher(NULL);
287 #else
288 ui_loop->Run();
289 #endif 295 #endif
296 run_loop->Run();
290 } else { 297 } else {
291 loop->Run(); 298 run_loop->Run();
jar (doing other things) 2012/06/23 02:43:19 Line 296 and 298 can now be pulled out of the if b
jbates 2012/06/25 20:04:57 Done.
292 } 299 }
293 } 300 }
294 301
302 static void DeferredQuitRunLoop(
303 const base::WeakPtr<base::RunLoop>& run_loop,
304 int num_quit_deferrals) {
305 if (num_quit_deferrals <= 0) {
306 if (run_loop) run_loop->Quit();
jar (doing other things) 2012/06/23 02:43:19 Do we gain value by iterating and posting when we
jbates 2012/06/25 20:04:57 Done.
307 } else {
308 MessageLoop::current()->PostTask(FROM_HERE,
309 base::Bind(&DeferredQuitRunLoop, run_loop, num_quit_deferrals - 1));
310 }
311 }
312
313 // Quit the MessageLoop Run of the specified |run_id|.
jar (doing other things) 2012/06/23 02:43:19 nit: update comment "run_id"
jbates 2012/06/25 20:04:57 Done.
314 void QuitRunLoop(const base::WeakPtr<base::RunLoop>& run_loop) {
jar (doing other things) 2012/06/23 02:43:19 This name is not as jarring... but if you did enca
jbates 2012/06/25 20:04:57 Changed to QuitThisRunLoop to be consistent with R
315 DeferredQuitRunLoop(run_loop, kNumQuitDeferrals);
316 }
317
295 void RunAllPendingInMessageLoop() { 318 void RunAllPendingInMessageLoop() {
296 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 319 MessageLoop::current()->PostTask(FROM_HERE,
320 MessageLoop::QuitWhenIdleClosure());
297 ui_test_utils::RunMessageLoop(); 321 ui_test_utils::RunMessageLoop();
298 } 322 }
299 323
300 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) { 324 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) {
301 if (content::BrowserThread::CurrentlyOn(thread_id)) { 325 if (content::BrowserThread::CurrentlyOn(thread_id)) {
302 RunAllPendingInMessageLoop(); 326 RunAllPendingInMessageLoop();
303 return; 327 return;
304 } 328 }
305 content::BrowserThread::ID current_thread_id; 329 content::BrowserThread::ID current_thread_id;
306 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) { 330 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) {
307 NOTREACHED(); 331 NOTREACHED();
308 return; 332 return;
309 } 333 }
334
335 base::RunLoop run_loop;
310 content::BrowserThread::PostTask(thread_id, FROM_HERE, 336 content::BrowserThread::PostTask(thread_id, FROM_HERE,
311 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id)); 337 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
312 338 run_loop.QuitClosure()));
313 ui_test_utils::RunMessageLoop(); 339 ui_test_utils::RunRunLoop(run_loop.AsWeakPtr());
314 } 340 }
315 341
316 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 342 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
317 WebContents* web_contents = browser->GetActiveWebContents(); 343 WebContents* web_contents = browser->GetActiveWebContents();
318 if (!web_contents) 344 if (!web_contents)
319 return false; 345 return false;
320 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 346 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
321 if (!last_entry) 347 if (!last_entry)
322 return false; 348 return false;
323 title->assign(last_entry->GetTitleForDisplay("")); 349 title->assign(last_entry->GetTitleForDisplay(""));
324 return true; 350 return true;
325 } 351 }
326 352
327 void WaitForNavigations(NavigationController* controller, 353 void WaitForNavigations(NavigationController* controller,
328 int number_of_navigations) { 354 int number_of_navigations) {
329 content::TestNavigationObserver observer( 355 content::TestNavigationObserver observer(
330 content::Source<NavigationController>(controller), NULL, 356 content::Source<NavigationController>(controller), NULL,
331 number_of_navigations); 357 number_of_navigations);
358 base::RunLoop run_loop;
332 observer.WaitForObservation( 359 observer.WaitForObservation(
333 base::Bind(&ui_test_utils::RunMessageLoop), 360 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
334 base::Bind(&MessageLoop::Quit, 361 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
335 base::Unretained(MessageLoopForUI::current())));
336 } 362 }
337 363
338 void WaitForNewTab(Browser* browser) { 364 void WaitForNewTab(Browser* browser) {
339 WindowedNotificationObserver observer( 365 WindowedNotificationObserver observer(
340 chrome::NOTIFICATION_TAB_ADDED, 366 chrome::NOTIFICATION_TAB_ADDED,
341 content::Source<content::WebContentsDelegate>(browser)); 367 content::Source<content::WebContentsDelegate>(browser));
342 observer.Wait(); 368 observer.Wait();
343 } 369 }
344 370
345 void WaitForLoadStop(WebContents* tab) { 371 void WaitForLoadStop(WebContents* tab) {
(...skipping 29 matching lines...) Expand all
375 Browser::OpenURLOffTheRecord(profile, url); 401 Browser::OpenURLOffTheRecord(profile, url);
376 Browser* browser = browser::FindTabbedBrowser( 402 Browser* browser = browser::FindTabbedBrowser(
377 profile->GetOffTheRecordProfile(), false); 403 profile->GetOffTheRecordProfile(), false);
378 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1); 404 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1);
379 } 405 }
380 406
381 void NavigateToURL(browser::NavigateParams* params) { 407 void NavigateToURL(browser::NavigateParams* params) {
382 content::TestNavigationObserver observer( 408 content::TestNavigationObserver observer(
383 content::NotificationService::AllSources(), NULL, 1); 409 content::NotificationService::AllSources(), NULL, 1);
384 browser::Navigate(params); 410 browser::Navigate(params);
411 base::RunLoop run_loop;
385 observer.WaitForObservation( 412 observer.WaitForObservation(
386 base::Bind(&ui_test_utils::RunMessageLoop), 413 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
387 base::Bind(&MessageLoop::Quit, 414 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
388 base::Unretained(MessageLoopForUI::current())));
389
390 } 415 }
391 416
392 void NavigateToURL(Browser* browser, const GURL& url) { 417 void NavigateToURL(Browser* browser, const GURL& url) {
393 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, 418 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
394 BROWSER_TEST_WAIT_FOR_NAVIGATION); 419 BROWSER_TEST_WAIT_FOR_NAVIGATION);
395 } 420 }
396 421
397 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 422 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
398 // blocking until the |number_of_navigations| specified complete. 423 // blocking until the |number_of_navigations| specified complete.
399 // |disposition| indicates what tab the download occurs in, and 424 // |disposition| indicates what tab the download occurs in, and
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 << " Unable to wait for navigation to \"" << url.spec() 474 << " Unable to wait for navigation to \"" << url.spec()
450 << "\" because the new tab is not available yet"; 475 << "\" because the new tab is not available yet";
451 return; 476 return;
452 } else if ((disposition == CURRENT_TAB) || 477 } else if ((disposition == CURRENT_TAB) ||
453 (disposition == NEW_FOREGROUND_TAB) || 478 (disposition == NEW_FOREGROUND_TAB) ||
454 (disposition == SINGLETON_TAB)) { 479 (disposition == SINGLETON_TAB)) {
455 // The currently selected tab is the right one. 480 // The currently selected tab is the right one.
456 web_contents = browser->GetActiveWebContents(); 481 web_contents = browser->GetActiveWebContents();
457 } 482 }
458 if (disposition == CURRENT_TAB) { 483 if (disposition == CURRENT_TAB) {
484 base::RunLoop run_loop;
459 same_tab_observer.WaitForObservation( 485 same_tab_observer.WaitForObservation(
460 base::Bind(&ui_test_utils::RunMessageLoop), 486 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
461 base::Bind(&MessageLoop::Quit, 487 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
462 base::Unretained(MessageLoopForUI::current())));
463 return; 488 return;
464 } else if (web_contents) { 489 } else if (web_contents) {
465 NavigationController* controller = &web_contents->GetController(); 490 NavigationController* controller = &web_contents->GetController();
466 WaitForNavigations(controller, number_of_navigations); 491 WaitForNavigations(controller, number_of_navigations);
467 return; 492 return;
468 } 493 }
469 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" 494 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \""
470 << url.spec() << "\"" 495 << url.spec() << "\""
471 << " because we can't get the tab contents"; 496 << " because we can't get the tab contents";
472 } 497 }
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 int type, 721 int type,
697 const content::NotificationSource& source) { 722 const content::NotificationSource& source) {
698 content::NotificationRegistrar registrar; 723 content::NotificationRegistrar registrar;
699 registrar.Add(observer, type, source); 724 registrar.Add(observer, type, source);
700 RunMessageLoop(); 725 RunMessageLoop();
701 } 726 }
702 727
703 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 728 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
704 if (model->IsLoaded()) 729 if (model->IsLoaded())
705 return; 730 return;
706 BookmarkLoadObserver observer; 731 base::RunLoop run_loop;
732 BookmarkLoadObserver observer(run_loop.AsWeakPtr());
707 model->AddObserver(&observer); 733 model->AddObserver(&observer);
708 RunMessageLoop(); 734 RunRunLoop(run_loop.AsWeakPtr());
709 model->RemoveObserver(&observer); 735 model->RemoveObserver(&observer);
710 ASSERT_TRUE(model->IsLoaded()); 736 ASSERT_TRUE(model->IsLoaded());
711 } 737 }
712 738
713 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) { 739 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) {
714 if (service->loaded()) 740 if (service->loaded())
715 return; 741 return;
716 service->Load(); 742 service->Load();
717 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); 743 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
718 ASSERT_TRUE(service->loaded()); 744 ASSERT_TRUE(service->loaded());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 782
757 bool SendKeyPressSync(const Browser* browser, 783 bool SendKeyPressSync(const Browser* browser,
758 ui::KeyboardCode key, 784 ui::KeyboardCode key,
759 bool control, 785 bool control,
760 bool shift, 786 bool shift,
761 bool alt, 787 bool alt,
762 bool command) { 788 bool command) {
763 gfx::NativeWindow window = NULL; 789 gfx::NativeWindow window = NULL;
764 if (!GetNativeWindow(browser, &window)) 790 if (!GetNativeWindow(browser, &window))
765 return false; 791 return false;
766 792 scoped_refptr<MessageLoopRunner> runner =
793 new MessageLoopRunner;
jar (doing other things) 2012/06/23 02:43:19 nit: no need to wrap.
jbates 2012/06/25 20:04:57 Done.
767 bool result; 794 bool result;
768 result = ui_controls::SendKeyPressNotifyWhenDone( 795 result = ui_controls::SendKeyPressNotifyWhenDone(
769 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 796 window, key, control, shift, alt, command, runner->QuitNowClosure());
770 #if defined(OS_WIN) 797 #if defined(OS_WIN)
771 if (!result && BringBrowserWindowToFront(browser)) { 798 if (!result && BringBrowserWindowToFront(browser)) {
772 result = ui_controls::SendKeyPressNotifyWhenDone( 799 result = ui_controls::SendKeyPressNotifyWhenDone(
773 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 800 window, key, control, shift, alt, command, runner->QuitNowClosure());
774 } 801 }
775 #endif 802 #endif
776 if (!result) { 803 if (!result) {
777 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; 804 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
778 return false; 805 return false;
779 } 806 }
780 807
781 // Run the message loop. It'll stop running when either the key was received 808 // Run the message loop. It'll stop running when either the key was received
782 // or the test timed out (in which case testing::Test::HasFatalFailure should 809 // or the test timed out (in which case testing::Test::HasFatalFailure should
783 // be set). 810 // be set).
784 RunMessageLoop(); 811 runner->Run();
785 return !testing::Test::HasFatalFailure(); 812 return !testing::Test::HasFatalFailure();
786 } 813 }
787 814
788 bool SendKeyPressAndWait(const Browser* browser, 815 bool SendKeyPressAndWait(const Browser* browser,
789 ui::KeyboardCode key, 816 ui::KeyboardCode key,
790 bool control, 817 bool control,
791 bool shift, 818 bool shift,
792 bool alt, 819 bool alt,
793 bool command, 820 bool command,
794 int type, 821 int type,
795 const content::NotificationSource& source) { 822 const content::NotificationSource& source) {
796 WindowedNotificationObserver observer(type, source); 823 WindowedNotificationObserver observer(type, source);
797 824
798 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) 825 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
799 return false; 826 return false;
800 827
801 observer.Wait(); 828 observer.Wait();
802 return !testing::Test::HasFatalFailure(); 829 return !testing::Test::HasFatalFailure();
803 } 830 }
804 831
805 bool SendMouseMoveSync(const gfx::Point& location) { 832 bool SendMouseMoveSync(const gfx::Point& location) {
806 if (!ui_controls::SendMouseMoveNotifyWhenDone(location.x(), location.y(), 833 scoped_refptr<MessageLoopRunner> runner =
807 MessageLoop::QuitClosure())) { 834 new MessageLoopRunner;
jar (doing other things) 2012/06/23 02:43:19 nit: un-wrap (here and below in file, like line 84
jbates 2012/06/25 20:04:57 Done.
835 if (!ui_controls::SendMouseMoveNotifyWhenDone(
836 location.x(), location.y(), runner->QuitNowClosure())) {
808 return false; 837 return false;
809 } 838 }
810 RunMessageLoop(); 839 runner->Run();
811 return !testing::Test::HasFatalFailure(); 840 return !testing::Test::HasFatalFailure();
812 } 841 }
813 842
814 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) { 843 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
844 scoped_refptr<MessageLoopRunner> runner =
845 new MessageLoopRunner;
815 if (!ui_controls::SendMouseEventsNotifyWhenDone( 846 if (!ui_controls::SendMouseEventsNotifyWhenDone(
816 type, state, MessageLoop::QuitClosure())) { 847 type, state, runner->QuitNowClosure())) {
817 return false; 848 return false;
818 } 849 }
819 RunMessageLoop(); 850 runner->Run();
820 return !testing::Test::HasFatalFailure(); 851 return !testing::Test::HasFatalFailure();
821 } 852 }
822 853
823 TimedMessageLoopRunner::TimedMessageLoopRunner() 854 MessageLoopRunner::MessageLoopRunner() {
824 : loop_(new MessageLoopForUI()),
825 owned_(true),
826 quit_loop_invoked_(false) {
827 } 855 }
828 856
829 TimedMessageLoopRunner::~TimedMessageLoopRunner() { 857 MessageLoopRunner::~MessageLoopRunner() {
830 if (owned_)
831 delete loop_;
832 } 858 }
833 859
834 void TimedMessageLoopRunner::RunFor(int ms) { 860 void MessageLoopRunner::Run() {
835 QuitAfter(ms); 861 ui_test_utils::RunRunLoop(run_loop_.AsWeakPtr());
836 quit_loop_invoked_ = false;
837 loop_->Run();
838 } 862 }
839 863
840 void TimedMessageLoopRunner::Quit() { 864 base::Closure MessageLoopRunner::QuitNowClosure() {
841 quit_loop_invoked_ = true; 865 return base::Bind(&MessageLoopRunner::QuitNow, this);
842 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
843 } 866 }
844 867
845 void TimedMessageLoopRunner::QuitAfter(int ms) { 868 void MessageLoopRunner::QuitNow() {
846 quit_loop_invoked_ = true; 869 ui_test_utils::QuitRunLoop(run_loop_.AsWeakPtr());
847 loop_->PostDelayedTask(
848 FROM_HERE,
849 MessageLoop::QuitClosure(),
850 base::TimeDelta::FromMilliseconds(ms));
851 } 870 }
852 871
853 TestWebSocketServer::TestWebSocketServer() 872 TestWebSocketServer::TestWebSocketServer()
854 : started_(false), 873 : started_(false),
855 port_(kDefaultWsPort), 874 port_(kDefaultWsPort),
856 secure_(false) { 875 secure_(false) {
857 #if defined(OS_POSIX) 876 #if defined(OS_POSIX)
858 process_group_id_ = base::kNullProcessHandle; 877 process_group_id_ = base::kNullProcessHandle;
859 #endif 878 #endif
860 } 879 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 registrar_.Add(this, notification_type, source); 1024 registrar_.Add(this, notification_type, source);
1006 } 1025 }
1007 1026
1008 WindowedNotificationObserver::~WindowedNotificationObserver() {} 1027 WindowedNotificationObserver::~WindowedNotificationObserver() {}
1009 1028
1010 void WindowedNotificationObserver::Wait() { 1029 void WindowedNotificationObserver::Wait() {
1011 if (seen_) 1030 if (seen_)
1012 return; 1031 return;
1013 1032
1014 running_ = true; 1033 running_ = true;
1015 ui_test_utils::RunMessageLoop(); 1034 message_loop_runner_ = new MessageLoopRunner;
1035 message_loop_runner_->Run();
1016 EXPECT_TRUE(seen_); 1036 EXPECT_TRUE(seen_);
1017 } 1037 }
1018 1038
1019 void WindowedNotificationObserver::Observe( 1039 void WindowedNotificationObserver::Observe(
1020 int type, 1040 int type,
1021 const content::NotificationSource& source, 1041 const content::NotificationSource& source,
1022 const content::NotificationDetails& details) { 1042 const content::NotificationDetails& details) {
1023 source_ = source; 1043 source_ = source;
1024 details_ = details; 1044 details_ = details;
1025 seen_ = true; 1045 seen_ = true;
1026 if (!running_) 1046 if (!running_)
1027 return; 1047 return;
1028 1048
1029 MessageLoopForUI::current()->Quit(); 1049 message_loop_runner_->QuitNow();
1030 running_ = false; 1050 running_ = false;
1031 } 1051 }
1032 1052
1033 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver( 1053 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver(
1034 const content::NotificationSource& source) 1054 const content::NotificationSource& source)
1035 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source), 1055 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source),
1036 added_tab_(NULL) { 1056 added_tab_(NULL) {
1037 } 1057 }
1038 1058
1039 void WindowedTabAddedNotificationObserver::Observe( 1059 void WindowedTabAddedNotificationObserver::Observe(
(...skipping 30 matching lines...) Expand all
1070 expected_titles_.push_back(expected_title); 1090 expected_titles_.push_back(expected_title);
1071 } 1091 }
1072 1092
1073 TitleWatcher::~TitleWatcher() { 1093 TitleWatcher::~TitleWatcher() {
1074 } 1094 }
1075 1095
1076 const string16& TitleWatcher::WaitAndGetTitle() { 1096 const string16& TitleWatcher::WaitAndGetTitle() {
1077 if (expected_title_observed_) 1097 if (expected_title_observed_)
1078 return observed_title_; 1098 return observed_title_;
1079 quit_loop_on_observation_ = true; 1099 quit_loop_on_observation_ = true;
1080 ui_test_utils::RunMessageLoop(); 1100 message_loop_runner_ = new MessageLoopRunner;
1101 message_loop_runner_->Run();
1081 return observed_title_; 1102 return observed_title_;
1082 } 1103 }
1083 1104
1084 void TitleWatcher::Observe(int type, 1105 void TitleWatcher::Observe(int type,
1085 const content::NotificationSource& source, 1106 const content::NotificationSource& source,
1086 const content::NotificationDetails& details) { 1107 const content::NotificationDetails& details) {
1087 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { 1108 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
1088 WebContents* source_contents = content::Source<WebContents>(source).ptr(); 1109 WebContents* source_contents = content::Source<WebContents>(source).ptr();
1089 ASSERT_EQ(web_contents_, source_contents); 1110 ASSERT_EQ(web_contents_, source_contents);
1090 } else if (type == content::NOTIFICATION_LOAD_STOP) { 1111 } else if (type == content::NOTIFICATION_LOAD_STOP) {
1091 NavigationController* controller = 1112 NavigationController* controller =
1092 content::Source<NavigationController>(source).ptr(); 1113 content::Source<NavigationController>(source).ptr();
1093 ASSERT_EQ(&web_contents_->GetController(), controller); 1114 ASSERT_EQ(&web_contents_->GetController(), controller);
1094 } else { 1115 } else {
1095 FAIL() << "Unexpected notification received."; 1116 FAIL() << "Unexpected notification received.";
1096 } 1117 }
1097 1118
1098 std::vector<string16>::const_iterator it = 1119 std::vector<string16>::const_iterator it =
1099 std::find(expected_titles_.begin(), 1120 std::find(expected_titles_.begin(),
1100 expected_titles_.end(), 1121 expected_titles_.end(),
1101 web_contents_->GetTitle()); 1122 web_contents_->GetTitle());
1102 if (it == expected_titles_.end()) 1123 if (it == expected_titles_.end())
1103 return; 1124 return;
1104 observed_title_ = *it; 1125 observed_title_ = *it;
1105 expected_title_observed_ = true; 1126 expected_title_observed_ = true;
1106 if (quit_loop_on_observation_) 1127 if (quit_loop_on_observation_) {
1107 MessageLoopForUI::current()->Quit(); 1128 // Only call Quit once, on first Observe:
1129 quit_loop_on_observation_ = false;
1130 message_loop_runner_->QuitNow();
1131 }
1108 } 1132 }
1109 1133
1110 BrowserAddedObserver::BrowserAddedObserver() 1134 BrowserAddedObserver::BrowserAddedObserver()
1111 : notification_observer_( 1135 : notification_observer_(
1112 chrome::NOTIFICATION_BROWSER_OPENED, 1136 chrome::NOTIFICATION_BROWSER_OPENED,
1113 content::NotificationService::AllSources()) { 1137 content::NotificationService::AllSources()) {
1114 original_browsers_.insert(BrowserList::begin(), BrowserList::end()); 1138 original_browsers_.insert(BrowserList::begin(), BrowserList::end());
1115 } 1139 }
1116 1140
1117 BrowserAddedObserver::~BrowserAddedObserver() { 1141 BrowserAddedObserver::~BrowserAddedObserver() {
(...skipping 14 matching lines...) Expand all
1132 DOMMessageQueue::~DOMMessageQueue() {} 1156 DOMMessageQueue::~DOMMessageQueue() {}
1133 1157
1134 void DOMMessageQueue::Observe(int type, 1158 void DOMMessageQueue::Observe(int type,
1135 const content::NotificationSource& source, 1159 const content::NotificationSource& source,
1136 const content::NotificationDetails& details) { 1160 const content::NotificationDetails& details) {
1137 content::Details<DomOperationNotificationDetails> dom_op_details(details); 1161 content::Details<DomOperationNotificationDetails> dom_op_details(details);
1138 content::Source<RenderViewHost> sender(source); 1162 content::Source<RenderViewHost> sender(source);
1139 message_queue_.push(dom_op_details->json); 1163 message_queue_.push(dom_op_details->json);
1140 if (waiting_for_message_) { 1164 if (waiting_for_message_) {
1141 waiting_for_message_ = false; 1165 waiting_for_message_ = false;
1142 MessageLoopForUI::current()->Quit(); 1166 message_loop_runner_->QuitNow();
1143 } 1167 }
1144 } 1168 }
1145 1169
1146 void DOMMessageQueue::ClearQueue() { 1170 void DOMMessageQueue::ClearQueue() {
1147 message_queue_ = std::queue<std::string>(); 1171 message_queue_ = std::queue<std::string>();
1148 } 1172 }
1149 1173
1150 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1174 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1151 if (message_queue_.empty()) { 1175 if (message_queue_.empty()) {
1152 waiting_for_message_ = true; 1176 waiting_for_message_ = true;
1153 // This will be quit when a new message comes in. 1177 // This will be quit when a new message comes in.
1154 RunMessageLoop(); 1178 message_loop_runner_ = new MessageLoopRunner;
1179 message_loop_runner_->Run();
1155 } 1180 }
1156 // The queue should not be empty, unless we were quit because of a timeout. 1181 // The queue should not be empty, unless we were quit because of a timeout.
1157 if (message_queue_.empty()) 1182 if (message_queue_.empty())
1158 return false; 1183 return false;
1159 if (message) 1184 if (message)
1160 *message = message_queue_.front(); 1185 *message = message_queue_.front();
1161 return true; 1186 return true;
1162 } 1187 }
1163 1188
1164 // Coordinates taking snapshots of a |RenderWidget|. 1189 // Coordinates taking snapshots of a |RenderWidget|.
1165 class SnapshotTaker { 1190 class SnapshotTaker {
1166 public: 1191 public:
1167 SnapshotTaker() : bitmap_(NULL) {} 1192 SnapshotTaker() : bitmap_(NULL) {}
1168 1193
1169 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1194 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1170 const gfx::Size& page_size, 1195 const gfx::Size& page_size,
1171 const gfx::Size& desired_size, 1196 const gfx::Size& desired_size,
1172 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1197 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1173 bitmap_ = bitmap; 1198 bitmap_ = bitmap;
1174 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator(); 1199 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
1175 generator->MonitorRenderer(rwh, true); 1200 generator->MonitorRenderer(rwh, true);
1176 snapshot_taken_ = false; 1201 snapshot_taken_ = false;
1177 generator->AskForSnapshot( 1202 generator->AskForSnapshot(
1178 rwh, 1203 rwh,
1179 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)), 1204 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)),
1180 page_size, 1205 page_size,
1181 desired_size); 1206 desired_size);
1182 ui_test_utils::RunMessageLoop(); 1207 message_loop_runner_ = new MessageLoopRunner;
1208 message_loop_runner_->Run();
1183 return snapshot_taken_; 1209 return snapshot_taken_;
1184 } 1210 }
1185 1211
1186 bool TakeEntirePageSnapshot(RenderViewHost* rvh, 1212 bool TakeEntirePageSnapshot(RenderViewHost* rvh,
1187 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1213 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1188 const wchar_t* script = 1214 const wchar_t* script =
1189 L"window.domAutomationController.send(" 1215 L"window.domAutomationController.send("
1190 L" JSON.stringify([document.width, document.height]))"; 1216 L" JSON.stringify([document.width, document.height]))";
1191 std::string json; 1217 std::string json;
1192 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( 1218 if (!ui_test_utils::ExecuteJavaScriptAndExtractString(
(...skipping 14 matching lines...) Expand all
1207 // Take the snapshot. 1233 // Take the snapshot.
1208 gfx::Size page_size(width, height); 1234 gfx::Size page_size(width, height);
1209 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap); 1235 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap);
1210 } 1236 }
1211 1237
1212 private: 1238 private:
1213 // Called when the ThumbnailGenerator has taken the snapshot. 1239 // Called when the ThumbnailGenerator has taken the snapshot.
1214 void OnSnapshotTaken(const SkBitmap& bitmap) { 1240 void OnSnapshotTaken(const SkBitmap& bitmap) {
1215 *bitmap_ = bitmap; 1241 *bitmap_ = bitmap;
1216 snapshot_taken_ = true; 1242 snapshot_taken_ = true;
1217 MessageLoop::current()->Quit(); 1243 message_loop_runner_->QuitNow();
1218 } 1244 }
1219 1245
1220 SkBitmap* bitmap_; 1246 SkBitmap* bitmap_;
1221 // Whether the snapshot was actually taken and received by this SnapshotTaker. 1247 // Whether the snapshot was actually taken and received by this SnapshotTaker.
1222 // This will be false if the test times out. 1248 // This will be false if the test times out.
1223 bool snapshot_taken_; 1249 bool snapshot_taken_;
1250 scoped_refptr<MessageLoopRunner> message_loop_runner_;
1224 1251
1225 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker); 1252 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker);
1226 }; 1253 };
1227 1254
1228 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1255 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1229 const gfx::Size& page_size, 1256 const gfx::Size& page_size,
1230 SkBitmap* bitmap) { 1257 SkBitmap* bitmap) {
1231 DCHECK(bitmap); 1258 DCHECK(bitmap);
1232 SnapshotTaker taker; 1259 SnapshotTaker taker;
1233 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1260 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1234 } 1261 }
1235 1262
1236 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1263 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1237 DCHECK(bitmap); 1264 DCHECK(bitmap);
1238 SnapshotTaker taker; 1265 SnapshotTaker taker;
1239 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1266 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1240 } 1267 }
1241 1268
1242 void OverrideGeolocation(double latitude, double longitude) { 1269 void OverrideGeolocation(double latitude, double longitude) {
1243 content::Geoposition position; 1270 content::Geoposition position;
1244 position.latitude = latitude; 1271 position.latitude = latitude;
1245 position.longitude = longitude; 1272 position.longitude = longitude;
1246 position.altitude = 0.; 1273 position.altitude = 0.;
1247 position.accuracy = 0.; 1274 position.accuracy = 0.;
1248 position.timestamp = base::Time::Now(); 1275 position.timestamp = base::Time::Now();
1276 scoped_refptr<MessageLoopRunner> runner =
1277 new MessageLoopRunner;
1249 content::OverrideLocationForTesting(position, 1278 content::OverrideLocationForTesting(position,
1250 base::Bind(MessageLoop::QuitClosure())); 1279 runner->QuitNowClosure());
1251 RunMessageLoop(); 1280 runner->Run();
1252 } 1281 }
1253 1282
1254 namespace internal { 1283 namespace internal {
1255 1284
1256 void ClickTask(ui_controls::MouseButton button, 1285 void ClickTask(ui_controls::MouseButton button,
1257 int state, 1286 int state,
1258 const base::Closure& followup) { 1287 const base::Closure& followup) {
1259 if (!followup.is_null()) 1288 if (!followup.is_null())
1260 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 1289 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1261 else 1290 else
1262 ui_controls::SendMouseEvents(button, state); 1291 ui_controls::SendMouseEvents(button, state);
1263 } 1292 }
1264 1293
1265 } // namespace internal 1294 } // namespace internal
1266 } // namespace ui_test_utils 1295 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698