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

Side by Side Diff: chrome/test/base/ui_test_utils.h

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: fetch/merge, no change 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 #ifndef CHROME_TEST_BASE_UI_TEST_UTILS_H_ 5 #ifndef CHROME_TEST_BASE_UI_TEST_UTILS_H_
6 #define CHROME_TEST_BASE_UI_TEST_UTILS_H_ 6 #define CHROME_TEST_BASE_UI_TEST_UTILS_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <queue> 10 #include <queue>
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER | 89 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER |
90 BROWSER_TEST_WAIT_FOR_TAB | 90 BROWSER_TEST_WAIT_FOR_TAB |
91 BROWSER_TEST_WAIT_FOR_NAVIGATION 91 BROWSER_TEST_WAIT_FOR_NAVIGATION
92 }; 92 };
93 93
94 // Turns on nestable tasks, runs the message loop, then resets nestable tasks 94 // Turns on nestable tasks, runs the message loop, then resets nestable tasks
95 // to what they were originally. Prefer this over MessageLoop::Run for in 95 // to what they were originally. Prefer this over MessageLoop::Run for in
96 // process browser tests that need to block until a condition is met. 96 // process browser tests that need to block until a condition is met.
97 void RunMessageLoop(); 97 void RunMessageLoop();
98 98
99 // Variant of RunMessageLoop that takes RunLoop.
100 void RunRunLoop(const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
101
102 // Quit the given RunLoop. Allows a few generations of pending tasks to run.
103 void QuitRunLoop(const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
104
105 base::Closure QuitRunLoopClosure(
106 const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
107
99 // Turns on nestable tasks, runs all pending tasks in the message loop, 108 // Turns on nestable tasks, runs all pending tasks in the message loop,
100 // then resets nestable tasks to what they were originally. Prefer this 109 // then resets nestable tasks to what they were originally. Prefer this
101 // over MessageLoop::RunAllPending for in process browser tests to run 110 // over MessageLoop::RunAllPending for in process browser tests to run
102 // all pending tasks. 111 // all pending tasks.
103 void RunAllPendingInMessageLoop(); 112 void RunAllPendingInMessageLoop();
104 113
105 // Blocks the current thread until all the pending messages in the loop of the 114 // Blocks the current thread until all the pending messages in the loop of the
106 // thread |thread_id| have been processed. 115 // thread |thread_id| have been processed.
107 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id); 116 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id);
108 117
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 const content::NotificationSource& source) 303 const content::NotificationSource& source)
295 WARN_UNUSED_RESULT; 304 WARN_UNUSED_RESULT;
296 305
297 // Sends a move event blocking until received. Returns true if the event was 306 // Sends a move event blocking until received. Returns true if the event was
298 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone, 307 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone,
299 // see it for details. 308 // see it for details.
300 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT; 309 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT;
301 bool SendMouseEventsSync(ui_controls::MouseButton type, 310 bool SendMouseEventsSync(ui_controls::MouseButton type,
302 int state) WARN_UNUSED_RESULT; 311 int state) WARN_UNUSED_RESULT;
303 312
304 // Run a message loop only for the specified amount of time. 313 // Helper class to Run and Quit the message loop. Run and Quit can only happen
305 class TimedMessageLoopRunner { 314 // once per instance. Make a new instance for each use. Calling Quit after Run
315 // has returned is safe and has no effect.
316 class MessageLoopRunner
317 : public base::RefCounted<MessageLoopRunner> {
306 public: 318 public:
307 // Create new MessageLoopForUI and attach to it. 319 MessageLoopRunner();
308 TimedMessageLoopRunner();
309 320
310 // Attach to an existing message loop. 321 // Run the current MessageLoop. This can't be called recursively.
311 explicit TimedMessageLoopRunner(MessageLoop* loop) 322 void Run();
312 : loop_(loop), owned_(false), quit_loop_invoked_(false) {}
313 323
314 ~TimedMessageLoopRunner(); 324 // Quit the matching call to Run (nested MessageLoops are unaffected).
325 void QuitNow();
315 326
316 // Run the message loop for ms milliseconds. 327 // Hand this closure off to code that uses callbacks to notify completion.
317 void RunFor(int ms); 328 // Example:
318 329 // scoped_refptr<MessageLoopRunner> runner =
319 // Post Quit task to the message loop. 330 // new MessageLoopRunner;
320 void Quit(); 331 // // Must be no nested MessageLoop::Run calls during kick_off_some_api:
321 332 // kick_off_some_api(runner.QuitNowClosure());
322 // Post delayed Quit task to the message loop. 333 // runner.Run();
323 void QuitAfter(int ms); 334 base::Closure QuitNowClosure();
324
325 bool WasTimedOut() const {
326 return !quit_loop_invoked_;
327 }
328 335
329 private: 336 private:
330 MessageLoop* loop_; 337 friend class base::RefCounted<MessageLoopRunner>;
331 bool owned_; 338 ~MessageLoopRunner();
332 bool quit_loop_invoked_;
333 339
334 DISALLOW_COPY_AND_ASSIGN(TimedMessageLoopRunner); 340 MessageLoop::RunLoop run_loop_;
341
342 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner);
335 }; 343 };
336 344
337 // This is a utility class for running a python websocket server 345 // This is a utility class for running a python websocket server
338 // during tests. The server is started during the construction of the 346 // during tests. The server is started during the construction of the
339 // object, and is stopped when the destructor is called. Note that 347 // object, and is stopped when the destructor is called. Note that
340 // because of the underlying script that is used: 348 // because of the underlying script that is used:
341 // 349 //
342 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver 350 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver
343 // 351 //
344 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the 352 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 const content::NotificationSource& source, 447 const content::NotificationSource& source,
440 const content::NotificationDetails& details) OVERRIDE; 448 const content::NotificationDetails& details) OVERRIDE;
441 449
442 private: 450 private:
443 bool seen_; 451 bool seen_;
444 bool running_; 452 bool running_;
445 content::NotificationRegistrar registrar_; 453 content::NotificationRegistrar registrar_;
446 454
447 content::NotificationSource source_; 455 content::NotificationSource source_;
448 content::NotificationDetails details_; 456 content::NotificationDetails details_;
457 scoped_refptr<MessageLoopRunner> message_loop_runner_;
449 458
450 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); 459 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver);
451 }; 460 };
452 461
453 // A WindowedNotificationObserver hard-wired to observe 462 // A WindowedNotificationObserver hard-wired to observe
454 // chrome::NOTIFICATION_TAB_ADDED. 463 // chrome::NOTIFICATION_TAB_ADDED.
455 class WindowedTabAddedNotificationObserver 464 class WindowedTabAddedNotificationObserver
456 : public WindowedNotificationObserver { 465 : public WindowedNotificationObserver {
457 public: 466 public:
458 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either 467 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 541
533 private: 542 private:
534 // content::NotificationObserver 543 // content::NotificationObserver
535 virtual void Observe(int type, 544 virtual void Observe(int type,
536 const content::NotificationSource& source, 545 const content::NotificationSource& source,
537 const content::NotificationDetails& details) OVERRIDE; 546 const content::NotificationDetails& details) OVERRIDE;
538 547
539 content::WebContents* web_contents_; 548 content::WebContents* web_contents_;
540 std::vector<string16> expected_titles_; 549 std::vector<string16> expected_titles_;
541 content::NotificationRegistrar notification_registrar_; 550 content::NotificationRegistrar notification_registrar_;
551 scoped_refptr<MessageLoopRunner> message_loop_runner_;
542 552
543 // The most recently observed expected title, if any. 553 // The most recently observed expected title, if any.
544 string16 observed_title_; 554 string16 observed_title_;
545 555
546 bool expected_title_observed_; 556 bool expected_title_observed_;
547 bool quit_loop_on_observation_; 557 bool quit_loop_on_observation_;
548 558
549 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); 559 DISALLOW_COPY_AND_ASSIGN(TitleWatcher);
550 }; 560 };
551 561
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 641
632 // Overridden content::NotificationObserver methods. 642 // Overridden content::NotificationObserver methods.
633 virtual void Observe(int type, 643 virtual void Observe(int type,
634 const content::NotificationSource& source, 644 const content::NotificationSource& source,
635 const content::NotificationDetails& details) OVERRIDE; 645 const content::NotificationDetails& details) OVERRIDE;
636 646
637 private: 647 private:
638 content::NotificationRegistrar registrar_; 648 content::NotificationRegistrar registrar_;
639 std::queue<std::string> message_queue_; 649 std::queue<std::string> message_queue_;
640 bool waiting_for_message_; 650 bool waiting_for_message_;
651 scoped_refptr<MessageLoopRunner> message_loop_runner_;
641 652
642 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); 653 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue);
643 }; 654 };
644 655
645 // Takes a snapshot of the given render widget, rendered at |page_size|. The 656 // Takes a snapshot of the given render widget, rendered at |page_size|. The
646 // snapshot is set to |bitmap|. Returns true on success. 657 // snapshot is set to |bitmap|. Returns true on success.
647 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh, 658 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh,
648 const gfx::Size& page_size, 659 const gfx::Size& page_size,
649 SkBitmap* bitmap) WARN_UNUSED_RESULT; 660 SkBitmap* bitmap) WARN_UNUSED_RESULT;
650 661
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 // ui_controls_linux.cc and ui_controls_mac.cc 700 // ui_controls_linux.cc and ui_controls_mac.cc
690 void ClickTask(ui_controls::MouseButton button, 701 void ClickTask(ui_controls::MouseButton button,
691 int state, 702 int state,
692 const base::Closure& followup); 703 const base::Closure& followup);
693 704
694 } // namespace internal 705 } // namespace internal
695 706
696 } // namespace ui_test_utils 707 } // namespace ui_test_utils
697 708
698 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_ 709 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698