OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/sync/api/model_type_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "components/sync/api/data_type_error_handler_mock.h" | |
10 #include "components/sync/api/fake_model_type_change_processor.h" | |
11 #include "components/sync/api/stub_model_type_service.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace syncer { | |
15 | |
16 // A mock MTCP that lets us know when DisableSync is called. | |
17 class MockModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { | |
18 public: | |
19 explicit MockModelTypeChangeProcessor(const base::Closure& disabled_callback) | |
20 : disabled_callback_(disabled_callback) {} | |
21 ~MockModelTypeChangeProcessor() override {} | |
22 | |
23 void DisableSync() override { disabled_callback_.Run(); } | |
24 | |
25 private: | |
26 base::Closure disabled_callback_; | |
27 }; | |
28 | |
29 class MockModelTypeService : public StubModelTypeService { | |
30 public: | |
31 MockModelTypeService() | |
32 : StubModelTypeService(base::Bind(&MockModelTypeService::CreateProcessor, | |
33 base::Unretained(this))) {} | |
34 ~MockModelTypeService() override {} | |
35 | |
36 void CreateChangeProcessor() { ModelTypeService::CreateChangeProcessor(); } | |
37 | |
38 MockModelTypeChangeProcessor* change_processor() const { | |
39 return static_cast<MockModelTypeChangeProcessor*>( | |
40 ModelTypeService::change_processor()); | |
41 } | |
42 | |
43 bool on_processor_set_called() const { return on_processor_set_called_; } | |
44 void clear_on_processor_set_called() { on_processor_set_called_ = false; } | |
45 bool processor_disable_sync_called() const { | |
46 return processor_disable_sync_called_; | |
47 } | |
48 | |
49 private: | |
50 std::unique_ptr<ModelTypeChangeProcessor> CreateProcessor( | |
51 ModelType type, | |
52 ModelTypeService* service) { | |
53 return base::MakeUnique<MockModelTypeChangeProcessor>(base::Bind( | |
54 &MockModelTypeService::OnProcessorDisableSync, base::Unretained(this))); | |
55 } | |
56 | |
57 void OnChangeProcessorSet() override { on_processor_set_called_ = true; } | |
58 | |
59 void OnProcessorDisableSync() { processor_disable_sync_called_ = true; } | |
60 | |
61 bool on_processor_set_called_ = false; | |
62 bool processor_disable_sync_called_ = false; | |
63 }; | |
64 | |
65 class ModelTypeServiceTest : public ::testing::Test { | |
66 public: | |
67 ModelTypeServiceTest() {} | |
68 ~ModelTypeServiceTest() override {} | |
69 | |
70 void OnSyncStarting() { | |
71 service_.OnSyncStarting( | |
72 base::MakeUnique<DataTypeErrorHandlerMock>(), | |
73 base::Bind(&ModelTypeServiceTest::OnProcessorStarted, | |
74 base::Unretained(this))); | |
75 } | |
76 | |
77 bool start_callback_called() const { return start_callback_called_; } | |
78 MockModelTypeService* service() { return &service_; } | |
79 | |
80 private: | |
81 void OnProcessorStarted( | |
82 SyncError error, | |
83 std::unique_ptr<ActivationContext> activation_context) { | |
84 start_callback_called_ = true; | |
85 } | |
86 | |
87 bool start_callback_called_; | |
88 MockModelTypeService service_; | |
89 }; | |
90 | |
91 // CreateChangeProcessor should construct a processor and call | |
92 // OnChangeProcessorSet if and only if one doesn't already exist. | |
93 TEST_F(ModelTypeServiceTest, CreateChangeProcessor) { | |
94 EXPECT_FALSE(service()->change_processor()); | |
95 EXPECT_FALSE(service()->on_processor_set_called()); | |
96 service()->CreateChangeProcessor(); | |
97 ModelTypeChangeProcessor* processor = service()->change_processor(); | |
98 EXPECT_TRUE(processor); | |
99 EXPECT_TRUE(service()->on_processor_set_called()); | |
100 | |
101 // A second call shouldn't make a new processor. | |
102 service()->clear_on_processor_set_called(); | |
103 service()->CreateChangeProcessor(); | |
104 EXPECT_EQ(processor, service()->change_processor()); | |
105 EXPECT_FALSE(service()->on_processor_set_called()); | |
106 } | |
107 | |
108 // OnSyncStarting should create a processor and call OnSyncStarting on it. | |
109 TEST_F(ModelTypeServiceTest, OnSyncStarting) { | |
110 EXPECT_FALSE(service()->change_processor()); | |
111 OnSyncStarting(); | |
112 EXPECT_TRUE(service()->change_processor()); | |
113 // FakeModelTypeProcessor is the one that calls the callback, so if it was | |
114 // called then we know the call on the processor was made. | |
115 EXPECT_TRUE(start_callback_called()); | |
116 } | |
117 | |
118 // DisableSync should call DisableSync on the processor and then delete it. | |
119 TEST_F(ModelTypeServiceTest, DisableSync) { | |
120 service()->CreateChangeProcessor(); | |
121 EXPECT_TRUE(service()->change_processor()); | |
122 EXPECT_FALSE(service()->processor_disable_sync_called()); | |
123 | |
124 service()->DisableSync(); | |
125 EXPECT_FALSE(service()->change_processor()); | |
126 EXPECT_TRUE(service()->processor_disable_sync_called()); | |
127 } | |
128 | |
129 // ResolveConflicts should return USE_REMOTE unless the remote data is deleted. | |
130 TEST_F(ModelTypeServiceTest, DefaultConflictResolution) { | |
131 EntityData local_data; | |
132 EntityData remote_data; | |
133 | |
134 // There is no deleted/deleted case because that's not a conflict. | |
135 | |
136 local_data.specifics.mutable_preference()->set_value("value"); | |
137 EXPECT_FALSE(local_data.is_deleted()); | |
138 EXPECT_TRUE(remote_data.is_deleted()); | |
139 EXPECT_EQ(ConflictResolution::USE_LOCAL, | |
140 service()->ResolveConflict(local_data, remote_data).type()); | |
141 | |
142 remote_data.specifics.mutable_preference()->set_value("value"); | |
143 EXPECT_FALSE(local_data.is_deleted()); | |
144 EXPECT_FALSE(remote_data.is_deleted()); | |
145 EXPECT_EQ(ConflictResolution::USE_REMOTE, | |
146 service()->ResolveConflict(local_data, remote_data).type()); | |
147 | |
148 local_data.specifics.clear_preference(); | |
149 EXPECT_TRUE(local_data.is_deleted()); | |
150 EXPECT_FALSE(remote_data.is_deleted()); | |
151 EXPECT_EQ(ConflictResolution::USE_REMOTE, | |
152 service()->ResolveConflict(local_data, remote_data).type()); | |
153 } | |
154 | |
155 } // namespace syncer | |
OLD | NEW |