Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: components/sync_driver/non_ui_model_type_controller_unittest.cc

Issue 1717433002: [USS] Break NonBlockingDataTypeController into UI specific one and non-UI specific one (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: typo Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/sync_driver/non_ui_model_type_controller_unittest.cc
diff --git a/components/sync_driver/non_blocking_data_type_controller_unittest.cc b/components/sync_driver/non_ui_model_type_controller_unittest.cc
similarity index 64%
copy from components/sync_driver/non_blocking_data_type_controller_unittest.cc
copy to components/sync_driver/non_ui_model_type_controller_unittest.cc
index 21416077e89cdee3251cf3e6de2f47bf7cbfe588..a309c36a04f34b4ad6f19ab6b1e1b3928be3c6d8 100644
--- a/components/sync_driver/non_blocking_data_type_controller_unittest.cc
+++ b/components/sync_driver/non_ui_model_type_controller_unittest.cc
@@ -1,8 +1,8 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/sync_driver/non_blocking_data_type_controller.h"
+#include "components/sync_driver/non_ui_model_type_controller.h"
#include <utility>
@@ -28,51 +28,37 @@ namespace sync_driver_v2 {
namespace {
-// Test controller derived from NonBlockingDataTypeController.
-class TestController : public NonBlockingDataTypeController {
+// Test controller derived from NonUIModelTypeController.
+class TestNonUIModelTypeController : public NonUIModelTypeController {
public:
- TestController(const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
- const base::Closure& error_callback,
- syncer::ModelType model_type,
- sync_driver::SyncClient* sync_client)
- : NonBlockingDataTypeController(ui_thread,
- error_callback,
- model_type,
- sync_client),
- model_type_(model_type) {}
-
- // TODO(stanisc): This will likely have to change. It should be controller's
- // job to locate the service via SyncClient, create an instance of
- // SharedModelTypeProcessor, pass it to the service to own, and to store the
- // weak pointer to the type processor. For now this continues using the
- // earlier design where the controller and it's task runner are initialized
- // from outside.
- // Note that for the test purposes a nullptr |model_task_runner| indicates a
- // special case of running on UI thread (see RunOnModelThread below).
- void Initialize(const scoped_refptr<base::TaskRunner>& model_task_runner,
- const base::WeakPtr<syncer_v2::SharedModelTypeProcessor>&
- type_processor) {
- model_task_runner_ = model_task_runner;
- type_processor_ = type_processor;
+ TestNonUIModelTypeController(
+ const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
+ const scoped_refptr<base::TaskRunner>& model_task_runner,
+ const base::Closure& error_callback,
+ syncer::ModelType model_type,
+ sync_driver::SyncClient* sync_client)
+ : NonUIModelTypeController(ui_thread,
+ error_callback,
+ model_type,
+ sync_client),
+ model_task_runner_(model_task_runner) {}
+
+ base::WeakPtr<syncer_v2::SharedModelTypeProcessor> get_type_processor()
+ const {
+ return type_processor();
}
- syncer::ModelType type() const override { return model_type_; }
-
bool RunOnModelThread(const tracked_objects::Location& from_here,
const base::Closure& task) override {
- if (model_task_runner_) {
- return model_task_runner_->PostTask(from_here, task);
- } else {
- // Special case for model running on the UI thread.
- task.Run();
- return true;
- }
+ DCHECK(model_task_runner_);
+ return model_task_runner_->PostTask(from_here, task);
}
+ void InitializeProcessorInTest() { InitializeProcessor(); }
+
private:
- ~TestController() override {}
+ ~TestNonUIModelTypeController() override {}
- syncer::ModelType model_type_;
scoped_refptr<base::TaskRunner> model_task_runner_;
};
@@ -161,10 +147,10 @@ class MockBackendDataTypeConfigurer
} // namespace
-class NonBlockingDataTypeControllerTest : public testing::Test,
- public sync_driver::FakeSyncClient {
+class NonUIModelTypeControllerTest : public testing::Test,
+ public sync_driver::FakeSyncClient {
public:
- NonBlockingDataTypeControllerTest()
+ NonUIModelTypeControllerTest()
: auto_run_tasks_(true),
load_models_callback_called_(false),
association_callback_called_(false),
@@ -172,63 +158,65 @@ class NonBlockingDataTypeControllerTest : public testing::Test,
sync_thread_runner_(new base::TestSimpleTaskRunner()),
configurer_(&backend_, sync_thread_runner_) {}
- ~NonBlockingDataTypeControllerTest() override {}
+ ~NonUIModelTypeControllerTest() override {}
void SetUp() override {
- controller_ = new TestController(ui_loop_.task_runner(), base::Closure(),
- syncer::DICTIONARY, this);
+ model_thread_.Start();
+ model_thread_runner_ = model_thread_.task_runner();
+ InitializeModelTypeService();
+ controller_ = new TestNonUIModelTypeController(
+ ui_loop_.task_runner(), model_thread_runner_, base::Closure(),
+ syncer::DICTIONARY, this);
+ controller_->InitializeProcessorInTest();
+ InitializeTypeProcessor();
}
void TearDown() override {
- ClearTypeProcessor();
+ ClearModelTypeService();
controller_ = NULL;
RunQueuedUIThreadTasks();
}
base::WeakPtr<syncer_v2::ModelTypeService> GetModelTypeServiceForType(
syncer::ModelType type) override {
- return service_.AsWeakPtr();
+ return service_->AsWeakPtr();
}
protected:
- void CreateTypeProcessor() {
- // TODO(stanisc): Controller should discover the service via SyncClient.
- type_processor_.reset(
- new syncer_v2::SharedModelTypeProcessor(syncer::DICTIONARY, &service_));
- type_processor_for_ui_ = type_processor_->AsWeakPtrForUI();
- }
-
- void InitTypeProcessorOnUIThread() {
- CreateTypeProcessor();
- model_thread_runner_ = ui_loop_.task_runner();
- // Don't pass task runner to the controller in this case. It will be making
- // prompt calls instead of posting tasks.
- controller_->Initialize(nullptr, type_processor_for_ui_);
+ void InitializeTypeProcessor() {
+ if (!model_thread_runner_ ||
+ model_thread_runner_->BelongsToCurrentThread()) {
+ type_processor_ = controller_->get_type_processor();
+ } else {
+ model_thread_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&NonUIModelTypeControllerTest::InitializeTypeProcessor,
+ base::Unretained(this)));
+ RunQueuedModelThreadTasks();
+ }
}
- void InitTypeProcessorOnBackendThread() {
- model_thread_.Start();
- model_thread_runner_ = model_thread_.task_runner();
-
- // TODO(stanisc): It should be controller's job to
- // create TypeProcessor on the model thread.
- model_thread_runner_->PostTask(
- FROM_HERE,
- base::Bind(&NonBlockingDataTypeControllerTest::CreateTypeProcessor,
- base::Unretained(this)));
- RunQueuedModelThreadTasks();
-
- controller_->Initialize(model_thread_runner_, type_processor_for_ui_);
+ void InitializeModelTypeService() {
+ if (!model_thread_runner_ ||
+ model_thread_runner_->BelongsToCurrentThread()) {
+ service_.reset(new syncer_v2::FakeModelTypeService());
+ } else {
+ model_thread_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&NonUIModelTypeControllerTest::InitializeModelTypeService,
+ base::Unretained(this)));
+ RunQueuedModelThreadTasks();
+ }
}
- void ClearTypeProcessor() {
+ void ClearModelTypeService() {
if (!model_thread_runner_ ||
model_thread_runner_->BelongsToCurrentThread()) {
- type_processor_.reset();
+ service_.reset();
} else {
model_thread_runner_->PostTask(
FROM_HERE,
- base::Bind(&NonBlockingDataTypeControllerTest::ClearTypeProcessor,
+ base::Bind(&NonUIModelTypeControllerTest::ClearModelTypeService,
base::Unretained(this)));
RunQueuedModelThreadTasks();
}
@@ -241,7 +229,7 @@ class NonBlockingDataTypeControllerTest : public testing::Test,
} else {
model_thread_runner_->PostTask(
FROM_HERE,
- base::Bind(&NonBlockingDataTypeControllerTest::TestTypeProcessor,
+ base::Bind(&NonUIModelTypeControllerTest::TestTypeProcessor,
base::Unretained(this), isAllowingChanges, isConnected));
RunQueuedModelThreadTasks();
}
@@ -253,29 +241,40 @@ class NonBlockingDataTypeControllerTest : public testing::Test,
make_scoped_ptr(new syncer_v2::MetadataBatch()));
} else {
model_thread_runner_->PostTask(
- FROM_HERE,
- base::Bind(&NonBlockingDataTypeControllerTest::OnMetadataLoaded,
- base::Unretained(this)));
+ FROM_HERE, base::Bind(&NonUIModelTypeControllerTest::OnMetadataLoaded,
+ base::Unretained(this)));
RunQueuedModelThreadTasks();
}
}
void LoadModels() {
- if (!type_processor_->IsAllowingChanges()) {
- OnMetadataLoaded();
- }
- controller_->LoadModels(
- base::Bind(&NonBlockingDataTypeControllerTest::LoadModelsDone,
- base::Unretained(this)));
+ OnMetadataLoadedOnModelThread();
+ controller_->LoadModels(base::Bind(
+ &NonUIModelTypeControllerTest::LoadModelsDone, base::Unretained(this)));
if (auto_run_tasks_) {
RunAllTasks();
}
}
+ void OnMetadataLoadedOnModelThread() {
+ if (model_thread_runner_->BelongsToCurrentThread()) {
+ if (!type_processor_->IsAllowingChanges()) {
+ OnMetadataLoaded();
+ }
+ } else {
+ model_thread_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &NonUIModelTypeControllerTest::OnMetadataLoadedOnModelThread,
+ base::Unretained(this)));
+ RunQueuedModelThreadTasks();
+ }
+ }
+
void StartAssociating() {
controller_->StartAssociating(
- base::Bind(&NonBlockingDataTypeControllerTest::AssociationDone,
+ base::Bind(&NonUIModelTypeControllerTest::AssociationDone,
base::Unretained(this)));
// The callback is expected to be promptly called.
EXPECT_TRUE(association_callback_called_);
@@ -337,9 +336,8 @@ class NonBlockingDataTypeControllerTest : public testing::Test,
association_callback_called_ = true;
}
- scoped_ptr<syncer_v2::SharedModelTypeProcessor> type_processor_;
- base::WeakPtr<syncer_v2::SharedModelTypeProcessor> type_processor_for_ui_;
- scoped_refptr<TestController> controller_;
+ base::WeakPtr<syncer_v2::SharedModelTypeProcessor> type_processor_;
+ scoped_refptr<TestNonUIModelTypeController> controller_;
bool auto_run_tasks_;
bool load_models_callback_called_;
@@ -351,27 +349,15 @@ class NonBlockingDataTypeControllerTest : public testing::Test,
scoped_refptr<base::TestSimpleTaskRunner> sync_thread_runner_;
MockSyncBackend backend_;
MockBackendDataTypeConfigurer configurer_;
- syncer_v2::FakeModelTypeService service_;
+ scoped_ptr<syncer_v2::FakeModelTypeService> service_;
};
-TEST_F(NonBlockingDataTypeControllerTest, InitialState) {
+TEST_F(NonUIModelTypeControllerTest, InitialState) {
EXPECT_EQ(syncer::DICTIONARY, controller_->type());
EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_->state());
}
-TEST_F(NonBlockingDataTypeControllerTest, LoadModelsOnUIThread) {
- InitTypeProcessorOnUIThread();
- TestTypeProcessor(false, false); // not enabled, not connected.
- LoadModels();
- EXPECT_EQ(sync_driver::DataTypeController::MODEL_LOADED,
- controller_->state());
- EXPECT_TRUE(load_models_callback_called_);
- EXPECT_FALSE(load_models_error_.IsSet());
- TestTypeProcessor(true, false); // enabled, not connected.
-}
-
-TEST_F(NonBlockingDataTypeControllerTest, LoadModelsOnBackendThread) {
- InitTypeProcessorOnBackendThread();
+TEST_F(NonUIModelTypeControllerTest, LoadModelsOnBackendThread) {
TestTypeProcessor(false, false); // not enabled, not connected.
SetAutoRunTasks(false);
LoadModels();
@@ -385,8 +371,7 @@ TEST_F(NonBlockingDataTypeControllerTest, LoadModelsOnBackendThread) {
TestTypeProcessor(true, false); // enabled, not connected.
}
-TEST_F(NonBlockingDataTypeControllerTest, LoadModelsTwice) {
- InitTypeProcessorOnUIThread();
+TEST_F(NonUIModelTypeControllerTest, LoadModelsTwice) {
LoadModels();
SetAutoRunTasks(false);
LoadModels();
@@ -396,21 +381,7 @@ TEST_F(NonBlockingDataTypeControllerTest, LoadModelsTwice) {
EXPECT_TRUE(load_models_error_.IsSet());
}
-TEST_F(NonBlockingDataTypeControllerTest, ActivateDataTypeOnUIThread) {
- InitTypeProcessorOnUIThread();
- LoadModels();
- EXPECT_EQ(sync_driver::DataTypeController::MODEL_LOADED,
- controller_->state());
-
- StartAssociating();
- EXPECT_EQ(sync_driver::DataTypeController::RUNNING, controller_->state());
-
- ActivateDataType();
- TestTypeProcessor(true, true); // enabled, connected.
-}
-
-TEST_F(NonBlockingDataTypeControllerTest, ActivateDataTypeOnBackendThread) {
- InitTypeProcessorOnBackendThread();
+TEST_F(NonUIModelTypeControllerTest, ActivateDataTypeOnBackendThread) {
LoadModels();
EXPECT_EQ(sync_driver::DataTypeController::MODEL_LOADED,
controller_->state());
@@ -422,8 +393,7 @@ TEST_F(NonBlockingDataTypeControllerTest, ActivateDataTypeOnBackendThread) {
TestTypeProcessor(true, true); // enabled, connected.
}
-TEST_F(NonBlockingDataTypeControllerTest, Stop) {
- InitTypeProcessorOnBackendThread();
+TEST_F(NonUIModelTypeControllerTest, Stop) {
LoadModels();
StartAssociating();
ActivateDataType();
« no previous file with comments | « components/sync_driver/non_ui_model_type_controller.cc ('k') | components/sync_driver/ui_model_type_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698