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 #include "sync/internal_api/public/simple_metadata_change_list.h" | |
5 | |
6 #include "base/bind.h" | |
7 #include "base/run_loop.h" | |
8 #include "sync/api/mock_model_type_store.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace syncer_v2 { | |
12 namespace { | |
13 | |
14 using WriteBatch = ModelTypeStore::WriteBatch; | |
15 | |
16 void RecordMetadataWrite(std::map<std::string, std::string>* map, | |
17 WriteBatch* batch, | |
18 const std::string& key, | |
19 const std::string& value) { | |
20 (*map)[key] = value; | |
21 } | |
22 | |
23 void RecordGlobalMetadataWrite(std::string* str, | |
24 WriteBatch* batch, | |
25 const std::string& value) { | |
26 *str = value; | |
27 } | |
28 | |
29 void RecordMetadataDelete(std::set<std::string>* set, | |
30 WriteBatch* batch, | |
31 const std::string& id) { | |
32 set->insert(id); | |
33 } | |
34 | |
35 void RecordGlobalMetadataDelete(bool* was_delete_called, WriteBatch* batch) { | |
36 *was_delete_called = true; | |
37 } | |
38 | |
39 class SimpleMetadataChangeListTest : public testing::Test { | |
40 protected: | |
41 SimpleMetadataChangeListTest() {} | |
42 | |
43 MockModelTypeStore* store() { return &store_; } | |
44 | |
45 private: | |
46 // MockModelTypeStore needs MessageLoop | |
47 base::MessageLoop message_loop_; | |
48 | |
49 MockModelTypeStore store_; | |
50 }; | |
51 | |
52 TEST_F(SimpleMetadataChangeListTest, TransferChangesEmptyChangeList) { | |
53 SimpleMetadataChangeList cl; | |
54 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | |
55 | |
56 std::map<std::string, std::string> change_map; | |
57 std::set<std::string> delete_set; | |
58 std::string global_metadata; | |
59 store()->RegisterWriteMetadataHandler( | |
60 base::Bind(&RecordMetadataWrite, &change_map)); | |
61 store()->RegisterWriteGlobalMetadataHandler( | |
62 base::Bind(&RecordGlobalMetadataWrite, &global_metadata)); | |
63 store()->RegisterDeleteMetadataHandler( | |
64 base::Bind(&RecordMetadataDelete, &delete_set)); | |
65 | |
66 cl.TransferChanges(store(), batch.get()); | |
67 | |
68 EXPECT_EQ(change_map.size(), 0ul); | |
69 EXPECT_EQ(delete_set.size(), 0ul); | |
70 EXPECT_EQ(global_metadata.size(), 0ul); | |
71 } | |
72 | |
73 TEST_F(SimpleMetadataChangeListTest, TransferChangesClearsLocalState) { | |
74 SimpleMetadataChangeList cl; | |
75 sync_pb::EntityMetadata metadata; | |
76 metadata.set_client_tag_hash("some_hash"); | |
77 cl.UpdateMetadata("client_tag", metadata); | |
78 | |
79 sync_pb::DataTypeState state; | |
80 state.set_encryption_key_name("ekn"); | |
81 cl.UpdateDataTypeState(state); | |
82 | |
83 EXPECT_NE(cl.GetMetadataChanges().size(), 0ul); | |
84 EXPECT_TRUE(cl.HasDataTypeStateChange()); | |
85 | |
86 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | |
87 cl.TransferChanges(store(), batch.get()); | |
88 | |
89 EXPECT_EQ(cl.GetMetadataChanges().size(), 0ul); | |
90 EXPECT_FALSE(cl.HasDataTypeStateChange()); | |
91 } | |
92 | |
93 TEST_F(SimpleMetadataChangeListTest, TransferChangesMultipleInvocationsSafe) { | |
94 SimpleMetadataChangeList cl; | |
95 sync_pb::EntityMetadata metadata; | |
96 metadata.set_client_tag_hash("some_hash"); | |
97 cl.UpdateMetadata("client_tag", metadata); | |
98 | |
99 sync_pb::DataTypeState state; | |
100 state.set_encryption_key_name("ekn"); | |
101 cl.UpdateDataTypeState(state); | |
102 | |
103 std::string global_metadata; | |
104 std::map<std::string, std::string> change_map; | |
105 store()->RegisterWriteMetadataHandler( | |
106 base::Bind(&RecordMetadataWrite, &change_map)); | |
107 store()->RegisterWriteGlobalMetadataHandler( | |
108 base::Bind(&RecordGlobalMetadataWrite, &global_metadata)); | |
109 | |
110 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | |
111 cl.TransferChanges(store(), batch.get()); | |
112 cl.TransferChanges(store(), batch.get()); | |
113 cl.TransferChanges(store(), batch.get()); | |
114 | |
115 EXPECT_EQ(state.SerializeAsString(), global_metadata); | |
116 EXPECT_EQ(metadata.SerializeAsString(), change_map["client_tag"]); | |
117 } | |
118 | |
119 TEST_F(SimpleMetadataChangeListTest, TransferChangesMultipleChanges) { | |
120 SimpleMetadataChangeList cl; | |
121 | |
122 sync_pb::EntityMetadata metadata; | |
123 metadata.set_client_tag_hash("some_hash"); | |
124 cl.UpdateMetadata("client_tag", metadata); | |
125 metadata.set_specifics_hash("specifics_hash"); | |
126 cl.UpdateMetadata("client_tag", metadata); | |
127 | |
128 sync_pb::EntityMetadata metadata2; | |
129 metadata2.set_client_tag_hash("some_other_hash"); | |
130 cl.UpdateMetadata("client_tag2", metadata2); | |
131 | |
132 sync_pb::DataTypeState state; | |
133 state.set_encryption_key_name("ekn"); | |
134 cl.UpdateDataTypeState(state); | |
135 state.set_encryption_key_name("ekn2"); | |
136 cl.UpdateDataTypeState(state); | |
137 | |
138 std::string global_metadata; | |
139 std::map<std::string, std::string> change_map; | |
140 store()->RegisterWriteGlobalMetadataHandler( | |
141 base::Bind(&RecordGlobalMetadataWrite, &global_metadata)); | |
142 store()->RegisterWriteMetadataHandler( | |
143 base::Bind(&RecordMetadataWrite, &change_map)); | |
144 | |
145 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | |
146 cl.TransferChanges(store(), batch.get()); | |
147 | |
148 EXPECT_EQ(metadata.SerializeAsString(), change_map["client_tag"]); | |
149 EXPECT_EQ(metadata2.SerializeAsString(), change_map["client_tag2"]); | |
150 EXPECT_EQ(state.SerializeAsString(), global_metadata); | |
151 } | |
152 | |
153 TEST_F(SimpleMetadataChangeListTest, TransferChangesDeletesClearedItems) { | |
154 SimpleMetadataChangeList cl; | |
155 sync_pb::EntityMetadata metadata; | |
156 metadata.set_client_tag_hash("some_hash"); | |
157 cl.UpdateMetadata("client_tag", metadata); | |
158 cl.ClearMetadata("client_tag"); | |
159 | |
160 sync_pb::DataTypeState state; | |
161 state.set_encryption_key_name("ekn"); | |
162 cl.UpdateDataTypeState(state); | |
163 cl.ClearDataTypeState(); | |
164 | |
165 std::map<std::string, std::string> change_map; | |
166 std::set<std::string> delete_set; | |
167 std::string global_metadata; | |
168 bool delete_called = false; | |
169 store()->RegisterWriteMetadataHandler( | |
170 base::Bind(&RecordMetadataWrite, &change_map)); | |
171 store()->RegisterWriteGlobalMetadataHandler( | |
172 base::Bind(&RecordGlobalMetadataWrite, &global_metadata)); | |
173 store()->RegisterDeleteMetadataHandler( | |
174 base::Bind(&RecordMetadataDelete, &delete_set)); | |
175 store()->RegisterDeleteGlobalMetadataHandler( | |
176 base::Bind(&RecordGlobalMetadataDelete, &delete_called)); | |
177 | |
178 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | |
179 cl.TransferChanges(store(), batch.get()); | |
180 | |
181 EXPECT_TRUE(delete_set.find("client_tag") != delete_set.end()); | |
182 EXPECT_TRUE(change_map.find("client_tag") == change_map.end()); | |
183 EXPECT_TRUE(delete_called); | |
184 EXPECT_TRUE(global_metadata.empty()); | |
185 } | |
186 | |
187 } // namespace | |
188 } // namespace syncer_v2 | |
OLD | NEW |