OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "sync/internal_api/public/model_type_store_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace syncer_v2 { | |
16 | |
17 class ModelTypeStoreImplTest : public testing::Test { | |
18 public: | |
19 void TearDown() override { | |
20 if (store_) { | |
21 store_.reset(); | |
22 PumpLoop(); | |
23 } | |
24 } | |
25 | |
26 ModelTypeStore* store() { return store_.get(); } | |
27 | |
28 void OnInitDone(ModelTypeStore::Result result, | |
29 std::unique_ptr<ModelTypeStore> store) { | |
30 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
31 store_ = std::move(store); | |
32 } | |
33 | |
34 void PumpLoop() { | |
35 base::RunLoop run_loop; | |
36 run_loop.RunUntilIdle(); | |
37 } | |
38 | |
39 void CreateStore() { | |
40 ModelTypeStore::CreateInMemoryStoreForTest(base::Bind( | |
41 &ModelTypeStoreImplTest::OnInitDone, base::Unretained(this))); | |
42 PumpLoop(); | |
43 } | |
44 | |
45 void WriteTestData() { | |
46 std::unique_ptr<ModelTypeStore::WriteBatch> write_batch = | |
47 store()->CreateWriteBatch(); | |
48 store()->WriteData(write_batch.get(), "id1", "data1"); | |
49 store()->WriteMetadata(write_batch.get(), "id1", "metadata1"); | |
50 store()->WriteData(write_batch.get(), "id2", "data2"); | |
51 store()->WriteGlobalMetadata(write_batch.get(), "global_metadata"); | |
52 ModelTypeStore::Result result; | |
53 store()->CommitWriteBatch(std::move(write_batch), | |
54 base::Bind(&CaptureResult, &result)); | |
55 PumpLoop(); | |
56 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
57 } | |
58 | |
59 // Following functions capture parameters passed to callbacks into variables | |
60 // provided by test. They can be passed as callbacks to ModelTypeStore | |
61 // functions. | |
62 static void CaptureResult(ModelTypeStore::Result* dst, | |
63 ModelTypeStore::Result result) { | |
64 *dst = result; | |
65 } | |
66 | |
67 static void CaptureResultWithRecords( | |
68 ModelTypeStore::Result* dst_result, | |
69 std::unique_ptr<ModelTypeStore::RecordList>* dst_records, | |
70 ModelTypeStore::Result result, | |
71 std::unique_ptr<ModelTypeStore::RecordList> records) { | |
72 *dst_result = result; | |
73 *dst_records = std::move(records); | |
74 } | |
75 | |
76 static void CaptureResutRecordsAndString( | |
77 ModelTypeStore::Result* dst_result, | |
78 std::unique_ptr<ModelTypeStore::RecordList>* dst_records, | |
79 std::string* dst_value, | |
80 ModelTypeStore::Result result, | |
81 std::unique_ptr<ModelTypeStore::RecordList> records, | |
82 const std::string& value) { | |
83 *dst_result = result; | |
84 *dst_records = std::move(records); | |
85 *dst_value = value; | |
86 } | |
87 | |
88 static void CaptureResutRecordsAndIdList( | |
89 ModelTypeStore::Result* dst_result, | |
90 std::unique_ptr<ModelTypeStore::RecordList>* dst_records, | |
91 std::unique_ptr<ModelTypeStore::IdList>* dst_id_list, | |
92 ModelTypeStore::Result result, | |
93 std::unique_ptr<ModelTypeStore::RecordList> records, | |
94 std::unique_ptr<ModelTypeStore::IdList> missing_id_list) { | |
95 *dst_result = result; | |
96 *dst_records = std::move(records); | |
97 *dst_id_list = std::move(missing_id_list); | |
98 } | |
99 | |
100 private: | |
101 base::MessageLoop message_loop_; | |
102 std::unique_ptr<ModelTypeStore> store_; | |
103 }; | |
104 | |
105 // Matcher to verify contents of returned RecordList . | |
106 MATCHER_P2(RecordMatches, id, value, "") { | |
107 return arg.id == id && arg.value == value; | |
108 } | |
109 | |
110 // Test that CreateInMemoryStoreForTest triggers store initialization that | |
111 // results with callback being called with valid store pointer. Test that | |
112 // resulting store is empty functional one. | |
113 TEST_F(ModelTypeStoreImplTest, CreateInMemoryStore) { | |
114 ModelTypeStore::CreateInMemoryStoreForTest( | |
115 base::Bind(&ModelTypeStoreImplTest::OnInitDone, base::Unretained(this))); | |
116 ASSERT_EQ(nullptr, store()); | |
117 PumpLoop(); | |
118 ASSERT_NE(nullptr, store()); | |
119 | |
120 ModelTypeStore::Result result; | |
121 std::unique_ptr<ModelTypeStore::RecordList> records; | |
122 store()->ReadAllData( | |
123 base::Bind(&CaptureResultWithRecords, &result, &records)); | |
124 PumpLoop(); | |
125 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
126 ASSERT_TRUE(records->empty()); | |
127 | |
128 std::string global_metadata; | |
129 store()->ReadAllMetadata(base::Bind(&CaptureResutRecordsAndString, &result, | |
130 &records, &global_metadata)); | |
131 PumpLoop(); | |
132 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
133 ASSERT_TRUE(records->empty()); | |
134 ASSERT_TRUE(global_metadata.empty()); | |
135 } | |
136 | |
137 // Test that records that are written to store later can be read from it. | |
138 TEST_F(ModelTypeStoreImplTest, WriteThenRead) { | |
139 CreateStore(); | |
140 WriteTestData(); | |
141 | |
142 ModelTypeStore::Result result; | |
143 std::unique_ptr<ModelTypeStore::RecordList> records; | |
144 store()->ReadAllData( | |
145 base::Bind(&CaptureResultWithRecords, &result, &records)); | |
146 PumpLoop(); | |
147 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
148 ASSERT_THAT(*records, | |
149 testing::UnorderedElementsAre(RecordMatches("id1", "data1"), | |
150 RecordMatches("id2", "data2"))); | |
151 | |
152 std::string global_metadata; | |
153 store()->ReadAllMetadata(base::Bind(&CaptureResutRecordsAndString, &result, | |
154 &records, &global_metadata)); | |
155 PumpLoop(); | |
156 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
157 ASSERT_THAT(*records, | |
158 testing::UnorderedElementsAre(RecordMatches("id1", "metadata1"))); | |
159 ASSERT_EQ("global_metadata", global_metadata); | |
160 } | |
161 | |
162 // Test that if global metadata is not set then ReadAllMetadata still succeeds | |
163 // and returns entry metadata records. | |
164 TEST_F(ModelTypeStoreImplTest, MissingGlobalMetadata) { | |
165 CreateStore(); | |
166 WriteTestData(); | |
167 | |
168 ModelTypeStore::Result result; | |
169 | |
170 std::unique_ptr<ModelTypeStore::WriteBatch> write_batch = | |
171 store()->CreateWriteBatch(); | |
172 store()->DeleteGlobalMetadata(write_batch.get()); | |
173 store()->CommitWriteBatch(std::move(write_batch), | |
174 base::Bind(&CaptureResult, &result)); | |
175 PumpLoop(); | |
176 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
177 | |
178 std::unique_ptr<ModelTypeStore::RecordList> records; | |
179 std::string global_metadata; | |
180 store()->ReadAllMetadata(base::Bind(&CaptureResutRecordsAndString, &result, | |
181 &records, &global_metadata)); | |
182 PumpLoop(); | |
183 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
184 ASSERT_THAT(*records, | |
185 testing::UnorderedElementsAre(RecordMatches("id1", "metadata1"))); | |
186 ASSERT_EQ(std::string(), global_metadata); | |
187 } | |
188 | |
189 // Test that when reading data records by id, if one of the ids is missing | |
190 // operation still succeeds and missing id is returned in missing_id_list. | |
191 TEST_F(ModelTypeStoreImplTest, ReadMissingDataRecords) { | |
192 CreateStore(); | |
193 WriteTestData(); | |
194 | |
195 ModelTypeStore::Result result; | |
196 | |
197 ModelTypeStore::IdList id_list; | |
198 id_list.push_back("id1"); | |
199 id_list.push_back("id3"); | |
200 | |
201 std::unique_ptr<ModelTypeStore::RecordList> records; | |
202 std::unique_ptr<ModelTypeStore::IdList> missing_id_list; | |
203 | |
204 store()->ReadData(id_list, base::Bind(&CaptureResutRecordsAndIdList, &result, | |
205 &records, &missing_id_list)); | |
206 PumpLoop(); | |
207 ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result); | |
208 ASSERT_THAT(*records, | |
209 testing::UnorderedElementsAre(RecordMatches("id1", "data1"))); | |
210 ASSERT_THAT(*missing_id_list, testing::UnorderedElementsAre("id3")); | |
211 } | |
212 | |
213 } // namespace syncer_v2 | |
OLD | NEW |