OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_path.h" |
| 6 #include "base/file_util.h" |
5 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/path_service.h" |
6 #include "base/string_util.h" | 9 #include "base/string_util.h" |
7 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
8 #include "base/values.h" | 11 #include "base/values.h" |
9 #include "chrome/browser/bookmarks/bookmark_codec.h" | 12 #include "chrome/browser/bookmarks/bookmark_codec.h" |
10 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
11 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h" | 14 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h" |
12 #include "chrome/browser/bookmarks/bookmark_utils.h" | 15 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "content/common/json_value_serializer.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
14 | 19 |
15 namespace { | 20 namespace { |
16 | 21 |
17 const char kUrl1Title[] = "url1"; | 22 const char kUrl1Title[] = "url1"; |
18 const char kUrl1Url[] = "http://www.url1.com"; | 23 const char kUrl1Url[] = "http://www.url1.com"; |
19 const char kUrl2Title[] = "url2"; | 24 const char kUrl2Title[] = "url2"; |
20 const char kUrl2Url[] = "http://www.url2.com"; | 25 const char kUrl2Url[] = "http://www.url2.com"; |
21 const char kUrl3Title[] = "url3"; | 26 const char kUrl3Title[] = "url3"; |
22 const char kUrl3Url[] = "http://www.url3.com"; | 27 const char kUrl3Url[] = "http://www.url3.com"; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 EXPECT_EQ(computed_checksum, stored_checksum); | 109 EXPECT_EQ(computed_checksum, stored_checksum); |
105 | 110 |
106 *checksum = computed_checksum; | 111 *checksum = computed_checksum; |
107 return value.release(); | 112 return value.release(); |
108 } | 113 } |
109 | 114 |
110 bool Decode(BookmarkCodec* codec, BookmarkModel* model, const Value& value) { | 115 bool Decode(BookmarkCodec* codec, BookmarkModel* model, const Value& value) { |
111 int64 max_id; | 116 int64 max_id; |
112 bool result = codec->Decode(AsMutable(model->GetBookmarkBarNode()), | 117 bool result = codec->Decode(AsMutable(model->GetBookmarkBarNode()), |
113 AsMutable(model->other_node()), | 118 AsMutable(model->other_node()), |
| 119 AsMutable(model->synced_node()), |
114 &max_id, value); | 120 &max_id, value); |
115 model->set_next_node_id(max_id); | 121 model->set_next_node_id(max_id); |
116 return result; | 122 return result; |
117 } | 123 } |
118 | 124 |
119 BookmarkModel* DecodeHelper(const Value& value, | 125 BookmarkModel* DecodeHelper(const Value& value, |
120 const std::string& expected_stored_checksum, | 126 const std::string& expected_stored_checksum, |
121 std::string* computed_checksum, | 127 std::string* computed_checksum, |
122 bool expected_changes) { | 128 bool expected_changes) { |
123 BookmarkCodec decoder; | 129 BookmarkCodec decoder; |
(...skipping 30 matching lines...) Expand all Loading... |
154 EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end()); | 160 EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end()); |
155 assigned_ids->insert(node_id); | 161 assigned_ids->insert(node_id); |
156 for (int i = 0; i < node->child_count(); ++i) | 162 for (int i = 0; i < node->child_count(); ++i) |
157 CheckIDs(node->GetChild(i), assigned_ids); | 163 CheckIDs(node->GetChild(i), assigned_ids); |
158 } | 164 } |
159 | 165 |
160 void ExpectIDsUnique(BookmarkModel* model) { | 166 void ExpectIDsUnique(BookmarkModel* model) { |
161 std::set<int64> assigned_ids; | 167 std::set<int64> assigned_ids; |
162 CheckIDs(model->GetBookmarkBarNode(), &assigned_ids); | 168 CheckIDs(model->GetBookmarkBarNode(), &assigned_ids); |
163 CheckIDs(model->other_node(), &assigned_ids); | 169 CheckIDs(model->other_node(), &assigned_ids); |
| 170 CheckIDs(model->synced_node(), &assigned_ids); |
164 } | 171 } |
165 }; | 172 }; |
166 | 173 |
167 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { | 174 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { |
168 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 175 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
169 std::string enc_checksum; | 176 std::string enc_checksum; |
170 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); | 177 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); |
171 | 178 |
172 EXPECT_TRUE(value.get() != NULL); | 179 EXPECT_TRUE(value.get() != NULL); |
173 | 180 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 BookmarkCodec encoder2; | 287 BookmarkCodec encoder2; |
281 scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model)); | 288 scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model)); |
282 | 289 |
283 BookmarkModel decoded_model2(NULL); | 290 BookmarkModel decoded_model2(NULL); |
284 BookmarkCodec decoder2; | 291 BookmarkCodec decoder2; |
285 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); | 292 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); |
286 BookmarkModelTestUtils::AssertModelsEqual(&decoded_model, | 293 BookmarkModelTestUtils::AssertModelsEqual(&decoded_model, |
287 &decoded_model2, | 294 &decoded_model2, |
288 true); | 295 true); |
289 } | 296 } |
| 297 |
| 298 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutSyncedBookmarks) { |
| 299 FilePath test_data_directory; |
| 300 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
| 301 FilePath test_file = test_data_directory.AppendASCII( |
| 302 "bookmarks/model_without_sync.json"); |
| 303 ASSERT_TRUE(file_util::PathExists(test_file)); |
| 304 |
| 305 JSONFileValueSerializer serializer(test_file); |
| 306 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); |
| 307 |
| 308 BookmarkModel decoded_model(NULL); |
| 309 BookmarkCodec decoder; |
| 310 ASSERT_TRUE(Decode(&decoder, &decoded_model, *root.get())); |
| 311 ExpectIDsUnique(&decoded_model); |
| 312 |
| 313 const BookmarkNode* bbn = decoded_model.GetBookmarkBarNode(); |
| 314 ASSERT_EQ(1, bbn->child_count()); |
| 315 |
| 316 const BookmarkNode* child = bbn->GetChild(0); |
| 317 EXPECT_EQ(BookmarkNode::FOLDER, child->type()); |
| 318 EXPECT_EQ(ASCIIToUTF16("Folder A"), child->GetTitle()); |
| 319 ASSERT_EQ(1, child->child_count()); |
| 320 |
| 321 child = child->GetChild(0); |
| 322 EXPECT_EQ(BookmarkNode::URL, child->type()); |
| 323 EXPECT_EQ(ASCIIToUTF16("Bookmark Manager"), child->GetTitle()); |
| 324 |
| 325 const BookmarkNode* other = decoded_model.other_node(); |
| 326 ASSERT_EQ(1, other->child_count()); |
| 327 |
| 328 child = other->GetChild(0); |
| 329 EXPECT_EQ(BookmarkNode::FOLDER, child->type()); |
| 330 EXPECT_EQ(ASCIIToUTF16("Folder B"), child->GetTitle()); |
| 331 ASSERT_EQ(1, child->child_count()); |
| 332 |
| 333 child = child->GetChild(0); |
| 334 EXPECT_EQ(BookmarkNode::URL, child->type()); |
| 335 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child->GetTitle()); |
| 336 |
| 337 ASSERT_TRUE(decoded_model.synced_node() != NULL); |
| 338 } |
OLD | NEW |