Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bookmarks / bookmark_test_helpers.cc
1 // Copyright 2013 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_test_helpers.h"
6
7 #include "base/basictypes.h"
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
14 #include "chrome/browser/bookmarks/bookmark_model.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "content/public/test/test_utils.h"
17 #include "url/gurl.h"
18
19 namespace {
20
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   virtual ~BookmarkLoadObserver();
28
29  private:
30   // BaseBookmarkModelObserver:
31   virtual void BookmarkModelChanged() OVERRIDE;
32   virtual void BookmarkModelLoaded(BookmarkModel* model,
33                                    bool ids_reassigned) OVERRIDE;
34
35   base::Closure quit_task_;
36
37   DISALLOW_COPY_AND_ASSIGN(BookmarkLoadObserver);
38 };
39
40 BookmarkLoadObserver::BookmarkLoadObserver(const base::Closure& quit_task)
41     : quit_task_(quit_task) {}
42
43 BookmarkLoadObserver::~BookmarkLoadObserver() {}
44
45 void BookmarkLoadObserver::BookmarkModelChanged() {}
46
47 void BookmarkLoadObserver::BookmarkModelLoaded(BookmarkModel* model,
48                                                bool ids_reassigned) {
49   quit_task_.Run();
50 }
51
52 // Helper function which does the actual work of creating the nodes for
53 // a particular level in the hierarchy.
54 std::string::size_type AddNodesFromString(BookmarkModel* model,
55                                           const BookmarkNode* node,
56                                           const std::string& model_string,
57                                           std::string::size_type start_pos) {
58   DCHECK(node);
59   int index = node->child_count();
60   static const std::string folder_tell(":[");
61   std::string::size_type end_pos = model_string.find(' ', start_pos);
62   while (end_pos != std::string::npos) {
63     std::string::size_type part_length = end_pos - start_pos;
64     std::string node_name = model_string.substr(start_pos, part_length);
65     // Are we at the end of a folder group?
66     if (node_name != "]") {
67       // No, is it a folder?
68       std::string tell;
69       if (part_length > 2)
70         tell = node_name.substr(part_length - 2, 2);
71       if (tell == folder_tell) {
72         node_name = node_name.substr(0, part_length - 2);
73         const BookmarkNode* new_node =
74             model->AddFolder(node, index, base::UTF8ToUTF16(node_name));
75         end_pos = AddNodesFromString(model, new_node, model_string,
76                                      end_pos + 1);
77       } else {
78         std::string url_string("http://");
79         url_string += std::string(node_name.begin(), node_name.end());
80         url_string += ".com";
81         model->AddURL(
82             node, index, base::UTF8ToUTF16(node_name), GURL(url_string));
83         ++end_pos;
84       }
85       ++index;
86       start_pos = end_pos;
87       end_pos = model_string.find(' ', start_pos);
88     } else {
89       ++end_pos;
90       break;
91     }
92   }
93   return end_pos;
94 }
95
96 }  // namespace
97
98 namespace test {
99
100 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
101   if (model->loaded())
102     return;
103   base::RunLoop run_loop;
104   BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
105   model->AddObserver(&observer);
106   content::RunThisRunLoop(&run_loop);
107   model->RemoveObserver(&observer);
108   DCHECK(model->loaded());
109 }
110
111 void WaitForBookmarkModelToLoad(Profile* profile) {
112   WaitForBookmarkModelToLoad(BookmarkModelFactory::GetForProfile(profile));
113 }
114
115 std::string ModelStringFromNode(const BookmarkNode* node) {
116   // Since the children of the node are not available as a vector,
117   // we'll just have to do it the hard way.
118   int child_count = node->child_count();
119   std::string child_string;
120   for (int i = 0; i < child_count; ++i) {
121     const BookmarkNode* child = node->GetChild(i);
122     if (child->is_folder()) {
123       child_string += base::UTF16ToUTF8(child->GetTitle()) + ":[ " +
124           ModelStringFromNode(child) + "] ";
125     } else {
126       child_string += base::UTF16ToUTF8(child->GetTitle()) + " ";
127     }
128   }
129   return child_string;
130 }
131
132 void AddNodesFromModelString(BookmarkModel* model,
133                              const BookmarkNode* node,
134                              const std::string& model_string) {
135   DCHECK(node);
136   std::string::size_type start_pos = 0;
137   std::string::size_type end_pos =
138       AddNodesFromString(model, node, model_string, start_pos);
139   DCHECK(end_pos == std::string::npos);
140 }
141
142 }  // namespace test