Introduce a RunLoop class that supports the TaskQueue interface
on the current thread.

This simplifies writing async tests that use TaskQueue and doesn't
require spinning up a new thread for simple things. The implementation
is currently based on rtc::Thread, which could also be useful in
some circumstances while migrating code over to TQ.

Remove PressEnterToContinue from the test_common files since
it's very specific and only used from one file.

Bug: none
Change-Id: I8b2c6c40809271a109ec17cf7e1120847645d58a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174260
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31160}
diff --git a/test/run_loop.cc b/test/run_loop.cc
index 1fc200f..643da5d 100644
--- a/test/run_loop.cc
+++ b/test/run_loop.cc
@@ -9,15 +9,65 @@
  */
 #include "test/run_loop.h"
 
-#include <stdio.h>
+#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 namespace test {
 
-void PressEnterToContinue(TaskQueueBase* /*task_queue*/) {
-  puts(">> Press ENTER to continue...");
-  while (getc(stdin) != '\n' && !feof(stdin))
-    ;
+RunLoop::RunLoop() {
+  worker_thread_.WrapCurrent();
 }
+
+RunLoop::~RunLoop() {
+  worker_thread_.UnwrapCurrent();
+}
+
+TaskQueueBase* RunLoop::task_queue() {
+  return &worker_thread_;
+}
+
+void RunLoop::Run() {
+  worker_thread_.ProcessMessages(WorkerThread::kForever);
+}
+
+void RunLoop::Quit() {
+  socket_server_.FailNextWait();
+}
+
+void RunLoop::Flush() {
+  worker_thread_.PostTask(
+      ToQueuedTask([this]() { socket_server_.FailNextWait(); }));
+  worker_thread_.ProcessMessages(1000);
+}
+
+RunLoop::FakeSocketServer::FakeSocketServer() = default;
+RunLoop::FakeSocketServer::~FakeSocketServer() = default;
+
+void RunLoop::FakeSocketServer::FailNextWait() {
+  fail_next_wait_ = true;
+}
+
+bool RunLoop::FakeSocketServer::Wait(int cms, bool process_io) {
+  if (fail_next_wait_) {
+    fail_next_wait_ = false;
+    return false;
+  }
+  return true;
+}
+
+void RunLoop::FakeSocketServer::WakeUp() {}
+
+rtc::Socket* RunLoop::FakeSocketServer::CreateSocket(int family, int type) {
+  return nullptr;
+}
+
+rtc::AsyncSocket* RunLoop::FakeSocketServer::CreateAsyncSocket(int family,
+                                                               int type) {
+  return nullptr;
+}
+
+RunLoop::WorkerThread::WorkerThread(rtc::SocketServer* ss)
+    : rtc::Thread(ss), tq_setter_(this) {}
+
 }  // namespace test
 }  // namespace webrtc