OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/bookmarks/bookmark_codec.h" | 5 #include "chrome/browser/bookmarks/bookmark_codec.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 BookmarkModel* CreateTestModel3() { | 86 BookmarkModel* CreateTestModel3() { |
87 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL)); | 87 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL)); |
88 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 88 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
89 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); | 89 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); |
90 const BookmarkNode* folder1 = model->AddFolder(bookmark_bar, 1, | 90 const BookmarkNode* folder1 = model->AddFolder(bookmark_bar, 1, |
91 ASCIIToUTF16(kFolder1Title)); | 91 ASCIIToUTF16(kFolder1Title)); |
92 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); | 92 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); |
93 return model.release(); | 93 return model.release(); |
94 } | 94 } |
95 | 95 |
96 void GetBookmarksBarChildValue(Value* value, | 96 void GetBookmarksBarChildValue(base::Value* value, |
97 size_t index, | 97 size_t index, |
98 DictionaryValue** result_value) { | 98 base::DictionaryValue** result_value) { |
99 ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType()); | 99 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType()); |
100 | 100 |
101 DictionaryValue* d_value = static_cast<DictionaryValue*>(value); | 101 base::DictionaryValue* d_value = static_cast<base::DictionaryValue*>(value); |
102 Value* roots; | 102 base::Value* roots; |
103 ASSERT_TRUE(d_value->Get(BookmarkCodec::kRootsKey, &roots)); | 103 ASSERT_TRUE(d_value->Get(BookmarkCodec::kRootsKey, &roots)); |
104 ASSERT_EQ(Value::TYPE_DICTIONARY, roots->GetType()); | 104 ASSERT_EQ(base::Value::TYPE_DICTIONARY, roots->GetType()); |
105 | 105 |
106 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | 106 base::DictionaryValue* roots_d_value = |
107 Value* bb_value; | 107 static_cast<base::DictionaryValue*>(roots); |
| 108 base::Value* bb_value; |
108 ASSERT_TRUE(roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, | 109 ASSERT_TRUE(roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, |
109 &bb_value)); | 110 &bb_value)); |
110 ASSERT_EQ(Value::TYPE_DICTIONARY, bb_value->GetType()); | 111 ASSERT_EQ(base::Value::TYPE_DICTIONARY, bb_value->GetType()); |
111 | 112 |
112 DictionaryValue* bb_d_value = static_cast<DictionaryValue*>(bb_value); | 113 base::DictionaryValue* bb_d_value = |
113 Value* bb_children_value; | 114 static_cast<base::DictionaryValue*>(bb_value); |
| 115 base::Value* bb_children_value; |
114 ASSERT_TRUE(bb_d_value->Get(BookmarkCodec::kChildrenKey, | 116 ASSERT_TRUE(bb_d_value->Get(BookmarkCodec::kChildrenKey, |
115 &bb_children_value)); | 117 &bb_children_value)); |
116 ASSERT_EQ(Value::TYPE_LIST, bb_children_value->GetType()); | 118 ASSERT_EQ(base::Value::TYPE_LIST, bb_children_value->GetType()); |
117 | 119 |
118 ListValue* bb_children_l_value = static_cast<ListValue*>(bb_children_value); | 120 base::ListValue* bb_children_l_value = |
119 Value* child_value; | 121 static_cast<base::ListValue*>(bb_children_value); |
| 122 base::Value* child_value; |
120 ASSERT_TRUE(bb_children_l_value->Get(index, &child_value)); | 123 ASSERT_TRUE(bb_children_l_value->Get(index, &child_value)); |
121 ASSERT_EQ(Value::TYPE_DICTIONARY, child_value->GetType()); | 124 ASSERT_EQ(base::Value::TYPE_DICTIONARY, child_value->GetType()); |
122 | 125 |
123 *result_value = static_cast<DictionaryValue*>(child_value); | 126 *result_value = static_cast<base::DictionaryValue*>(child_value); |
124 } | 127 } |
125 | 128 |
126 Value* EncodeHelper(BookmarkModel* model, std::string* checksum) { | 129 base::Value* EncodeHelper(BookmarkModel* model, std::string* checksum) { |
127 BookmarkCodec encoder; | 130 BookmarkCodec encoder; |
128 // Computed and stored checksums should be empty. | 131 // Computed and stored checksums should be empty. |
129 EXPECT_EQ("", encoder.computed_checksum()); | 132 EXPECT_EQ("", encoder.computed_checksum()); |
130 EXPECT_EQ("", encoder.stored_checksum()); | 133 EXPECT_EQ("", encoder.stored_checksum()); |
131 | 134 |
132 scoped_ptr<Value> value(encoder.Encode(model)); | 135 scoped_ptr<base::Value> value(encoder.Encode(model)); |
133 const std::string& computed_checksum = encoder.computed_checksum(); | 136 const std::string& computed_checksum = encoder.computed_checksum(); |
134 const std::string& stored_checksum = encoder.stored_checksum(); | 137 const std::string& stored_checksum = encoder.stored_checksum(); |
135 | 138 |
136 // Computed and stored checksums should not be empty and should be equal. | 139 // Computed and stored checksums should not be empty and should be equal. |
137 EXPECT_FALSE(computed_checksum.empty()); | 140 EXPECT_FALSE(computed_checksum.empty()); |
138 EXPECT_FALSE(stored_checksum.empty()); | 141 EXPECT_FALSE(stored_checksum.empty()); |
139 EXPECT_EQ(computed_checksum, stored_checksum); | 142 EXPECT_EQ(computed_checksum, stored_checksum); |
140 | 143 |
141 *checksum = computed_checksum; | 144 *checksum = computed_checksum; |
142 return value.release(); | 145 return value.release(); |
143 } | 146 } |
144 | 147 |
145 bool Decode(BookmarkCodec* codec, BookmarkModel* model, const Value& value) { | 148 bool Decode(BookmarkCodec* codec, |
| 149 BookmarkModel* model, |
| 150 const base::Value& value) { |
146 int64 max_id; | 151 int64 max_id; |
147 bool result = codec->Decode(AsMutable(model->bookmark_bar_node()), | 152 bool result = codec->Decode(AsMutable(model->bookmark_bar_node()), |
148 AsMutable(model->other_node()), | 153 AsMutable(model->other_node()), |
149 AsMutable(model->mobile_node()), | 154 AsMutable(model->mobile_node()), |
150 &max_id, value); | 155 &max_id, value); |
151 model->set_next_node_id(max_id); | 156 model->set_next_node_id(max_id); |
152 AsMutable(model->root_node())-> | 157 AsMutable(model->root_node())-> |
153 SetMetaInfoMap(codec->model_meta_info_map()); | 158 SetMetaInfoMap(codec->model_meta_info_map()); |
154 AsMutable(model->root_node())-> | 159 AsMutable(model->root_node())-> |
155 set_sync_transaction_version(codec->model_sync_transaction_version()); | 160 set_sync_transaction_version(codec->model_sync_transaction_version()); |
156 | 161 |
157 return result; | 162 return result; |
158 } | 163 } |
159 | 164 |
160 BookmarkModel* DecodeHelper(const Value& value, | 165 BookmarkModel* DecodeHelper(const base::Value& value, |
161 const std::string& expected_stored_checksum, | 166 const std::string& expected_stored_checksum, |
162 std::string* computed_checksum, | 167 std::string* computed_checksum, |
163 bool expected_changes) { | 168 bool expected_changes) { |
164 BookmarkCodec decoder; | 169 BookmarkCodec decoder; |
165 // Computed and stored checksums should be empty. | 170 // Computed and stored checksums should be empty. |
166 EXPECT_EQ("", decoder.computed_checksum()); | 171 EXPECT_EQ("", decoder.computed_checksum()); |
167 EXPECT_EQ("", decoder.stored_checksum()); | 172 EXPECT_EQ("", decoder.stored_checksum()); |
168 | 173 |
169 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL)); | 174 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL)); |
170 EXPECT_TRUE(Decode(&decoder, model.get(), value)); | 175 EXPECT_TRUE(Decode(&decoder, model.get(), value)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 std::set<int64> assigned_ids; | 207 std::set<int64> assigned_ids; |
203 CheckIDs(model->bookmark_bar_node(), &assigned_ids); | 208 CheckIDs(model->bookmark_bar_node(), &assigned_ids); |
204 CheckIDs(model->other_node(), &assigned_ids); | 209 CheckIDs(model->other_node(), &assigned_ids); |
205 CheckIDs(model->mobile_node(), &assigned_ids); | 210 CheckIDs(model->mobile_node(), &assigned_ids); |
206 } | 211 } |
207 }; | 212 }; |
208 | 213 |
209 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { | 214 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { |
210 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 215 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
211 std::string enc_checksum; | 216 std::string enc_checksum; |
212 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); | 217 scoped_ptr<base::Value> value( |
| 218 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
213 | 219 |
214 EXPECT_TRUE(value.get() != NULL); | 220 EXPECT_TRUE(value.get() != NULL); |
215 | 221 |
216 std::string dec_checksum; | 222 std::string dec_checksum; |
217 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper( | 223 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper( |
218 *value.get(), enc_checksum, &dec_checksum, false)); | 224 *value.get(), enc_checksum, &dec_checksum, false)); |
219 } | 225 } |
220 | 226 |
221 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) { | 227 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) { |
222 // Encode two identical models and make sure the check-sums are same as long | 228 // Encode two identical models and make sure the check-sums are same as long |
223 // as the data is the same. | 229 // as the data is the same. |
224 scoped_ptr<BookmarkModel> model1(CreateTestModel1()); | 230 scoped_ptr<BookmarkModel> model1(CreateTestModel1()); |
225 std::string enc_checksum1; | 231 std::string enc_checksum1; |
226 scoped_ptr<Value> value1(EncodeHelper(model1.get(), &enc_checksum1)); | 232 scoped_ptr<base::Value> value1(EncodeHelper(model1.get(), &enc_checksum1)); |
227 EXPECT_TRUE(value1.get() != NULL); | 233 EXPECT_TRUE(value1.get() != NULL); |
228 | 234 |
229 scoped_ptr<BookmarkModel> model2(CreateTestModel1()); | 235 scoped_ptr<BookmarkModel> model2(CreateTestModel1()); |
230 std::string enc_checksum2; | 236 std::string enc_checksum2; |
231 scoped_ptr<Value> value2(EncodeHelper(model2.get(), &enc_checksum2)); | 237 scoped_ptr<base::Value> value2(EncodeHelper(model2.get(), &enc_checksum2)); |
232 EXPECT_TRUE(value2.get() != NULL); | 238 EXPECT_TRUE(value2.get() != NULL); |
233 | 239 |
234 ASSERT_EQ(enc_checksum1, enc_checksum2); | 240 ASSERT_EQ(enc_checksum1, enc_checksum2); |
235 } | 241 } |
236 | 242 |
237 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) { | 243 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) { |
238 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 244 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
239 std::string enc_checksum; | 245 std::string enc_checksum; |
240 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); | 246 scoped_ptr<base::Value> value( |
| 247 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
241 | 248 |
242 EXPECT_TRUE(value.get() != NULL); | 249 EXPECT_TRUE(value.get() != NULL); |
243 | 250 |
244 // Change something in the encoded value before decoding it. | 251 // Change something in the encoded value before decoding it. |
245 DictionaryValue* child1_value; | 252 base::DictionaryValue* child1_value; |
246 GetBookmarksBarChildValue(value.get(), 0, &child1_value); | 253 GetBookmarksBarChildValue(value.get(), 0, &child1_value); |
247 std::string title; | 254 std::string title; |
248 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title)); | 255 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title)); |
249 child1_value->SetString(BookmarkCodec::kNameKey, title + "1"); | 256 child1_value->SetString(BookmarkCodec::kNameKey, title + "1"); |
250 | 257 |
251 std::string dec_checksum; | 258 std::string dec_checksum; |
252 scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper( | 259 scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper( |
253 *value.get(), enc_checksum, &dec_checksum, true)); | 260 *value.get(), enc_checksum, &dec_checksum, true)); |
254 | 261 |
255 // Undo the change and make sure the checksum is same as original. | 262 // Undo the change and make sure the checksum is same as original. |
256 child1_value->SetString(BookmarkCodec::kNameKey, title); | 263 child1_value->SetString(BookmarkCodec::kNameKey, title); |
257 scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper( | 264 scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper( |
258 *value.get(), enc_checksum, &dec_checksum, false)); | 265 *value.get(), enc_checksum, &dec_checksum, false)); |
259 } | 266 } |
260 | 267 |
261 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) { | 268 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) { |
262 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); | 269 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
263 | 270 |
264 // The test depends on existence of multiple children under bookmark bar, so | 271 // The test depends on existence of multiple children under bookmark bar, so |
265 // make sure that's the case. | 272 // make sure that's the case. |
266 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count(); | 273 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count(); |
267 ASSERT_GT(bb_child_count, 1); | 274 ASSERT_GT(bb_child_count, 1); |
268 | 275 |
269 std::string enc_checksum; | 276 std::string enc_checksum; |
270 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); | 277 scoped_ptr<base::Value> value( |
| 278 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
271 | 279 |
272 EXPECT_TRUE(value.get() != NULL); | 280 EXPECT_TRUE(value.get() != NULL); |
273 | 281 |
274 // Change IDs for all children of bookmark bar to be 1. | 282 // Change IDs for all children of bookmark bar to be 1. |
275 DictionaryValue* child_value; | 283 base::DictionaryValue* child_value; |
276 for (int i = 0; i < bb_child_count; ++i) { | 284 for (int i = 0; i < bb_child_count; ++i) { |
277 GetBookmarksBarChildValue(value.get(), i, &child_value); | 285 GetBookmarksBarChildValue(value.get(), i, &child_value); |
278 std::string id; | 286 std::string id; |
279 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id)); | 287 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id)); |
280 child_value->SetString(BookmarkCodec::kIdKey, "1"); | 288 child_value->SetString(BookmarkCodec::kIdKey, "1"); |
281 } | 289 } |
282 | 290 |
283 std::string dec_checksum; | 291 std::string dec_checksum; |
284 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper( | 292 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper( |
285 *value.get(), enc_checksum, &dec_checksum, true)); | 293 *value.get(), enc_checksum, &dec_checksum, true)); |
286 | 294 |
287 ExpectIDsUnique(decoded_model.get()); | 295 ExpectIDsUnique(decoded_model.get()); |
288 | 296 |
289 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe. | 297 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe. |
290 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node(); | 298 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node(); |
291 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url1"), | 299 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url1"), |
292 GURL("http://newurl1.com")); | 300 GURL("http://newurl1.com")); |
293 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url2"), | 301 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url2"), |
294 GURL("http://newurl2.com")); | 302 GURL("http://newurl2.com")); |
295 | 303 |
296 ExpectIDsUnique(decoded_model.get()); | 304 ExpectIDsUnique(decoded_model.get()); |
297 } | 305 } |
298 | 306 |
299 TEST_F(BookmarkCodecTest, PersistIDsTest) { | 307 TEST_F(BookmarkCodecTest, PersistIDsTest) { |
300 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); | 308 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
301 BookmarkCodec encoder; | 309 BookmarkCodec encoder; |
302 scoped_ptr<Value> model_value(encoder.Encode(model_to_encode.get())); | 310 scoped_ptr<base::Value> model_value(encoder.Encode(model_to_encode.get())); |
303 | 311 |
304 BookmarkModel decoded_model(NULL); | 312 BookmarkModel decoded_model(NULL); |
305 BookmarkCodec decoder; | 313 BookmarkCodec decoder; |
306 ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get())); | 314 ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get())); |
307 ASSERT_NO_FATAL_FAILURE( | 315 ASSERT_NO_FATAL_FAILURE( |
308 AssertModelsEqual(model_to_encode.get(), &decoded_model)); | 316 AssertModelsEqual(model_to_encode.get(), &decoded_model)); |
309 | 317 |
310 // Add a couple of more items to the decoded bookmark model and make sure | 318 // Add a couple of more items to the decoded bookmark model and make sure |
311 // ID persistence is working properly. | 319 // ID persistence is working properly. |
312 const BookmarkNode* bookmark_bar = decoded_model.bookmark_bar_node(); | 320 const BookmarkNode* bookmark_bar = decoded_model.bookmark_bar_node(); |
313 decoded_model.AddURL( | 321 decoded_model.AddURL( |
314 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kUrl3Title), | 322 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kUrl3Title), |
315 GURL(kUrl3Url)); | 323 GURL(kUrl3Url)); |
316 const BookmarkNode* folder2_node = decoded_model.AddFolder( | 324 const BookmarkNode* folder2_node = decoded_model.AddFolder( |
317 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title)); | 325 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title)); |
318 decoded_model.AddURL(folder2_node, 0, ASCIIToUTF16(kUrl4Title), | 326 decoded_model.AddURL(folder2_node, 0, ASCIIToUTF16(kUrl4Title), |
319 GURL(kUrl4Url)); | 327 GURL(kUrl4Url)); |
320 | 328 |
321 BookmarkCodec encoder2; | 329 BookmarkCodec encoder2; |
322 scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model)); | 330 scoped_ptr<base::Value> model_value2(encoder2.Encode(&decoded_model)); |
323 | 331 |
324 BookmarkModel decoded_model2(NULL); | 332 BookmarkModel decoded_model2(NULL); |
325 BookmarkCodec decoder2; | 333 BookmarkCodec decoder2; |
326 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); | 334 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); |
327 ASSERT_NO_FATAL_FAILURE(AssertModelsEqual(&decoded_model, &decoded_model2)); | 335 ASSERT_NO_FATAL_FAILURE(AssertModelsEqual(&decoded_model, &decoded_model2)); |
328 } | 336 } |
329 | 337 |
330 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) { | 338 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) { |
331 base::FilePath test_data_directory; | 339 base::FilePath test_data_directory; |
332 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); | 340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
333 base::FilePath test_file = test_data_directory.AppendASCII( | 341 base::FilePath test_file = test_data_directory.AppendASCII( |
334 "bookmarks/model_without_sync.json"); | 342 "bookmarks/model_without_sync.json"); |
335 ASSERT_TRUE(base::PathExists(test_file)); | 343 ASSERT_TRUE(base::PathExists(test_file)); |
336 | 344 |
337 JSONFileValueSerializer serializer(test_file); | 345 JSONFileValueSerializer serializer(test_file); |
338 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); | 346 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
339 | 347 |
340 BookmarkModel decoded_model(NULL); | 348 BookmarkModel decoded_model(NULL); |
341 BookmarkCodec decoder; | 349 BookmarkCodec decoder; |
342 ASSERT_TRUE(Decode(&decoder, &decoded_model, *root.get())); | 350 ASSERT_TRUE(Decode(&decoder, &decoded_model, *root.get())); |
343 ExpectIDsUnique(&decoded_model); | 351 ExpectIDsUnique(&decoded_model); |
344 | 352 |
345 const BookmarkNode* bbn = decoded_model.bookmark_bar_node(); | 353 const BookmarkNode* bbn = decoded_model.bookmark_bar_node(); |
346 ASSERT_EQ(1, bbn->child_count()); | 354 ASSERT_EQ(1, bbn->child_count()); |
347 | 355 |
348 const BookmarkNode* child = bbn->GetChild(0); | 356 const BookmarkNode* child = bbn->GetChild(0); |
(...skipping 20 matching lines...) Expand all Loading... |
369 ASSERT_TRUE(decoded_model.mobile_node() != NULL); | 377 ASSERT_TRUE(decoded_model.mobile_node() != NULL); |
370 } | 378 } |
371 | 379 |
372 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) { | 380 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) { |
373 // Add meta info and encode. | 381 // Add meta info and encode. |
374 scoped_ptr<BookmarkModel> model(CreateTestModel1()); | 382 scoped_ptr<BookmarkModel> model(CreateTestModel1()); |
375 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1"); | 383 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1"); |
376 model->SetNodeMetaInfo(model->bookmark_bar_node()->GetChild(0), | 384 model->SetNodeMetaInfo(model->bookmark_bar_node()->GetChild(0), |
377 "node_info", "value2"); | 385 "node_info", "value2"); |
378 std::string checksum; | 386 std::string checksum; |
379 scoped_ptr<Value> value(EncodeHelper(model.get(), &checksum)); | 387 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); |
380 ASSERT_TRUE(value.get() != NULL); | 388 ASSERT_TRUE(value.get() != NULL); |
381 | 389 |
382 // Decode and check for meta info. | 390 // Decode and check for meta info. |
383 model.reset(DecodeHelper(*value, checksum, &checksum, false)); | 391 model.reset(DecodeHelper(*value, checksum, &checksum, false)); |
384 std::string meta_value; | 392 std::string meta_value; |
385 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value)); | 393 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value)); |
386 EXPECT_EQ("value1", meta_value); | 394 EXPECT_EQ("value1", meta_value); |
387 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value)); | 395 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value)); |
388 const BookmarkNode* bbn = model->bookmark_bar_node(); | 396 const BookmarkNode* bbn = model->bookmark_bar_node(); |
389 ASSERT_EQ(1, bbn->child_count()); | 397 ASSERT_EQ(1, bbn->child_count()); |
390 const BookmarkNode* child = bbn->GetChild(0); | 398 const BookmarkNode* child = bbn->GetChild(0); |
391 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value)); | 399 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value)); |
392 EXPECT_EQ("value2", meta_value); | 400 EXPECT_EQ("value2", meta_value); |
393 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value)); | 401 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value)); |
394 } | 402 } |
395 | 403 |
396 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) { | 404 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) { |
397 // Add sync transaction version and encode. | 405 // Add sync transaction version and encode. |
398 scoped_ptr<BookmarkModel> model(CreateTestModel2()); | 406 scoped_ptr<BookmarkModel> model(CreateTestModel2()); |
399 model->SetNodeSyncTransactionVersion(model->root_node(), 1); | 407 model->SetNodeSyncTransactionVersion(model->root_node(), 1); |
400 const BookmarkNode* bbn = model->bookmark_bar_node(); | 408 const BookmarkNode* bbn = model->bookmark_bar_node(); |
401 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42); | 409 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42); |
402 | 410 |
403 std::string checksum; | 411 std::string checksum; |
404 scoped_ptr<Value> value(EncodeHelper(model.get(), &checksum)); | 412 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); |
405 ASSERT_TRUE(value.get() != NULL); | 413 ASSERT_TRUE(value.get() != NULL); |
406 | 414 |
407 // Decode and verify. | 415 // Decode and verify. |
408 model.reset(DecodeHelper(*value, checksum, &checksum, false)); | 416 model.reset(DecodeHelper(*value, checksum, &checksum, false)); |
409 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); | 417 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); |
410 bbn = model->bookmark_bar_node(); | 418 bbn = model->bookmark_bar_node(); |
411 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); | 419 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); |
412 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, | 420 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, |
413 bbn->GetChild(0)->sync_transaction_version()); | 421 bbn->GetChild(0)->sync_transaction_version()); |
414 } | 422 } |
415 | 423 |
416 // Verifies that we can still decode the old codec format after changing the | 424 // Verifies that we can still decode the old codec format after changing the |
417 // way meta info is stored. | 425 // way meta info is stored. |
418 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) { | 426 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) { |
419 base::FilePath test_data_directory; | 427 base::FilePath test_data_directory; |
420 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); | 428 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
421 base::FilePath test_file = test_data_directory.AppendASCII( | 429 base::FilePath test_file = test_data_directory.AppendASCII( |
422 "bookmarks/meta_info_as_string.json"); | 430 "bookmarks/meta_info_as_string.json"); |
423 ASSERT_TRUE(base::PathExists(test_file)); | 431 ASSERT_TRUE(base::PathExists(test_file)); |
424 | 432 |
425 JSONFileValueSerializer serializer(test_file); | 433 JSONFileValueSerializer serializer(test_file); |
426 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); | 434 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
427 | 435 |
428 BookmarkModel model(NULL); | 436 BookmarkModel model(NULL); |
429 BookmarkCodec decoder; | 437 BookmarkCodec decoder; |
430 ASSERT_TRUE(Decode(&decoder, &model, *root.get())); | 438 ASSERT_TRUE(Decode(&decoder, &model, *root.get())); |
431 | 439 |
432 EXPECT_EQ(1, model.root_node()->sync_transaction_version()); | 440 EXPECT_EQ(1, model.root_node()->sync_transaction_version()); |
433 const BookmarkNode* bbn = model.bookmark_bar_node(); | 441 const BookmarkNode* bbn = model.bookmark_bar_node(); |
434 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, | 442 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, |
435 bbn->GetChild(0)->sync_transaction_version()); | 443 bbn->GetChild(0)->sync_transaction_version()); |
436 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); | 444 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); |
437 | 445 |
438 const char kSyncTransactionVersionKey[] = "sync.transaction_version"; | 446 const char kSyncTransactionVersionKey[] = "sync.transaction_version"; |
439 const char kNormalKey[] = "key"; | 447 const char kNormalKey[] = "key"; |
440 const char kNestedKey[] = "nested.key"; | 448 const char kNestedKey[] = "nested.key"; |
441 std::string meta_value; | 449 std::string meta_value; |
442 EXPECT_FALSE(model.root_node()->GetMetaInfo(kSyncTransactionVersionKey, | 450 EXPECT_FALSE(model.root_node()->GetMetaInfo(kSyncTransactionVersionKey, |
443 &meta_value)); | 451 &meta_value)); |
444 EXPECT_FALSE(bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey, | 452 EXPECT_FALSE(bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey, |
445 &meta_value)); | 453 &meta_value)); |
446 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value)); | 454 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value)); |
447 EXPECT_EQ("value", meta_value); | 455 EXPECT_EQ("value", meta_value); |
448 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value)); | 456 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value)); |
449 EXPECT_EQ("value2", meta_value); | 457 EXPECT_EQ("value2", meta_value); |
450 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value)); | 458 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value)); |
451 EXPECT_EQ("value3", meta_value); | 459 EXPECT_EQ("value3", meta_value); |
452 } | 460 } |
OLD | NEW |