Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_codec_unittest.cc
blob47357793bcbb01df7204a4d104d7c7322fbac865
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
3 // found in the LICENSE file.
5 #include "chrome/browser/bookmarks/bookmark_codec.h"
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 "chrome/browser/bookmarks/bookmark_model.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using base::ASCIIToUTF16;
22 namespace {
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";
35 // Helper to get a mutable bookmark node.
36 BookmarkNode* AsMutable(const BookmarkNode* node) {
37 return const_cast<BookmarkNode*>(node);
40 // Helper to verify the two given bookmark nodes.
41 void AssertNodesEqual(const BookmarkNode* expected,
42 const BookmarkNode* actual) {
43 ASSERT_TRUE(expected);
44 ASSERT_TRUE(actual);
45 EXPECT_EQ(expected->id(), actual->id());
46 EXPECT_EQ(expected->GetTitle(), actual->GetTitle());
47 EXPECT_EQ(expected->type(), actual->type());
48 EXPECT_TRUE(expected->date_added() == actual->date_added());
49 if (expected->is_url()) {
50 EXPECT_EQ(expected->url(), actual->url());
51 } else {
52 EXPECT_TRUE(expected->date_folder_modified() ==
53 actual->date_folder_modified());
54 ASSERT_EQ(expected->child_count(), actual->child_count());
55 for (int i = 0; i < expected->child_count(); ++i)
56 AssertNodesEqual(expected->GetChild(i), actual->GetChild(i));
60 // Verifies that the two given bookmark models are the same.
61 void AssertModelsEqual(BookmarkModel* expected, BookmarkModel* actual) {
62 ASSERT_NO_FATAL_FAILURE(AssertNodesEqual(expected->bookmark_bar_node(),
63 actual->bookmark_bar_node()));
64 ASSERT_NO_FATAL_FAILURE(
65 AssertNodesEqual(expected->other_node(), actual->other_node()));
66 ASSERT_NO_FATAL_FAILURE(
67 AssertNodesEqual(expected->mobile_node(), actual->mobile_node()));
70 } // namespace
72 class BookmarkCodecTest : public testing::Test {
73 protected:
74 // Helpers to create bookmark models with different data.
75 BookmarkModel* CreateTestModel1() {
76 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
77 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
78 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
79 return model.release();
81 BookmarkModel* CreateTestModel2() {
82 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
83 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
84 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
85 model->AddURL(bookmark_bar, 1, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
86 return model.release();
88 BookmarkModel* CreateTestModel3() {
89 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
90 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
91 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
92 const BookmarkNode* folder1 = model->AddFolder(bookmark_bar, 1,
93 ASCIIToUTF16(kFolder1Title));
94 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
95 return model.release();
98 void GetBookmarksBarChildValue(base::Value* value,
99 size_t index,
100 base::DictionaryValue** result_value) {
101 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
103 base::DictionaryValue* d_value = static_cast<base::DictionaryValue*>(value);
104 base::Value* roots;
105 ASSERT_TRUE(d_value->Get(BookmarkCodec::kRootsKey, &roots));
106 ASSERT_EQ(base::Value::TYPE_DICTIONARY, roots->GetType());
108 base::DictionaryValue* roots_d_value =
109 static_cast<base::DictionaryValue*>(roots);
110 base::Value* bb_value;
111 ASSERT_TRUE(roots_d_value->Get(BookmarkCodec::kRootFolderNameKey,
112 &bb_value));
113 ASSERT_EQ(base::Value::TYPE_DICTIONARY, bb_value->GetType());
115 base::DictionaryValue* bb_d_value =
116 static_cast<base::DictionaryValue*>(bb_value);
117 base::Value* bb_children_value;
118 ASSERT_TRUE(bb_d_value->Get(BookmarkCodec::kChildrenKey,
119 &bb_children_value));
120 ASSERT_EQ(base::Value::TYPE_LIST, bb_children_value->GetType());
122 base::ListValue* bb_children_l_value =
123 static_cast<base::ListValue*>(bb_children_value);
124 base::Value* child_value;
125 ASSERT_TRUE(bb_children_l_value->Get(index, &child_value));
126 ASSERT_EQ(base::Value::TYPE_DICTIONARY, child_value->GetType());
128 *result_value = static_cast<base::DictionaryValue*>(child_value);
131 base::Value* EncodeHelper(BookmarkModel* model, std::string* checksum) {
132 BookmarkCodec encoder;
133 // Computed and stored checksums should be empty.
134 EXPECT_EQ("", encoder.computed_checksum());
135 EXPECT_EQ("", encoder.stored_checksum());
137 scoped_ptr<base::Value> value(encoder.Encode(model));
138 const std::string& computed_checksum = encoder.computed_checksum();
139 const std::string& stored_checksum = encoder.stored_checksum();
141 // Computed and stored checksums should not be empty and should be equal.
142 EXPECT_FALSE(computed_checksum.empty());
143 EXPECT_FALSE(stored_checksum.empty());
144 EXPECT_EQ(computed_checksum, stored_checksum);
146 *checksum = computed_checksum;
147 return value.release();
150 bool Decode(BookmarkCodec* codec,
151 BookmarkModel* model,
152 const base::Value& value) {
153 int64 max_id;
154 bool result = codec->Decode(AsMutable(model->bookmark_bar_node()),
155 AsMutable(model->other_node()),
156 AsMutable(model->mobile_node()),
157 &max_id, value);
158 model->set_next_node_id(max_id);
159 AsMutable(model->root_node())->
160 SetMetaInfoMap(codec->model_meta_info_map());
161 AsMutable(model->root_node())->
162 set_sync_transaction_version(codec->model_sync_transaction_version());
164 return result;
167 BookmarkModel* DecodeHelper(const base::Value& value,
168 const std::string& expected_stored_checksum,
169 std::string* computed_checksum,
170 bool expected_changes) {
171 BookmarkCodec decoder;
172 // Computed and stored checksums should be empty.
173 EXPECT_EQ("", decoder.computed_checksum());
174 EXPECT_EQ("", decoder.stored_checksum());
176 scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
177 EXPECT_TRUE(Decode(&decoder, model.get(), value));
179 *computed_checksum = decoder.computed_checksum();
180 const std::string& stored_checksum = decoder.stored_checksum();
182 // Computed and stored checksums should not be empty.
183 EXPECT_FALSE(computed_checksum->empty());
184 EXPECT_FALSE(stored_checksum.empty());
186 // Stored checksum should be as expected.
187 EXPECT_EQ(expected_stored_checksum, stored_checksum);
189 // The two checksums should be equal if expected_changes is true; otherwise
190 // they should be different.
191 if (expected_changes)
192 EXPECT_NE(*computed_checksum, stored_checksum);
193 else
194 EXPECT_EQ(*computed_checksum, stored_checksum);
196 return model.release();
199 void CheckIDs(const BookmarkNode* node, std::set<int64>* assigned_ids) {
200 DCHECK(node);
201 int64 node_id = node->id();
202 EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end());
203 assigned_ids->insert(node_id);
204 for (int i = 0; i < node->child_count(); ++i)
205 CheckIDs(node->GetChild(i), assigned_ids);
208 void ExpectIDsUnique(BookmarkModel* model) {
209 std::set<int64> assigned_ids;
210 CheckIDs(model->bookmark_bar_node(), &assigned_ids);
211 CheckIDs(model->other_node(), &assigned_ids);
212 CheckIDs(model->mobile_node(), &assigned_ids);
216 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) {
217 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
218 std::string enc_checksum;
219 scoped_ptr<base::Value> value(
220 EncodeHelper(model_to_encode.get(), &enc_checksum));
222 EXPECT_TRUE(value.get() != NULL);
224 std::string dec_checksum;
225 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper(
226 *value.get(), enc_checksum, &dec_checksum, false));
229 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) {
230 // Encode two identical models and make sure the check-sums are same as long
231 // as the data is the same.
232 scoped_ptr<BookmarkModel> model1(CreateTestModel1());
233 std::string enc_checksum1;
234 scoped_ptr<base::Value> value1(EncodeHelper(model1.get(), &enc_checksum1));
235 EXPECT_TRUE(value1.get() != NULL);
237 scoped_ptr<BookmarkModel> model2(CreateTestModel1());
238 std::string enc_checksum2;
239 scoped_ptr<base::Value> value2(EncodeHelper(model2.get(), &enc_checksum2));
240 EXPECT_TRUE(value2.get() != NULL);
242 ASSERT_EQ(enc_checksum1, enc_checksum2);
245 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) {
246 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
247 std::string enc_checksum;
248 scoped_ptr<base::Value> value(
249 EncodeHelper(model_to_encode.get(), &enc_checksum));
251 EXPECT_TRUE(value.get() != NULL);
253 // Change something in the encoded value before decoding it.
254 base::DictionaryValue* child1_value;
255 GetBookmarksBarChildValue(value.get(), 0, &child1_value);
256 std::string title;
257 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title));
258 child1_value->SetString(BookmarkCodec::kNameKey, title + "1");
260 std::string dec_checksum;
261 scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper(
262 *value.get(), enc_checksum, &dec_checksum, true));
264 // Undo the change and make sure the checksum is same as original.
265 child1_value->SetString(BookmarkCodec::kNameKey, title);
266 scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper(
267 *value.get(), enc_checksum, &dec_checksum, false));
270 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) {
271 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
273 // The test depends on existence of multiple children under bookmark bar, so
274 // make sure that's the case.
275 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count();
276 ASSERT_GT(bb_child_count, 1);
278 std::string enc_checksum;
279 scoped_ptr<base::Value> value(
280 EncodeHelper(model_to_encode.get(), &enc_checksum));
282 EXPECT_TRUE(value.get() != NULL);
284 // Change IDs for all children of bookmark bar to be 1.
285 base::DictionaryValue* child_value;
286 for (int i = 0; i < bb_child_count; ++i) {
287 GetBookmarksBarChildValue(value.get(), i, &child_value);
288 std::string id;
289 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id));
290 child_value->SetString(BookmarkCodec::kIdKey, "1");
293 std::string dec_checksum;
294 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper(
295 *value.get(), enc_checksum, &dec_checksum, true));
297 ExpectIDsUnique(decoded_model.get());
299 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe.
300 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node();
301 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url1"),
302 GURL("http://newurl1.com"));
303 decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url2"),
304 GURL("http://newurl2.com"));
306 ExpectIDsUnique(decoded_model.get());
309 TEST_F(BookmarkCodecTest, PersistIDsTest) {
310 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
311 BookmarkCodec encoder;
312 scoped_ptr<base::Value> model_value(encoder.Encode(model_to_encode.get()));
314 BookmarkModel decoded_model(NULL);
315 BookmarkCodec decoder;
316 ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get()));
317 ASSERT_NO_FATAL_FAILURE(
318 AssertModelsEqual(model_to_encode.get(), &decoded_model));
320 // Add a couple of more items to the decoded bookmark model and make sure
321 // ID persistence is working properly.
322 const BookmarkNode* bookmark_bar = decoded_model.bookmark_bar_node();
323 decoded_model.AddURL(
324 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kUrl3Title),
325 GURL(kUrl3Url));
326 const BookmarkNode* folder2_node = decoded_model.AddFolder(
327 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title));
328 decoded_model.AddURL(folder2_node, 0, ASCIIToUTF16(kUrl4Title),
329 GURL(kUrl4Url));
331 BookmarkCodec encoder2;
332 scoped_ptr<base::Value> model_value2(encoder2.Encode(&decoded_model));
334 BookmarkModel decoded_model2(NULL);
335 BookmarkCodec decoder2;
336 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get()));
337 ASSERT_NO_FATAL_FAILURE(AssertModelsEqual(&decoded_model, &decoded_model2));
340 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) {
341 base::FilePath test_data_directory;
342 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory));
343 base::FilePath test_file = test_data_directory.AppendASCII(
344 "bookmarks/model_without_sync.json");
345 ASSERT_TRUE(base::PathExists(test_file));
347 JSONFileValueSerializer serializer(test_file);
348 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
350 BookmarkModel decoded_model(NULL);
351 BookmarkCodec decoder;
352 ASSERT_TRUE(Decode(&decoder, &decoded_model, *root.get()));
353 ExpectIDsUnique(&decoded_model);
355 const BookmarkNode* bbn = decoded_model.bookmark_bar_node();
356 ASSERT_EQ(1, bbn->child_count());
358 const BookmarkNode* child = bbn->GetChild(0);
359 EXPECT_EQ(BookmarkNode::FOLDER, child->type());
360 EXPECT_EQ(ASCIIToUTF16("Folder A"), child->GetTitle());
361 ASSERT_EQ(1, child->child_count());
363 child = child->GetChild(0);
364 EXPECT_EQ(BookmarkNode::URL, child->type());
365 EXPECT_EQ(ASCIIToUTF16("Bookmark Manager"), child->GetTitle());
367 const BookmarkNode* other = decoded_model.other_node();
368 ASSERT_EQ(1, other->child_count());
370 child = other->GetChild(0);
371 EXPECT_EQ(BookmarkNode::FOLDER, child->type());
372 EXPECT_EQ(ASCIIToUTF16("Folder B"), child->GetTitle());
373 ASSERT_EQ(1, child->child_count());
375 child = child->GetChild(0);
376 EXPECT_EQ(BookmarkNode::URL, child->type());
377 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child->GetTitle());
379 ASSERT_TRUE(decoded_model.mobile_node() != NULL);
382 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) {
383 // Add meta info and encode.
384 scoped_ptr<BookmarkModel> model(CreateTestModel1());
385 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1");
386 model->SetNodeMetaInfo(model->bookmark_bar_node()->GetChild(0),
387 "node_info", "value2");
388 std::string checksum;
389 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum));
390 ASSERT_TRUE(value.get() != NULL);
392 // Decode and check for meta info.
393 model.reset(DecodeHelper(*value, checksum, &checksum, false));
394 std::string meta_value;
395 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value));
396 EXPECT_EQ("value1", meta_value);
397 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value));
398 const BookmarkNode* bbn = model->bookmark_bar_node();
399 ASSERT_EQ(1, bbn->child_count());
400 const BookmarkNode* child = bbn->GetChild(0);
401 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value));
402 EXPECT_EQ("value2", meta_value);
403 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value));
406 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) {
407 // Add sync transaction version and encode.
408 scoped_ptr<BookmarkModel> model(CreateTestModel2());
409 model->SetNodeSyncTransactionVersion(model->root_node(), 1);
410 const BookmarkNode* bbn = model->bookmark_bar_node();
411 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42);
413 std::string checksum;
414 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum));
415 ASSERT_TRUE(value.get() != NULL);
417 // Decode and verify.
418 model.reset(DecodeHelper(*value, checksum, &checksum, false));
419 EXPECT_EQ(1, model->root_node()->sync_transaction_version());
420 bbn = model->bookmark_bar_node();
421 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version());
422 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion,
423 bbn->GetChild(0)->sync_transaction_version());
426 // Verifies that we can still decode the old codec format after changing the
427 // way meta info is stored.
428 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) {
429 base::FilePath test_data_directory;
430 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory));
431 base::FilePath test_file = test_data_directory.AppendASCII(
432 "bookmarks/meta_info_as_string.json");
433 ASSERT_TRUE(base::PathExists(test_file));
435 JSONFileValueSerializer serializer(test_file);
436 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
438 BookmarkModel model(NULL);
439 BookmarkCodec decoder;
440 ASSERT_TRUE(Decode(&decoder, &model, *root.get()));
442 EXPECT_EQ(1, model.root_node()->sync_transaction_version());
443 const BookmarkNode* bbn = model.bookmark_bar_node();
444 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion,
445 bbn->GetChild(0)->sync_transaction_version());
446 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version());
448 const char kSyncTransactionVersionKey[] = "sync.transaction_version";
449 const char kNormalKey[] = "key";
450 const char kNestedKey[] = "nested.key";
451 std::string meta_value;
452 EXPECT_FALSE(model.root_node()->GetMetaInfo(kSyncTransactionVersionKey,
453 &meta_value));
454 EXPECT_FALSE(bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey,
455 &meta_value));
456 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value));
457 EXPECT_EQ("value", meta_value);
458 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value));
459 EXPECT_EQ("value2", meta_value);
460 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value));
461 EXPECT_EQ("value3", meta_value);