Commit 27ff7475 authored by fdoray's avatar fdoray Committed by Commit bot

Use ScopedTaskEnvironment instead of MessageLoopForUI in device 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
R=gab@chromium.org
TBR=reillyg@chromium.org

Review-Url: https://codereview.chromium.org/2849613002
Cr-Commit-Position: refs/heads/master@{#467760}
parent ca6cb1bc
......@@ -16,6 +16,8 @@
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string16.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "device/geolocation/access_token_store.h"
#include "device/geolocation/fake_location_provider.h"
......@@ -98,7 +100,10 @@ void DummyFunction(const LocationProvider* provider,
class GeolocationProviderTest : public testing::Test {
protected:
GeolocationProviderTest() : arbitrator_(new FakeLocationProvider) {
GeolocationProviderTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
arbitrator_(new FakeLocationProvider) {
provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_));
}
......@@ -123,7 +128,9 @@ class GeolocationProviderTest : public testing::Test {
// test completes.
base::ShadowingAtExitManager at_exit_;
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::ThreadChecker thread_checker_;
// Owned by the GeolocationProviderImpl class.
FakeLocationProvider* arbitrator_;
......@@ -136,7 +143,7 @@ class GeolocationProviderTest : public testing::Test {
bool GeolocationProviderTest::ProvidersStarted() {
DCHECK(provider()->IsRunning());
DCHECK(base::MessageLoop::current() == &message_loop_);
DCHECK(thread_checker_.CalledOnValidThread());
provider()->task_runner()->PostTaskAndReply(
FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
......@@ -153,7 +160,7 @@ void GeolocationProviderTest::GetProvidersStarted() {
void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
DCHECK(provider()->IsRunning());
DCHECK(base::MessageLoop::current() == &message_loop_);
DCHECK(thread_checker_.CalledOnValidThread());
provider()->task_runner()->PostTask(
FROM_HERE,
base::Bind(&GeolocationProviderImpl::OnLocationUpdate,
......
......@@ -2,15 +2,15 @@
// 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 "device/geolocation/wifi_data_provider_chromeos.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_manager_client.h"
#include "chromeos/network/geolocation_handler.h"
#include "device/geolocation/wifi_data_provider_chromeos.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
......@@ -18,7 +18,9 @@ namespace device {
class GeolocationChromeOsWifiDataProviderTest : public testing::Test {
protected:
GeolocationChromeOsWifiDataProviderTest() {}
GeolocationChromeOsWifiDataProviderTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override {
chromeos::DBusThreadManager::Initialize();
......@@ -59,7 +61,7 @@ class GeolocationChromeOsWifiDataProviderTest : public testing::Test {
base::RunLoop().RunUntilIdle();
}
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
scoped_refptr<WifiDataProviderChromeOs> provider_;
chromeos::ShillManagerClient* manager_client_;
chromeos::ShillManagerClient::TestInterface* manager_test_;
......
......@@ -10,6 +10,7 @@
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_task_runner_handle.h"
#include "device/geolocation/wifi_data_provider_manager.h"
......@@ -82,7 +83,9 @@ class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
class GeolocationWifiDataProviderCommonTest : public testing::Test {
public:
GeolocationWifiDataProviderCommonTest()
: wifi_data_callback_(base::Bind(&base::DoNothing)),
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
wifi_data_callback_(base::Bind(&base::DoNothing)),
provider_(new WifiDataProviderCommonWithMock),
wlan_api_(provider_->wlan_api_.get()),
polling_policy_(provider_->polling_policy_.get()) {}
......@@ -97,7 +100,7 @@ class GeolocationWifiDataProviderCommonTest : public testing::Test {
}
protected:
const base::MessageLoopForUI message_loop_;
const base::test::ScopedTaskEnvironment scoped_task_environment_;
WifiDataProviderManager::WifiDataUpdateCallback wifi_data_callback_;
const scoped_refptr<WifiDataProviderCommonWithMock> provider_;
......
......@@ -10,8 +10,8 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
......@@ -97,9 +97,13 @@ class GeolocationWifiDataProviderLinuxTest : public testing::Test {
}
protected:
GeolocationWifiDataProviderLinuxTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
// WifiDataProvider requires a task runner to be present. The |message_loop_|
// is defined here, as it should outlive |wifi_provider_linux_|.
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
scoped_refptr<dbus::MockBus> mock_bus_;
scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_;
scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_;
......
......@@ -5,6 +5,7 @@
#include <list>
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_io_thread.h"
#include "device/base/mock_device_client.h"
#include "device/hid/mock_hid_service.h"
......@@ -17,7 +18,10 @@ namespace device {
class U2fRegisterTest : public testing::Test {
public:
U2fRegisterTest() : io_thread_(base::TestIOThread::kAutoStart) {}
U2fRegisterTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
io_thread_(base::TestIOThread::kAutoStart) {}
void SetUp() override {
MockHidService* hid_service = device_client_.hid_service();
......@@ -25,7 +29,7 @@ class U2fRegisterTest : public testing::Test {
}
protected:
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::TestIOThread io_thread_;
device::MockDeviceClient device_client_;
};
......
......@@ -6,6 +6,7 @@
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_io_thread.h"
#include "device/base/mock_device_client.h"
#include "device/hid/mock_hid_service.h"
......@@ -65,10 +66,13 @@ class TestResponseCallback {
class U2fRequestTest : public testing::Test {
public:
U2fRequestTest() : io_thread_(base::TestIOThread::kAutoStart) {}
U2fRequestTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
io_thread_(base::TestIOThread::kAutoStart) {}
protected:
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::TestIOThread io_thread_;
device::MockDeviceClient device_client_;
};
......
......@@ -5,6 +5,7 @@
#include <list>
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_io_thread.h"
#include "device/base/mock_device_client.h"
#include "device/hid/mock_hid_service.h"
......@@ -16,7 +17,10 @@
namespace device {
class U2fSignTest : public testing::Test {
public:
U2fSignTest() : io_thread_(base::TestIOThread::kAutoStart) {}
U2fSignTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
io_thread_(base::TestIOThread::kAutoStart) {}
void SetUp() override {
MockHidService* hid_service = device_client_.hid_service();
......@@ -24,7 +28,7 @@ class U2fSignTest : public testing::Test {
}
protected:
base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::TestIOThread io_thread_;
device::MockDeviceClient device_client_;
};
......
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