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"
21 // BookmarkLoadObserver is used when blocking until the BookmarkModel finishes
22 // loading. As soon as the BookmarkModel finishes loading the message loop is
24 class BookmarkLoadObserver
: public BaseBookmarkModelObserver
{
26 explicit BookmarkLoadObserver(const base::Closure
& quit_task
);
27 ~BookmarkLoadObserver() override
;
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
) {
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
) {
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?
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
,
77 std::string
url_string("http://");
78 url_string
+= std::string(node_name
.begin(), node_name
.end());
81 node
, index
, base::UTF8ToUTF16(node_name
), GURL(url_string
));
86 end_pos
= model_string
.find(' ', start_pos
);
97 void WaitForBookmarkModelToLoad(BookmarkModel
* model
) {
100 base::RunLoop run_loop
;
101 base::MessageLoop::ScopedNestableTaskAllower
allow(
102 base::MessageLoop::current());
104 BookmarkLoadObserver
observer(run_loop
.QuitClosure());
105 model
->AddObserver(&observer
);
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
) + "] ";
122 child_string
+= base::UTF16ToUTF8(child
->GetTitle()) + " ";
128 void AddNodesFromModelString(BookmarkModel
* model
,
129 const BookmarkNode
* node
,
130 const std::string
& model_string
) {
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
);
139 } // namespace bookmarks