OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 5 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" | 13 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" |
13 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
15 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 17 #include "url/gurl.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 // BookmarkLoadObserver is used when blocking until the BookmarkModel finishes | 21 // BookmarkLoadObserver is used when blocking until the BookmarkModel finishes |
20 // loading. As soon as the BookmarkModel finishes loading the message loop is | 22 // loading. As soon as the BookmarkModel finishes loading the message loop is |
21 // quit. | 23 // quit. |
22 class BookmarkLoadObserver : public BaseBookmarkModelObserver { | 24 class BookmarkLoadObserver : public BaseBookmarkModelObserver { |
23 public: | 25 public: |
24 explicit BookmarkLoadObserver(const base::Closure& quit_task); | 26 explicit BookmarkLoadObserver(const base::Closure& quit_task); |
25 virtual ~BookmarkLoadObserver(); | 27 virtual ~BookmarkLoadObserver(); |
(...skipping 12 matching lines...) Expand all Loading... |
38 : quit_task_(quit_task) {} | 40 : quit_task_(quit_task) {} |
39 | 41 |
40 BookmarkLoadObserver::~BookmarkLoadObserver() {} | 42 BookmarkLoadObserver::~BookmarkLoadObserver() {} |
41 | 43 |
42 void BookmarkLoadObserver::BookmarkModelChanged() {} | 44 void BookmarkLoadObserver::BookmarkModelChanged() {} |
43 | 45 |
44 void BookmarkLoadObserver::Loaded(BookmarkModel* model, bool ids_reassigned) { | 46 void BookmarkLoadObserver::Loaded(BookmarkModel* model, bool ids_reassigned) { |
45 quit_task_.Run(); | 47 quit_task_.Run(); |
46 } | 48 } |
47 | 49 |
| 50 // Helper function which does the actual work of creating the nodes for |
| 51 // a particular level in the hierarchy. |
| 52 std::string::size_type AddNodesFromString(BookmarkModel* model, |
| 53 const BookmarkNode* node, |
| 54 const std::string& model_string, |
| 55 std::string::size_type start_pos) { |
| 56 DCHECK(node); |
| 57 int index = node->child_count(); |
| 58 static const std::string folder_tell(":["); |
| 59 std::string::size_type end_pos = model_string.find(' ', start_pos); |
| 60 while (end_pos != std::string::npos) { |
| 61 std::string::size_type part_length = end_pos - start_pos; |
| 62 std::string node_name = model_string.substr(start_pos, part_length); |
| 63 // Are we at the end of a folder group? |
| 64 if (node_name != "]") { |
| 65 // No, is it a folder? |
| 66 std::string tell; |
| 67 if (part_length > 2) |
| 68 tell = node_name.substr(part_length - 2, 2); |
| 69 if (tell == folder_tell) { |
| 70 node_name = node_name.substr(0, part_length - 2); |
| 71 const BookmarkNode* new_node = |
| 72 model->AddFolder(node, index, UTF8ToUTF16(node_name)); |
| 73 end_pos = AddNodesFromString(model, new_node, model_string, |
| 74 end_pos + 1); |
| 75 } else { |
| 76 std::string url_string("http://"); |
| 77 url_string += std::string(node_name.begin(), node_name.end()); |
| 78 url_string += ".com"; |
| 79 model->AddURL(node, index, UTF8ToUTF16(node_name), GURL(url_string)); |
| 80 ++end_pos; |
| 81 } |
| 82 ++index; |
| 83 start_pos = end_pos; |
| 84 end_pos = model_string.find(' ', start_pos); |
| 85 } else { |
| 86 ++end_pos; |
| 87 break; |
| 88 } |
| 89 } |
| 90 return end_pos; |
| 91 } |
| 92 |
48 } // namespace | 93 } // namespace |
49 | 94 |
50 namespace test { | 95 namespace test { |
51 | 96 |
52 void WaitForBookmarkModelToLoad(BookmarkModel* model) { | 97 void WaitForBookmarkModelToLoad(BookmarkModel* model) { |
53 if (model->loaded()) | 98 if (model->loaded()) |
54 return; | 99 return; |
55 base::RunLoop run_loop; | 100 base::RunLoop run_loop; |
56 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); | 101 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); |
57 model->AddObserver(&observer); | 102 model->AddObserver(&observer); |
58 content::RunThisRunLoop(&run_loop); | 103 content::RunThisRunLoop(&run_loop); |
59 model->RemoveObserver(&observer); | 104 model->RemoveObserver(&observer); |
60 DCHECK(model->loaded()); | 105 DCHECK(model->loaded()); |
61 } | 106 } |
62 | 107 |
63 void WaitForBookmarkModelToLoad(Profile* profile) { | 108 void WaitForBookmarkModelToLoad(Profile* profile) { |
64 WaitForBookmarkModelToLoad(BookmarkModelFactory::GetForProfile(profile)); | 109 WaitForBookmarkModelToLoad(BookmarkModelFactory::GetForProfile(profile)); |
65 } | 110 } |
66 | 111 |
| 112 std::string ModelStringFromNode(const BookmarkNode* node) { |
| 113 // Since the children of the node are not available as a vector, |
| 114 // we'll just have to do it the hard way. |
| 115 int child_count = node->child_count(); |
| 116 std::string child_string; |
| 117 for (int i = 0; i < child_count; ++i) { |
| 118 const BookmarkNode* child = node->GetChild(i); |
| 119 if (child->is_folder()) { |
| 120 child_string += UTF16ToUTF8(child->GetTitle()) + ":[ " + |
| 121 ModelStringFromNode(child) + "] "; |
| 122 } else { |
| 123 child_string += UTF16ToUTF8(child->GetTitle()) + " "; |
| 124 } |
| 125 } |
| 126 return child_string; |
| 127 } |
| 128 |
| 129 void AddNodesFromModelString(BookmarkModel* model, |
| 130 const BookmarkNode* node, |
| 131 const std::string& model_string) { |
| 132 DCHECK(node); |
| 133 std::string::size_type start_pos = 0; |
| 134 std::string::size_type end_pos = |
| 135 AddNodesFromString(model, node, model_string, start_pos); |
| 136 DCHECK(end_pos == std::string::npos); |
| 137 } |
| 138 |
67 } // namespace test | 139 } // namespace test |
OLD | NEW |