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 sync_driver { | 25 namespace syncer { |
26 class SyncClient; | 26 class SyncClient; |
27 } // namespace sync_driver | 27 } // namespace syncer |
28 | 28 |
29 namespace sync_driver_v2 { | 29 namespace syncer { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 syncer::ModelType kTestModelType = syncer::AUTOFILL; | 33 ModelType kTestModelType = 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(sync_driver::SyncClient* sync_client) | 39 explicit TestDataTypeController(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 | 54 class MockModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { |
55 : public syncer_v2::FakeModelTypeChangeProcessor { | |
56 public: | 55 public: |
57 explicit MockModelTypeChangeProcessor(int* disable_sync_call_count) | 56 explicit MockModelTypeChangeProcessor(int* disable_sync_call_count) |
58 : disable_sync_call_count_(disable_sync_call_count) {} | 57 : disable_sync_call_count_(disable_sync_call_count) {} |
59 | 58 |
60 void DisableSync() override { (*disable_sync_call_count_)++; } | 59 void DisableSync() override { (*disable_sync_call_count_)++; } |
61 | 60 |
62 private: | 61 private: |
63 int* disable_sync_call_count_; | 62 int* disable_sync_call_count_; |
64 }; | 63 }; |
65 | 64 |
66 class NonBlockingDataTypeControllerTest : public testing::Test { | 65 class NonBlockingDataTypeControllerTest : public testing::Test { |
67 public: | 66 public: |
68 NonBlockingDataTypeControllerTest() | 67 NonBlockingDataTypeControllerTest() |
69 : disable_sync_call_count_(0), | 68 : disable_sync_call_count_(0), |
70 sync_prefs_(sync_client_.GetPrefService()), | 69 sync_prefs_(sync_client_.GetPrefService()), |
71 model_type_service_( | 70 model_type_service_( |
72 base::Bind(&NonBlockingDataTypeControllerTest::CreateProcessor, | 71 base::Bind(&NonBlockingDataTypeControllerTest::CreateProcessor, |
73 base::Unretained(this))), | 72 base::Unretained(this))), |
74 controller_(&sync_client_) {} | 73 controller_(&sync_client_) {} |
75 | 74 |
76 void SetUp() override { | 75 void SetUp() override { |
77 sync_client_.SetModelTypeService(&model_type_service_); | 76 sync_client_.SetModelTypeService(&model_type_service_); |
78 } | 77 } |
79 | 78 |
80 void TearDown() override { PumpLoop(); } | 79 void TearDown() override { PumpLoop(); } |
81 | 80 |
82 protected: | 81 protected: |
83 std::unique_ptr<syncer_v2::ModelTypeChangeProcessor> CreateProcessor( | 82 std::unique_ptr<ModelTypeChangeProcessor> CreateProcessor( |
84 syncer::ModelType type, | 83 ModelType type, |
85 syncer_v2::ModelTypeService* service) { | 84 ModelTypeService* service) { |
86 auto processor = base::MakeUnique<MockModelTypeChangeProcessor>( | 85 auto processor = base::MakeUnique<MockModelTypeChangeProcessor>( |
87 &disable_sync_call_count_); | 86 &disable_sync_call_count_); |
88 processor_ = processor.get(); | 87 processor_ = processor.get(); |
89 return std::move(processor); | 88 return std::move(processor); |
90 } | 89 } |
91 | 90 |
92 // Gets controller from NOT_RUNNING to RUNNING state. | 91 // Gets controller from NOT_RUNNING to RUNNING state. |
93 void ActivateController() { | 92 void ActivateController() { |
94 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, | 93 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
95 controller_.state()); | |
96 controller_.LoadModels( | 94 controller_.LoadModels( |
97 base::Bind(&NonBlockingDataTypeControllerTest::LoadModelsDone, | 95 base::Bind(&NonBlockingDataTypeControllerTest::LoadModelsDone, |
98 base::Unretained(this))); | 96 base::Unretained(this))); |
99 | 97 |
100 EXPECT_EQ(sync_driver::DataTypeController::MODEL_STARTING, | 98 EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_.state()); |
101 controller_.state()); | |
102 PumpLoop(); | 99 PumpLoop(); |
103 EXPECT_EQ(sync_driver::DataTypeController::MODEL_LOADED, | 100 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_.state()); |
104 controller_.state()); | |
105 controller_.StartAssociating( | 101 controller_.StartAssociating( |
106 base::Bind(&NonBlockingDataTypeControllerTest::AssociationDone, | 102 base::Bind(&NonBlockingDataTypeControllerTest::AssociationDone, |
107 base::Unretained(this))); | 103 base::Unretained(this))); |
108 EXPECT_EQ(sync_driver::DataTypeController::RUNNING, controller_.state()); | 104 EXPECT_EQ(DataTypeController::RUNNING, controller_.state()); |
109 } | 105 } |
110 | 106 |
111 void LoadModelsDone(syncer::ModelType type, const syncer::SyncError& error) {} | 107 void LoadModelsDone(ModelType type, const SyncError& error) {} |
112 | 108 |
113 void AssociationDone(sync_driver::DataTypeController::ConfigureResult result, | 109 void AssociationDone(DataTypeController::ConfigureResult result, |
114 const syncer::SyncMergeResult& local_merge_result, | 110 const SyncMergeResult& local_merge_result, |
115 const syncer::SyncMergeResult& syncer_merge_result) {} | 111 const SyncMergeResult& syncer_merge_result) {} |
116 | 112 |
117 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 113 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
118 | 114 |
119 int disable_sync_call_count_; | 115 int disable_sync_call_count_; |
120 base::MessageLoop message_loop_; | 116 base::MessageLoop message_loop_; |
121 sync_driver::FakeSyncClient sync_client_; | 117 FakeSyncClient sync_client_; |
122 sync_driver::SyncPrefs sync_prefs_; | 118 SyncPrefs sync_prefs_; |
123 MockModelTypeChangeProcessor* processor_; | 119 MockModelTypeChangeProcessor* processor_; |
124 syncer_v2::StubModelTypeService model_type_service_; | 120 StubModelTypeService model_type_service_; |
125 TestDataTypeController controller_; | 121 TestDataTypeController controller_; |
126 }; | 122 }; |
127 | 123 |
128 // Test emulates normal browser shutdown. Ensures that DisableSync is not | 124 // Test emulates normal browser shutdown. Ensures that DisableSync is not |
129 // called. | 125 // called. |
130 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeEnabled) { | 126 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeEnabled) { |
131 // Enable datatype through preferences. | 127 // Enable datatype through preferences. |
132 sync_prefs_.SetFirstSetupComplete(); | 128 sync_prefs_.SetFirstSetupComplete(); |
133 sync_prefs_.SetKeepEverythingSynced(true); | 129 sync_prefs_.SetKeepEverythingSynced(true); |
134 | 130 |
135 ActivateController(); | 131 ActivateController(); |
136 | 132 |
137 controller_.Stop(); | 133 controller_.Stop(); |
138 PumpLoop(); | 134 PumpLoop(); |
139 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); | 135 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
140 // Ensure that DisableSync is not called and service still has valid change | 136 // Ensure that DisableSync is not called and service still has valid change |
141 // processor. | 137 // processor. |
142 EXPECT_EQ(0, disable_sync_call_count_); | 138 EXPECT_EQ(0, disable_sync_call_count_); |
143 EXPECT_TRUE(model_type_service_.HasChangeProcessor()); | 139 EXPECT_TRUE(model_type_service_.HasChangeProcessor()); |
144 } | 140 } |
145 | 141 |
146 // Test emulates scenario when user disables datatype. DisableSync should be | 142 // Test emulates scenario when user disables datatype. DisableSync should be |
147 // called. | 143 // called. |
148 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeDisabled) { | 144 TEST_F(NonBlockingDataTypeControllerTest, StopWhenDatatypeDisabled) { |
149 // Enable datatype through preferences. | 145 // Enable datatype through preferences. |
150 sync_prefs_.SetFirstSetupComplete(); | 146 sync_prefs_.SetFirstSetupComplete(); |
151 sync_prefs_.SetKeepEverythingSynced(true); | 147 sync_prefs_.SetKeepEverythingSynced(true); |
152 ActivateController(); | 148 ActivateController(); |
153 | 149 |
154 // Disable datatype through preferences. | 150 // Disable datatype through preferences. |
155 sync_prefs_.SetKeepEverythingSynced(false); | 151 sync_prefs_.SetKeepEverythingSynced(false); |
156 sync_prefs_.SetPreferredDataTypes(syncer::ModelTypeSet(kTestModelType), | 152 sync_prefs_.SetPreferredDataTypes(ModelTypeSet(kTestModelType), |
157 syncer::ModelTypeSet()); | 153 ModelTypeSet()); |
158 | 154 |
159 controller_.Stop(); | 155 controller_.Stop(); |
160 PumpLoop(); | 156 PumpLoop(); |
161 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); | 157 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
162 // Ensure that DisableSync is called and change processor is reset. | 158 // Ensure that DisableSync is called and change processor is reset. |
163 EXPECT_EQ(1, disable_sync_call_count_); | 159 EXPECT_EQ(1, disable_sync_call_count_); |
164 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); | 160 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); |
165 } | 161 } |
166 | 162 |
167 // Test emulates disabling sync by signing out. DisableSync should be called. | 163 // Test emulates disabling sync by signing out. DisableSync should be called. |
168 TEST_F(NonBlockingDataTypeControllerTest, StopWithInitialSyncPrefs) { | 164 TEST_F(NonBlockingDataTypeControllerTest, StopWithInitialSyncPrefs) { |
169 // Enable datatype through preferences. | 165 // Enable datatype through preferences. |
170 sync_prefs_.SetFirstSetupComplete(); | 166 sync_prefs_.SetFirstSetupComplete(); |
171 sync_prefs_.SetKeepEverythingSynced(true); | 167 sync_prefs_.SetKeepEverythingSynced(true); |
172 ActivateController(); | 168 ActivateController(); |
173 | 169 |
174 // Clearing preferences emulates signing out. | 170 // Clearing preferences emulates signing out. |
175 sync_prefs_.ClearPreferences(); | 171 sync_prefs_.ClearPreferences(); |
176 controller_.Stop(); | 172 controller_.Stop(); |
177 PumpLoop(); | 173 PumpLoop(); |
178 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); | 174 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
179 // Ensure that DisableSync is called and change processor is reset. | 175 // Ensure that DisableSync is called and change processor is reset. |
180 EXPECT_EQ(1, disable_sync_call_count_); | 176 EXPECT_EQ(1, disable_sync_call_count_); |
181 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); | 177 EXPECT_FALSE(model_type_service_.HasChangeProcessor()); |
182 } | 178 } |
183 | 179 |
184 // Test emulates disabling sync when datatype is not loaded yet. DisableSync | 180 // Test emulates disabling sync when datatype is not loaded yet. DisableSync |
185 // should not be called as service is potentially not ready to handle it. | 181 // should not be called as service is potentially not ready to handle it. |
186 TEST_F(NonBlockingDataTypeControllerTest, StopBeforeLoadModels) { | 182 TEST_F(NonBlockingDataTypeControllerTest, StopBeforeLoadModels) { |
187 // Enable datatype through preferences. | 183 // Enable datatype through preferences. |
188 sync_prefs_.SetFirstSetupComplete(); | 184 sync_prefs_.SetFirstSetupComplete(); |
189 sync_prefs_.SetKeepEverythingSynced(true); | 185 sync_prefs_.SetKeepEverythingSynced(true); |
190 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); | 186 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
191 | 187 |
192 // Clearing preferences emulates signing out. | 188 // Clearing preferences emulates signing out. |
193 sync_prefs_.ClearPreferences(); | 189 sync_prefs_.ClearPreferences(); |
194 controller_.Stop(); | 190 controller_.Stop(); |
195 PumpLoop(); | 191 PumpLoop(); |
196 EXPECT_EQ(sync_driver::DataTypeController::NOT_RUNNING, controller_.state()); | 192 EXPECT_EQ(DataTypeController::NOT_RUNNING, controller_.state()); |
197 // Ensure that DisableSync is not called. | 193 // Ensure that DisableSync is not called. |
198 EXPECT_EQ(0, disable_sync_call_count_); | 194 EXPECT_EQ(0, disable_sync_call_count_); |
199 } | 195 } |
200 | 196 |
201 } // namespace | 197 } // namespace |
202 | 198 |
203 } // namespace sync_driver_v2 | 199 } // namespace syncer |
OLD | NEW |