OLD | NEW |
| (Empty) |
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/test/model_test_utils.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | |
9 #include "googleurl/src/gurl.h" | |
10 | |
11 namespace model_test_utils { | |
12 | |
13 std::string ModelStringFromNode(const BookmarkNode* node) { | |
14 // Since the children of the node are not available as a vector, | |
15 // we'll just have to do it the hard way. | |
16 int child_count = node->child_count(); | |
17 std::string child_string; | |
18 for (int i = 0; i < child_count; ++i) { | |
19 const BookmarkNode* child = node->GetChild(i); | |
20 if (child->is_folder()) { | |
21 child_string += UTF16ToUTF8(child->GetTitle()) + ":[ " + | |
22 ModelStringFromNode(child) + "] "; | |
23 } else { | |
24 child_string += UTF16ToUTF8(child->GetTitle()) + " "; | |
25 } | |
26 } | |
27 return child_string; | |
28 } | |
29 | |
30 // Helper function which does the actual work of creating the nodes for | |
31 // a particular level in the hierarchy. | |
32 std::string::size_type AddNodesFromString(BookmarkModel& model, | |
33 const BookmarkNode* node, | |
34 const std::string& model_string, | |
35 std::string::size_type start_pos) { | |
36 DCHECK(node); | |
37 int index = node->child_count(); | |
38 static const std::string folder_tell(":["); | |
39 std::string::size_type end_pos = model_string.find(' ', start_pos); | |
40 while (end_pos != std::string::npos) { | |
41 std::string::size_type part_length = end_pos - start_pos; | |
42 std::string node_name = model_string.substr(start_pos, part_length); | |
43 // Are we at the end of a folder group? | |
44 if (node_name != "]") { | |
45 // No, is it a folder? | |
46 std::string tell; | |
47 if (part_length > 2) | |
48 tell = node_name.substr(part_length - 2, 2); | |
49 if (tell == folder_tell) { | |
50 node_name = node_name.substr(0, part_length - 2); | |
51 const BookmarkNode* new_node = | |
52 model.AddFolder(node, index, UTF8ToUTF16(node_name)); | |
53 end_pos = AddNodesFromString(model, new_node, model_string, | |
54 end_pos + 1); | |
55 } else { | |
56 std::string url_string("http://"); | |
57 url_string += std::string(node_name.begin(), node_name.end()); | |
58 url_string += ".com"; | |
59 model.AddURL(node, index, UTF8ToUTF16(node_name), GURL(url_string)); | |
60 ++end_pos; | |
61 } | |
62 ++index; | |
63 start_pos = end_pos; | |
64 end_pos = model_string.find(' ', start_pos); | |
65 } else { | |
66 ++end_pos; | |
67 break; | |
68 } | |
69 } | |
70 return end_pos; | |
71 } | |
72 | |
73 void AddNodesFromModelString(BookmarkModel& model, | |
74 const BookmarkNode* node, | |
75 const std::string& model_string) { | |
76 DCHECK(node); | |
77 const std::string folder_tell(":["); | |
78 std::string::size_type start_pos = 0; | |
79 std::string::size_type end_pos = | |
80 AddNodesFromString(model, node, model_string, start_pos); | |
81 DCHECK(end_pos == std::string::npos); | |
82 } | |
83 | |
84 } // namespace model_test_utils | |
OLD | NEW |