Commit be003c08 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[TaskEnvironment] Migrate iOS's TestWebThreadBundle too

This is the iOS equivalent of
https://chromium-review.googlesource.com/c/chromium/src/+/1747171

Required to facilitate the mass-rename of "thread_bundle" variables.

R=rohitrao@chromium.org

Bug: 992483
Change-Id: I244370aa92eb53933cfe4076209b84b58b658504
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1757366Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Auto-Submit: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#688256}
parent e525e385
...@@ -20,7 +20,7 @@ namespace web { ...@@ -20,7 +20,7 @@ namespace web {
class WebSubThread; class WebSubThread;
class WebThreadImpl; class WebThreadImpl;
// DEPRECATED: use TestWebThreadBundle instead. // DEPRECATED: use WebTaskEnvironment instead.
// A WebThread for unit tests; this lets unit tests outside of web create // A WebThread for unit tests; this lets unit tests outside of web create
// WebThread instances. // WebThread instances.
class TestWebThread { class TestWebThread {
......
...@@ -7,27 +7,31 @@ ...@@ -7,27 +7,31 @@
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
// TestWebThreadBundle is a convenience class for creating a set of // TODO(crbug.com/992483): Rename this header to web_task_environment.h and
// TestWebThreads and a thread pool in unit tests. For most tests, it is // migrate all users.
// sufficient to just instantiate the TestWebThreadBundle as a member variable. //
// It is a good idea to put the TestWebThreadBundle as the first member variable // WebTaskEnvironment is the iOS equivalent of content::BrowserTaskEnvironment.
// in test classes, so it is destroyed last, and the test threads always exist //
// from the perspective of other classes. // It's is a convenience class for creating a set of TestWebThreads and a thread
// pool in unit tests. For most tests, it is sufficient to just instantiate the
// WebTaskEnvironment as a member variable. It is a good idea to put the
// WebTaskEnvironment as the first member variable in test classes, so it is
// destroyed last, and the test threads always exist from the perspective of
// other classes.
// //
// By default, all of the created TestWebThreads will be backed by a single // By default, all of the created TestWebThreads will be backed by a single
// shared MessageLoop. If a test truly needs separate threads, it can do so by // shared MessageLoop. If a test truly needs separate threads, it can do so by
// passing the appropriate combination of option values during the // passing the appropriate combination of option values during the
// TestWebThreadBundle construction. // WebTaskEnvironment construction.
// //
// To synchronously run tasks posted to TestWebThreads that use the shared // To synchronously run tasks posted to TestWebThreads that use the shared
// MessageLoop, call RunLoop::Run/RunUntilIdle() on the thread where the // MessageLoop, call RunLoop::Run/RunUntilIdle() on the thread where the
// TestWebThreadBundle lives. The destructor of TestWebThreadBundle runs // WebTaskEnvironment lives. The destructor of WebTaskEnvironment runs remaining
// remaining TestWebThreads tasks and remaining BLOCK_SHUTDOWN thread pool // TestWebThreads tasks and remaining BLOCK_SHUTDOWN thread pool tasks.
// tasks.
// //
// Some tests using the IO thread expect a MessageLoopForIO. Passing // Some tests using the IO thread expect a MessageLoopForIO. Passing IO_MAINLOOP
// IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop. // will use a MessageLoopForIO for the main MessageLoop. Most of the time, this
// Most of the time, this avoids needing to use a REAL_IO_THREAD. // avoids needing to use a REAL_IO_THREAD.
#include <memory> #include <memory>
...@@ -41,7 +45,7 @@ namespace web { ...@@ -41,7 +45,7 @@ namespace web {
class TestWebThread; class TestWebThread;
class TestWebThreadBundle : public base::test::TaskEnvironment { class WebTaskEnvironment : public base::test::TaskEnvironment {
public: public:
// Used to specify the type of MessageLoop that backs the UI thread, and // Used to specify the type of MessageLoop that backs the UI thread, and
// which of the named WebThreads should be backed by a real // which of the named WebThreads should be backed by a real
...@@ -52,9 +56,9 @@ class TestWebThreadBundle : public base::test::TaskEnvironment { ...@@ -52,9 +56,9 @@ class TestWebThreadBundle : public base::test::TaskEnvironment {
REAL_IO_THREAD = 1 << 1, REAL_IO_THREAD = 1 << 1,
}; };
explicit TestWebThreadBundle(int options = Options::DEFAULT); explicit WebTaskEnvironment(int options = Options::DEFAULT);
~TestWebThreadBundle() override; ~WebTaskEnvironment() override;
private: private:
void Init(int options); void Init(int options);
...@@ -62,7 +66,13 @@ class TestWebThreadBundle : public base::test::TaskEnvironment { ...@@ -62,7 +66,13 @@ class TestWebThreadBundle : public base::test::TaskEnvironment {
std::unique_ptr<TestWebThread> ui_thread_; std::unique_ptr<TestWebThread> ui_thread_;
std::unique_ptr<TestWebThread> io_thread_; std::unique_ptr<TestWebThread> io_thread_;
DISALLOW_COPY_AND_ASSIGN(TestWebThreadBundle); DISALLOW_COPY_AND_ASSIGN(WebTaskEnvironment);
};
// TODO(crbug.com/992483): Mass migrate users and remove this.
class TestWebThreadBundle : public WebTaskEnvironment {
public:
using WebTaskEnvironment::WebTaskEnvironment;
}; };
} // namespace web } // namespace web
......
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
namespace web { namespace web {
TestWebThreadBundle::TestWebThreadBundle(int options) WebTaskEnvironment::WebTaskEnvironment(int options)
: base::test::TaskEnvironment(options == IO_MAINLOOP ? MainThreadType::IO : base::test::TaskEnvironment(options == IO_MAINLOOP ? MainThreadType::IO
: MainThreadType::UI) { : MainThreadType::UI) {
Init(options); Init(options);
} }
TestWebThreadBundle::~TestWebThreadBundle() { WebTaskEnvironment::~WebTaskEnvironment() {
// To ensure a clean teardown, each thread's message loop must be flushed // To ensure a clean teardown, each thread's message loop must be flushed
// just before the thread is destroyed. But stopping a fake thread does not // just before the thread is destroyed. But stopping a fake thread does not
// automatically flush the message loop, so do it manually. // automatically flush the message loop, so do it manually.
...@@ -40,13 +40,13 @@ TestWebThreadBundle::~TestWebThreadBundle() { ...@@ -40,13 +40,13 @@ TestWebThreadBundle::~TestWebThreadBundle() {
WebThreadImpl::ResetTaskExecutorForTesting(); WebThreadImpl::ResetTaskExecutorForTesting();
} }
void TestWebThreadBundle::Init(int options) { void WebTaskEnvironment::Init(int options) {
WebThreadImpl::CreateTaskExecutor(); WebThreadImpl::CreateTaskExecutor();
ui_thread_ = ui_thread_ =
std::make_unique<TestWebThread>(WebThread::UI, GetMainThreadTaskRunner()); std::make_unique<TestWebThread>(WebThread::UI, GetMainThreadTaskRunner());
if (options & TestWebThreadBundle::REAL_IO_THREAD) { if (options & WebTaskEnvironment::REAL_IO_THREAD) {
io_thread_ = std::make_unique<TestWebThread>(WebThread::IO); io_thread_ = std::make_unique<TestWebThread>(WebThread::IO);
io_thread_->StartIOThread(); io_thread_->StartIOThread();
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment