Skip to content

Commit

Permalink
Use ScopedTaskEnvironment instead of MessageLoop in components unit t…
Browse files Browse the repository at this point in the history
…ests.

ScopedTaskEnvironment allows usage of ThreadTaskRunnerHandle and
base/task_scheduler/post_task.h within its scope. It should be
instantiated in every unit test 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=sdefresne@chromium.org

Review-Url: https://codereview.chromium.org/2798963003
Cr-Commit-Position: refs/heads/master@{#462510}
  • Loading branch information
fdoray authored and Commit bot committed Apr 6, 2017
1 parent 4cf067d commit 0d83127
Show file tree
Hide file tree
Showing 27 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/autofill/core/browser/autocomplete_history_manager.h"
#include "components/autofill/core/browser/autofill_external_delegate.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ class AutocompleteHistoryManagerTest : public testing::Test {

void TearDown() override { autocomplete_manager_.reset(); }

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
scoped_refptr<MockWebDataService> web_data_service_;
std::unique_ptr<AutocompleteHistoryManager> autocomplete_manager_;
std::unique_ptr<AutofillDriver> autofill_driver_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/test/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -1107,7 +1108,7 @@ class AutofillManagerTest : public testing::Test {
}

protected:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
MockAutofillClient autofill_client_;
std::unique_ptr<MockAutofillDriver> autofill_driver_;
std::unique_ptr<TestAutofillManager> autofill_manager_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/test/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/user_action_tester.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_experiments.h"
Expand Down Expand Up @@ -333,7 +334,7 @@ class AutofillMetricsTest : public testing::Test {
void EnableWalletSync();
void EnableUkmLogging();

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
TestAutofillClient autofill_client_;
std::unique_ptr<AccountTrackerService> account_tracker_;
std::unique_ptr<FakeSigninManagerBase> signin_manager_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "base/run_loop.h"
#include "base/strings/stringprintf.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 "components/autofill/core/browser/webdata/autofill_entry.h"
Expand Down Expand Up @@ -276,7 +277,7 @@ class AutocompleteSyncBridgeTest : public testing::Test {
}

ScopedTempDir temp_dir_;
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
FakeAutofillBackend backend_;
AutofillTable table_;
WebDatabase db_;
Expand Down
3 changes: 2 additions & 1 deletion components/browser_sync/profile_sync_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
Expand Down Expand Up @@ -350,7 +351,7 @@ class ProfileSyncServiceTest : public ::testing::Test {
}

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
ProfileSyncServiceBundle profile_sync_service_bundle_;
std::unique_ptr<ProfileSyncService> service_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/captive_portal/captive_portal_testing_utils.h"
Expand Down Expand Up @@ -113,7 +114,7 @@ class CaptivePortalDetectorTest : public testing::Test,
}

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<CaptivePortalDetector> detector_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>

#include "base/test/scoped_task_environment.h"
#include "base/values.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_server.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ class DataReductionProxyConfiguratorTest : public testing::Test {
}
}

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<DataReductionProxyTestContext> test_context_;
std::unique_ptr<DataReductionProxyConfigurator> config_;
};
Expand Down
3 changes: 2 additions & 1 deletion components/display_compositor/yuv_readback_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_suite.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
Expand Down Expand Up @@ -476,7 +477,7 @@ class YUVReadbackTest : public testing::Test {
gpu::gles2::GLES2Interface* gl_;
std::unique_ptr<display_compositor::GLHelper> helper_;
gl::DisableNullDrawGLBindings enable_pixel_output_;
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};

TEST_F(YUVReadbackTest, YUVReadbackOptTest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/macros.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/google/core/browser/google_pref_names.h"
#include "components/google/core/browser/google_url_tracker_client.h"
Expand Down Expand Up @@ -132,7 +133,7 @@ class GoogleURLTrackerTest : public testing::Test {
void clear_listener_notified() { listener_.clear_notified(); }

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
TestingPrefServiceSimple prefs_;

// Creating this allows us to call
Expand Down
3 changes: 2 additions & 1 deletion components/history/core/browser/history_backend_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/histogram_tester.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "components/favicon_base/favicon_usage_data.h"
Expand Down Expand Up @@ -282,7 +283,7 @@ class HistoryBackendTestBase : public testing::Test {
URLsModifiedList urls_modified_notifications_;
URLsDeletedList urls_deleted_notifications_;

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::FilePath test_dir_;

DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestBase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
Expand Down Expand Up @@ -231,7 +232,7 @@ class WebHistoryServiceTest : public testing::Test {
}

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
FakeProfileOAuth2TokenService token_service_;
AccountTrackerService account_tracker_;
TestSigninClient signin_client_;
Expand Down
3 changes: 2 additions & 1 deletion components/invalidation/impl/gcm_network_channel_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "google_apis/gaia/google_service_auth_error.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ class GCMNetworkChannelTest
}

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
TestGCMNetworkChannelDelegate* delegate_;
std::unique_ptr<GCMNetworkChannel> gcm_network_channel_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/invalidation/impl/fake_invalidation_handler.h"
#include "components/invalidation/impl/fake_invalidation_state_tracker.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ class InvalidationNotifierTestDelegate {
}

private:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<InvalidationNotifier> invalidator_;
};

Expand Down
3 changes: 2 additions & 1 deletion components/nacl/loader/nacl_ipc_adapter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <memory>

#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread_task_runner_handle.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ class NaClIPCAdapterTest : public testing::Test {
return adapter_->Send(&msg);
}

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

scoped_refptr<NaClIPCAdapter> adapter_;

Expand Down
3 changes: 2 additions & 1 deletion components/ntp_tiles/icon_cacher_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/favicon/core/favicon_client.h"
Expand Down Expand Up @@ -177,7 +178,7 @@ class IconCacherTest : public ::testing::Test {

void WaitForTasksToFinish() { task_runner_->RunUntilIdle(); }

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
PopularSites::Site site_;
std::unique_ptr<MockImageFetcher> image_fetcher_;
std::unique_ptr<MockImageDecoder> image_decoder_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "components/history/core/browser/history_backend.h"
#include "components/history/core/browser/history_database.h"
#include "components/history/core/browser/history_service.h"
Expand Down Expand Up @@ -97,7 +98,7 @@ class HQPPerfTestOnePopularURL : public testing::Test {
private:
base::TimeDelta RunTest(const base::string16& text);

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<FakeAutocompleteProviderClient> client_;

scoped_refptr<HistoryQuickProvider> provider_;
Expand Down
3 changes: 2 additions & 1 deletion components/omnibox/browser/zero_suggest_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "components/history/core/browser/top_sites.h"
#include "components/metrics/proto/omnibox_event.pb.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
Expand Down Expand Up @@ -165,7 +166,7 @@ class ZeroSuggestProviderTest : public testing::Test,
void CreatePersonalizedFieldTrial();
void CreateMostVisitedFieldTrial();

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

// Needed for OmniboxFieldTrial::ActivateStaticTrials().
std::unique_ptr<base::FieldTrialList> field_trial_list_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "components/password_manager/core/browser/mock_password_store.h"
#include "components/password_manager/core/browser/stub_password_manager_client.h"
#include "testing/gmock/include/gmock/gmock.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ class PasswordReuseDetectionManagerTest : public ::testing::Test {
protected:
// It's needed for an initialisation of thread runners that are used in
// MockPasswordStore.
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
MockPasswordManagerClient client_;
scoped_refptr<MockPasswordStore> store_;

Expand Down
3 changes: 2 additions & 1 deletion components/prefs/pref_member_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
Expand Down Expand Up @@ -104,7 +105,7 @@ class PrefMemberTestClass {
} // anonymous namespace

class PrefMemberTest : public testing::Test {
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};

TEST_F(PrefMemberTest, BasicGetAndSet) {
Expand Down
3 changes: 2 additions & 1 deletion components/suggestions/image_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_delegate.h"
Expand Down Expand Up @@ -139,7 +140,7 @@ class ImageManagerTest : public testing::Test {
int num_callback_null_called_;
int num_callback_valid_called_;

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

// Under test.
std::unique_ptr<ImageManager> image_manager_;
Expand Down
3 changes: 2 additions & 1 deletion components/sync/base/weak_handle_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "components/sync/base/weak_handle.h"

#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -62,7 +63,7 @@ class WeakHandleTest : public ::testing::Test {
h.Call(from_here, &Base::Test);
}

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};

TEST_F(WeakHandleTest, Uninitialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "components/sync/engine_impl/js_mutation_event_observer.h"

#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/values.h"
#include "components/sync/base/model_type.h"
#include "components/sync/js/js_event_details.h"
Expand All @@ -27,7 +28,7 @@ class JsMutationEventObserverTest : public testing::Test {
private:
// This must be destroyed after the member variables below in order
// for WeakHandles to be destroyed properly.
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

protected:
StrictMock<MockJsEventHandler> mock_js_event_handler_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/location.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/values.h"
#include "components/sync/base/cryptographer.h"
#include "components/sync/base/fake_encryptor.h"
Expand Down Expand Up @@ -33,7 +34,7 @@ class JsSyncEncryptionHandlerObserverTest : public testing::Test {
private:
// This must be destroyed after the member variables below in order
// for WeakHandles to be destroyed properly.
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

protected:
StrictMock<MockJsEventHandler> mock_js_event_handler_;
Expand Down
Loading

0 comments on commit 0d83127

Please sign in to comment.