Home | History | Annotate | Download | only in bookmarks
      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.
      4 
      5 #include "base/file_util.h"
      6 #include "base/files/file_path.h"
      7 #include "base/json/json_file_value_serializer.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/path_service.h"
     10 #include "base/strings/string_util.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "base/values.h"
     13 #include "chrome/browser/bookmarks/bookmark_codec.h"
     14 #include "chrome/browser/bookmarks/bookmark_model.h"
     15 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h"
     16 #include "chrome/common/chrome_paths.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 namespace {
     20 
     21 const char kUrl1Title[] = "url1";
     22 const char kUrl1Url[] = "http://www.url1.com";
     23 const char kUrl2Title[] = "url2";
     24 const char kUrl2Url[] = "http://www.url2.com";
     25 const char kUrl3Title[] = "url3";
     26 const char kUrl3Url[] = "http://www.url3.com";
     27 const char kUrl4Title[] = "url4";
     28 const char kUrl4Url[] = "http://www.url4.com";
     29 const char kFolder1Title[] = "folder1";
     30 const char kFolder2Title[] = "folder2";
     31 
     32 // Helper to get a mutable bookmark node.
     33 BookmarkNode* AsMutable(const BookmarkNode* node) {
     34   return const_cast<BookmarkNode*>(node);
     35 }
     36 
     37 }  // namespace
     38 
     39 class BookmarkCodecTest : public testing::Test {
     40  protected:
     41   // Helpers to create bookmark models with different data.
     42   BookmarkModel* CreateTestModel1() {
     43     scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
     44     const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
     45     model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
     46     return model.release();
     47   }
     48   BookmarkModel* CreateTestModel2() {
     49     scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
     50     const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
     51     model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
     52     model->AddURL(bookmark_bar, 1, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
     53     return model.release();
     54   }
     55   BookmarkModel* CreateTestModel3() {
     56     scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
     57     const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
     58     model->AddURL(bookmark_bar, 0, ASCIIToUTF16(kUrl1Title), GURL(kUrl1Url));
     59     const BookmarkNode* folder1 = model->AddFolder(bookmark_bar, 1,
     60                                                    ASCIIToUTF16(kFolder1Title));
     61     model->AddURL(folder1, 0, ASCIIToUTF16(kUrl2Title), GURL(kUrl2Url));
     62     return model.release();
     63   }
     64 
     65   void GetBookmarksBarChildValue(Value* value,
     66                                  size_t index,
     67                                  DictionaryValue** result_value) {
     68     ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType());
     69 
     70     DictionaryValue* d_value = static_cast<DictionaryValue*>(value);
     71     Value* roots;
     72     ASSERT_TRUE(d_value->Get(BookmarkCodec::kRootsKey, &roots));
     73     ASSERT_EQ(Value::TYPE_DICTIONARY, roots->GetType());
     74 
     75     DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots);
     76     Value* bb_value;
     77     ASSERT_TRUE(roots_d_value->Get(BookmarkCodec::kRootFolderNameKey,
     78                                    &bb_value));
     79     ASSERT_EQ(Value::TYPE_DICTIONARY, bb_value->GetType());
     80 
     81     DictionaryValue* bb_d_value = static_cast<DictionaryValue*>(bb_value);
     82     Value* bb_children_value;
     83     ASSERT_TRUE(bb_d_value->Get(BookmarkCodec::kChildrenKey,
     84                                 &bb_children_value));
     85     ASSERT_EQ(Value::TYPE_LIST, bb_children_value->GetType());
     86 
     87     ListValue* bb_children_l_value = static_cast<ListValue*>(bb_children_value);
     88     Value* child_value;
     89     ASSERT_TRUE(bb_children_l_value->Get(index, &child_value));
     90     ASSERT_EQ(Value::TYPE_DICTIONARY, child_value->GetType());
     91 
     92     *result_value = static_cast<DictionaryValue*>(child_value);
     93   }
     94 
     95   Value* EncodeHelper(BookmarkModel* model, std::string* checksum) {
     96     BookmarkCodec encoder;
     97     // Computed and stored checksums should be empty.
     98     EXPECT_EQ("", encoder.computed_checksum());
     99     EXPECT_EQ("", encoder.stored_checksum());
    100 
    101     scoped_ptr<Value> value(encoder.Encode(model));
    102     const std::string& computed_checksum = encoder.computed_checksum();
    103     const std::string& stored_checksum = encoder.stored_checksum();
    104 
    105     // Computed and stored checksums should not be empty and should be equal.
    106     EXPECT_FALSE(computed_checksum.empty());
    107     EXPECT_FALSE(stored_checksum.empty());
    108     EXPECT_EQ(computed_checksum, stored_checksum);
    109 
    110     *checksum = computed_checksum;
    111     return value.release();
    112   }
    113 
    114   bool Decode(BookmarkCodec* codec, BookmarkModel* model, const Value& value) {
    115     int64 max_id;
    116     bool result = codec->Decode(AsMutable(model->bookmark_bar_node()),
    117                                 AsMutable(model->other_node()),
    118                                 AsMutable(model->mobile_node()),
    119                                 &max_id, value);
    120     model->set_next_node_id(max_id);
    121     AsMutable(model->root_node())->set_meta_info_str(codec->model_meta_info());
    122     return result;
    123   }
    124 
    125   BookmarkModel* DecodeHelper(const Value& value,
    126                               const std::string& expected_stored_checksum,
    127                               std::string* computed_checksum,
    128                               bool expected_changes) {
    129     BookmarkCodec decoder;
    130     // Computed and stored checksums should be empty.
    131     EXPECT_EQ("", decoder.computed_checksum());
    132     EXPECT_EQ("", decoder.stored_checksum());
    133 
    134     scoped_ptr<BookmarkModel> model(new BookmarkModel(NULL));
    135     EXPECT_TRUE(Decode(&decoder, model.get(), value));
    136 
    137     *computed_checksum = decoder.computed_checksum();
    138     const std::string& stored_checksum = decoder.stored_checksum();
    139 
    140     // Computed and stored checksums should not be empty.
    141     EXPECT_FALSE(computed_checksum->empty());
    142     EXPECT_FALSE(stored_checksum.empty());
    143 
    144     // Stored checksum should be as expected.
    145     EXPECT_EQ(expected_stored_checksum, stored_checksum);
    146 
    147     // The two checksums should be equal if expected_changes is true; otherwise
    148     // they should be different.
    149     if (expected_changes)
    150       EXPECT_NE(*computed_checksum, stored_checksum);
    151     else
    152       EXPECT_EQ(*computed_checksum, stored_checksum);
    153 
    154     return model.release();
    155   }
    156 
    157   void CheckIDs(const BookmarkNode* node, std::set<int64>* assigned_ids) {
    158     DCHECK(node);
    159     int64 node_id = node->id();
    160     EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end());
    161     assigned_ids->insert(node_id);
    162     for (int i = 0; i < node->child_count(); ++i)
    163       CheckIDs(node->GetChild(i), assigned_ids);
    164   }
    165 
    166   void ExpectIDsUnique(BookmarkModel* model) {
    167     std::set<int64> assigned_ids;
    168     CheckIDs(model->bookmark_bar_node(), &assigned_ids);
    169     CheckIDs(model->other_node(), &assigned_ids);
    170     CheckIDs(model->mobile_node(), &assigned_ids);
    171   }
    172 };
    173 
    174 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) {
    175   scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
    176   std::string enc_checksum;
    177   scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum));
    178 
    179   EXPECT_TRUE(value.get() != NULL);
    180 
    181   std::string dec_checksum;
    182   scoped_ptr<BookmarkModel> decoded_model(DecodeHelper(
    183       *value.get(), enc_checksum, &dec_checksum, false));
    184 }
    185 
    186 TEST_F(BookmarkCodecTest, ChecksumEncodeIdenticalModelsTest) {
    187   // Encode two identical models and make sure the check-sums are same as long
    188   // as the data is the same.
    189   scoped_ptr<BookmarkModel> model1(CreateTestModel1());
    190   std::string enc_checksum1;
    191   scoped_ptr<Value> value1(EncodeHelper(model1.get(), &enc_checksum1));
    192   EXPECT_TRUE(value1.get() != NULL);
    193 
    194   scoped_ptr<BookmarkModel> model2(CreateTestModel1());
    195   std::string enc_checksum2;
    196   scoped_ptr<Value> value2(EncodeHelper(model2.get(), &enc_checksum2));
    197   EXPECT_TRUE(value2.get() != NULL);
    198 
    199   ASSERT_EQ(enc_checksum1, enc_checksum2);
    200 }
    201 
    202 TEST_F(BookmarkCodecTest, ChecksumManualEditTest) {
    203   scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1());
    204   std::string enc_checksum;
    205   scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum));
    206 
    207   EXPECT_TRUE(value.get() != NULL);
    208 
    209   // Change something in the encoded value before decoding it.
    210   DictionaryValue* child1_value;
    211   GetBookmarksBarChildValue(value.get(), 0, &child1_value);
    212   std::string title;
    213   ASSERT_TRUE(child1_value->GetString(BookmarkCodec::kNameKey, &title));
    214   child1_value->SetString(BookmarkCodec::kNameKey, title + "1");
    215 
    216   std::string dec_checksum;
    217   scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper(
    218       *value.get(), enc_checksum, &dec_checksum, true));
    219 
    220   // Undo the change and make sure the checksum is same as original.
    221   child1_value->SetString(BookmarkCodec::kNameKey, title);
    222   scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper(
    223       *value.get(), enc_checksum, &dec_checksum, false));
    224 }
    225 
    226 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) {
    227   scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
    228 
    229   // The test depends on existence of multiple children under bookmark bar, so
    230   // make sure that's the case.
    231   int bb_child_count = model_to_encode->bookmark_bar_node()->child_count();
    232   ASSERT_GT(bb_child_count, 1);
    233 
    234   std::string enc_checksum;
    235   scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum));
    236 
    237   EXPECT_TRUE(value.get() != NULL);
    238 
    239   // Change IDs for all children of bookmark bar to be 1.
    240   DictionaryValue* child_value;
    241   for (int i = 0; i < bb_child_count; ++i) {
    242     GetBookmarksBarChildValue(value.get(), i, &child_value);
    243     std::string id;
    244     ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id));
    245     child_value->SetString(BookmarkCodec::kIdKey, "1");
    246   }
    247 
    248   std::string dec_checksum;
    249   scoped_ptr<BookmarkModel> decoded_model(DecodeHelper(
    250       *value.get(), enc_checksum, &dec_checksum, true));
    251 
    252   ExpectIDsUnique(decoded_model.get());
    253 
    254   // add a few extra nodes to bookmark model and make sure IDs are still uniuqe.
    255   const BookmarkNode* bb_node = decoded_model->bookmark_bar_node();
    256   decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url1"),
    257                         GURL("http://newurl1.com"));
    258   decoded_model->AddURL(bb_node, 0, ASCIIToUTF16("new url2"),
    259                         GURL("http://newurl2.com"));
    260 
    261   ExpectIDsUnique(decoded_model.get());
    262 }
    263 
    264 TEST_F(BookmarkCodecTest, PersistIDsTest) {
    265   scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3());
    266   BookmarkCodec encoder;
    267   scoped_ptr<Value> model_value(encoder.Encode(model_to_encode.get()));
    268 
    269   BookmarkModel decoded_model(NULL);
    270   BookmarkCodec decoder;
    271   ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get()));
    272   BookmarkModelTestUtils::AssertModelsEqual(model_to_encode.get(),
    273                                             &decoded_model,
    274                                             true);
    275 
    276   // Add a couple of more items to the decoded bookmark model and make sure
    277   // ID persistence is working properly.
    278   const BookmarkNode* bookmark_bar = decoded_model.bookmark_bar_node();
    279   decoded_model.AddURL(
    280       bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kUrl3Title),
    281       GURL(kUrl3Url));
    282   const BookmarkNode* folder2_node = decoded_model.AddFolder(
    283       bookmark_bar, bookmark_bar->child_count(), ASCIIToUTF16(kFolder2Title));
    284   decoded_model.AddURL(folder2_node, 0, ASCIIToUTF16(kUrl4Title),
    285                        GURL(kUrl4Url));
    286 
    287   BookmarkCodec encoder2;
    288   scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model));
    289 
    290   BookmarkModel decoded_model2(NULL);
    291   BookmarkCodec decoder2;
    292   ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get()));
    293   BookmarkModelTestUtils::AssertModelsEqual(&decoded_model,
    294                                             &decoded_model2,
    295                                             true);
    296 }
    297 
    298 TEST_F(BookmarkCodecTest, CanDecodeModelWithoutMobileBookmarks) {
    299   base::FilePath test_data_directory;
    300   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory));
    301   base::FilePath test_file = test_data_directory.AppendASCII(
    302       "bookmarks/model_without_sync.json");
    303   ASSERT_TRUE(base::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.bookmark_bar_node();
    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.mobile_node() != NULL);
    338 }
    339 
    340 TEST_F(BookmarkCodecTest, EncodeAndDecodeMetaInfo) {
    341   // Add meta info and encode.
    342   scoped_ptr<BookmarkModel> model(CreateTestModel1());
    343   model->SetNodeMetaInfo(model->root_node(), "model_info", "value1");
    344   model->SetNodeMetaInfo(model->bookmark_bar_node()->GetChild(0),
    345                          "node_info", "value2");
    346   std::string checksum;
    347   scoped_ptr<Value> value(EncodeHelper(model.get(), &checksum));
    348   ASSERT_TRUE(value.get() != NULL);
    349 
    350   // Decode and check for meta info.
    351   model.reset(DecodeHelper(*value, checksum, &checksum, false));
    352   std::string meta_value;
    353   EXPECT_TRUE(model->root_node()->GetMetaInfo("model_info", &meta_value));
    354   EXPECT_EQ("value1", meta_value);
    355   EXPECT_FALSE(model->root_node()->GetMetaInfo("other_key", &meta_value));
    356   const BookmarkNode* bbn = model->bookmark_bar_node();
    357   ASSERT_EQ(1, bbn->child_count());
    358   const BookmarkNode* child = bbn->GetChild(0);
    359   EXPECT_TRUE(child->GetMetaInfo("node_info", &meta_value));
    360   EXPECT_EQ("value2", meta_value);
    361   EXPECT_FALSE(child->GetMetaInfo("other_key", &meta_value));
    362 }
    363