Commit 72a22011 authored by fdoray's avatar fdoray Committed by Commit bot

Use ScopedTaskEnvironment instead of MessageLoopForUI in chrome tests.

ScopedTaskEnvironment allows usage of ThreadTaskRunnerHandle and
base/task_scheduler/post_task.h within its scope. It should be
instantiated in everytest that uses either of these APIs
(i.e. no test should instantiate a MessageLoop directly).

Motivation for ScopedTaskEnvironment can be found in:
https://docs.google.com/document/d/1QabRo8c7D9LsYY3cEcaPQbOCLo8Tu-6VLykYXyl3Pkk/edit

BUG=708584

Review-Url: https://codereview.chromium.org/2851593002
Cr-Commit-Position: refs/heads/master@{#468608}
parent 0eb55d6c
......@@ -15,9 +15,9 @@
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromeos/dbus/mock_cryptohome_client.h"
......@@ -62,7 +62,9 @@ ACTION_P2(SendSanitizedUsername, call_status, sanitized_username) {
class UserCloudPolicyStoreChromeOSTest : public testing::Test {
protected:
UserCloudPolicyStoreChromeOSTest() {}
UserCloudPolicyStoreChromeOSTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override {
EXPECT_CALL(cryptohome_client_, GetSanitizedUsername(cryptohome_id_, _))
......@@ -72,8 +74,9 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test {
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
store_.reset(new UserCloudPolicyStoreChromeOS(
&cryptohome_client_, &session_manager_client_, loop_.task_runner(),
account_id_, user_policy_dir(), false /* is_active_directory */));
&cryptohome_client_, &session_manager_client_,
base::ThreadTaskRunnerHandle::Get(), account_id_, user_policy_dir(),
false /* is_active_directory */));
store_->AddObserver(&observer_);
// Install the initial public key, so that by default the validation of
......@@ -212,7 +215,7 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test {
.AppendASCII("policy.pub");
}
base::MessageLoopForUI loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
chromeos::MockCryptohomeClient cryptohome_client_;
chromeos::MockSessionManagerClient session_manager_client_;
UserPolicyBuilder policy_;
......
......@@ -13,6 +13,7 @@
#include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "chrome/browser/chromeos/policy/proto/install_attributes.pb.h"
#include "chromeos/chromeos_paths.h"
#include "chromeos/cryptohome/cryptohome_util.h"
......@@ -48,7 +49,9 @@ static const char kTestUserDeprecated[] = "test@example.com";
class InstallAttributesTest : public testing::Test {
protected:
InstallAttributesTest() {}
InstallAttributesTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
......@@ -76,7 +79,7 @@ class InstallAttributesTest : public testing::Test {
attribute->set_value(value);
}
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::ScopedTempDir temp_dir_;
std::unique_ptr<InstallAttributes> install_attributes_;
......
......@@ -14,6 +14,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/password_manager/native_backend_libsecret.h"
......@@ -273,7 +274,9 @@ class NativeBackendLibsecretTest : public testing::Test {
SYNCED,
};
NativeBackendLibsecretTest() {}
NativeBackendLibsecretTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override {
ASSERT_FALSE(global_mock_libsecret_items);
......@@ -598,7 +601,7 @@ class NativeBackendLibsecretTest : public testing::Test {
EXPECT_TRUE(global_mock_libsecret_items->empty());
}
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
// Provide some test forms to avoid having to set them up in each test.
PasswordForm form_google_;
......
......@@ -9,9 +9,9 @@
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/ui/app_list/search/history_factory.h"
......@@ -90,7 +90,9 @@ class StoreFlushWaiter {
class SearchHistoryTest : public testing::Test {
public:
SearchHistoryTest() {}
SearchHistoryTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
~SearchHistoryTest() override {}
// testing::Test overrides:
......@@ -144,7 +146,7 @@ class SearchHistoryTest : public testing::Test {
}
private:
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::ScopedTempDir temp_dir_;
std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_;
......
......@@ -8,7 +8,7 @@
#include "base/command_line.h"
#include "base/mac/scoped_nsobject.h"
#include "base/message_loop/message_loop.h"
#include "base/test/scoped_task_environment.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "chrome/common/chrome_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -34,7 +34,9 @@ namespace {
class AlertIndicatorButtonTest : public CocoaTest {
public:
AlertIndicatorButtonTest() {
AlertIndicatorButtonTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
std::string("--") + switches::kEnableTabAudioMuting);
......@@ -82,7 +84,9 @@ class AlertIndicatorButtonTest : public CocoaTest {
}
base::scoped_nsobject<AlertIndicatorButton> button_;
base::MessageLoopForUI message_loop_; // Needed for gfx::Animation.
// Needed for gfx::Animation.
base::test::ScopedTaskEnvironment scoped_task_environment_;
};
TEST_VIEW(AlertIndicatorButtonTest, button_)
......
......@@ -5,7 +5,7 @@
#import "chrome/browser/ui/cocoa/toolbar/app_toolbar_button_cell.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/test/scoped_task_environment.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
@interface TestAppToolbarButton : NSButton
......@@ -21,7 +21,9 @@
class AppToolbarButtonCellTest : public CocoaTest {
protected:
AppToolbarButtonCellTest() {
AppToolbarButtonCellTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {
base::scoped_nsobject<NSButton> button([[TestAppToolbarButton alloc]
initWithFrame:NSMakeRect(0, 0, 29, 29)]);
button_ = button;
......@@ -30,7 +32,9 @@ class AppToolbarButtonCellTest : public CocoaTest {
NSButton* button_;
base::scoped_nsobject<AppToolbarButtonCell> cell_;
base::MessageLoopForUI message_loop_; // Needed for gfx::Animation.
// Needed for gfx::Animation.
base::test::ScopedTaskEnvironment scoped_task_environment_;
private:
DISALLOW_COPY_AND_ASSIGN(AppToolbarButtonCellTest);
......
......@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop/message_loop.h"
#include "chrome/browser/ui/omnibox/clipboard_utils.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h"
#include "chrome/browser/ui/omnibox/clipboard_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "ui/base/clipboard/clipboard.h"
......@@ -19,13 +20,17 @@ namespace {
class ClipboardUtilsTest : public PlatformTest {
public:
ClipboardUtilsTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void TearDown() override {
ui::Clipboard::DestroyClipboardForCurrentThread();
}
private:
// Windows requires a message loop for clipboard access.
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};
TEST_F(ClipboardUtilsTest, GetClipboardText) {
......
......@@ -5,7 +5,7 @@
#include "chrome/browser/ui/views/frame/web_contents_close_handler.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/test/scoped_task_environment.h"
#include "chrome/browser/ui/views/frame/web_contents_close_handler_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -13,9 +13,10 @@ class MockWebContentsCloseHandlerDelegate
: public WebContentsCloseHandlerDelegate {
public:
explicit MockWebContentsCloseHandlerDelegate()
: got_clone_(false),
got_destroy_(false) {
}
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
got_clone_(false),
got_destroy_(false) {}
~MockWebContentsCloseHandlerDelegate() override {}
void Clear() {
......@@ -33,7 +34,7 @@ class MockWebContentsCloseHandlerDelegate
void DestroyClonedLayer() override { got_destroy_ = true; }
private:
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
bool got_clone_;
bool got_destroy_;
......@@ -90,4 +91,3 @@ TEST_F(WebContentsCloseHandlerTest, DontDestroyImmediatleyAfterCancel) {
EXPECT_FALSE(close_handler_delegate_.got_clone());
EXPECT_TRUE(IsTimerRunning());
}
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