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