Add RunLoop::QuitWhenIdleClosure().

BUG=616447

Review-Url: https://codereview.chromium.org/2053223004
Cr-Commit-Position: refs/heads/master@{#399234}
diff --git a/base/run_loop_unittest.cc b/base/run_loop_unittest.cc
index b0bd2a1..a87ced0 100644
--- a/base/run_loop_unittest.cc
+++ b/base/run_loop_unittest.cc
@@ -89,4 +89,28 @@
   EXPECT_EQ(4, counter_);
 }
 
+TEST_F(RunLoopTest, QuitWhenIdleClosure) {
+  message_loop_.task_runner()->PostTask(FROM_HERE,
+                                        run_loop_.QuitWhenIdleClosure());
+  message_loop_.task_runner()->PostTask(
+      FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_)));
+  message_loop_.task_runner()->PostDelayedTask(
+      FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1));
+
+  run_loop_.Run();
+  EXPECT_EQ(1, counter_);
+}
+
+// Verify that the QuitWhenIdleClosure() can run after the RunLoop has been
+// deleted. It should have no effect.
+TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
+  Closure quit_when_idle_closure;
+  {
+    RunLoop run_loop;
+    quit_when_idle_closure = run_loop.QuitWhenIdleClosure();
+    run_loop.RunUntilIdle();
+  }
+  quit_when_idle_closure.Run();
+}
+
 }  // namespace base