OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/driver/non_blocking_data_type_controller.h" | 5 #include "components/sync/driver/non_blocking_data_type_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "components/sync/api/fake_model_type_change_processor.h" | 18 #include "components/sync/api/fake_model_type_change_processor.h" |
19 #include "components/sync/api/stub_model_type_service.h" | 19 #include "components/sync/api/stub_model_type_service.h" |
20 #include "components/sync/base/model_type.h" | 20 #include "components/sync/base/model_type.h" |
21 #include "components/sync/driver/fake_sync_client.h" | 21 #include "components/sync/driver/fake_sync_client.h" |
22 #include "components/sync/driver/sync_prefs.h" | 22 #include "components/sync/driver/sync_prefs.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace syncer { | 25 namespace sync_driver { |
26 class SyncClient; | 26 class SyncClient; |
27 } // namespace syncer | 27 } // namespace sync_driver |
28 | 28 |
29 namespace syncer { | 29 namespace sync_driver_v2 { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 ModelType kTestModelType = AUTOFILL; | 33 syncer::ModelType kTestModelType = syncer::AUTOFILL; |
34 | 34 |
35 // Implementation of NonBlockingDataTypeController being tested. | 35 // Implementation of NonBlockingDataTypeController being tested. |
36 // It posts all tasks to current thread. | 36 // It posts all tasks to current thread. |
37 class TestDataTypeController : public NonBlockingDataTypeController { | 37 class TestDataTypeController : public NonBlockingDataTypeController { |
38 public: | 38 public: |
39 explicit TestDataTypeController(SyncClient* sync_client) | 39 explicit TestDataTypeController(sync_driver::SyncClient* sync_client) |
40 : NonBlockingDataTypeController(kTestModelType, | 40 : NonBlockingDataTypeController(kTestModelType, |
41 base::Closure(), | 41 base::Closure(), |
42 sync_client) {} | 42 sync_client) {} |
43 ~TestDataTypeController() override {} | 43 ~TestDataTypeController() override {} |
44 | 44 |
45 protected: | 45 protected: |
46 bool RunOnModelThread(const tracked_objects::Location& from_here, | 46 bool RunOnModelThread(const tracked_objects::Location& from_here, |
47 const base::Closure& task) override { | 47 const base::Closure& task) override { |
48 base::ThreadTaskRunnerHandle::Get()->PostTask(from_here, task); | 48 base::ThreadTaskRunnerHandle::Get()->PostTask(from_here, task); |
49 return true; | 49 return true; |
50 } | 50 } |
51 }; | 51 }; |
52 | 52 |
53 // Mock change processor to observe calls to DisableSync. | 53 // Mock change processor to observe calls to DisableSync. |
54 class MockModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { | 54 class MockModelTypeChangeProcessor |
| 55 : public syncer_v2::FakeModelTypeChangeProcessor { |
55 public: | 56 public: |
56 explicit MockModelTypeChangeProcessor(int* disable_sync_call_count) | 57 explicit MockModelTypeChangeProcessor(int* disable_sync_call_count) |
57 : disable_sync_call_count_(disable_sync_call_count) {} | 58 : disable_sync_call_count_(disable_sync_call_count) {} |
58 | 59 |
59 void DisableSync() override { (*disable_sync_call_count_)++; } | 60 void DisableSync() override { (*disable_sync_call_count_)++; } |
60 | 61 |
61 private: | 62 private: |
62 int* disable_sync_call_count_; | 63 int* disable_sync_call_count_; |
63 }; | 64 }; |
64 | 65 |
65 class NonBlockingDataTypeControllerTest : public testing::Test { | 66 class NonBlockingDataTypeControllerTest : public testing::Test { |
66 public: | 67 public: |
67 NonBlockingDataTypeControllerTest() | 68 NonBlockingDataTypeControllerTest() |
68 : disable_sync_call_count_(0), | 69 : disable_sync_call_count_(0), |
69 sync_prefs_(sync_client_.GetPrefService()), | 70 sync_prefs_(sync_client_.GetPrefService()), |
70 model_type_service_( | 71 model_type_service_( |
71 base::Bind(&NonBlockingDataTypeControllerTest::CreateProcessor, | 72 base::Bind(&NonBlockingDataTypeControllerTest::CreateProcessor, |
72 base::Unretained(this))), | 73 base::Unretained(this))), |
73 controller_(&sync_client_) {} | 74 controller_(&sync_client_) {} |
74 | 75 |
75 void SetUp() override { | 76 void SetUp() override { |
76 sync_client_.SetModelTypeService(&model_type_service_); | 77 sync_client_.SetModelTypeService(&model_type_service_); |
77 } | 78 } |
78 | 79 |
79 void TearDown() override { PumpLoop(); } | 80 void TearDown() override { PumpLoop(); } |
80 | 81 |
81 protected: | 82 protected: |
82 std::unique_ptr<ModelTypeChangeProcessor> CreateProcessor( | 83 std::unique_ptr<syncer_v2::ModelTypeChangeProcessor> CreateProcessor( |
83 ModelType type, | 84 syncer::ModelType type, |
84 ModelTypeService* service) { | 85 syncer_v2::ModelTypeService* service) { |
85 auto processor = base::MakeUnique<MockModelTypeChangeProcessor>( | 86 auto processor = base::MakeUnique<MockModelTypeChangeProcessor>( |
86 &disable_sync_call_count_); | 87 &disable_sync_call_count_); |
87 processor_ = processor.get(); | 88 processor_ = processor.get(); |
88 return std::move(processor); | 89 return std::move(processor); |
89 } | 90 } |
90 | 91 |
91 // Gets controller from NOT_RUNNING to RUNNING state. | 92 // Gets controller from NOT_RUNNING to RUNNING state. |
92 void ActivateController() { | 93 void ActivateController() { |
93 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 94 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, |
| 95 controller_.state()); |
94 controller_.LoadModels( | 96 controller_.LoadModels( |
95 base::Bind(&NonBlockingDataTypeControllerTest::LoadModelsDone, | 97 base::Bind(&NonBlockingDataTypeControllerTest::LoadModelsDone, |
96 base::Unretained(this))); | 98 base::Unretained(this))); |
97 | 99 |
98 EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_.state()); | 100 EXPECT_EQ(sync_driver::DataTypeController::MODEL_STARTING, |
| 101 controller_.state()); |
99 PumpLoop(); | 102 PumpLoop(); |
100 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_.state()); | 103 EXPECT_EQ(sync_driver::DataTypeController::MODEL_LOADED, |
| 104 controller_.state()); |
101 controller_.StartAssociating( | 105 controller_.StartAssociating( |
102 base::Bind(&NonBlockingDataTypeControllerTest::AssociationDone, | 106 base::Bind(&NonBlockingDataTypeControllerTest::AssociationDone, |
103 base::Unretained(this))); | 107 base::Unretained(this))); |
104 EXPECT_EQ(DataTypeController::RUNNING, controller_.state()); | 108 EXPECT_EQ(sync_driver::DataTypeController::RUNNING, controller_.state()); |
105 } | 109 } |
106 | 110 |
107 void LoadModelsDone(ModelType type, const SyncError& error) {} | 111 void LoadModelsDone(syncer::ModelType type, const syncer::SyncError& error) {} |
108 | 112 |
109 void AssociationDone(DataTypeController::ConfigureResult result, | 113 void AssociationDone(sync_driver::DataTypeController::ConfigureResult result, |
110 const SyncMergeResult& local_merge_result, | 114 const syncer::SyncMergeResult& local_merge_result, |
111 const SyncMergeResult& syncer_merge_result) {} | 115 const syncer::SyncMergeResult& syncer_merge_result) {} |
112 | 116 |
113 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 117 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
114 | 118 |
115 int disable_sync_call_count_; | 119 int disable_sync_call_count_; |
116 base::MessageLoop message_loop_; | 120 base::MessageLoop message_loop_; |
117 FakeSyncClient sync_client_; | 121 sync_driver::FakeSyncClient sync_client_; |
118 SyncPrefs sync_prefs_; | 122 sync_driver::SyncPrefs sync_prefs_; |
119 MockModelTypeChangeProcessor* processor_; | 123 MockModelTypeChangeProcessor* processor_; |
120 StubModelTypeService model_type_service_; | 124 syncer_v2::StubModelTypeService model_type_service_; |
121 TestDataTypeController controller_; | 125 TestDataTypeController controller_; |
122 }; | 126 }; |
123 | 127 |
124 // Test emulates normal browser shutdown. Ensures that DisableSync is not | 128 // Test emulates normal browser shutdown. Ensures that DisableSync is not |
125 // called. | 129 // called. |
126 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeEnabled) { | 130 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeEnabled) { |
127 // Enable datatype through preferences. | 131 // Enable datatype through preferences. |
128 sync_prefs_.SetFirstSetupComplete(); | 132 sync_prefs_.SetFirstSetupComplete(); |
129 sync_prefs_.SetKeepEverythingSynced(true); | 133 sync_prefs_.SetKeepEverythingSynced(true); |
130 | 134 |
131 ActivateController(); | 135 ActivateController(); |
132 | 136 |
133 controller_.Stop(); | 137 controller_.Stop(); |
134 PumpLoop(); | 138 PumpLoop(); |
135 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 139 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); |
136 // Ensure that DisableSync is not called and service still has valid change | 140 // Ensure that DisableSync is not called and service still has valid change |
137 // processor. | 141 // processor. |
138 EXPECT_EQ(0, disable_sync_call_count_); | 142 EXPECT_EQ(0, disable_sync_call_count_); |
139 EXPECT_TRUE(model_type_service_.HasChangeProcessor()); | 143 EXPECT_TRUE(model_type_service_.HasChangeProcessor()); |
140 } | 144 } |
141 | 145 |
142 // Test emulates scenario when user disables datatype. DisableSync should be | 146 // Test emulates scenario when user disables datatype. DisableSync should be |
143 // called. | 147 // called. |
144 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeDisabled) { | 148 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeDisabled) { |
145 // Enable datatype through preferences. | 149 // Enable datatype through preferences. |
146 sync_prefs_.SetFirstSetupComplete(); | 150 sync_prefs_.SetFirstSetupComplete(); |
147 sync_prefs_.SetKeepEverythingSynced(true); | 151 sync_prefs_.SetKeepEverythingSynced(true); |
148 ActivateController(); | 152 ActivateController(); |
149 | 153 |
150 // Disable datatype through preferences. | 154 // Disable datatype through preferences. |
151 sync_prefs_.SetKeepEverythingSynced(false); | 155 sync_prefs_.SetKeepEverythingSynced(false); |
152 sync_prefs_.SetPreferredDataTypes(ModelTypeSet(kTestModelType), | 156 sync_prefs_.SetPreferredDataTypes(syncer::ModelTypeSet(kTestModelType), |
153 ModelTypeSet()); | 157 syncer::ModelTypeSet()); |
154 | 158 |
155 controller_.Stop(); | 159 controller_.Stop(); |
156 PumpLoop(); | 160 PumpLoop(); |
157 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 161 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); |
158 // Ensure that DisableSync is called and change processor is reset. | 162 // Ensure that DisableSync is called and change processor is reset. |
159 EXPECT_EQ(1, disable_sync_call_count_); | 163 EXPECT_EQ(1, disable_sync_call_count_); |
160 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); | 164 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); |
161 } | 165 } |
162 | 166 |
163 // Test emulates disabling sync by signing out. DisableSync should be called. | 167 // Test emulates disabling sync by signing out. DisableSync should be called. |
164 TEST_F(NonBlockingDataTypeControllerTest, StopWithInitialSyncPrefs) { | 168 TEST_F(NonBlockingDataTypeControllerTest, StopWithInitialSyncPrefs) { |
165 // Enable datatype through preferences. | 169 // Enable datatype through preferences. |
166 sync_prefs_.SetFirstSetupComplete(); | 170 sync_prefs_.SetFirstSetupComplete(); |
167 sync_prefs_.SetKeepEverythingSynced(true); | 171 sync_prefs_.SetKeepEverythingSynced(true); |
168 ActivateController(); | 172 ActivateController(); |
169 | 173 |
170 // Clearing preferences emulates signing out. | 174 // Clearing preferences emulates signing out. |
171 sync_prefs_.ClearPreferences(); | 175 sync_prefs_.ClearPreferences(); |
172 controller_.Stop(); | 176 controller_.Stop(); |
173 PumpLoop(); | 177 PumpLoop(); |
174 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 178 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); |
175 // Ensure that DisableSync is called and change processor is reset. | 179 // Ensure that DisableSync is called and change processor is reset. |
176 EXPECT_EQ(1, disable_sync_call_count_); | 180 EXPECT_EQ(1, disable_sync_call_count_); |
177 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); | 181 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); |
178 } | 182 } |
179 | 183 |
180 // Test emulates disabling sync when datatype is not loaded yet. DisableSync | 184 // Test emulates disabling sync when datatype is not loaded yet. DisableSync |
181 // should not be called as service is potentially not ready to handle it. | 185 // should not be called as service is potentially not ready to handle it. |
182 TEST_F(NonBlockingDataTypeControllerTest, StopBeforeLoadModels) { | 186 TEST_F(NonBlockingDataTypeControllerTest, StopBeforeLoadModels) { |
183 // Enable datatype through preferences. | 187 // Enable datatype through preferences. |
184 sync_prefs_.SetFirstSetupComplete(); | 188 sync_prefs_.SetFirstSetupComplete(); |
185 sync_prefs_.SetKeepEverythingSynced(true); | 189 sync_prefs_.SetKeepEverythingSynced(true); |
186 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 190 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); |
187 | 191 |
188 // Clearing preferences emulates signing out. | 192 // Clearing preferences emulates signing out. |
189 sync_prefs_.ClearPreferences(); | 193 sync_prefs_.ClearPreferences(); |
190 controller_.Stop(); | 194 controller_.Stop(); |
191 PumpLoop(); | 195 PumpLoop(); |
192 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); | 196 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); |
193 // Ensure that DisableSync is not called. | 197 // Ensure that DisableSync is not called. |
194 EXPECT_EQ(0, disable_sync_call_count_); | 198 EXPECT_EQ(0, disable_sync_call_count_); |
195 } | 199 } |
196 | 200 |
197 } // namespace | 201 } // namespace |
198 | 202 |
199 } // namespace syncer | 203 } // namespace sync_driver_v2 |
OLD | NEW |