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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/base/ui_test_utils.h
diff --git a/chrome/test/base/ui_test_utils.h b/chrome/test/base/ui_test_utils.h
index 4e2644d9470937de4fad3fcaf618c4182fd60b49..9415b825fe744aef0fd2553f86b4a4ff64443017 100644
--- a/chrome/test/base/ui_test_utils.h
+++ b/chrome/test/base/ui_test_utils.h
@@ -96,6 +96,15 @@ enum BrowserTestWaitFlags {
// process browser tests that need to block until a condition is met.
void RunMessageLoop();
+// Variant of RunMessageLoop that takes RunLoop.
+void RunRunLoop(const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
+
+// Quit the given RunLoop. Allows a few generations of pending tasks to run.
+void QuitRunLoop(const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
+
+base::Closure QuitRunLoopClosure(
+ const base::WeakPtr<MessageLoop::RunLoop>& run_loop);
+
// Turns on nestable tasks, runs all pending tasks in the message loop,
// then resets nestable tasks to what they were originally. Prefer this
// over MessageLoop::RunAllPending for in process browser tests to run
@@ -301,37 +310,36 @@ bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT;
bool SendMouseEventsSync(ui_controls::MouseButton type,
int state) WARN_UNUSED_RESULT;
-// Run a message loop only for the specified amount of time.
-class TimedMessageLoopRunner {
+// Helper class to Run and Quit the message loop. Run and Quit can only happen
+// once per instance. Make a new instance for each use. Calling Quit after Run
+// has returned is safe and has no effect.
+class MessageLoopRunner
+ : public base::RefCounted<MessageLoopRunner> {
public:
- // Create new MessageLoopForUI and attach to it.
- TimedMessageLoopRunner();
-
- // Attach to an existing message loop.
- explicit TimedMessageLoopRunner(MessageLoop* loop)
- : loop_(loop), owned_(false), quit_loop_invoked_(false) {}
+ MessageLoopRunner();
- ~TimedMessageLoopRunner();
+ // Run the current MessageLoop. This can't be called recursively.
+ void Run();
- // Run the message loop for ms milliseconds.
- void RunFor(int ms);
+ // Quit the matching call to Run (nested MessageLoops are unaffected).
+ void QuitNow();
- // Post Quit task to the message loop.
- void Quit();
-
- // Post delayed Quit task to the message loop.
- void QuitAfter(int ms);
-
- bool WasTimedOut() const {
- return !quit_loop_invoked_;
- }
+ // Hand this closure off to code that uses callbacks to notify completion.
+ // Example:
+ // scoped_refptr<MessageLoopRunner> runner =
+ // new MessageLoopRunner;
+ // // Must be no nested MessageLoop::Run calls during kick_off_some_api:
+ // kick_off_some_api(runner.QuitNowClosure());
+ // runner.Run();
+ base::Closure QuitNowClosure();
private:
- MessageLoop* loop_;
- bool owned_;
- bool quit_loop_invoked_;
+ friend class base::RefCounted<MessageLoopRunner>;
+ ~MessageLoopRunner();
+
+ MessageLoop::RunLoop run_loop_;
- DISALLOW_COPY_AND_ASSIGN(TimedMessageLoopRunner);
+ DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner);
};
// This is a utility class for running a python websocket server
@@ -446,6 +454,7 @@ class WindowedNotificationObserver : public content::NotificationObserver {
content::NotificationSource source_;
content::NotificationDetails details_;
+ scoped_refptr<MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver);
};
@@ -539,6 +548,7 @@ class TitleWatcher : public content::NotificationObserver {
content::WebContents* web_contents_;
std::vector<string16> expected_titles_;
content::NotificationRegistrar notification_registrar_;
+ scoped_refptr<MessageLoopRunner> message_loop_runner_;
// The most recently observed expected title, if any.
string16 observed_title_;
@@ -638,6 +648,7 @@ class DOMMessageQueue : public content::NotificationObserver {
content::NotificationRegistrar registrar_;
std::queue<std::string> message_queue_;
bool waiting_for_message_;
+ scoped_refptr<MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue);
};

Powered by Google App Engine
This is Rietveld 408576698