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

Unified Diff: base/test/scoped_task_environment.cc

Issue 2868093002: Add base::test::ScopedTaskEnvironment::RunUntilIdle(). (Closed)
Patch Set: Created 3 years, 7 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: base/test/scoped_task_environment.cc
diff --git a/base/test/scoped_task_environment.cc b/base/test/scoped_task_environment.cc
index af871e6a11d4c273197960d73ed3a1a227d32c86..d1c0907c4b0d7e32ab411826fe006c98efc3a7d6 100644
--- a/base/test/scoped_task_environment.cc
+++ b/base/test/scoped_task_environment.cc
@@ -12,6 +12,27 @@
namespace base {
namespace test {
+namespace {
+
+class TaskObserver : public MessageLoop::TaskObserver {
+ public:
+ TaskObserver() = default;
+
+ // MessageLoop::TaskObserver:
+ void WillProcessTask(const PendingTask& pending_task) override {}
+ void DidProcessTask(const PendingTask& pending_task) override {
+ ran_task_ = true;
robliao 2017/05/09 21:52:52 It would be more accurate to call this called_did_
+ }
+
+ bool ran_task() const { return ran_task_; }
+
+ private:
+ bool ran_task_ = false;
+ DISALLOW_COPY_AND_ASSIGN(TaskObserver);
+};
+
+} // namespace
+
ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type)
: message_loop_(main_thread_type == MainThreadType::DEFAULT
? MessageLoop::TYPE_DEFAULT
@@ -45,5 +66,19 @@ ScopedTaskEnvironment::~ScopedTaskEnvironment() {
TaskScheduler::SetInstance(nullptr);
}
+void ScopedTaskEnvironment::RunUntilIdle() {
+ for (;;) {
+ TaskScheduler::GetInstance()->FlushForTesting();
+
+ TaskObserver task_observer;
+ MessageLoop::current()->AddTaskObserver(&task_observer);
+ RunLoop().RunUntilIdle();
+ MessageLoop::current()->RemoveTaskObserver(&task_observer);
+
+ if (!task_observer.ran_task())
+ return;
+ }
+}
+
} // namespace test
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698