Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: components/bookmarks/core/browser/bookmark_codec_unittest.cc

Issue 284893003: Move bookmarks/core/... to bookmarks/... (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing errors reported by presubmit Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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/bookmarks/core/browser/bookmark_codec.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "components/bookmarks/core/browser/bookmark_model.h"
17 #include "components/bookmarks/core/test/test_bookmark_client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using base::ASCIIToUTF16;
21
22 namespace {
23
24 const char kUrl1Title[] = "url1";
25 const char kUrl1Url[] = "http://www.url1.com";
26 const char kUrl2Title[] = "url2";
27 const char kUrl2Url[] = "http://www.url2.com";
28 const char kUrl3Title[] = "url3";
29 const char kUrl3Url[] = "http://www.url3.com";
30 const char kUrl4Title[] = "url4";
31 const char kUrl4Url[] = "http://www.url4.com";
32 const char kFolder1Title[] = "folder1";
33 const char kFolder2Title[] = "folder2";
34
35 const base::FilePath& GetTestDataDir() {
36 CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ());
37 if (dir.empty()) {
38 PathService::Get(base::DIR_SOURCE_ROOT, &dir);
39 dir = dir.AppendASCII("components");
40 dir = dir.AppendASCII("test");
41 dir = dir.AppendASCII("data");
42 }
43 return dir;
44 }
45
46 // Helper to get a mutable bookmark node.
47 BookmarkNode* AsMutable(const BookmarkNode* node) {
48 return const_cast<BookmarkNode*>(node);
49 }
50
51 // Helper to verify the two given bookmark nodes.
52 void AssertNodesEqual(const BookmarkNode* expected,
53 const BookmarkNode* actual) {
54 ASSERT_TRUE(expected);
55 ASSERT_TRUE(actual);
56 EXPECT_EQ(expected->id(), actual->id());
57 EXPECT_EQ(expected->GetTitle(), actual->GetTitle());
58 EXPECT_EQ(expected->type(), actual->type());
59 EXPECT_TRUE(expected->date_added() == actual->date_added());
60 if (expected->is_url()) {
61 EXPECT_EQ(expected->url(), actual->url());
62 } else {
63 EXPECT_TRUE(expected->date_folder_modified() ==
64 actual->date_folder_modified());
65 ASSERT_EQ(expected->child_count(), actual->child_count());
66 for (int i = 0; i < expected->child_count(); ++i)
67 AssertNodesEqual(expected->GetChild(i), actual->GetChild(i));
68 }
69 }
70
71 // Verifies that the two given bookmark models are the same.
72 void AssertModelsEqual(BookmarkModel* expected, BookmarkModel* actual) {
73 ASSERT_NO_FATAL_FAILURE(AssertNodesEqual(expected->bookmark_bar_node(),
74 actual->bookmark_bar_node()));
75 ASSERT_NO_FATAL_FAILURE(
76 AssertNodesEqual(expected->other_node(), actual->other_node()));
77 ASSERT_NO_FATAL_FAILURE(
78 AssertNodesEqual(expected->mobile_node(), actual->mobile_node()));
79 }
80
81 } // namespace
82
83 class BookmarkCodecTest : public testing::Test {
84 protected:
85 // Helpers to create bookmark models with different data.
86 BookmarkModel* CreateTestModel1() {
87 scoped_ptr<BookmarkModel> model(client_.CreateModel(false));
88 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
89 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
90 return model.release();
91 }
92 BookmarkModel* CreateTestModel2() {
93 scoped_ptr<BookmarkModel> model(client_.CreateModel(false));
94 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
95 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
96 model->AddURL(bookmark_bar, 1, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
97 return model.release();
98 }
99 BookmarkModel* CreateTestModel3() {
100 scoped_ptr<BookmarkModel> model(client_.CreateModel(false));
101 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
102 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
103 const BookmarkNode* folder1 =
104 model->AddFolder(bookmark_bar, 1, ASCIIToUTF16(kFolder1Title));
105 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
106 return model.release();
107 }
108
109 void GetBookmarksBarChildValue(base::Value* value,
110 size_t index,
111 base::DictionaryValue** result_value) {
112 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
113
114 base::DictionaryValue* d_value = static_cast<base::DictionaryValue*>(value);
115 base::Value* roots;
116 ASSERT_TRUE(d_value->Get(BookmarkCodec::kRootsKey, &roots));
117 ASSERT_EQ(base::Value::TYPE_DICTIONARY, roots->GetType());
118
119 base::DictionaryValue* roots_d_value =
120 static_cast<base::DictionaryValue*>(roots);
121 base::Value* bb_value;
122 ASSERT_TRUE(
123 roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, &bb_value));
124 ASSERT_EQ(base::Value::TYPE_DICTIONARY, bb_value->GetType());
125
126 base::DictionaryValue* bb_d_value =
127 static_cast<base::DictionaryValue*>(bb_value);
128 base::Value* bb_children_value;
129 ASSERT_TRUE(
130 bb_d_value->Get(BookmarkCodec::kChildrenKey, &bb_children_value));
131 ASSERT_EQ(base::Value::TYPE_LIST, bb_children_value->GetType());
132
133 base::ListValue* bb_children_l_value =
134 static_cast<base::ListValue*>(bb_children_value);
135 base::Value* child_value;
136 ASSERT_TRUE(bb_children_l_value->Get(index, &child_value));
137 ASSERT_EQ(base::Value::TYPE_DICTIONARY, child_value->GetType());
138
139 *result_value = static_cast<base::DictionaryValue*>(child_value);
140 }
141
142 base::Value* EncodeHelper(BookmarkModel* model, std::string* checksum) {
143 BookmarkCodec encoder;
144 // Computed and stored checksums should be empty.
145 EXPECT_EQ("", encoder.computed_checksum());
146 EXPECT_EQ("", encoder.stored_checksum());
147
148 scoped_ptr<base::Value> value(encoder.Encode(model));
149 const std::string& computed_checksum = encoder.computed_checksum();
150 const std::string& stored_checksum = encoder.stored_checksum();
151
152 // Computed and stored checksums should not be empty and should be equal.
153 EXPECT_FALSE(computed_checksum.empty());
154 EXPECT_FALSE(stored_checksum.empty());
155 EXPECT_EQ(computed_checksum, stored_checksum);
156
157 *checksum = computed_checksum;
158 return value.release();
159 }
160
161 bool Decode(BookmarkCodec* codec,
162 BookmarkModel* model,
163 const base::Value& value) {
164 int64 max_id;
165 bool result = codec->Decode(AsMutable(model->bookmark_bar_node()),
166 AsMutable(model->other_node()),
167 AsMutable(model->mobile_node()),
168 &max_id,
169 value);
170 model->set_next_node_id(max_id);
171 AsMutable(model->root_node())->SetMetaInfoMap(codec->model_meta_info_map());
172 AsMutable(model->root_node())
173 ->set_sync_transaction_version(codec->model_sync_transaction_version());
174
175 return result;
176 }
177
178 BookmarkModel* DecodeHelper(const base::Value& value,
179 const std::string& expected_stored_checksum,
180 std::string* computed_checksum,
181 bool expected_changes) {
182 BookmarkCodec decoder;
183 // Computed and stored checksums should be empty.
184 EXPECT_EQ("", decoder.computed_checksum());
185 EXPECT_EQ("", decoder.stored_checksum());
186
187 scoped_ptr<BookmarkModel> model(client_.CreateModel(false));
188 EXPECT_TRUE(Decode(&decoder, model.get(), value));
189
190 *computed_checksum = decoder.computed_checksum();
191 const std::string& stored_checksum = decoder.stored_checksum();
192
193 // Computed and stored checksums should not be empty.
194 EXPECT_FALSE(computed_checksum->empty());
195 EXPECT_FALSE(stored_checksum.empty());
196
197 // Stored checksum should be as expected.
198 EXPECT_EQ(expected_stored_checksum, stored_checksum);
199
200 // The two checksums should be equal if expected_changes is true; otherwise
201 // they should be different.
202 if (expected_changes)
203 EXPECT_NE(*computed_checksum, stored_checksum);
204 else
205 EXPECT_EQ(*computed_checksum, stored_checksum);
206
207 return model.release();
208 }
209
210 void CheckIDs(const BookmarkNode* node, std::set<int64>* assigned_ids) {
211 DCHECK(node);
212 int64 node_id = node->id();
213 EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end());
214 assigned_ids->insert(node_id);
215 for (int i = 0; i < node->child_count(); ++i)
216 CheckIDs(node->GetChild(i), assigned_ids);
217 }
218
219 void ExpectIDsUnique(BookmarkModel* model) {
220 std::set<int64> assigned_ids;
221 CheckIDs(model->bookmark_bar_node(), &assigned_ids);
222 CheckIDs(model->other_node(), &assigned_ids);
223 CheckIDs(model->mobile_node(), &assigned_ids);
224 }
225
226 test::TestBookmarkClient client_;
227 };
228
229 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) {
230 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
231 std::string enc_checksum;
232 scoped_ptr<base::Value> value(
233 EncodeHelper(model_to_encode.get(), &enc_checksum));
234
235 EXPECT_TRUE(value.get() != NULL);
236
237 std::string dec_checksum;
238 scoped_ptr<BookmarkModel> decoded_model(
239 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false));
240 }
241
242 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) {
243 // Encode two identical models and make sure the check-sums are same as long
244 // as the data is the same.
245 scoped_ptr<BookmarkModel> model1(CreateTestModel1());
246 std::string enc_checksum1;
247 scoped_ptr<base::Value> value1(EncodeHelper(model1.get(), &enc_checksum1));
248 EXPECT_TRUE(value1.get() != NULL);
249
250 scoped_ptr<BookmarkModel> model2(CreateTestModel1());
251 std::string enc_checksum2;
252 scoped_ptr<base::Value> value2(EncodeHelper(model2.get(), &enc_checksum2));
253 EXPECT_TRUE(value2.get() != NULL);
254
255 ASSERT_EQ(enc_checksum1, enc_checksum2);
256 }
257
258 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) {
259 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
260 std::string enc_checksum;
261 scoped_ptr<base::Value> value(
262 EncodeHelper(model_to_encode.get(), &enc_checksum));
263
264 EXPECT_TRUE(value.get() != NULL);
265
266 // Change something in the encoded value before decoding it.
267 base::DictionaryValue* child1_value;
268 GetBookmarksBarChildValue(value.get(), 0, &child1_value);
269 std::string title;
270 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title));
271 child1_value->SetString(BookmarkCodec::kNameKey, title + "1");
272
273 std::string dec_checksum;
274 scoped_ptr<BookmarkModel> decoded_model1(
275 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true));
276
277 // Undo the change and make sure the checksum is same as original.
278 child1_value->SetString(BookmarkCodec::kNameKey, title);
279 scoped_ptr<BookmarkModel> decoded_model2(
280 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false));
281 }
282
283 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) {
284 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
285
286 // The test depends on existence of multiple children under bookmark bar, so
287 // make sure that's the case.
288 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count();
289 ASSERT_GT(bb_child_count, 1);
290
291 std::string enc_checksum;
292 scoped_ptr<base::Value> value(
293 EncodeHelper(model_to_encode.get(), &enc_checksum));
294
295 EXPECT_TRUE(value.get() != NULL);
296
297 // Change IDs for all children of bookmark bar to be 1.
298 base::DictionaryValue* child_value;
299 for (int i = 0; i < bb_child_count; ++i) {
300 GetBookmarksBarChildValue(value.get(), i, &child_value);
301 std::string id;
302 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id));
303 child_value->SetString(BookmarkCodec::kIdKey, "1");
304 }
305
306 std::string dec_checksum;
307 scoped_ptr<BookmarkModel> decoded_model(
308 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true));
309
310 ExpectIDsUnique(decoded_model.get());
311
312 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe.
313 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node();
314 decoded_model->AddURL(
315 bb_node, 0, ASCIIToUTF16("new url1"), GURL("http://newurl1.com"));
316 decoded_model->AddURL(
317 bb_node, 0, ASCIIToUTF16("new url2"), GURL("http://newurl2.com"));
318
319 ExpectIDsUnique(decoded_model.get());
320 }
321
322 TEST_F(BookmarkCodecTest, PersistIDsTest) {
323 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
324 BookmarkCodec encoder;
325 scoped_ptr<base::Value> model_value(encoder.Encode(model_to_encode.get()));
326
327 scoped_ptr<BookmarkModel> decoded_model(client_.CreateModel(false));
328 BookmarkCodec decoder;
329 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *model_value.get()));
330 ASSERT_NO_FATAL_FAILURE(
331 AssertModelsEqual(model_to_encode.get(), decoded_model.get()));
332
333 // Add a couple of more items to the decoded bookmark model and make sure
334 // ID persistence is working properly.
335 const BookmarkNode* bookmark_bar = decoded_model->bookmark_bar_node();
336 decoded_model->AddURL(bookmark_bar,
337 bookmark_bar->child_count(),
338 ASCIIToUTF16(kUrl3Title),
339 GURL(kUrl3Url));
340 const BookmarkNode* folder2_node = decoded_model->AddFolder(
341 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title));
342 decoded_model->AddURL(
343 folder2_node, 0, ASCIIToUTF16(kUrl4Title), GURL(kUrl4Url));
344
345 BookmarkCodec encoder2;
346 scoped_ptr<base::Value> model_value2(encoder2.Encode(decoded_model.get()));
347
348 scoped_ptr<BookmarkModel> decoded_model2(client_.CreateModel(false));
349 BookmarkCodec decoder2;
350 ASSERT_TRUE(Decode(&decoder2, decoded_model2.get(), *model_value2.get()));
351 ASSERT_NO_FATAL_FAILURE(
352 AssertModelsEqual(decoded_model.get(), decoded_model2.get()));
353 }
354
355 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) {
356 base::FilePath test_data_directory;
357 base::FilePath test_file =
358 GetTestDataDir().AppendASCII("bookmarks/model_without_sync.json");
359 ASSERT_TRUE(base::PathExists(test_file));
360
361 JSONFileValueSerializer serializer(test_file);
362 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
363
364 scoped_ptr<BookmarkModel> decoded_model(client_.CreateModel(false));
365 BookmarkCodec decoder;
366 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *root.get()));
367 ExpectIDsUnique(decoded_model.get());
368
369 const BookmarkNode* bbn = decoded_model->bookmark_bar_node();
370 ASSERT_EQ(1, bbn->child_count());
371
372 const BookmarkNode* child = bbn->GetChild(0);
373 EXPECT_EQ(BookmarkNode::FOLDER, child->type());
374 EXPECT_EQ(ASCIIToUTF16("Folder A"), child->GetTitle());
375 ASSERT_EQ(1, child->child_count());
376
377 child = child->GetChild(0);
378 EXPECT_EQ(BookmarkNode::URL, child->type());
379 EXPECT_EQ(ASCIIToUTF16("Bookmark Manager"), child->GetTitle());
380
381 const BookmarkNode* other = decoded_model->other_node();
382 ASSERT_EQ(1, other->child_count());
383
384 child = other->GetChild(0);
385 EXPECT_EQ(BookmarkNode::FOLDER, child->type());
386 EXPECT_EQ(ASCIIToUTF16("Folder B"), child->GetTitle());
387 ASSERT_EQ(1, child->child_count());
388
389 child = child->GetChild(0);
390 EXPECT_EQ(BookmarkNode::URL, child->type());
391 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child->GetTitle());
392
393 ASSERT_TRUE(decoded_model->mobile_node() != NULL);
394 }
395
396 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) {
397 // Add meta info and encode.
398 scoped_ptr<BookmarkModel> model(CreateTestModel1());
399 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1");
400 model->SetNodeMetaInfo(
401 model->bookmark_bar_node()->GetChild(0), "node_info", "value2");
402 std::string checksum;
403 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum));
404 ASSERT_TRUE(value.get() != NULL);
405
406 // Decode and check for meta info.
407 model.reset(DecodeHelper(*value, checksum, &checksum, false));
408 std::string meta_value;
409 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value));
410 EXPECT_EQ("value1", meta_value);
411 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value));
412 const BookmarkNode* bbn = model->bookmark_bar_node();
413 ASSERT_EQ(1, bbn->child_count());
414 const BookmarkNode* child = bbn->GetChild(0);
415 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value));
416 EXPECT_EQ("value2", meta_value);
417 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value));
418 }
419
420 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) {
421 // Add sync transaction version and encode.
422 scoped_ptr<BookmarkModel> model(CreateTestModel2());
423 model->SetNodeSyncTransactionVersion(model->root_node(), 1);
424 const BookmarkNode* bbn = model->bookmark_bar_node();
425 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42);
426
427 std::string checksum;
428 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum));
429 ASSERT_TRUE(value.get() != NULL);
430
431 // Decode and verify.
432 model.reset(DecodeHelper(*value, checksum, &checksum, false));
433 EXPECT_EQ(1, model->root_node()->sync_transaction_version());
434 bbn = model->bookmark_bar_node();
435 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version());
436 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion,
437 bbn->GetChild(0)->sync_transaction_version());
438 }
439
440 // Verifies that we can still decode the old codec format after changing the
441 // way meta info is stored.
442 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) {
443 base::FilePath test_data_directory;
444 base::FilePath test_file =
445 GetTestDataDir().AppendASCII("bookmarks/meta_info_as_string.json");
446 ASSERT_TRUE(base::PathExists(test_file));
447
448 JSONFileValueSerializer serializer(test_file);
449 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
450
451 scoped_ptr<BookmarkModel> model(client_.CreateModel(false));
452 BookmarkCodec decoder;
453 ASSERT_TRUE(Decode(&decoder, model.get(), *root.get()));
454
455 EXPECT_EQ(1, model->root_node()->sync_transaction_version());
456 const BookmarkNode* bbn = model->bookmark_bar_node();
457 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion,
458 bbn->GetChild(0)->sync_transaction_version());
459 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version());
460
461 const char kSyncTransactionVersionKey[] = "sync.transaction_version";
462 const char kNormalKey[] = "key";
463 const char kNestedKey[] = "nested.key";
464 std::string meta_value;
465 EXPECT_FALSE(
466 model->root_node()->GetMetaInfo(kSyncTransactionVersionKey, &meta_value));
467 EXPECT_FALSE(
468 bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey, &meta_value));
469 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value));
470 EXPECT_EQ("value", meta_value);
471 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value));
472 EXPECT_EQ("value2", meta_value);
473 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value));
474 EXPECT_EQ("value3", meta_value);
475 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698