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 <memory> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "sync/api/fake_model_type_change_processor.h" | |
10 #include "sync/api/fake_model_type_service.h" | |
11 #include "sync/internal_api/public/test/data_type_error_handler_mock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace syncer_v2 { | |
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 FakeModelTypeService { | |
30 public: | |
31 MockModelTypeService() | |
32 : FakeModelTypeService(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 syncer::ModelType type, | |
52 ModelTypeService* service) { | |
53 return base::WrapUnique(new MockModelTypeChangeProcessor( | |
54 base::Bind(&MockModelTypeService::OnProcessorDisableSync, | |
55 base::Unretained(this)))); | |
56 } | |
57 | |
58 void OnChangeProcessorSet() override { on_processor_set_called_ = true; } | |
59 | |
60 void OnProcessorDisableSync() { processor_disable_sync_called_ = true; } | |
61 | |
62 bool on_processor_set_called_ = false; | |
63 bool processor_disable_sync_called_ = false; | |
64 }; | |
65 | |
66 class ModelTypeServiceTest : public ::testing::Test { | |
67 public: | |
68 ModelTypeServiceTest() {} | |
69 ~ModelTypeServiceTest() override {} | |
70 | |
71 void OnSyncStarting() { | |
72 service_.OnSyncStarting( | |
73 &error_handler_, 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 syncer::SyncError error, | |
83 std::unique_ptr<ActivationContext> activation_context) { | |
84 start_callback_called_ = true; | |
85 } | |
86 | |
87 bool start_callback_called_; | |
88 syncer::DataTypeErrorHandlerMock error_handler_; | |
89 MockModelTypeService service_; | |
90 }; | |
91 | |
92 // CreateChangeProcessor should construct a processor and call | |
93 // OnChangeProcessorSet if and only if one doesn't already exist. | |
94 TEST_F(ModelTypeServiceTest, CreateChangeProcessor) { | |
95 EXPECT_FALSE(service()->change_processor()); | |
96 EXPECT_FALSE(service()->on_processor_set_called()); | |
97 service()->CreateChangeProcessor(); | |
98 ModelTypeChangeProcessor* processor = service()->change_processor(); | |
99 EXPECT_TRUE(processor); | |
100 EXPECT_TRUE(service()->on_processor_set_called()); | |
101 | |
102 // A second call shouldn't make a new processor. | |
103 service()->clear_on_processor_set_called(); | |
104 service()->CreateChangeProcessor(); | |
105 EXPECT_EQ(processor, service()->change_processor()); | |
106 EXPECT_FALSE(service()->on_processor_set_called()); | |
107 } | |
108 | |
109 // OnSyncStarting should create a processor and call OnSyncStarting on it. | |
110 TEST_F(ModelTypeServiceTest, OnSyncStarting) { | |
111 EXPECT_FALSE(service()->change_processor()); | |
112 OnSyncStarting(); | |
113 EXPECT_TRUE(service()->change_processor()); | |
114 // FakeModelTypeProcessor is the one that calls the callback, so if it was | |
115 // called then we know the call on the processor was made. | |
116 EXPECT_TRUE(start_callback_called()); | |
117 } | |
118 | |
119 // DisableSync should call DisableSync on the processor and then delete it. | |
120 TEST_F(ModelTypeServiceTest, DisableSync) { | |
121 service()->CreateChangeProcessor(); | |
122 EXPECT_TRUE(service()->change_processor()); | |
123 EXPECT_FALSE(service()->processor_disable_sync_called()); | |
124 | |
125 service()->DisableSync(); | |
126 EXPECT_FALSE(service()->change_processor()); | |
127 EXPECT_TRUE(service()->processor_disable_sync_called()); | |
128 } | |
129 | |
130 // ResolveConflicts should return USE_REMOTE unless the remote data is deleted. | |
131 TEST_F(ModelTypeServiceTest, DefaultConflictResolution) { | |
132 EntityData local_data; | |
133 EntityData remote_data; | |
134 | |
135 // There is no deleted/deleted case because that's not a conflict. | |
136 | |
137 local_data.specifics.mutable_preference()->set_value("value"); | |
138 EXPECT_FALSE(local_data.is_deleted()); | |
139 EXPECT_TRUE(remote_data.is_deleted()); | |
140 EXPECT_EQ(ConflictResolution::USE_LOCAL, | |
141 service()->ResolveConflict(local_data, remote_data).type()); | |
142 | |
143 remote_data.specifics.mutable_preference()->set_value("value"); | |
144 EXPECT_FALSE(local_data.is_deleted()); | |
145 EXPECT_FALSE(remote_data.is_deleted()); | |
146 EXPECT_EQ(ConflictResolution::USE_REMOTE, | |
147 service()->ResolveConflict(local_data, remote_data).type()); | |
148 | |
149 local_data.specifics.clear_preference(); | |
150 EXPECT_TRUE(local_data.is_deleted()); | |
151 EXPECT_FALSE(remote_data.is_deleted()); | |
152 EXPECT_EQ(ConflictResolution::USE_REMOTE, | |
153 service()->ResolveConflict(local_data, remote_data).type()); | |
154 } | |
155 | |
156 } // namespace syncer_v2 | |
OLD | NEW |