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

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: win_shared build 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER | 93 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER |
94 BROWSER_TEST_WAIT_FOR_TAB | 94 BROWSER_TEST_WAIT_FOR_TAB |
95 BROWSER_TEST_WAIT_FOR_NAVIGATION 95 BROWSER_TEST_WAIT_FOR_NAVIGATION
96 }; 96 };
97 97
98 // Turns on nestable tasks, runs the message loop, then resets nestable tasks 98 // Turns on nestable tasks, runs the message loop, then resets nestable tasks
99 // to what they were originally. Prefer this over MessageLoop::Run for in 99 // to what they were originally. Prefer this over MessageLoop::Run for in
100 // process browser tests that need to block until a condition is met. 100 // process browser tests that need to block until a condition is met.
101 void RunMessageLoop(); 101 void RunMessageLoop();
102 102
103 // Variant of RunMessageLoop that takes a |run_id| (from
104 // MessageLoop::current()->GetRunID()).
105 void RunMessageLoopWithID(const MessageLoop::RunID& run_id);
106
107 // Quit the MessageLoop Run of the specified |run_id|.
108 void QuitMessageLoopWithID(const MessageLoop::RunID& run_id);
109
110 // Get a MessageLoop::QuitNowWithID task that will repost itself a few times to
111 // allow the MessageLoop to finish pending tasks (and tasks posted by those
112 // pending tasks...).
113 base::Closure QuitMessageLoopWithIDClosure(const MessageLoop::RunID& run_id);
114
103 // Turns on nestable tasks, runs all pending tasks in the message loop, 115 // Turns on nestable tasks, runs all pending tasks in the message loop,
104 // then resets nestable tasks to what they were originally. Prefer this 116 // then resets nestable tasks to what they were originally. Prefer this
105 // over MessageLoop::RunAllPending for in process browser tests to run 117 // over MessageLoop::RunAllPending for in process browser tests to run
106 // all pending tasks. 118 // all pending tasks.
107 void RunAllPendingInMessageLoop(); 119 void RunAllPendingInMessageLoop();
108 120
109 // Blocks the current thread until all the pending messages in the loop of the 121 // Blocks the current thread until all the pending messages in the loop of the
110 // thread |thread_id| have been processed. 122 // thread |thread_id| have been processed.
111 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id); 123 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id);
112 124
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 const content::NotificationSource& source) 318 const content::NotificationSource& source)
307 WARN_UNUSED_RESULT; 319 WARN_UNUSED_RESULT;
308 320
309 // Sends a move event blocking until received. Returns true if the event was 321 // Sends a move event blocking until received. Returns true if the event was
310 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone, 322 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone,
311 // see it for details. 323 // see it for details.
312 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT; 324 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT;
313 bool SendMouseEventsSync(ui_controls::MouseButton type, 325 bool SendMouseEventsSync(ui_controls::MouseButton type,
314 int state) WARN_UNUSED_RESULT; 326 int state) WARN_UNUSED_RESULT;
315 327
316 // Run a message loop only for the specified amount of time. 328 // Helper class to Run and Quit the message loop. Run and Quit can only happen
317 class TimedMessageLoopRunner { 329 // once per instance. Make a new instance for each use. Calling Quit after Run
330 // has returned is safe and has no effect.
331 class MessageLoopRunner
332 : public base::RefCounted<MessageLoopRunner> {
318 public: 333 public:
319 // Create new MessageLoopForUI and attach to it. 334 MessageLoopRunner();
320 TimedMessageLoopRunner();
321 335
322 // Attach to an existing message loop. 336 // Run the current MessageLoop. This can't be called recursively.
323 explicit TimedMessageLoopRunner(MessageLoop* loop) 337 void Run();
324 : loop_(loop), owned_(false), quit_loop_invoked_(false) {}
325 338
326 ~TimedMessageLoopRunner(); 339 // Quit the matching call to Run (nested MessageLoops are unaffected).
340 void QuitNow();
327 341
328 // Run the message loop for ms milliseconds. 342 // Hand this closure off to code that uses callbacks to notify completion.
329 void RunFor(int ms); 343 // Example:
330 344 // scoped_refptr<MessageLoopRunner> runner =
331 // Post Quit task to the message loop. 345 // new MessageLoopRunner;
332 void Quit(); 346 // // Must be no nested MessageLoop::Run calls during kick_off_some_api:
333 347 // kick_off_some_api(runner.QuitNowClosure());
334 // Post delayed Quit task to the message loop. 348 // runner.Run();
335 void QuitAfter(int ms); 349 base::Closure QuitNowClosure();
336
337 bool WasTimedOut() const {
338 return !quit_loop_invoked_;
339 }
340 350
341 private: 351 private:
342 MessageLoop* loop_; 352 friend class base::RefCounted<MessageLoopRunner>;
343 bool owned_; 353 ~MessageLoopRunner();
344 bool quit_loop_invoked_;
345 354
346 DISALLOW_COPY_AND_ASSIGN(TimedMessageLoopRunner); 355 MessageLoop::RunID message_loop_run_id_;
356 bool run_called_;
357 bool quit_received_;
358 bool running_;
359
360 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner);
347 }; 361 };
348 362
349 // This is a utility class for running a python websocket server 363 // This is a utility class for running a python websocket server
350 // during tests. The server is started during the construction of the 364 // during tests. The server is started during the construction of the
351 // object, and is stopped when the destructor is called. Note that 365 // object, and is stopped when the destructor is called. Note that
352 // because of the underlying script that is used: 366 // because of the underlying script that is used:
353 // 367 //
354 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver 368 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver
355 // 369 //
356 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the 370 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 const content::NotificationSource& source, 465 const content::NotificationSource& source,
452 const content::NotificationDetails& details) OVERRIDE; 466 const content::NotificationDetails& details) OVERRIDE;
453 467
454 private: 468 private:
455 bool seen_; 469 bool seen_;
456 bool running_; 470 bool running_;
457 content::NotificationRegistrar registrar_; 471 content::NotificationRegistrar registrar_;
458 472
459 content::NotificationSource source_; 473 content::NotificationSource source_;
460 content::NotificationDetails details_; 474 content::NotificationDetails details_;
475 scoped_refptr<MessageLoopRunner> message_loop_runner_;
461 476
462 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); 477 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver);
463 }; 478 };
464 479
465 // A WindowedNotificationObserver hard-wired to observe 480 // A WindowedNotificationObserver hard-wired to observe
466 // chrome::NOTIFICATION_TAB_ADDED. 481 // chrome::NOTIFICATION_TAB_ADDED.
467 class WindowedTabAddedNotificationObserver 482 class WindowedTabAddedNotificationObserver
468 : public WindowedNotificationObserver { 483 : public WindowedNotificationObserver {
469 public: 484 public:
470 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either 485 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 559
545 private: 560 private:
546 // content::NotificationObserver 561 // content::NotificationObserver
547 virtual void Observe(int type, 562 virtual void Observe(int type,
548 const content::NotificationSource& source, 563 const content::NotificationSource& source,
549 const content::NotificationDetails& details) OVERRIDE; 564 const content::NotificationDetails& details) OVERRIDE;
550 565
551 content::WebContents* web_contents_; 566 content::WebContents* web_contents_;
552 std::vector<string16> expected_titles_; 567 std::vector<string16> expected_titles_;
553 content::NotificationRegistrar notification_registrar_; 568 content::NotificationRegistrar notification_registrar_;
569 scoped_refptr<MessageLoopRunner> message_loop_runner_;
554 570
555 // The most recently observed expected title, if any. 571 // The most recently observed expected title, if any.
556 string16 observed_title_; 572 string16 observed_title_;
557 573
558 bool expected_title_observed_; 574 bool expected_title_observed_;
559 bool quit_loop_on_observation_; 575 bool quit_loop_on_observation_;
560 576
561 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); 577 DISALLOW_COPY_AND_ASSIGN(TitleWatcher);
562 }; 578 };
563 579
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 659
644 // Overridden content::NotificationObserver methods. 660 // Overridden content::NotificationObserver methods.
645 virtual void Observe(int type, 661 virtual void Observe(int type,
646 const content::NotificationSource& source, 662 const content::NotificationSource& source,
647 const content::NotificationDetails& details) OVERRIDE; 663 const content::NotificationDetails& details) OVERRIDE;
648 664
649 private: 665 private:
650 content::NotificationRegistrar registrar_; 666 content::NotificationRegistrar registrar_;
651 std::queue<std::string> message_queue_; 667 std::queue<std::string> message_queue_;
652 bool waiting_for_message_; 668 bool waiting_for_message_;
669 scoped_refptr<MessageLoopRunner> message_loop_runner_;
653 670
654 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); 671 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue);
655 }; 672 };
656 673
657 // Takes a snapshot of the given render widget, rendered at |page_size|. The 674 // Takes a snapshot of the given render widget, rendered at |page_size|. The
658 // snapshot is set to |bitmap|. Returns true on success. 675 // snapshot is set to |bitmap|. Returns true on success.
659 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh, 676 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh,
660 const gfx::Size& page_size, 677 const gfx::Size& page_size,
661 SkBitmap* bitmap) WARN_UNUSED_RESULT; 678 SkBitmap* bitmap) WARN_UNUSED_RESULT;
662 679
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 // ui_controls_linux.cc and ui_controls_mac.cc 718 // ui_controls_linux.cc and ui_controls_mac.cc
702 void ClickTask(ui_controls::MouseButton button, 719 void ClickTask(ui_controls::MouseButton button,
703 int state, 720 int state,
704 const base::Closure& followup); 721 const base::Closure& followup);
705 722
706 } // namespace internal 723 } // namespace internal
707 724
708 } // namespace ui_test_utils 725 } // namespace ui_test_utils
709 726
710 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_ 727 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698