Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / bookmarks / test / bookmark_test_helpers.cc
blobb10afa67d17137941556e20ac1d3786370f50092
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
3 // found in the LICENSE file.
5 #include "components/bookmarks/test/bookmark_test_helpers.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "url/gurl.h"
16 namespace bookmarks {
17 namespace test {
19 namespace {
21 // BookmarkLoadObserver is used when blocking until the BookmarkModel finishes
22 // loading. As soon as the BookmarkModel finishes loading the message loop is
23 // quit.
24 class BookmarkLoadObserver : public BaseBookmarkModelObserver {
25 public:
26 explicit BookmarkLoadObserver(const base::Closure& quit_task);
27 ~BookmarkLoadObserver() override;
29 private:
30 // BaseBookmarkModelObserver:
31 void BookmarkModelChanged() override;
32 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
34 base::Closure quit_task_;
36 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadObserver);
39 BookmarkLoadObserver::BookmarkLoadObserver(const base::Closure& quit_task)
40 : quit_task_(quit_task) {}
42 BookmarkLoadObserver::~BookmarkLoadObserver() {}
44 void BookmarkLoadObserver::BookmarkModelChanged() {}
46 void BookmarkLoadObserver::BookmarkModelLoaded(BookmarkModel* model,
47 bool ids_reassigned) {
48 quit_task_.Run();
51 // Helper function which does the actual work of creating the nodes for
52 // a particular level in the hierarchy.
53 std::string::size_type AddNodesFromString(BookmarkModel* model,
54 const BookmarkNode* node,
55 const std::string& model_string,
56 std::string::size_type start_pos) {
57 DCHECK(node);
58 int index = node->child_count();
59 static const std::string folder_tell(":[");
60 std::string::size_type end_pos = model_string.find(' ', start_pos);
61 while (end_pos != std::string::npos) {
62 std::string::size_type part_length = end_pos - start_pos;
63 std::string node_name = model_string.substr(start_pos, part_length);
64 // Are we at the end of a folder group?
65 if (node_name != "]") {
66 // No, is it a folder?
67 std::string tell;
68 if (part_length > 2)
69 tell = node_name.substr(part_length - 2, 2);
70 if (tell == folder_tell) {
71 node_name = node_name.substr(0, part_length - 2);
72 const BookmarkNode* new_node =
73 model->AddFolder(node, index, base::UTF8ToUTF16(node_name));
74 end_pos = AddNodesFromString(model, new_node, model_string,
75 end_pos + 1);
76 } else {
77 std::string url_string("http://");
78 url_string += std::string(node_name.begin(), node_name.end());
79 url_string += ".com";
80 model->AddURL(
81 node, index, base::UTF8ToUTF16(node_name), GURL(url_string));
82 ++end_pos;
84 ++index;
85 start_pos = end_pos;
86 end_pos = model_string.find(' ', start_pos);
87 } else {
88 ++end_pos;
89 break;
92 return end_pos;
95 } // namespace
97 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
98 if (model->loaded())
99 return;
100 base::RunLoop run_loop;
101 base::MessageLoop::ScopedNestableTaskAllower allow(
102 base::MessageLoop::current());
104 BookmarkLoadObserver observer(run_loop.QuitClosure());
105 model->AddObserver(&observer);
106 run_loop.Run();
107 model->RemoveObserver(&observer);
108 DCHECK(model->loaded());
111 std::string ModelStringFromNode(const BookmarkNode* node) {
112 // Since the children of the node are not available as a vector,
113 // we'll just have to do it the hard way.
114 int child_count = node->child_count();
115 std::string child_string;
116 for (int i = 0; i < child_count; ++i) {
117 const BookmarkNode* child = node->GetChild(i);
118 if (child->is_folder()) {
119 child_string += base::UTF16ToUTF8(child->GetTitle()) + ":[ " +
120 ModelStringFromNode(child) + "] ";
121 } else {
122 child_string += base::UTF16ToUTF8(child->GetTitle()) + " ";
125 return child_string;
128 void AddNodesFromModelString(BookmarkModel* model,
129 const BookmarkNode* node,
130 const std::string& model_string) {
131 DCHECK(node);
132 std::string::size_type start_pos = 0;
133 std::string::size_type end_pos =
134 AddNodesFromString(model, node, model_string, start_pos);
135 DCHECK(end_pos == std::string::npos);
138 } // namespace test
139 } // namespace bookmarks