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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "url/gurl.h" 11 12 namespace { 13 14 // Helper to verify the two given bookmark nodes. 15 // The IDs of the bookmark nodes are compared only if check_ids is true. 16 void AssertNodesEqual(const BookmarkNode* expected, 17 const BookmarkNode* actual, 18 bool check_ids) { 19 ASSERT_TRUE(expected); 20 ASSERT_TRUE(actual); 21 if (check_ids) 22 EXPECT_EQ(expected->id(), actual->id()); 23 EXPECT_EQ(expected->GetTitle(), actual->GetTitle()); 24 EXPECT_EQ(expected->type(), actual->type()); 25 EXPECT_TRUE(expected->date_added() == actual->date_added()); 26 if (expected->is_url()) { 27 EXPECT_EQ(expected->url(), actual->url()); 28 } else { 29 EXPECT_TRUE(expected->date_folder_modified() == 30 actual->date_folder_modified()); 31 ASSERT_EQ(expected->child_count(), actual->child_count()); 32 for (int i = 0; i < expected->child_count(); ++i) 33 AssertNodesEqual(expected->GetChild(i), actual->GetChild(i), check_ids); 34 } 35 } 36 37 // Helper function which does the actual work of creating the nodes for 38 // a particular level in the hierarchy. 39 std::string::size_type AddNodesFromString(BookmarkModel* model, 40 const BookmarkNode* node, 41 const std::string& model_string, 42 std::string::size_type start_pos) { 43 DCHECK(node); 44 int index = node->child_count(); 45 static const std::string folder_tell(":["); 46 std::string::size_type end_pos = model_string.find(' ', start_pos); 47 while (end_pos != std::string::npos) { 48 std::string::size_type part_length = end_pos - start_pos; 49 std::string node_name = model_string.substr(start_pos, part_length); 50 // Are we at the end of a folder group? 51 if (node_name != "]") { 52 // No, is it a folder? 53 std::string tell; 54 if (part_length > 2) 55 tell = node_name.substr(part_length - 2, 2); 56 if (tell == folder_tell) { 57 node_name = node_name.substr(0, part_length - 2); 58 const BookmarkNode* new_node = 59 model->AddFolder(node, index, UTF8ToUTF16(node_name)); 60 end_pos = AddNodesFromString(model, new_node, model_string, 61 end_pos + 1); 62 } else { 63 std::string url_string("http://"); 64 url_string += std::string(node_name.begin(), node_name.end()); 65 url_string += ".com"; 66 model->AddURL(node, index, UTF8ToUTF16(node_name), GURL(url_string)); 67 ++end_pos; 68 } 69 ++index; 70 start_pos = end_pos; 71 end_pos = model_string.find(' ', start_pos); 72 } else { 73 ++end_pos; 74 break; 75 } 76 } 77 return end_pos; 78 } 79 80 } // namespace 81 82 // static 83 void BookmarkModelTestUtils::AssertModelsEqual(BookmarkModel* expected, 84 BookmarkModel* actual, 85 bool check_ids) { 86 AssertNodesEqual(expected->bookmark_bar_node(), 87 actual->bookmark_bar_node(), 88 check_ids); 89 AssertNodesEqual(expected->other_node(), actual->other_node(), check_ids); 90 AssertNodesEqual(expected->mobile_node(), actual->mobile_node(), check_ids); 91 } 92 93 // static 94 std::string BookmarkModelTestUtils::ModelStringFromNode( 95 const BookmarkNode* node) { 96 // Since the children of the node are not available as a vector, 97 // we'll just have to do it the hard way. 98 int child_count = node->child_count(); 99 std::string child_string; 100 for (int i = 0; i < child_count; ++i) { 101 const BookmarkNode* child = node->GetChild(i); 102 if (child->is_folder()) { 103 child_string += UTF16ToUTF8(child->GetTitle()) + ":[ " + 104 ModelStringFromNode(child) + "] "; 105 } else { 106 child_string += UTF16ToUTF8(child->GetTitle()) + " "; 107 } 108 } 109 return child_string; 110 } 111 112 // static 113 void BookmarkModelTestUtils::AddNodesFromModelString( 114 BookmarkModel* model, 115 const BookmarkNode* node, 116 const std::string& model_string) { 117 DCHECK(node); 118 std::string::size_type start_pos = 0; 119 std::string::size_type end_pos = 120 AddNodesFromString(model, node, model_string, start_pos); 121 DCHECK(end_pos == std::string::npos); 122 } 123