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

Migrate MessageLoop::current() to MessageLoopCurrent in test_browser_thread_bundle.cc

The scripted change in patch set 1 was incorrect as it didn't consider that
TestBrowserThread's constructor is receiving a MessageLoop*.

Thankfully, as of recent changes to BrowserThreadImpl/TestBrowserThread this
is no longer necessary.

Update TestBrowserThread to not require a MessageLoop* and use this to get
rid of direct MessageLoop usage in TestBrowserThreadBundle altogether :)

This CL was uploaded by git cl split (originally).

R=tommi@chromium.org

Bug: 825327
Change-Id: I6e529c7a2547bcfa7ae3ee9b017da91f3d068514
Reviewed-on: https://chromium-review.googlesource.com/1024433
Commit-Queue: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarTommi <tommi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553655}
parent d6009a71
...@@ -170,6 +170,13 @@ ScopedTaskEnvironment::GetMainThreadTaskRunner() { ...@@ -170,6 +170,13 @@ ScopedTaskEnvironment::GetMainThreadTaskRunner() {
return mock_time_task_runner_; return mock_time_task_runner_;
} }
bool ScopedTaskEnvironment::MainThreadHasPendingTask() const {
if (message_loop_)
return !message_loop_->IsIdleForTesting();
DCHECK(mock_time_task_runner_);
return mock_time_task_runner_->HasPendingTask();
}
void ScopedTaskEnvironment::RunUntilIdle() { void ScopedTaskEnvironment::RunUntilIdle() {
// TODO(gab): This can be heavily simplified to essentially: // TODO(gab): This can be heavily simplified to essentially:
// bool HasMainThreadTasks() { // bool HasMainThreadTasks() {
...@@ -270,11 +277,6 @@ std::unique_ptr<TickClock> ScopedTaskEnvironment::DeprecatedGetMockTickClock() { ...@@ -270,11 +277,6 @@ std::unique_ptr<TickClock> ScopedTaskEnvironment::DeprecatedGetMockTickClock() {
return mock_time_task_runner_->DeprecatedGetMockTickClock(); return mock_time_task_runner_->DeprecatedGetMockTickClock();
} }
bool ScopedTaskEnvironment::MainThreadHasPendingTask() const {
DCHECK(mock_time_task_runner_);
return mock_time_task_runner_->HasPendingTask();
}
size_t ScopedTaskEnvironment::GetPendingMainThreadTaskCount() const { size_t ScopedTaskEnvironment::GetPendingMainThreadTaskCount() const {
DCHECK(mock_time_task_runner_); DCHECK(mock_time_task_runner_);
return mock_time_task_runner_->GetPendingTaskCount(); return mock_time_task_runner_->GetPendingTaskCount();
......
...@@ -98,10 +98,12 @@ class ScopedTaskEnvironment { ...@@ -98,10 +98,12 @@ class ScopedTaskEnvironment {
// TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle.
~ScopedTaskEnvironment(); ~ScopedTaskEnvironment();
// Only valid for instances with a MOCK_TIME MainThreadType.
// Returns a TaskRunner that schedules tasks on the main thread. // Returns a TaskRunner that schedules tasks on the main thread.
scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner();
// Returns whether the main thread's TaskRunner has pending tasks.
bool MainThreadHasPendingTask() const;
// Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the // Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the
// TaskScheduler's non-delayed queues are empty. // TaskScheduler's non-delayed queues are empty.
void RunUntilIdle(); void RunUntilIdle();
...@@ -123,10 +125,6 @@ class ScopedTaskEnvironment { ...@@ -123,10 +125,6 @@ class ScopedTaskEnvironment {
const TickClock* GetMockTickClock(); const TickClock* GetMockTickClock();
std::unique_ptr<TickClock> DeprecatedGetMockTickClock(); std::unique_ptr<TickClock> DeprecatedGetMockTickClock();
// Only valid for instances with a MOCK_TIME MainThreadType.
// Returns whether the main thread's TaskRunner has pending tasks.
bool MainThreadHasPendingTask() const;
// Only valid for instances with a MOCK_TIME MainThreadType. // Only valid for instances with a MOCK_TIME MainThreadType.
// Returns the number of pending tasks of the main thread's TaskRunner. // Returns the number of pending tasks of the main thread's TaskRunner.
size_t GetPendingMainThreadTaskCount() const; size_t GetPendingMainThreadTaskCount() const;
......
...@@ -19,11 +19,16 @@ TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier) ...@@ -19,11 +19,16 @@ TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier)
real_thread_->AllowBlockingForTesting(); real_thread_->AllowBlockingForTesting();
} }
TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier, TestBrowserThread::TestBrowserThread(
base::MessageLoop* message_loop) BrowserThread::ID identifier,
scoped_refptr<base::SingleThreadTaskRunner> thread_runner)
: identifier_(identifier), : identifier_(identifier),
fake_thread_( fake_thread_(
new BrowserThreadImpl(identifier_, message_loop->task_runner())) {} new BrowserThreadImpl(identifier_, std::move(thread_runner))) {}
TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier,
base::MessageLoop* message_loop)
: TestBrowserThread(identifier, message_loop->task_runner()) {}
TestBrowserThread::~TestBrowserThread() { TestBrowserThread::~TestBrowserThread() {
// The upcoming BrowserThreadImpl::ResetGlobalsForTesting() call requires that // The upcoming BrowserThreadImpl::ResetGlobalsForTesting() call requires that
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
namespace base { namespace base {
...@@ -29,7 +31,13 @@ class TestBrowserThread { ...@@ -29,7 +31,13 @@ class TestBrowserThread {
// MessageLoopForIO if |identifier == BrowserThread::IO|. // MessageLoopForIO if |identifier == BrowserThread::IO|.
explicit TestBrowserThread(BrowserThread::ID identifier); explicit TestBrowserThread(BrowserThread::ID identifier);
// Constructs a TestBrowserThread based on |message_loop| (no |real_thread_|). // Constructs a TestBrowserThread "running" on |thread_runner| (no
// |real_thread_|).
TestBrowserThread(BrowserThread::ID identifier,
scoped_refptr<base::SingleThreadTaskRunner> thread_runner);
// Deprecated: Forwards |message_loop->task_runner()| to the above
// constructor.
TestBrowserThread(BrowserThread::ID identifier, TestBrowserThread(BrowserThread::ID identifier,
base::MessageLoop* message_loop); base::MessageLoop* message_loop);
......
...@@ -5,9 +5,10 @@ ...@@ -5,9 +5,10 @@
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop_current.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/browser/after_startup_task_utils.h" #include "content/browser/after_startup_task_utils.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h" #include "content/public/test/test_browser_thread.h"
...@@ -55,7 +56,7 @@ TestBrowserThreadBundle::~TestBrowserThreadBundle() { ...@@ -55,7 +56,7 @@ TestBrowserThreadBundle::~TestBrowserThreadBundle() {
// blocked upon it could make a test flaky whereas by flushing we guarantee // blocked upon it could make a test flaky whereas by flushing we guarantee
// it will blow up). // it will blow up).
RunAllTasksUntilIdle(); RunAllTasksUntilIdle();
CHECK(base::MessageLoop::current()->IsIdleForTesting()); CHECK(!scoped_task_environment_->MainThreadHasPendingTask());
} }
// |scoped_task_environment_| needs to explicitly go away before fake threads // |scoped_task_environment_| needs to explicitly go away before fake threads
...@@ -90,19 +91,19 @@ void TestBrowserThreadBundle::Init() { ...@@ -90,19 +91,19 @@ void TestBrowserThreadBundle::Init() {
// ScopedTaskEnvironment may already exist if this TestBrowserThreadBundle is // ScopedTaskEnvironment may already exist if this TestBrowserThreadBundle is
// instantiated in a test whose parent fixture provides a // instantiated in a test whose parent fixture provides a
// ScopedTaskEnvironment. // ScopedTaskEnvironment.
if (!base::MessageLoop::current()) { if (!base::MessageLoopCurrent::IsSet()) {
scoped_task_environment_ = scoped_task_environment_ =
std::make_unique<base::test::ScopedTaskEnvironment>( std::make_unique<base::test::ScopedTaskEnvironment>(
options_ & IO_MAINLOOP options_ & IO_MAINLOOP
? base::test::ScopedTaskEnvironment::MainThreadType::IO ? base::test::ScopedTaskEnvironment::MainThreadType::IO
: base::test::ScopedTaskEnvironment::MainThreadType::UI); : base::test::ScopedTaskEnvironment::MainThreadType::UI);
} }
CHECK(options_ & IO_MAINLOOP ? base::MessageLoopForIO::IsCurrent() CHECK(options_ & IO_MAINLOOP ? base::MessageLoopCurrentForIO::IsSet()
: base::MessageLoopForUI::IsCurrent()); : base::MessageLoopCurrentForUI::IsSet());
// Set the current thread as the UI thread. // Set the current thread as the UI thread.
ui_thread_ = std::make_unique<TestBrowserThread>( ui_thread_ = std::make_unique<TestBrowserThread>(
BrowserThread::UI, base::MessageLoop::current()); BrowserThread::UI, base::ThreadTaskRunnerHandle::Get());
if (!(options_ & DONT_CREATE_BROWSER_THREADS)) if (!(options_ & DONT_CREATE_BROWSER_THREADS))
CreateBrowserThreads(); CreateBrowserThreads();
...@@ -116,7 +117,7 @@ void TestBrowserThreadBundle::CreateBrowserThreads() { ...@@ -116,7 +117,7 @@ void TestBrowserThreadBundle::CreateBrowserThreads() {
io_thread_->StartIOThread(); io_thread_->StartIOThread();
} else { } else {
io_thread_ = std::make_unique<TestBrowserThread>( io_thread_ = std::make_unique<TestBrowserThread>(
BrowserThread::IO, base::MessageLoop::current()); BrowserThread::IO, base::ThreadTaskRunnerHandle::Get());
} }
threads_created_ = true; threads_created_ = true;
......
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