| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/bookmarks/browser/bookmark_codec.h" | 5 #include "components/bookmarks/browser/bookmark_codec.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 12 #include "base/json/json_file_value_serializer.h" | 14 #include "base/json/json_file_value_serializer.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 15 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
| 20 #include "components/bookmarks/test/test_bookmark_client.h" | 21 #include "components/bookmarks/test/test_bookmark_client.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using base::ASCIIToUTF16; | 24 using base::ASCIIToUTF16; |
| 24 | 25 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 ASSERT_NO_FATAL_FAILURE( | 82 ASSERT_NO_FATAL_FAILURE( |
| 82 AssertNodesEqual(expected->mobile_node(), actual->mobile_node())); | 83 AssertNodesEqual(expected->mobile_node(), actual->mobile_node())); |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace | 86 } // namespace |
| 86 | 87 |
| 87 class BookmarkCodecTest : public testing::Test { | 88 class BookmarkCodecTest : public testing::Test { |
| 88 protected: | 89 protected: |
| 89 // Helpers to create bookmark models with different data. | 90 // Helpers to create bookmark models with different data. |
| 90 BookmarkModel* CreateTestModel1() { | 91 BookmarkModel* CreateTestModel1() { |
| 91 scoped_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); | 92 std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); |
| 92 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 93 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
| 93 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); | 94 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); |
| 94 return model.release(); | 95 return model.release(); |
| 95 } | 96 } |
| 96 BookmarkModel* CreateTestModel2() { | 97 BookmarkModel* CreateTestModel2() { |
| 97 scoped_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); | 98 std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); |
| 98 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 99 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
| 99 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); | 100 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); |
| 100 model->AddURL(bookmark_bar, 1, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); | 101 model->AddURL(bookmark_bar, 1, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); |
| 101 return model.release(); | 102 return model.release(); |
| 102 } | 103 } |
| 103 BookmarkModel* CreateTestModel3() { | 104 BookmarkModel* CreateTestModel3() { |
| 104 scoped_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); | 105 std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); |
| 105 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 106 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
| 106 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); | 107 model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url)); |
| 107 const BookmarkNode* folder1 = | 108 const BookmarkNode* folder1 = |
| 108 model->AddFolder(bookmark_bar, 1, ASCIIToUTF16(kFolder1Title)); | 109 model->AddFolder(bookmark_bar, 1, ASCIIToUTF16(kFolder1Title)); |
| 109 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); | 110 model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url)); |
| 110 return model.release(); | 111 return model.release(); |
| 111 } | 112 } |
| 112 | 113 |
| 113 void GetBookmarksBarChildValue(base::Value* value, | 114 void GetBookmarksBarChildValue(base::Value* value, |
| 114 size_t index, | 115 size_t index, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 143 | 144 |
| 144 child_value->GetAsDictionary(result_value); | 145 child_value->GetAsDictionary(result_value); |
| 145 } | 146 } |
| 146 | 147 |
| 147 base::Value* EncodeHelper(BookmarkModel* model, std::string* checksum) { | 148 base::Value* EncodeHelper(BookmarkModel* model, std::string* checksum) { |
| 148 BookmarkCodec encoder; | 149 BookmarkCodec encoder; |
| 149 // Computed and stored checksums should be empty. | 150 // Computed and stored checksums should be empty. |
| 150 EXPECT_EQ("", encoder.computed_checksum()); | 151 EXPECT_EQ("", encoder.computed_checksum()); |
| 151 EXPECT_EQ("", encoder.stored_checksum()); | 152 EXPECT_EQ("", encoder.stored_checksum()); |
| 152 | 153 |
| 153 scoped_ptr<base::Value> value(encoder.Encode(model)); | 154 std::unique_ptr<base::Value> value(encoder.Encode(model)); |
| 154 const std::string& computed_checksum = encoder.computed_checksum(); | 155 const std::string& computed_checksum = encoder.computed_checksum(); |
| 155 const std::string& stored_checksum = encoder.stored_checksum(); | 156 const std::string& stored_checksum = encoder.stored_checksum(); |
| 156 | 157 |
| 157 // Computed and stored checksums should not be empty and should be equal. | 158 // Computed and stored checksums should not be empty and should be equal. |
| 158 EXPECT_FALSE(computed_checksum.empty()); | 159 EXPECT_FALSE(computed_checksum.empty()); |
| 159 EXPECT_FALSE(stored_checksum.empty()); | 160 EXPECT_FALSE(stored_checksum.empty()); |
| 160 EXPECT_EQ(computed_checksum, stored_checksum); | 161 EXPECT_EQ(computed_checksum, stored_checksum); |
| 161 | 162 |
| 162 *checksum = computed_checksum; | 163 *checksum = computed_checksum; |
| 163 return value.release(); | 164 return value.release(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 182 | 183 |
| 183 BookmarkModel* DecodeHelper(const base::Value& value, | 184 BookmarkModel* DecodeHelper(const base::Value& value, |
| 184 const std::string& expected_stored_checksum, | 185 const std::string& expected_stored_checksum, |
| 185 std::string* computed_checksum, | 186 std::string* computed_checksum, |
| 186 bool expected_changes) { | 187 bool expected_changes) { |
| 187 BookmarkCodec decoder; | 188 BookmarkCodec decoder; |
| 188 // Computed and stored checksums should be empty. | 189 // Computed and stored checksums should be empty. |
| 189 EXPECT_EQ("", decoder.computed_checksum()); | 190 EXPECT_EQ("", decoder.computed_checksum()); |
| 190 EXPECT_EQ("", decoder.stored_checksum()); | 191 EXPECT_EQ("", decoder.stored_checksum()); |
| 191 | 192 |
| 192 scoped_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); | 193 std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); |
| 193 EXPECT_TRUE(Decode(&decoder, model.get(), value)); | 194 EXPECT_TRUE(Decode(&decoder, model.get(), value)); |
| 194 | 195 |
| 195 *computed_checksum = decoder.computed_checksum(); | 196 *computed_checksum = decoder.computed_checksum(); |
| 196 const std::string& stored_checksum = decoder.stored_checksum(); | 197 const std::string& stored_checksum = decoder.stored_checksum(); |
| 197 | 198 |
| 198 // Computed and stored checksums should not be empty. | 199 // Computed and stored checksums should not be empty. |
| 199 EXPECT_FALSE(computed_checksum->empty()); | 200 EXPECT_FALSE(computed_checksum->empty()); |
| 200 EXPECT_FALSE(stored_checksum.empty()); | 201 EXPECT_FALSE(stored_checksum.empty()); |
| 201 | 202 |
| 202 // Stored checksum should be as expected. | 203 // Stored checksum should be as expected. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 223 | 224 |
| 224 void ExpectIDsUnique(BookmarkModel* model) { | 225 void ExpectIDsUnique(BookmarkModel* model) { |
| 225 std::set<int64_t> assigned_ids; | 226 std::set<int64_t> assigned_ids; |
| 226 CheckIDs(model->bookmark_bar_node(), &assigned_ids); | 227 CheckIDs(model->bookmark_bar_node(), &assigned_ids); |
| 227 CheckIDs(model->other_node(), &assigned_ids); | 228 CheckIDs(model->other_node(), &assigned_ids); |
| 228 CheckIDs(model->mobile_node(), &assigned_ids); | 229 CheckIDs(model->mobile_node(), &assigned_ids); |
| 229 } | 230 } |
| 230 }; | 231 }; |
| 231 | 232 |
| 232 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { | 233 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { |
| 233 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 234 std::unique_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
| 234 std::string enc_checksum; | 235 std::string enc_checksum; |
| 235 scoped_ptr<base::Value> value( | 236 std::unique_ptr<base::Value> value( |
| 236 EncodeHelper(model_to_encode.get(), &enc_checksum)); | 237 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
| 237 | 238 |
| 238 EXPECT_TRUE(value.get() != NULL); | 239 EXPECT_TRUE(value.get() != NULL); |
| 239 | 240 |
| 240 std::string dec_checksum; | 241 std::string dec_checksum; |
| 241 scoped_ptr<BookmarkModel> decoded_model( | 242 std::unique_ptr<BookmarkModel> decoded_model( |
| 242 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false)); | 243 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false)); |
| 243 } | 244 } |
| 244 | 245 |
| 245 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) { | 246 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) { |
| 246 // Encode two identical models and make sure the check-sums are same as long | 247 // Encode two identical models and make sure the check-sums are same as long |
| 247 // as the data is the same. | 248 // as the data is the same. |
| 248 scoped_ptr<BookmarkModel> model1(CreateTestModel1()); | 249 std::unique_ptr<BookmarkModel> model1(CreateTestModel1()); |
| 249 std::string enc_checksum1; | 250 std::string enc_checksum1; |
| 250 scoped_ptr<base::Value> value1(EncodeHelper(model1.get(), &enc_checksum1)); | 251 std::unique_ptr<base::Value> value1( |
| 252 EncodeHelper(model1.get(), &enc_checksum1)); |
| 251 EXPECT_TRUE(value1.get() != NULL); | 253 EXPECT_TRUE(value1.get() != NULL); |
| 252 | 254 |
| 253 scoped_ptr<BookmarkModel> model2(CreateTestModel1()); | 255 std::unique_ptr<BookmarkModel> model2(CreateTestModel1()); |
| 254 std::string enc_checksum2; | 256 std::string enc_checksum2; |
| 255 scoped_ptr<base::Value> value2(EncodeHelper(model2.get(), &enc_checksum2)); | 257 std::unique_ptr<base::Value> value2( |
| 258 EncodeHelper(model2.get(), &enc_checksum2)); |
| 256 EXPECT_TRUE(value2.get() != NULL); | 259 EXPECT_TRUE(value2.get() != NULL); |
| 257 | 260 |
| 258 ASSERT_EQ(enc_checksum1, enc_checksum2); | 261 ASSERT_EQ(enc_checksum1, enc_checksum2); |
| 259 } | 262 } |
| 260 | 263 |
| 261 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) { | 264 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) { |
| 262 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 265 std::unique_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
| 263 std::string enc_checksum; | 266 std::string enc_checksum; |
| 264 scoped_ptr<base::Value> value( | 267 std::unique_ptr<base::Value> value( |
| 265 EncodeHelper(model_to_encode.get(), &enc_checksum)); | 268 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
| 266 | 269 |
| 267 EXPECT_TRUE(value.get() != NULL); | 270 EXPECT_TRUE(value.get() != NULL); |
| 268 | 271 |
| 269 // Change something in the encoded value before decoding it. | 272 // Change something in the encoded value before decoding it. |
| 270 base::DictionaryValue* child1_value; | 273 base::DictionaryValue* child1_value; |
| 271 GetBookmarksBarChildValue(value.get(), 0, &child1_value); | 274 GetBookmarksBarChildValue(value.get(), 0, &child1_value); |
| 272 std::string title; | 275 std::string title; |
| 273 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title)); | 276 ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title)); |
| 274 child1_value->SetString(BookmarkCodec::kNameKey, title + "1"); | 277 child1_value->SetString(BookmarkCodec::kNameKey, title + "1"); |
| 275 | 278 |
| 276 std::string dec_checksum; | 279 std::string dec_checksum; |
| 277 scoped_ptr<BookmarkModel> decoded_model1( | 280 std::unique_ptr<BookmarkModel> decoded_model1( |
| 278 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true)); | 281 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true)); |
| 279 | 282 |
| 280 // Undo the change and make sure the checksum is same as original. | 283 // Undo the change and make sure the checksum is same as original. |
| 281 child1_value->SetString(BookmarkCodec::kNameKey, title); | 284 child1_value->SetString(BookmarkCodec::kNameKey, title); |
| 282 scoped_ptr<BookmarkModel> decoded_model2( | 285 std::unique_ptr<BookmarkModel> decoded_model2( |
| 283 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false)); | 286 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, false)); |
| 284 } | 287 } |
| 285 | 288 |
| 286 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) { | 289 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) { |
| 287 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); | 290 std::unique_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
| 288 | 291 |
| 289 // The test depends on existence of multiple children under bookmark bar, so | 292 // The test depends on existence of multiple children under bookmark bar, so |
| 290 // make sure that's the case. | 293 // make sure that's the case. |
| 291 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count(); | 294 int bb_child_count = model_to_encode->bookmark_bar_node()->child_count(); |
| 292 ASSERT_GT(bb_child_count, 1); | 295 ASSERT_GT(bb_child_count, 1); |
| 293 | 296 |
| 294 std::string enc_checksum; | 297 std::string enc_checksum; |
| 295 scoped_ptr<base::Value> value( | 298 std::unique_ptr<base::Value> value( |
| 296 EncodeHelper(model_to_encode.get(), &enc_checksum)); | 299 EncodeHelper(model_to_encode.get(), &enc_checksum)); |
| 297 | 300 |
| 298 EXPECT_TRUE(value.get() != NULL); | 301 EXPECT_TRUE(value.get() != NULL); |
| 299 | 302 |
| 300 // Change IDs for all children of bookmark bar to be 1. | 303 // Change IDs for all children of bookmark bar to be 1. |
| 301 base::DictionaryValue* child_value; | 304 base::DictionaryValue* child_value; |
| 302 for (int i = 0; i < bb_child_count; ++i) { | 305 for (int i = 0; i < bb_child_count; ++i) { |
| 303 GetBookmarksBarChildValue(value.get(), i, &child_value); | 306 GetBookmarksBarChildValue(value.get(), i, &child_value); |
| 304 std::string id; | 307 std::string id; |
| 305 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id)); | 308 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id)); |
| 306 child_value->SetString(BookmarkCodec::kIdKey, "1"); | 309 child_value->SetString(BookmarkCodec::kIdKey, "1"); |
| 307 } | 310 } |
| 308 | 311 |
| 309 std::string dec_checksum; | 312 std::string dec_checksum; |
| 310 scoped_ptr<BookmarkModel> decoded_model( | 313 std::unique_ptr<BookmarkModel> decoded_model( |
| 311 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true)); | 314 DecodeHelper(*value.get(), enc_checksum, &dec_checksum, true)); |
| 312 | 315 |
| 313 ExpectIDsUnique(decoded_model.get()); | 316 ExpectIDsUnique(decoded_model.get()); |
| 314 | 317 |
| 315 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe. | 318 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe. |
| 316 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node(); | 319 const BookmarkNode* bb_node = decoded_model->bookmark_bar_node(); |
| 317 decoded_model->AddURL( | 320 decoded_model->AddURL( |
| 318 bb_node, 0, ASCIIToUTF16("new url1"), GURL("http://newurl1.com")); | 321 bb_node, 0, ASCIIToUTF16("new url1"), GURL("http://newurl1.com")); |
| 319 decoded_model->AddURL( | 322 decoded_model->AddURL( |
| 320 bb_node, 0, ASCIIToUTF16("new url2"), GURL("http://newurl2.com")); | 323 bb_node, 0, ASCIIToUTF16("new url2"), GURL("http://newurl2.com")); |
| 321 | 324 |
| 322 ExpectIDsUnique(decoded_model.get()); | 325 ExpectIDsUnique(decoded_model.get()); |
| 323 } | 326 } |
| 324 | 327 |
| 325 TEST_F(BookmarkCodecTest, PersistIDsTest) { | 328 TEST_F(BookmarkCodecTest, PersistIDsTest) { |
| 326 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); | 329 std::unique_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
| 327 BookmarkCodec encoder; | 330 BookmarkCodec encoder; |
| 328 scoped_ptr<base::Value> model_value(encoder.Encode(model_to_encode.get())); | 331 std::unique_ptr<base::Value> model_value( |
| 332 encoder.Encode(model_to_encode.get())); |
| 329 | 333 |
| 330 scoped_ptr<BookmarkModel> decoded_model(TestBookmarkClient::CreateModel()); | 334 std::unique_ptr<BookmarkModel> decoded_model( |
| 335 TestBookmarkClient::CreateModel()); |
| 331 BookmarkCodec decoder; | 336 BookmarkCodec decoder; |
| 332 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *model_value.get())); | 337 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *model_value.get())); |
| 333 ASSERT_NO_FATAL_FAILURE( | 338 ASSERT_NO_FATAL_FAILURE( |
| 334 AssertModelsEqual(model_to_encode.get(), decoded_model.get())); | 339 AssertModelsEqual(model_to_encode.get(), decoded_model.get())); |
| 335 | 340 |
| 336 // Add a couple of more items to the decoded bookmark model and make sure | 341 // Add a couple of more items to the decoded bookmark model and make sure |
| 337 // ID persistence is working properly. | 342 // ID persistence is working properly. |
| 338 const BookmarkNode* bookmark_bar = decoded_model->bookmark_bar_node(); | 343 const BookmarkNode* bookmark_bar = decoded_model->bookmark_bar_node(); |
| 339 decoded_model->AddURL(bookmark_bar, | 344 decoded_model->AddURL(bookmark_bar, |
| 340 bookmark_bar->child_count(), | 345 bookmark_bar->child_count(), |
| 341 ASCIIToUTF16(kUrl3Title), | 346 ASCIIToUTF16(kUrl3Title), |
| 342 GURL(kUrl3Url)); | 347 GURL(kUrl3Url)); |
| 343 const BookmarkNode* folder2_node = decoded_model->AddFolder( | 348 const BookmarkNode* folder2_node = decoded_model->AddFolder( |
| 344 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title)); | 349 bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title)); |
| 345 decoded_model->AddURL( | 350 decoded_model->AddURL( |
| 346 folder2_node, 0, ASCIIToUTF16(kUrl4Title), GURL(kUrl4Url)); | 351 folder2_node, 0, ASCIIToUTF16(kUrl4Title), GURL(kUrl4Url)); |
| 347 | 352 |
| 348 BookmarkCodec encoder2; | 353 BookmarkCodec encoder2; |
| 349 scoped_ptr<base::Value> model_value2(encoder2.Encode(decoded_model.get())); | 354 std::unique_ptr<base::Value> model_value2( |
| 355 encoder2.Encode(decoded_model.get())); |
| 350 | 356 |
| 351 scoped_ptr<BookmarkModel> decoded_model2(TestBookmarkClient::CreateModel()); | 357 std::unique_ptr<BookmarkModel> decoded_model2( |
| 358 TestBookmarkClient::CreateModel()); |
| 352 BookmarkCodec decoder2; | 359 BookmarkCodec decoder2; |
| 353 ASSERT_TRUE(Decode(&decoder2, decoded_model2.get(), *model_value2.get())); | 360 ASSERT_TRUE(Decode(&decoder2, decoded_model2.get(), *model_value2.get())); |
| 354 ASSERT_NO_FATAL_FAILURE( | 361 ASSERT_NO_FATAL_FAILURE( |
| 355 AssertModelsEqual(decoded_model.get(), decoded_model2.get())); | 362 AssertModelsEqual(decoded_model.get(), decoded_model2.get())); |
| 356 } | 363 } |
| 357 | 364 |
| 358 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) { | 365 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) { |
| 359 base::FilePath test_file = | 366 base::FilePath test_file = |
| 360 GetTestDataDir().AppendASCII("bookmarks/model_without_sync.json"); | 367 GetTestDataDir().AppendASCII("bookmarks/model_without_sync.json"); |
| 361 ASSERT_TRUE(base::PathExists(test_file)); | 368 ASSERT_TRUE(base::PathExists(test_file)); |
| 362 | 369 |
| 363 JSONFileValueDeserializer deserializer(test_file); | 370 JSONFileValueDeserializer deserializer(test_file); |
| 364 scoped_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); | 371 std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); |
| 365 | 372 |
| 366 scoped_ptr<BookmarkModel> decoded_model(TestBookmarkClient::CreateModel()); | 373 std::unique_ptr<BookmarkModel> decoded_model( |
| 374 TestBookmarkClient::CreateModel()); |
| 367 BookmarkCodec decoder; | 375 BookmarkCodec decoder; |
| 368 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *root.get())); | 376 ASSERT_TRUE(Decode(&decoder, decoded_model.get(), *root.get())); |
| 369 ExpectIDsUnique(decoded_model.get()); | 377 ExpectIDsUnique(decoded_model.get()); |
| 370 | 378 |
| 371 const BookmarkNode* bbn = decoded_model->bookmark_bar_node(); | 379 const BookmarkNode* bbn = decoded_model->bookmark_bar_node(); |
| 372 ASSERT_EQ(1, bbn->child_count()); | 380 ASSERT_EQ(1, bbn->child_count()); |
| 373 | 381 |
| 374 const BookmarkNode* child = bbn->GetChild(0); | 382 const BookmarkNode* child = bbn->GetChild(0); |
| 375 EXPECT_EQ(BookmarkNode::FOLDER, child->type()); | 383 EXPECT_EQ(BookmarkNode::FOLDER, child->type()); |
| 376 EXPECT_EQ(ASCIIToUTF16("Folder A"), child->GetTitle()); | 384 EXPECT_EQ(ASCIIToUTF16("Folder A"), child->GetTitle()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 390 | 398 |
| 391 child = child->GetChild(0); | 399 child = child->GetChild(0); |
| 392 EXPECT_EQ(BookmarkNode::URL, child->type()); | 400 EXPECT_EQ(BookmarkNode::URL, child->type()); |
| 393 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child->GetTitle()); | 401 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child->GetTitle()); |
| 394 | 402 |
| 395 ASSERT_TRUE(decoded_model->mobile_node() != NULL); | 403 ASSERT_TRUE(decoded_model->mobile_node() != NULL); |
| 396 } | 404 } |
| 397 | 405 |
| 398 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) { | 406 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) { |
| 399 // Add meta info and encode. | 407 // Add meta info and encode. |
| 400 scoped_ptr<BookmarkModel> model(CreateTestModel1()); | 408 std::unique_ptr<BookmarkModel> model(CreateTestModel1()); |
| 401 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1"); | 409 model->SetNodeMetaInfo(model->root_node(), "model_info", "value1"); |
| 402 model->SetNodeMetaInfo( | 410 model->SetNodeMetaInfo( |
| 403 model->bookmark_bar_node()->GetChild(0), "node_info", "value2"); | 411 model->bookmark_bar_node()->GetChild(0), "node_info", "value2"); |
| 404 std::string checksum; | 412 std::string checksum; |
| 405 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); | 413 std::unique_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); |
| 406 ASSERT_TRUE(value.get() != NULL); | 414 ASSERT_TRUE(value.get() != NULL); |
| 407 | 415 |
| 408 // Decode and check for meta info. | 416 // Decode and check for meta info. |
| 409 model.reset(DecodeHelper(*value, checksum, &checksum, false)); | 417 model.reset(DecodeHelper(*value, checksum, &checksum, false)); |
| 410 std::string meta_value; | 418 std::string meta_value; |
| 411 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value)); | 419 EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value)); |
| 412 EXPECT_EQ("value1", meta_value); | 420 EXPECT_EQ("value1", meta_value); |
| 413 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value)); | 421 EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value)); |
| 414 const BookmarkNode* bbn = model->bookmark_bar_node(); | 422 const BookmarkNode* bbn = model->bookmark_bar_node(); |
| 415 ASSERT_EQ(1, bbn->child_count()); | 423 ASSERT_EQ(1, bbn->child_count()); |
| 416 const BookmarkNode* child = bbn->GetChild(0); | 424 const BookmarkNode* child = bbn->GetChild(0); |
| 417 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value)); | 425 EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value)); |
| 418 EXPECT_EQ("value2", meta_value); | 426 EXPECT_EQ("value2", meta_value); |
| 419 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value)); | 427 EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value)); |
| 420 } | 428 } |
| 421 | 429 |
| 422 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) { | 430 TEST_F(BookmarkCodecTest, EncodeAndDecodeSyncTransactionVersion) { |
| 423 // Add sync transaction version and encode. | 431 // Add sync transaction version and encode. |
| 424 scoped_ptr<BookmarkModel> model(CreateTestModel2()); | 432 std::unique_ptr<BookmarkModel> model(CreateTestModel2()); |
| 425 model->SetNodeSyncTransactionVersion(model->root_node(), 1); | 433 model->SetNodeSyncTransactionVersion(model->root_node(), 1); |
| 426 const BookmarkNode* bbn = model->bookmark_bar_node(); | 434 const BookmarkNode* bbn = model->bookmark_bar_node(); |
| 427 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42); | 435 model->SetNodeSyncTransactionVersion(bbn->GetChild(1), 42); |
| 428 | 436 |
| 429 std::string checksum; | 437 std::string checksum; |
| 430 scoped_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); | 438 std::unique_ptr<base::Value> value(EncodeHelper(model.get(), &checksum)); |
| 431 ASSERT_TRUE(value.get() != NULL); | 439 ASSERT_TRUE(value.get() != NULL); |
| 432 | 440 |
| 433 // Decode and verify. | 441 // Decode and verify. |
| 434 model.reset(DecodeHelper(*value, checksum, &checksum, false)); | 442 model.reset(DecodeHelper(*value, checksum, &checksum, false)); |
| 435 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); | 443 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); |
| 436 bbn = model->bookmark_bar_node(); | 444 bbn = model->bookmark_bar_node(); |
| 437 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); | 445 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); |
| 438 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, | 446 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, |
| 439 bbn->GetChild(0)->sync_transaction_version()); | 447 bbn->GetChild(0)->sync_transaction_version()); |
| 440 } | 448 } |
| 441 | 449 |
| 442 // Verifies that we can still decode the old codec format after changing the | 450 // Verifies that we can still decode the old codec format after changing the |
| 443 // way meta info is stored. | 451 // way meta info is stored. |
| 444 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) { | 452 TEST_F(BookmarkCodecTest, CanDecodeMetaInfoAsString) { |
| 445 base::FilePath test_file = | 453 base::FilePath test_file = |
| 446 GetTestDataDir().AppendASCII("bookmarks/meta_info_as_string.json"); | 454 GetTestDataDir().AppendASCII("bookmarks/meta_info_as_string.json"); |
| 447 ASSERT_TRUE(base::PathExists(test_file)); | 455 ASSERT_TRUE(base::PathExists(test_file)); |
| 448 | 456 |
| 449 JSONFileValueDeserializer deserializer(test_file); | 457 JSONFileValueDeserializer deserializer(test_file); |
| 450 scoped_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); | 458 std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); |
| 451 | 459 |
| 452 scoped_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); | 460 std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel()); |
| 453 BookmarkCodec decoder; | 461 BookmarkCodec decoder; |
| 454 ASSERT_TRUE(Decode(&decoder, model.get(), *root.get())); | 462 ASSERT_TRUE(Decode(&decoder, model.get(), *root.get())); |
| 455 | 463 |
| 456 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); | 464 EXPECT_EQ(1, model->root_node()->sync_transaction_version()); |
| 457 const BookmarkNode* bbn = model->bookmark_bar_node(); | 465 const BookmarkNode* bbn = model->bookmark_bar_node(); |
| 458 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, | 466 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion, |
| 459 bbn->GetChild(0)->sync_transaction_version()); | 467 bbn->GetChild(0)->sync_transaction_version()); |
| 460 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); | 468 EXPECT_EQ(42, bbn->GetChild(1)->sync_transaction_version()); |
| 461 | 469 |
| 462 const char kSyncTransactionVersionKey[] = "sync.transaction_version"; | 470 const char kSyncTransactionVersionKey[] = "sync.transaction_version"; |
| 463 const char kNormalKey[] = "key"; | 471 const char kNormalKey[] = "key"; |
| 464 const char kNestedKey[] = "nested.key"; | 472 const char kNestedKey[] = "nested.key"; |
| 465 std::string meta_value; | 473 std::string meta_value; |
| 466 EXPECT_FALSE( | 474 EXPECT_FALSE( |
| 467 model->root_node()->GetMetaInfo(kSyncTransactionVersionKey, &meta_value)); | 475 model->root_node()->GetMetaInfo(kSyncTransactionVersionKey, &meta_value)); |
| 468 EXPECT_FALSE( | 476 EXPECT_FALSE( |
| 469 bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey, &meta_value)); | 477 bbn->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey, &meta_value)); |
| 470 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value)); | 478 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNormalKey, &meta_value)); |
| 471 EXPECT_EQ("value", meta_value); | 479 EXPECT_EQ("value", meta_value); |
| 472 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value)); | 480 EXPECT_TRUE(bbn->GetChild(1)->GetMetaInfo(kNormalKey, &meta_value)); |
| 473 EXPECT_EQ("value2", meta_value); | 481 EXPECT_EQ("value2", meta_value); |
| 474 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value)); | 482 EXPECT_TRUE(bbn->GetChild(0)->GetMetaInfo(kNestedKey, &meta_value)); |
| 475 EXPECT_EQ("value3", meta_value); | 483 EXPECT_EQ("value3", meta_value); |
| 476 } | 484 } |
| 477 | 485 |
| 478 } // namespace bookmarks | 486 } // namespace bookmarks |
| OLD | NEW |