Commit 882d37ed authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

components: Prepare unittests for PostTask refactor.

This adds TestBrowserThreadBundles to unittests that will need it when
we replace BrowserThread::PostTask() and friends with base/post_task.h.

The TestBrowserThreadBundle initializes globals that are required for
things like posting a task to or obtaining a TaskRunner for a BrowserThread.

I'm making this change in a separate patch to make review of the
future automated refactoring patches easier.

Depends on https://crrev.com/c/1185887.

Bug: 878356
Change-Id: Ifd332b995e3449b26d9c93283fb708792b2e66eb
Reviewed-on: https://chromium-review.googlesource.com/1208050Reviewed-by: default avatarDerek Cheng <imcheng@chromium.org>
Reviewed-by: default avatarJialiu Lin <jialiul@chromium.org>
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589613}
parent b4550779
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "components/cast_channel/cast_test_util.h" #include "components/cast_channel/cast_test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "services/data_decoder/public/cpp/testing_json_parser.h" #include "services/data_decoder/public/cpp/testing_json_parser.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -56,6 +57,7 @@ class CastMessageHandlerTest : public testing::Test { ...@@ -56,6 +57,7 @@ class CastMessageHandlerTest : public testing::Test {
CastMessageHandlerTest() CastMessageHandlerTest()
: environment_( : environment_(
base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME),
thread_bundle_(content::TestBrowserThreadBundle::PLAIN_MAINLOOP),
cast_socket_service_(new base::TestSimpleTaskRunner()), cast_socket_service_(new base::TestSimpleTaskRunner()),
handler_(&cast_socket_service_, handler_(&cast_socket_service_,
/* connector */ nullptr, /* connector */ nullptr,
...@@ -98,6 +100,7 @@ class CastMessageHandlerTest : public testing::Test { ...@@ -98,6 +100,7 @@ class CastMessageHandlerTest : public testing::Test {
protected: protected:
base::test::ScopedTaskEnvironment environment_; base::test::ScopedTaskEnvironment environment_;
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
MockCastSocketService cast_socket_service_; MockCastSocketService cast_socket_service_;
data_decoder::TestingJsonParser::ScopedFactoryOverride parser_override_; data_decoder::TestingJsonParser::ScopedFactoryOverride parser_override_;
......
...@@ -36,6 +36,7 @@ class CastSocketServiceTest : public testing::Test { ...@@ -36,6 +36,7 @@ class CastSocketServiceTest : public testing::Test {
void TearDown() override { cast_socket_service_ = nullptr; } void TearDown() override { cast_socket_service_ = nullptr; }
protected: protected:
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<CastSocketService> cast_socket_service_; std::unique_ptr<CastSocketService> cast_socket_service_;
base::MockCallback<CastSocket::OnOpenCallback> mock_on_open_callback_; base::MockCallback<CastSocket::OnOpenCallback> mock_on_open_callback_;
MockCastSocketObserver mock_observer_; MockCastSocketObserver mock_observer_;
......
...@@ -6,15 +6,13 @@ ...@@ -6,15 +6,13 @@
#include <memory> #include <memory>
#include "base/bind.h" #include "base/bind.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/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/test_mock_time_task_runner.h" #include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/safe_browsing/db/test_database_manager.h" #include "components/safe_browsing/db/test_database_manager.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_bundle.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -48,16 +46,14 @@ class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { ...@@ -48,16 +46,14 @@ class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager {
class WhitelistCheckerClientTest : public testing::Test { class WhitelistCheckerClientTest : public testing::Test {
public: public:
WhitelistCheckerClientTest() : target_url_("http://foo.bar") {} WhitelistCheckerClientTest()
: environment_(
base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME),
thread_bundle_(content::TestBrowserThreadBundle::PLAIN_MAINLOOP),
target_url_("http://foo.bar") {}
void SetUp() override { void SetUp() override {
database_manager_ = new MockSafeBrowsingDatabaseManager; database_manager_ = new MockSafeBrowsingDatabaseManager;
task_runner_ = new base::TestMockTimeTaskRunner(base::Time::Now(),
base::TimeTicks::Now());
message_loop_.reset(new base::MessageLoop);
io_thread_ = std::make_unique<content::TestBrowserThread>(
content::BrowserThread::IO, base::MessageLoopCurrent::Get());
message_loop_->SetTaskRunner(task_runner_);
} }
void TearDown() override { void TearDown() override {
...@@ -67,18 +63,15 @@ class WhitelistCheckerClientTest : public testing::Test { ...@@ -67,18 +63,15 @@ class WhitelistCheckerClientTest : public testing::Test {
// Verify no callback is remaining. // Verify no callback is remaining.
// TODO(nparker): We should somehow EXPECT that no entry is remaining, // TODO(nparker): We should somehow EXPECT that no entry is remaining,
// rather than just invoking it. // rather than just invoking it.
task_runner_->FastForwardUntilNoTasksRemain(); environment_.FastForwardUntilNoTasksRemain();
} }
protected: protected:
base::test::ScopedTaskEnvironment environment_;
content::TestBrowserThreadBundle thread_bundle_;
GURL target_url_; GURL target_url_;
scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_;
// Needed for |database_manager_| teardown tasks.
std::unique_ptr<content::TestBrowserThread> io_thread_;
std::unique_ptr<base::MessageLoop> message_loop_;
scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
}; };
TEST_F(WhitelistCheckerClientTest, TestMatch) { TEST_F(WhitelistCheckerClientTest, TestMatch) {
...@@ -125,11 +118,11 @@ TEST_F(WhitelistCheckerClientTest, TestAsyncTimeout) { ...@@ -125,11 +118,11 @@ TEST_F(WhitelistCheckerClientTest, TestAsyncTimeout) {
MockBoolCallback callback; MockBoolCallback callback;
WhitelistCheckerClient::StartCheckCsdWhitelist(database_manager_, target_url_, WhitelistCheckerClient::StartCheckCsdWhitelist(database_manager_, target_url_,
callback.Get()); callback.Get());
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); environment_.FastForwardBy(base::TimeDelta::FromSeconds(1));
// No callback yet. // No callback yet.
EXPECT_CALL(callback, Run(true /* is_whitelisted */)); EXPECT_CALL(callback, Run(true /* is_whitelisted */));
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(5)); environment_.FastForwardBy(base::TimeDelta::FromSeconds(5));
} }
} // namespace safe_browsing } // namespace safe_browsing
...@@ -226,6 +226,7 @@ TEST(AdSamplerTriggerTestFinch, FrequencyDenominatorFeature) { ...@@ -226,6 +226,7 @@ TEST(AdSamplerTriggerTestFinch, FrequencyDenominatorFeature) {
// Make sure that setting the frequency denominator via Finch params works as // Make sure that setting the frequency denominator via Finch params works as
// expected, and that the default frequency is used when no Finch config is // expected, and that the default frequency is used when no Finch config is
// given. // given.
content::TestBrowserThreadBundle thread_bundle;
AdSamplerTrigger trigger_default(nullptr, nullptr, nullptr, nullptr, nullptr); AdSamplerTrigger trigger_default(nullptr, nullptr, nullptr, nullptr, nullptr);
EXPECT_EQ(kAdSamplerDefaultFrequency, EXPECT_EQ(kAdSamplerDefaultFrequency,
trigger_default.sampler_frequency_denominator_); trigger_default.sampler_frequency_denominator_);
......
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