[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 93027af | 2009-10-07 23:27:01 | [diff] [blame] | 5 | #include <set> |
[email protected] | f20d733 | 2011-03-08 21:11:53 | [diff] [blame] | 6 | #include <string> |
[email protected] | 93027af | 2009-10-07 23:27:01 | [diff] [blame] | 7 | |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 8 | #include "base/base_paths.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 9 | #include "base/basictypes.h" |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 12 | #include "base/file_util.h" |
[email protected] | 999f380 | 2009-05-22 19:28:00 | [diff] [blame] | 13 | #include "base/hash_tables.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 14 | #include "base/path_service.h" |
[email protected] | 6524385ef | 2010-08-18 06:34:13 | [diff] [blame] | 15 | #include "base/string16.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 16 | #include "base/string_number_conversions.h" |
[email protected] | b87c4a7 | 2010-11-15 22:03:42 | [diff] [blame] | 17 | #include "base/string_split.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/string_util.h" |
[email protected] | 853e01b | 2012-09-21 20:14:11 | [diff] [blame] | 19 | #include "base/time.h" |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 20 | #include "base/utf_string_conversions.h" |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 21 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | ddabf57e | 2012-08-02 00:53:47 | [diff] [blame] | 22 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 125b23418 | 2011-07-08 19:54:41 | [diff] [blame] | 23 | #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
[email protected] | 9333f18 | 2008-12-09 17:34:17 | [diff] [blame] | 24 | #include "chrome/browser/bookmarks/bookmark_utils.h" |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 25 | #include "chrome/browser/history/history_notifications.h" |
[email protected] | 9aac6686 | 2012-06-19 19:44:31 | [diff] [blame] | 26 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 27 | #include "chrome/common/chrome_constants.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 28 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 29 | #include "chrome/common/chrome_paths.h" |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 30 | #include "chrome/common/chrome_switches.h" |
[email protected] | 7bc6068 | 2011-07-29 20:55:59 | [diff] [blame] | 31 | #include "chrome/test/base/model_test_utils.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 32 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 33 | #include "content/public/browser/notification_details.h" |
| 34 | #include "content/public/browser/notification_registrar.h" |
| 35 | #include "content/public/browser/notification_source.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 36 | #include "content/public/test/test_browser_thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 38 | #include "ui/base/models/tree_node_iterator.h" |
[email protected] | f20d733 | 2011-03-08 21:11:53 | [diff] [blame] | 39 | #include "ui/base/models/tree_node_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 41 | using base::Time; |
| 42 | using base::TimeDelta; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 43 | using content::BrowserThread; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 44 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 45 | namespace { |
| 46 | |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 47 | // Test cases used to test the removal of extra whitespace when adding |
| 48 | // a new folder/bookmark or updating a title of a folder/bookmark. |
| 49 | static struct { |
| 50 | const std::string input_title; |
| 51 | const std::string expected_title; |
| 52 | } whitespace_test_cases[] = { |
| 53 | {"foobar", "foobar"}, |
| 54 | // Newlines. |
| 55 | {"foo\nbar", "foo bar"}, |
| 56 | {"foo\n\nbar", "foo bar"}, |
| 57 | {"foo\n\n\nbar", "foo bar"}, |
| 58 | {"foo\r\nbar", "foo bar"}, |
| 59 | {"foo\r\n\r\nbar", "foo bar"}, |
| 60 | {"\nfoo\nbar\n", "foo bar"}, |
| 61 | // Spaces. |
| 62 | {"foo bar", "foo bar"}, |
| 63 | {" foo bar ", "foo bar"}, |
| 64 | {" foo bar ", "foo bar"}, |
| 65 | // Tabs. |
| 66 | {"\tfoo\tbar\t", "foo bar"}, |
| 67 | {"\tfoo bar\t", "foo bar"}, |
| 68 | // Mixed cases. |
| 69 | {"\tfoo\nbar\t", "foo bar"}, |
| 70 | {"\tfoo\r\nbar\t", "foo bar"}, |
| 71 | {" foo\tbar\n", "foo bar"}, |
| 72 | {"\t foo \t bar \t", "foo bar"}, |
| 73 | {"\n foo\r\n\tbar\n \t", "foo bar"}, |
| 74 | }; |
| 75 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 76 | // Helper to get a mutable bookmark node. |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 77 | BookmarkNode* AsMutable(const BookmarkNode* node) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 78 | return const_cast<BookmarkNode*>(node); |
| 79 | } |
| 80 | |
| 81 | void SwapDateAdded(BookmarkNode* n1, BookmarkNode* n2) { |
| 82 | Time tmp = n1->date_added(); |
| 83 | n1->set_date_added(n2->date_added()); |
| 84 | n2->set_date_added(tmp); |
| 85 | } |
| 86 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 87 | class BookmarkModelTest : public testing::Test, |
[email protected] | bcd98a5 | 2011-02-25 07:50:19 | [diff] [blame] | 88 | public BookmarkModelObserver { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | public: |
| 90 | struct ObserverDetails { |
| 91 | ObserverDetails() { |
| 92 | Set(NULL, NULL, -1, -1); |
| 93 | } |
| 94 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 95 | void Set(const BookmarkNode* node1, |
| 96 | const BookmarkNode* node2, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | int index1, |
| 98 | int index2) { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 99 | node1_ = node1; |
| 100 | node2_ = node2; |
| 101 | index1_ = index1; |
| 102 | index2_ = index2; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 105 | void ExpectEquals(const BookmarkNode* node1, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 106 | const BookmarkNode* node2, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | int index1, |
| 108 | int index2) { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 109 | EXPECT_EQ(node1_, node1); |
| 110 | EXPECT_EQ(node2_, node2); |
| 111 | EXPECT_EQ(index1_, index1); |
| 112 | EXPECT_EQ(index2_, index2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 115 | private: |
| 116 | const BookmarkNode* node1_; |
| 117 | const BookmarkNode* node2_; |
| 118 | int index1_; |
| 119 | int index2_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | }; |
| 121 | |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 122 | BookmarkModelTest() |
[email protected] | 972ecf6 | 2011-11-22 00:44:38 | [diff] [blame] | 123 | : model_(NULL) { |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 124 | model_.AddObserver(this); |
| 125 | ClearCounts(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 128 | void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | // We never load from the db, so that this should never get invoked. |
| 130 | NOTREACHED(); |
| 131 | } |
| 132 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 133 | virtual void BookmarkNodeMoved(BookmarkModel* model, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 134 | const BookmarkNode* old_parent, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | int old_index, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 136 | const BookmarkNode* new_parent, |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 137 | int new_index) OVERRIDE { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 138 | ++moved_count_; |
| 139 | observer_details_.Set(old_parent, new_parent, old_index, new_index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 142 | virtual void BookmarkNodeAdded(BookmarkModel* model, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 143 | const BookmarkNode* parent, |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 144 | int index) OVERRIDE { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 145 | ++added_count_; |
| 146 | observer_details_.Set(parent, NULL, index, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 149 | virtual void BookmarkNodeRemoved(BookmarkModel* model, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 150 | const BookmarkNode* parent, |
[email protected] | 6696502 | 2009-07-15 17:20:01 | [diff] [blame] | 151 | int old_index, |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 152 | const BookmarkNode* node) OVERRIDE { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 153 | ++removed_count_; |
| 154 | observer_details_.Set(parent, NULL, old_index, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 157 | virtual void BookmarkNodeChanged(BookmarkModel* model, |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 158 | const BookmarkNode* node) OVERRIDE { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 159 | ++changed_count_; |
| 160 | observer_details_.Set(node, NULL, -1, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 163 | virtual void BookmarkNodeChildrenReordered( |
| 164 | BookmarkModel* model, |
| 165 | const BookmarkNode* node) OVERRIDE { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 166 | ++reordered_count_; |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 169 | virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, |
| 170 | const BookmarkNode* node) OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | // We never attempt to load favicons, so that this method never |
| 172 | // gets invoked. |
| 173 | } |
| 174 | |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 175 | virtual void ExtensiveBookmarkChangesBeginning( |
| 176 | BookmarkModel* model) OVERRIDE { |
| 177 | ++extensive_changes_beginning_count_; |
| 178 | } |
| 179 | |
| 180 | virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE { |
| 181 | ++extensive_changes_ended_count_; |
| 182 | } |
| 183 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | void ClearCounts() { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 185 | added_count_ = moved_count_ = removed_count_ = changed_count_ = |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 186 | reordered_count_ = extensive_changes_beginning_count_ = |
| 187 | extensive_changes_ended_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void AssertObserverCount(int added_count, |
| 191 | int moved_count, |
| 192 | int removed_count, |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 193 | int changed_count, |
| 194 | int reordered_count) { |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 195 | EXPECT_EQ(added_count_, added_count); |
| 196 | EXPECT_EQ(moved_count_, moved_count); |
| 197 | EXPECT_EQ(removed_count_, removed_count); |
| 198 | EXPECT_EQ(changed_count_, changed_count); |
| 199 | EXPECT_EQ(reordered_count_, reordered_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 202 | void AssertExtensiveChangesObserverCount( |
| 203 | int extensive_changes_beginning_count, |
| 204 | int extensive_changes_ended_count) { |
| 205 | EXPECT_EQ(extensive_changes_beginning_count_, |
| 206 | extensive_changes_beginning_count); |
| 207 | EXPECT_EQ(extensive_changes_ended_count_, extensive_changes_ended_count); |
| 208 | } |
| 209 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 210 | protected: |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 211 | BookmarkModel model_; |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 212 | ObserverDetails observer_details_; |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 213 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 214 | private: |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 215 | int added_count_; |
| 216 | int moved_count_; |
| 217 | int removed_count_; |
| 218 | int changed_count_; |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 219 | int reordered_count_; |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 220 | int extensive_changes_beginning_count_; |
| 221 | int extensive_changes_ended_count_; |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 222 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 223 | DISALLOW_COPY_AND_ASSIGN(BookmarkModelTest); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 226 | TEST_F(BookmarkModelTest, InitialState) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 227 | const BookmarkNode* bb_node = model_.bookmark_bar_node(); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 228 | ASSERT_TRUE(bb_node != NULL); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 229 | EXPECT_EQ(0, bb_node->child_count()); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 230 | EXPECT_EQ(BookmarkNode::BOOKMARK_BAR, bb_node->type()); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 231 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 232 | const BookmarkNode* other_node = model_.other_node(); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 233 | ASSERT_TRUE(other_node != NULL); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 234 | EXPECT_EQ(0, other_node->child_count()); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 235 | EXPECT_EQ(BookmarkNode::OTHER_NODE, other_node->type()); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 236 | |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 237 | const BookmarkNode* mobile_node = model_.mobile_node(); |
| 238 | ASSERT_TRUE(mobile_node != NULL); |
| 239 | EXPECT_EQ(0, mobile_node->child_count()); |
| 240 | EXPECT_EQ(BookmarkNode::MOBILE, mobile_node->type()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 241 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 242 | EXPECT_TRUE(bb_node->id() != other_node->id()); |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 243 | EXPECT_TRUE(bb_node->id() != mobile_node->id()); |
| 244 | EXPECT_TRUE(other_node->id() != mobile_node->id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 247 | TEST_F(BookmarkModelTest, AddURL) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 248 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 4fe6eb5 | 2010-08-12 00:47:09 | [diff] [blame] | 249 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | const GURL url("http://foo.com"); |
| 251 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 252 | const BookmarkNode* new_node = model_.AddURL(root, 0, title, url); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 253 | AssertObserverCount(1, 0, 0, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 254 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 256 | ASSERT_EQ(1, root->child_count()); |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 257 | ASSERT_EQ(title, new_node->GetTitle()); |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 258 | ASSERT_TRUE(url == new_node->url()); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 259 | ASSERT_EQ(BookmarkNode::URL, new_node->type()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 260 | ASSERT_TRUE(new_node == model_.GetMostRecentlyAddedNodeForURL(url)); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 261 | |
| 262 | EXPECT_TRUE(new_node->id() != root->id() && |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 263 | new_node->id() != model_.other_node()->id() && |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 264 | new_node->id() != model_.mobile_node()->id()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 2fcb5c2 | 2012-08-23 16:28:39 | [diff] [blame] | 267 | TEST_F(BookmarkModelTest, AddURLWithUnicodeTitle) { |
| 268 | const BookmarkNode* root = model_.bookmark_bar_node(); |
| 269 | const string16 title(WideToUTF16( |
| 270 | L"\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053")); |
| 271 | const GURL url("https://www.baidu.com/"); |
| 272 | |
| 273 | const BookmarkNode* new_node = model_.AddURL(root, 0, title, url); |
| 274 | AssertObserverCount(1, 0, 0, 0, 0); |
| 275 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
| 276 | |
| 277 | ASSERT_EQ(1, root->child_count()); |
| 278 | ASSERT_EQ(title, new_node->GetTitle()); |
| 279 | ASSERT_TRUE(url == new_node->url()); |
| 280 | ASSERT_EQ(BookmarkNode::URL, new_node->type()); |
| 281 | ASSERT_TRUE(new_node == model_.GetMostRecentlyAddedNodeForURL(url)); |
| 282 | |
| 283 | EXPECT_TRUE(new_node->id() != root->id() && |
| 284 | new_node->id() != model_.other_node()->id() && |
| 285 | new_node->id() != model_.mobile_node()->id()); |
| 286 | } |
| 287 | |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 288 | TEST_F(BookmarkModelTest, AddURLWithWhitespaceTitle) { |
| 289 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(whitespace_test_cases); ++i) { |
| 290 | const BookmarkNode* root = model_.bookmark_bar_node(); |
| 291 | const string16 title(ASCIIToUTF16(whitespace_test_cases[i].input_title)); |
| 292 | const GURL url("http://foo.com"); |
| 293 | |
| 294 | const BookmarkNode* new_node = model_.AddURL(root, i, title, url); |
| 295 | |
| 296 | int size = i + 1; |
| 297 | EXPECT_EQ(size, root->child_count()); |
| 298 | EXPECT_EQ(ASCIIToUTF16(whitespace_test_cases[i].expected_title), |
| 299 | new_node->GetTitle()); |
| 300 | EXPECT_EQ(BookmarkNode::URL, new_node->type()); |
| 301 | } |
| 302 | } |
| 303 | |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 304 | TEST_F(BookmarkModelTest, AddURLToMobileBookmarks) { |
| 305 | const BookmarkNode* root = model_.mobile_node(); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 306 | const string16 title(ASCIIToUTF16("foo")); |
| 307 | const GURL url("http://foo.com"); |
| 308 | |
| 309 | const BookmarkNode* new_node = model_.AddURL(root, 0, title, url); |
| 310 | AssertObserverCount(1, 0, 0, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 311 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 312 | |
| 313 | ASSERT_EQ(1, root->child_count()); |
| 314 | ASSERT_EQ(title, new_node->GetTitle()); |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 315 | ASSERT_TRUE(url == new_node->url()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 316 | ASSERT_EQ(BookmarkNode::URL, new_node->type()); |
| 317 | ASSERT_TRUE(new_node == model_.GetMostRecentlyAddedNodeForURL(url)); |
| 318 | |
| 319 | EXPECT_TRUE(new_node->id() != root->id() && |
| 320 | new_node->id() != model_.other_node()->id() && |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 321 | new_node->id() != model_.mobile_node()->id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 324 | TEST_F(BookmarkModelTest, AddFolder) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 325 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 4fe6eb5 | 2010-08-12 00:47:09 | [diff] [blame] | 326 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 328 | const BookmarkNode* new_node = model_.AddFolder(root, 0, title); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 329 | AssertObserverCount(1, 0, 0, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 330 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 332 | ASSERT_EQ(1, root->child_count()); |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 333 | ASSERT_EQ(title, new_node->GetTitle()); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 334 | ASSERT_EQ(BookmarkNode::FOLDER, new_node->type()); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 335 | |
| 336 | EXPECT_TRUE(new_node->id() != root->id() && |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 337 | new_node->id() != model_.other_node()->id() && |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 338 | new_node->id() != model_.mobile_node()->id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 340 | // Add another folder, just to make sure folder_ids are incremented correctly. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | ClearCounts(); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 342 | model_.AddFolder(root, 0, title); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 343 | AssertObserverCount(1, 0, 0, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 344 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 347 | TEST_F(BookmarkModelTest, AddFolderWithWhitespaceTitle) { |
| 348 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(whitespace_test_cases); ++i) { |
| 349 | const BookmarkNode* root = model_.bookmark_bar_node(); |
| 350 | const string16 title(ASCIIToUTF16(whitespace_test_cases[i].input_title)); |
| 351 | |
| 352 | const BookmarkNode* new_node = model_.AddFolder(root, i, title); |
| 353 | |
| 354 | int size = i + 1; |
| 355 | EXPECT_EQ(size, root->child_count()); |
| 356 | EXPECT_EQ(ASCIIToUTF16(whitespace_test_cases[i].expected_title), |
| 357 | new_node->GetTitle()); |
| 358 | EXPECT_EQ(BookmarkNode::FOLDER, new_node->type()); |
| 359 | } |
| 360 | } |
| 361 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 362 | TEST_F(BookmarkModelTest, RemoveURL) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 363 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 364 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | const GURL url("http://foo.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 366 | model_.AddURL(root, 0, title, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | ClearCounts(); |
| 368 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 369 | model_.Remove(root, 0); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 370 | ASSERT_EQ(0, root->child_count()); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 371 | AssertObserverCount(0, 0, 1, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 372 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | |
| 374 | // Make sure there is no mapping for the URL. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 375 | ASSERT_TRUE(model_.GetMostRecentlyAddedNodeForURL(url) == NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 9fcaee7 | 2011-03-21 21:53:44 | [diff] [blame] | 378 | TEST_F(BookmarkModelTest, RemoveFolder) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 379 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 380 | const BookmarkNode* folder = model_.AddFolder(root, 0, ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | |
| 382 | ClearCounts(); |
| 383 | |
| 384 | // Add a URL as a child. |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 385 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | const GURL url("http://foo.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 387 | model_.AddURL(folder, 0, title, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | |
| 389 | ClearCounts(); |
| 390 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 391 | // Now remove the folder. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 392 | model_.Remove(root, 0); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 393 | ASSERT_EQ(0, root->child_count()); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 394 | AssertObserverCount(0, 0, 1, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 395 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | |
| 397 | // Make sure there is no mapping for the URL. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 398 | ASSERT_TRUE(model_.GetMostRecentlyAddedNodeForURL(url) == NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 401 | TEST_F(BookmarkModelTest, SetTitle) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 402 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 4fe6eb5 | 2010-08-12 00:47:09 | [diff] [blame] | 403 | string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | const GURL url("http://foo.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 405 | const BookmarkNode* node = model_.AddURL(root, 0, title, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 406 | |
| 407 | ClearCounts(); |
| 408 | |
[email protected] | 4fe6eb5 | 2010-08-12 00:47:09 | [diff] [blame] | 409 | title = ASCIIToUTF16("foo2"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 410 | model_.SetTitle(node, title); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 411 | AssertObserverCount(0, 0, 0, 1, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 412 | observer_details_.ExpectEquals(node, NULL, -1, -1); |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 413 | EXPECT_EQ(title, node->GetTitle()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 416 | TEST_F(BookmarkModelTest, SetTitleWithWhitespace) { |
| 417 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(whitespace_test_cases); ++i) { |
| 418 | const BookmarkNode* root = model_.bookmark_bar_node(); |
| 419 | string16 title(ASCIIToUTF16("dummy")); |
| 420 | const GURL url("http://foo.com"); |
| 421 | const BookmarkNode* node = model_.AddURL(root, 0, title, url); |
| 422 | |
| 423 | title = ASCIIToUTF16(whitespace_test_cases[i].input_title); |
| 424 | model_.SetTitle(node, title); |
| 425 | EXPECT_EQ(ASCIIToUTF16(whitespace_test_cases[i].expected_title), |
| 426 | node->GetTitle()); |
| 427 | } |
| 428 | } |
| 429 | |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 430 | TEST_F(BookmarkModelTest, SetURL) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 431 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 432 | const string16 title(ASCIIToUTF16("foo")); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 433 | GURL url("http://foo.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 434 | const BookmarkNode* node = model_.AddURL(root, 0, title, url); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 435 | |
| 436 | ClearCounts(); |
| 437 | |
| 438 | url = GURL("http://foo2.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 439 | model_.SetURL(node, url); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 440 | AssertObserverCount(0, 0, 0, 1, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 441 | observer_details_.ExpectEquals(node, NULL, -1, -1); |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 442 | EXPECT_EQ(url, node->url()); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 445 | TEST_F(BookmarkModelTest, Move) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 446 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 447 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | const GURL url("http://foo.com"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 449 | const BookmarkNode* node = model_.AddURL(root, 0, title, url); |
| 450 | const BookmarkNode* folder1 = model_.AddFolder(root, 0, ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | ClearCounts(); |
| 452 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 453 | model_.Move(node, folder1, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 455 | AssertObserverCount(0, 1, 0, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 456 | observer_details_.ExpectEquals(root, folder1, 1, 0); |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 457 | EXPECT_TRUE(folder1 == node->parent()); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 458 | EXPECT_EQ(1, root->child_count()); |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 459 | EXPECT_EQ(folder1, root->GetChild(0)); |
| 460 | EXPECT_EQ(1, folder1->child_count()); |
| 461 | EXPECT_EQ(node, folder1->GetChild(0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 463 | // And remove the folder. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | ClearCounts(); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 465 | model_.Remove(root, 0); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 466 | AssertObserverCount(0, 0, 1, 0, 0); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 467 | observer_details_.ExpectEquals(root, NULL, 0, -1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 468 | EXPECT_TRUE(model_.GetMostRecentlyAddedNodeForURL(url) == NULL); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 469 | EXPECT_EQ(0, root->child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 472 | TEST_F(BookmarkModelTest, Copy) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 473 | const BookmarkNode* root = model_.bookmark_bar_node(); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 474 | static const std::string model_string("a 1:[ b c ] d 2:[ e f g ] h "); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 475 | model_test_utils::AddNodesFromModelString(model_, root, model_string); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 476 | |
| 477 | // Validate initial model. |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 478 | std::string actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 479 | EXPECT_EQ(model_string, actualModelString); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 480 | |
| 481 | // Copy 'd' to be after '1:b': URL item from bar to folder. |
| 482 | const BookmarkNode* nodeToCopy = root->GetChild(2); |
| 483 | const BookmarkNode* destination = root->GetChild(1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 484 | model_.Copy(nodeToCopy, destination, 1); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 485 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 486 | EXPECT_EQ("a 1:[ b d c ] d 2:[ e f g ] h ", actualModelString); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 487 | |
| 488 | // Copy '1:d' to be after 'a': URL item from folder to bar. |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 489 | const BookmarkNode* folder = root->GetChild(1); |
| 490 | nodeToCopy = folder->GetChild(1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 491 | model_.Copy(nodeToCopy, root, 1); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 492 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 493 | EXPECT_EQ("a d 1:[ b d c ] d 2:[ e f g ] h ", actualModelString); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 494 | |
| 495 | // Copy '1' to be after '2:e': Folder from bar to folder. |
| 496 | nodeToCopy = root->GetChild(2); |
| 497 | destination = root->GetChild(4); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 498 | model_.Copy(nodeToCopy, destination, 1); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 499 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 500 | EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f g ] h ", actualModelString); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 501 | |
| 502 | // Copy '2:1' to be after '2:f': Folder within same folder. |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 503 | folder = root->GetChild(4); |
| 504 | nodeToCopy = folder->GetChild(1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 505 | model_.Copy(nodeToCopy, folder, 3); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 506 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 507 | EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h ", |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 508 | actualModelString); |
| 509 | |
| 510 | // Copy first 'd' to be after 'h': URL item within the bar. |
| 511 | nodeToCopy = root->GetChild(1); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 512 | model_.Copy(nodeToCopy, root, 6); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 513 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 514 | EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ", |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 515 | actualModelString); |
| 516 | |
| 517 | // Copy '2' to be after 'a': Folder within the bar. |
| 518 | nodeToCopy = root->GetChild(4); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 519 | model_.Copy(nodeToCopy, root, 1); |
[email protected] | 94d2135c | 2010-04-29 18:24:32 | [diff] [blame] | 520 | actualModelString = model_test_utils::ModelStringFromNode(root); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 521 | EXPECT_EQ("a 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] d 1:[ b d c ] " |
| 522 | "d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ", |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 523 | actualModelString); |
| 524 | } |
| 525 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | // Tests that adding a URL to a folder updates the last modified time. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 527 | TEST_F(BookmarkModelTest, ParentForNewNodes) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 528 | ASSERT_EQ(model_.bookmark_bar_node(), model_.GetParentForNewNodes()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 530 | const string16 title(ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | const GURL url("http://foo.com"); |
| 532 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 533 | model_.AddURL(model_.other_node(), 0, title, url); |
| 534 | ASSERT_EQ(model_.other_node(), model_.GetParentForNewNodes()); |
| 535 | } |
| 536 | |
| 537 | // Tests that adding a URL to a folder updates the last modified time. |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 538 | TEST_F(BookmarkModelTest, ParentForNewMobileNodes) { |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 539 | ASSERT_EQ(model_.bookmark_bar_node(), model_.GetParentForNewNodes()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 540 | |
| 541 | const string16 title(ASCIIToUTF16("foo")); |
| 542 | const GURL url("http://foo.com"); |
| 543 | |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 544 | model_.AddURL(model_.mobile_node(), 0, title, url); |
| 545 | ASSERT_EQ(model_.mobile_node(), model_.GetParentForNewNodes()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // Make sure recently modified stays in sync when adding a URL. |
[email protected] | 9fcaee7 | 2011-03-21 21:53:44 | [diff] [blame] | 549 | TEST_F(BookmarkModelTest, MostRecentlyModifiedFolders) { |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 550 | // Add a folder. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 551 | const BookmarkNode* folder = model_.AddFolder(model_.other_node(), 0, |
| 552 | ASCIIToUTF16("foo")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | // Add a URL to it. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 554 | model_.AddURL(folder, 0, ASCIIToUTF16("blah"), GURL("http://foo.com")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 556 | // Make sure folder is in the most recently modified. |
| 557 | std::vector<const BookmarkNode*> most_recent_folders = |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 558 | bookmark_utils::GetMostRecentlyModifiedFolders(&model_, 1); |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 559 | ASSERT_EQ(1U, most_recent_folders.size()); |
| 560 | ASSERT_EQ(folder, most_recent_folders[0]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 561 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 562 | // Nuke the folder and do another fetch, making sure folder isn't in the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | // returned list. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 564 | model_.Remove(folder->parent(), 0); |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 565 | most_recent_folders = |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 566 | bookmark_utils::GetMostRecentlyModifiedFolders(&model_, 1); |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 567 | ASSERT_EQ(1U, most_recent_folders.size()); |
| 568 | ASSERT_TRUE(most_recent_folders[0] != folder); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 571 | // Make sure MostRecentlyAddedEntries stays in sync. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 572 | TEST_F(BookmarkModelTest, MostRecentlyAddedEntries) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 573 | // Add a couple of nodes such that the following holds for the time of the |
| 574 | // nodes: n1 > n2 > n3 > n4. |
| 575 | Time base_time = Time::Now(); |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 576 | BookmarkNode* n1 = AsMutable(model_.AddURL(model_.bookmark_bar_node(), |
| 577 | 0, |
| 578 | ASCIIToUTF16("blah"), |
| 579 | GURL("http://foo.com/0"))); |
| 580 | BookmarkNode* n2 = AsMutable(model_.AddURL(model_.bookmark_bar_node(), |
| 581 | 1, |
| 582 | ASCIIToUTF16("blah"), |
| 583 | GURL("http://foo.com/1"))); |
| 584 | BookmarkNode* n3 = AsMutable(model_.AddURL(model_.bookmark_bar_node(), |
| 585 | 2, |
| 586 | ASCIIToUTF16("blah"), |
| 587 | GURL("http://foo.com/2"))); |
| 588 | BookmarkNode* n4 = AsMutable(model_.AddURL(model_.bookmark_bar_node(), |
| 589 | 3, |
| 590 | ASCIIToUTF16("blah"), |
| 591 | GURL("http://foo.com/3"))); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 592 | n1->set_date_added(base_time + TimeDelta::FromDays(4)); |
| 593 | n2->set_date_added(base_time + TimeDelta::FromDays(3)); |
| 594 | n3->set_date_added(base_time + TimeDelta::FromDays(2)); |
| 595 | n4->set_date_added(base_time + TimeDelta::FromDays(1)); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 596 | |
| 597 | // Make sure order is honored. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 598 | std::vector<const BookmarkNode*> recently_added; |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 599 | bookmark_utils::GetMostRecentlyAddedEntries(&model_, 2, &recently_added); |
[email protected] | 67e4a1e9 | 2009-03-03 19:29:45 | [diff] [blame] | 600 | ASSERT_EQ(2U, recently_added.size()); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 601 | ASSERT_TRUE(n1 == recently_added[0]); |
| 602 | ASSERT_TRUE(n2 == recently_added[1]); |
| 603 | |
| 604 | // swap 1 and 2, then check again. |
| 605 | recently_added.clear(); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 606 | SwapDateAdded(n1, n2); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 607 | bookmark_utils::GetMostRecentlyAddedEntries(&model_, 4, &recently_added); |
[email protected] | 67e4a1e9 | 2009-03-03 19:29:45 | [diff] [blame] | 608 | ASSERT_EQ(4U, recently_added.size()); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 609 | ASSERT_TRUE(n2 == recently_added[0]); |
| 610 | ASSERT_TRUE(n1 == recently_added[1]); |
| 611 | ASSERT_TRUE(n3 == recently_added[2]); |
| 612 | ASSERT_TRUE(n4 == recently_added[3]); |
| 613 | } |
| 614 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 615 | // Makes sure GetMostRecentlyAddedNodeForURL stays in sync. |
| 616 | TEST_F(BookmarkModelTest, GetMostRecentlyAddedNodeForURL) { |
| 617 | // Add a couple of nodes such that the following holds for the time of the |
| 618 | // nodes: n1 > n2 |
| 619 | Time base_time = Time::Now(); |
| 620 | const GURL url("http://foo.com/0"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 621 | BookmarkNode* n1 = AsMutable(model_.AddURL( |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 622 | model_.bookmark_bar_node(), 0, ASCIIToUTF16("blah"), url)); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 623 | BookmarkNode* n2 = AsMutable(model_.AddURL( |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 624 | model_.bookmark_bar_node(), 1, ASCIIToUTF16("blah"), url)); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 625 | n1->set_date_added(base_time + TimeDelta::FromDays(4)); |
| 626 | n2->set_date_added(base_time + TimeDelta::FromDays(3)); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 627 | |
| 628 | // Make sure order is honored. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 629 | ASSERT_EQ(n1, model_.GetMostRecentlyAddedNodeForURL(url)); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 630 | |
| 631 | // swap 1 and 2, then check again. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 632 | SwapDateAdded(n1, n2); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 633 | ASSERT_EQ(n2, model_.GetMostRecentlyAddedNodeForURL(url)); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Makes sure GetBookmarks removes duplicates. |
| 637 | TEST_F(BookmarkModelTest, GetBookmarksWithDups) { |
| 638 | const GURL url("http://foo.com/0"); |
[email protected] | 0f7bee5 | 2012-08-06 20:04:17 | [diff] [blame] | 639 | const string16 title(ASCIIToUTF16("blah")); |
| 640 | model_.AddURL(model_.bookmark_bar_node(), 0, title, url); |
| 641 | model_.AddURL(model_.bookmark_bar_node(), 1, title, url); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 642 | |
[email protected] | 0f7bee5 | 2012-08-06 20:04:17 | [diff] [blame] | 643 | std::vector<BookmarkService::URLAndTitle> bookmarks; |
| 644 | model_.GetBookmarks(&bookmarks); |
| 645 | ASSERT_EQ(1U, bookmarks.size()); |
| 646 | EXPECT_EQ(url, bookmarks[0].url); |
| 647 | EXPECT_EQ(title, bookmarks[0].title); |
| 648 | |
| 649 | model_.AddURL(model_.bookmark_bar_node(), 2, ASCIIToUTF16("Title2"), url); |
| 650 | // Only one returned, even titles are different. |
| 651 | bookmarks.clear(); |
| 652 | model_.GetBookmarks(&bookmarks); |
| 653 | EXPECT_EQ(1U, bookmarks.size()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 654 | } |
| 655 | |
[email protected] | 4478c65 | 2010-08-27 00:08:21 | [diff] [blame] | 656 | TEST_F(BookmarkModelTest, HasBookmarks) { |
| 657 | const GURL url("http://foo.com/"); |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 658 | model_.AddURL(model_.bookmark_bar_node(), 0, ASCIIToUTF16("bar"), url); |
[email protected] | 4478c65 | 2010-08-27 00:08:21 | [diff] [blame] | 659 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 660 | EXPECT_TRUE(model_.HasBookmarks()); |
[email protected] | 4478c65 | 2010-08-27 00:08:21 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 663 | // content::NotificationObserver implementation used in verifying we've received |
| 664 | // the NOTIFY_URLS_STARRED method correctly. |
| 665 | class StarredListener : public content::NotificationObserver { |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 666 | public: |
| 667 | StarredListener() : notification_count_(0), details_(false) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 668 | registrar_.Add(this, chrome::NOTIFICATION_URLS_STARRED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 669 | content::Source<Profile>(NULL)); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 670 | } |
| 671 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 672 | // NotificationObserver: |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 673 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 674 | const content::NotificationSource& source, |
| 675 | const content::NotificationDetails& details) OVERRIDE { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 676 | if (type == chrome::NOTIFICATION_URLS_STARRED) { |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 677 | notification_count_++; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 678 | details_ = |
| 679 | *(content::Details<history::URLsStarredDetails>(details).ptr()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | // Number of times NOTIFY_URLS_STARRED has been observed. |
| 684 | int notification_count_; |
| 685 | |
| 686 | // Details from the last NOTIFY_URLS_STARRED. |
| 687 | history::URLsStarredDetails details_; |
| 688 | |
| 689 | private: |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 690 | content::NotificationRegistrar registrar_; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 691 | |
| 692 | DISALLOW_COPY_AND_ASSIGN(StarredListener); |
| 693 | }; |
| 694 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 695 | // Makes sure NOTIFY_URLS_STARRED is sent correctly. |
| 696 | TEST_F(BookmarkModelTest, NotifyURLsStarred) { |
| 697 | StarredListener listener; |
| 698 | const GURL url("http://foo.com/0"); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 699 | const BookmarkNode* n1 = model_.AddURL( |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 700 | model_.bookmark_bar_node(), 0, ASCIIToUTF16("blah"), url); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 701 | |
| 702 | // Starred notification should be sent. |
| 703 | EXPECT_EQ(1, listener.notification_count_); |
| 704 | ASSERT_TRUE(listener.details_.starred); |
[email protected] | 67e4a1e9 | 2009-03-03 19:29:45 | [diff] [blame] | 705 | ASSERT_EQ(1U, listener.details_.changed_urls.size()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 706 | EXPECT_TRUE(url == *(listener.details_.changed_urls.begin())); |
| 707 | listener.notification_count_ = 0; |
| 708 | listener.details_.changed_urls.clear(); |
| 709 | |
| 710 | // Add another bookmark for the same URL. This should not send any |
| 711 | // notification. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 712 | const BookmarkNode* n2 = model_.AddURL( |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 713 | model_.bookmark_bar_node(), 1, ASCIIToUTF16("blah"), url); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 714 | |
| 715 | EXPECT_EQ(0, listener.notification_count_); |
| 716 | |
| 717 | // Remove n2. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 718 | model_.Remove(n2->parent(), 1); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 719 | n2 = NULL; |
| 720 | |
| 721 | // Shouldn't have received any notification as n1 still exists with the same |
| 722 | // URL. |
| 723 | EXPECT_EQ(0, listener.notification_count_); |
| 724 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 725 | EXPECT_TRUE(model_.GetMostRecentlyAddedNodeForURL(url) == n1); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 726 | |
| 727 | // Remove n1. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 728 | model_.Remove(n1->parent(), 0); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 729 | |
| 730 | // Now we should get the notification. |
| 731 | EXPECT_EQ(1, listener.notification_count_); |
| 732 | ASSERT_FALSE(listener.details_.starred); |
[email protected] | 67e4a1e9 | 2009-03-03 19:29:45 | [diff] [blame] | 733 | ASSERT_EQ(1U, listener.details_.changed_urls.size()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 734 | EXPECT_TRUE(url == *(listener.details_.changed_urls.begin())); |
| 735 | } |
| 736 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 737 | // See comment in PopulateNodeFromString. |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 738 | typedef ui::TreeNodeWithValue<BookmarkNode::Type> TestNode; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 739 | |
| 740 | // Does the work of PopulateNodeFromString. index gives the index of the current |
| 741 | // element in description to process. |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 742 | void PopulateNodeImpl(const std::vector<std::string>& description, |
| 743 | size_t* index, |
| 744 | TestNode* parent) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 745 | while (*index < description.size()) { |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 746 | const std::string& element = description[*index]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 747 | (*index)++; |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 748 | if (element == "[") { |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 749 | // Create a new folder and recurse to add all the children. |
[email protected] | 9fcaee7 | 2011-03-21 21:53:44 | [diff] [blame] | 750 | // Folders are given a unique named by way of an ever increasing integer |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 751 | // value. The folders need not have a name, but one is assigned to help |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 752 | // in debugging. |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 753 | static int next_folder_id = 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 754 | TestNode* new_node = |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 755 | new TestNode(base::IntToString16(next_folder_id++), |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 756 | BookmarkNode::FOLDER); |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 757 | parent->Add(new_node, parent->child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 758 | PopulateNodeImpl(description, index, new_node); |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 759 | } else if (element == "]") { |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 760 | // End the current folder. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | return; |
| 762 | } else { |
| 763 | // Add a new URL. |
| 764 | |
| 765 | // All tokens must be space separated. If there is a [ or ] in the name it |
| 766 | // likely means a space was forgotten. |
| 767 | DCHECK(element.find('[') == std::string::npos); |
| 768 | DCHECK(element.find(']') == std::string::npos); |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 769 | parent->Add(new TestNode(UTF8ToUTF16(element), BookmarkNode::URL), |
| 770 | parent->child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // Creates and adds nodes to parent based on description. description consists |
| 776 | // of the following tokens (all space separated): |
[email protected] | 9fcaee7 | 2011-03-21 21:53:44 | [diff] [blame] | 777 | // [ : creates a new USER_FOLDER node. All elements following the [ until the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | // next balanced ] is encountered are added as children to the node. |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 779 | // ] : closes the last folder created by [ so that any further nodes are added |
| 780 | // to the current folders parent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 781 | // text: creates a new URL node. |
| 782 | // For example, "a [b] c" creates the following nodes: |
| 783 | // a 1 c |
| 784 | // | |
| 785 | // b |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 786 | // In words: a node of type URL with the title a, followed by a folder node with |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 787 | // the title 1 having the single child of type url with name b, followed by |
| 788 | // the url node with the title c. |
| 789 | // |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 790 | // NOTE: each name must be unique, and folders are assigned a unique title by |
| 791 | // way of an increasing integer. |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 792 | void PopulateNodeFromString(const std::string& description, TestNode* parent) { |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 793 | std::vector<std::string> elements; |
[email protected] | b87c4a7 | 2010-11-15 22:03:42 | [diff] [blame] | 794 | base::SplitStringAlongWhitespace(description, &elements); |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 795 | size_t index = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 796 | PopulateNodeImpl(elements, &index, parent); |
| 797 | } |
| 798 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 799 | // Populates the BookmarkNode with the children of parent. |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 800 | void PopulateBookmarkNode(TestNode* parent, |
| 801 | BookmarkModel* model, |
| 802 | const BookmarkNode* bb_node) { |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 803 | for (int i = 0; i < parent->child_count(); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | TestNode* child = parent->GetChild(i); |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 805 | if (child->value == BookmarkNode::FOLDER) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 806 | const BookmarkNode* new_bb_node = |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 807 | model->AddFolder(bb_node, i, child->GetTitle()); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 808 | PopulateBookmarkNode(child, model, new_bb_node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 809 | } else { |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 810 | model->AddURL(bb_node, i, child->GetTitle(), |
| 811 | GURL("http://" + UTF16ToASCII(child->GetTitle()))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 816 | // Test class that creates a BookmarkModel with a real history backend. |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 817 | class BookmarkModelTestWithProfile : public testing::Test { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 818 | public: |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 819 | BookmarkModelTestWithProfile() |
[email protected] | fe232e0 | 2012-08-07 15:09:38 | [diff] [blame] | 820 | : bb_model_(NULL), |
| 821 | ui_thread_(BrowserThread::UI, &message_loop_), |
[email protected] | faec631 | 2010-10-05 03:35:34 | [diff] [blame] | 822 | file_thread_(BrowserThread::FILE, &message_loop_) {} |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 823 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 824 | // testing::Test: |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 825 | virtual void TearDown() OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 826 | profile_.reset(NULL); |
| 827 | } |
| 828 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 829 | protected: |
| 830 | // Verifies the contents of the bookmark bar node match the contents of the |
| 831 | // TestNode. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 832 | void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) { |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 833 | ASSERT_EQ(expected->child_count(), actual->child_count()); |
| 834 | for (int i = 0; i < expected->child_count(); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 835 | TestNode* expected_child = expected->GetChild(i); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 836 | const BookmarkNode* actual_child = actual->GetChild(i); |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 837 | ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 838 | if (expected_child->value == BookmarkNode::FOLDER) { |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 839 | ASSERT_TRUE(actual_child->type() == BookmarkNode::FOLDER); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 840 | // Recurse throught children. |
| 841 | VerifyModelMatchesNode(expected_child, actual_child); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 842 | if (HasFatalFailure()) |
| 843 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 844 | } else { |
| 845 | // No need to check the URL, just the title is enough. |
[email protected] | 0890e60e | 2011-06-27 14:55:21 | [diff] [blame] | 846 | ASSERT_TRUE(actual_child->is_url()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
[email protected] | 999f380 | 2009-05-22 19:28:00 | [diff] [blame] | 851 | void VerifyNoDuplicateIDs(BookmarkModel* model) { |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 852 | ui::TreeNodeIterator<const BookmarkNode> it(model->root_node()); |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 853 | base::hash_set<int64> ids; |
[email protected] | 999f380 | 2009-05-22 19:28:00 | [diff] [blame] | 854 | while (it.has_next()) |
| 855 | ASSERT_TRUE(ids.insert(it.Next()->id()).second); |
| 856 | } |
| 857 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 858 | void BlockTillBookmarkModelLoaded() { |
[email protected] | ddabf57e | 2012-08-02 00:53:47 | [diff] [blame] | 859 | bb_model_ = BookmarkModelFactory::GetForProfile(profile_.get()); |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 860 | profile_->BlockUntilBookmarkModelLoaded(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 861 | } |
| 862 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 863 | // Destroys the current profile, creates a new one and creates the history |
| 864 | // service. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 865 | void RecreateProfile() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 866 | // Need to shutdown the old one before creating a new one. |
| 867 | profile_.reset(NULL); |
| 868 | profile_.reset(new TestingProfile()); |
[email protected] | d486a085 | 2009-11-02 21:40:00 | [diff] [blame] | 869 | profile_->CreateHistoryService(true, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 870 | } |
| 871 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 872 | // The profile. |
| 873 | scoped_ptr<TestingProfile> profile_; |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 874 | BookmarkModel* bb_model_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 875 | |
| 876 | private: |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 877 | MessageLoopForUI message_loop_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 878 | content::TestBrowserThread ui_thread_; |
| 879 | content::TestBrowserThread file_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 880 | }; |
| 881 | |
| 882 | // Creates a set of nodes in the bookmark bar model, then recreates the |
| 883 | // bookmark bar model which triggers loading from the db and checks the loaded |
| 884 | // structure to make sure it is what we first created. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 885 | TEST_F(BookmarkModelTestWithProfile, CreateAndRestore) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 886 | struct TestData { |
| 887 | // Structure of the children of the bookmark bar model node. |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 888 | const std::string bbn_contents; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 889 | // Structure of the children of the other node. |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 890 | const std::string other_contents; |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 891 | // Structure of the children of the synced node. |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 892 | const std::string mobile_contents; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | } data[] = { |
| 894 | // See PopulateNodeFromString for a description of these strings. |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 895 | { "", "" }, |
| 896 | { "a", "b" }, |
| 897 | { "a [ b ]", "" }, |
| 898 | { "", "[ b ] a [ c [ d e [ f ] ] ]" }, |
| 899 | { "a [ b ]", "" }, |
| 900 | { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 901 | }; |
[email protected] | 67e4a1e9 | 2009-03-03 19:29:45 | [diff] [blame] | 902 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 903 | // Recreate the profile. We need to reset with NULL first so that the last |
| 904 | // HistoryService releases the locks on the files it creates and we can |
| 905 | // delete them. |
| 906 | profile_.reset(NULL); |
| 907 | profile_.reset(new TestingProfile()); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 908 | profile_->CreateBookmarkModel(true); |
[email protected] | d486a085 | 2009-11-02 21:40:00 | [diff] [blame] | 909 | profile_->CreateHistoryService(true, false); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 910 | BlockTillBookmarkModelLoaded(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 911 | |
| 912 | TestNode bbn; |
| 913 | PopulateNodeFromString(data[i].bbn_contents, &bbn); |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 914 | PopulateBookmarkNode(&bbn, bb_model_, bb_model_->bookmark_bar_node()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 915 | |
| 916 | TestNode other; |
| 917 | PopulateNodeFromString(data[i].other_contents, &other); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 918 | PopulateBookmarkNode(&other, bb_model_, bb_model_->other_node()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 920 | TestNode mobile; |
| 921 | PopulateNodeFromString(data[i].mobile_contents, &mobile); |
| 922 | PopulateBookmarkNode(&mobile, bb_model_, bb_model_->mobile_node()); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 923 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 924 | profile_->CreateBookmarkModel(false); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 925 | BlockTillBookmarkModelLoaded(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 926 | |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 927 | VerifyModelMatchesNode(&bbn, bb_model_->bookmark_bar_node()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 928 | VerifyModelMatchesNode(&other, bb_model_->other_node()); |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 929 | VerifyModelMatchesNode(&mobile, bb_model_->mobile_node()); |
[email protected] | 999f380 | 2009-05-22 19:28:00 | [diff] [blame] | 930 | VerifyNoDuplicateIDs(bb_model_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 931 | } |
| 932 | } |
[email protected] | 1fcb8b0 | 2008-08-01 17:36:48 | [diff] [blame] | 933 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 934 | // Simple test that removes a bookmark. This test exercises the code paths in |
| 935 | // History that block till bookmark bar model is loaded. |
[email protected] | fa0e5c08 | 2012-08-30 03:21:53 | [diff] [blame] | 936 | TEST_F(BookmarkModelTestWithProfile, RemoveNotification) { |
| 937 | profile_.reset(new TestingProfile()); |
| 938 | |
[email protected] | d486a085 | 2009-11-02 21:40:00 | [diff] [blame] | 939 | profile_->CreateHistoryService(false, false); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 940 | profile_->CreateBookmarkModel(true); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 941 | BlockTillBookmarkModelLoaded(); |
| 942 | |
| 943 | // Add a URL. |
| 944 | GURL url("http://www.google.com"); |
[email protected] | 4394f2b | 2011-09-03 03:07:56 | [diff] [blame] | 945 | bookmark_utils::AddIfNotBookmarked(bb_model_, url, string16()); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 946 | |
[email protected] | 9aac6686 | 2012-06-19 19:44:31 | [diff] [blame] | 947 | HistoryServiceFactory::GetForProfile( |
| 948 | profile_.get(), Profile::EXPLICIT_ACCESS)->AddPage( |
[email protected] | 853e01b | 2012-09-21 20:14:11 | [diff] [blame] | 949 | url, base::Time::Now(), NULL, 1, GURL(), history::RedirectList(), |
| 950 | content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, false); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 951 | |
| 952 | // This won't actually delete the URL, rather it'll empty out the visits. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 953 | // This triggers blocking on the BookmarkModel. |
[email protected] | ca881b8 | 2012-07-26 21:55:26 | [diff] [blame] | 954 | HistoryServiceFactory::GetForProfile( |
| 955 | profile_.get(), Profile::EXPLICIT_ACCESS)->DeleteURL(url); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 956 | } |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 957 | |
| 958 | TEST_F(BookmarkModelTest, Sort) { |
| 959 | // Populate the bookmark bar node with nodes for 'B', 'a', 'd' and 'C'. |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 960 | // 'C' and 'a' are folders. |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 961 | TestNode bbn; |
[email protected] | 0b0a7d7 | 2010-08-25 04:54:09 | [diff] [blame] | 962 | PopulateNodeFromString("B [ a ] d [ a ]", &bbn); |
[email protected] | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 963 | const BookmarkNode* parent = model_.bookmark_bar_node(); |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 964 | PopulateBookmarkNode(&bbn, &model_, parent); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 965 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 966 | BookmarkNode* child1 = AsMutable(parent->GetChild(1)); |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 967 | child1->SetTitle(ASCIIToUTF16("a")); |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame] | 968 | delete child1->Remove(child1->GetChild(0)); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 969 | BookmarkNode* child3 = AsMutable(parent->GetChild(3)); |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 970 | child3->SetTitle(ASCIIToUTF16("C")); |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame] | 971 | delete child3->Remove(child3->GetChild(0)); |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 972 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 973 | ClearCounts(); |
| 974 | |
| 975 | // Sort the children of the bookmark bar node. |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 976 | model_.SortChildren(parent); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 977 | |
| 978 | // Make sure we were notified. |
| 979 | AssertObserverCount(0, 0, 0, 0, 1); |
| 980 | |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 981 | // Make sure the order matches (remember, 'a' and 'C' are folders and |
| 982 | // come first). |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 983 | EXPECT_EQ(parent->GetChild(0)->GetTitle(), ASCIIToUTF16("a")); |
| 984 | EXPECT_EQ(parent->GetChild(1)->GetTitle(), ASCIIToUTF16("C")); |
| 985 | EXPECT_EQ(parent->GetChild(2)->GetTitle(), ASCIIToUTF16("B")); |
| 986 | EXPECT_EQ(parent->GetChild(3)->GetTitle(), ASCIIToUTF16("d")); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 987 | } |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 988 | |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 989 | TEST_F(BookmarkModelTest, NodeVisibility) { |
| 990 | EXPECT_TRUE(model_.bookmark_bar_node()->IsVisible()); |
| 991 | EXPECT_TRUE(model_.other_node()->IsVisible()); |
| 992 | // Mobile node invisible by default |
| 993 | EXPECT_FALSE(model_.mobile_node()->IsVisible()); |
| 994 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 995 | // Change visibility of permanent nodes. |
| 996 | model_.SetPermanentNodeVisible(BookmarkNode::BOOKMARK_BAR, false); |
| 997 | EXPECT_FALSE(model_.bookmark_bar_node()->IsVisible()); |
| 998 | model_.SetPermanentNodeVisible(BookmarkNode::OTHER_NODE, false); |
| 999 | EXPECT_FALSE(model_.other_node()->IsVisible()); |
| 1000 | model_.SetPermanentNodeVisible(BookmarkNode::MOBILE, true); |
| 1001 | EXPECT_TRUE(model_.mobile_node()->IsVisible()); |
| 1002 | |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 1003 | // Arbitrary node should be visible |
| 1004 | TestNode bbn; |
| 1005 | PopulateNodeFromString("B", &bbn); |
| 1006 | const BookmarkNode* parent = model_.bookmark_bar_node(); |
| 1007 | PopulateBookmarkNode(&bbn, &model_, parent); |
| 1008 | EXPECT_TRUE(parent->GetChild(0)->IsVisible()); |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 1009 | |
| 1010 | // Bookmark bar should be visible now that it has a child. |
| 1011 | EXPECT_TRUE(model_.bookmark_bar_node()->IsVisible()); |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | TEST_F(BookmarkModelTest, MobileNodeVisibileWithChildren) { |
| 1015 | const BookmarkNode* root = model_.mobile_node(); |
| 1016 | const string16 title(ASCIIToUTF16("foo")); |
| 1017 | const GURL url("http://foo.com"); |
| 1018 | |
| 1019 | model_.AddURL(root, 0, title, url); |
| 1020 | EXPECT_TRUE(model_.mobile_node()->IsVisible()); |
| 1021 | } |
| 1022 | |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 1023 | TEST_F(BookmarkModelTest, ExtensiveChangesObserver) { |
| 1024 | AssertExtensiveChangesObserverCount(0, 0); |
| 1025 | EXPECT_FALSE(model_.IsDoingExtensiveChanges()); |
| 1026 | model_.BeginExtensiveChanges(); |
| 1027 | EXPECT_TRUE(model_.IsDoingExtensiveChanges()); |
| 1028 | AssertExtensiveChangesObserverCount(1, 0); |
| 1029 | model_.EndExtensiveChanges(); |
| 1030 | EXPECT_FALSE(model_.IsDoingExtensiveChanges()); |
| 1031 | AssertExtensiveChangesObserverCount(1, 1); |
| 1032 | } |
| 1033 | |
| 1034 | TEST_F(BookmarkModelTest, MultipleExtensiveChangesObserver) { |
| 1035 | AssertExtensiveChangesObserverCount(0, 0); |
| 1036 | EXPECT_FALSE(model_.IsDoingExtensiveChanges()); |
| 1037 | model_.BeginExtensiveChanges(); |
| 1038 | EXPECT_TRUE(model_.IsDoingExtensiveChanges()); |
| 1039 | AssertExtensiveChangesObserverCount(1, 0); |
| 1040 | model_.BeginExtensiveChanges(); |
| 1041 | EXPECT_TRUE(model_.IsDoingExtensiveChanges()); |
| 1042 | AssertExtensiveChangesObserverCount(1, 0); |
| 1043 | model_.EndExtensiveChanges(); |
| 1044 | EXPECT_TRUE(model_.IsDoingExtensiveChanges()); |
| 1045 | AssertExtensiveChangesObserverCount(1, 0); |
| 1046 | model_.EndExtensiveChanges(); |
| 1047 | EXPECT_FALSE(model_.IsDoingExtensiveChanges()); |
| 1048 | AssertExtensiveChangesObserverCount(1, 1); |
| 1049 | } |
| 1050 | |
[email protected] | ff6592a | 2011-07-14 21:41:14 | [diff] [blame] | 1051 | } // namespace |