OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "app/table_model_observer.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/string_util.h" | |
8 #include "base/time.h" | |
9 #include "chrome/browser/bookmarks/bookmark_table_model.h" | |
10 #include "chrome/browser/chrome_thread.h" | |
11 #include "chrome/test/testing_profile.h" | |
12 #include "grit/generated_resources.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using base::Time; | |
16 using base::TimeDelta; | |
17 | |
18 // Base class for bookmark model tests. | |
19 // Initial state of the bookmark model is as follows: | |
20 // bb | |
21 // url1 (t0) | |
22 // f1 | |
23 // o | |
24 // url2 (t0 + 2) | |
25 // f2 | |
26 // url3 (t0 + 1) | |
27 class BookmarkTableModelTest : public testing::Test, | |
28 public TableModelObserver { | |
29 public: | |
30 BookmarkTableModelTest() | |
31 : url1_("http://1"), | |
32 url2_("http://2"), | |
33 url3_("http://3"), | |
34 changed_count_(0), | |
35 item_changed_count_(0), | |
36 added_count_(0), | |
37 removed_count_(0), | |
38 ui_thread_(ChromeThread::UI, &loop_), | |
39 file_thread_(ChromeThread::FILE, &loop_) { | |
40 } | |
41 | |
42 virtual void SetUp() { | |
43 profile_.reset(new TestingProfile()); | |
44 profile_->CreateBookmarkModel(true); | |
45 profile_->BlockUntilBookmarkModelLoaded(); | |
46 // Populate with some default data. | |
47 Time t0 = Time::Now(); | |
48 const BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode(); | |
49 bookmark_model()->AddURLWithCreationTime(bb, 0, L"a", url1_, t0); | |
50 bookmark_model()->AddGroup(bb, 1, L"f1"); | |
51 | |
52 const BookmarkNode* other = bookmark_model()->other_node(); | |
53 bookmark_model()->AddURLWithCreationTime(other, 0, L"b", | |
54 url2_, t0 + TimeDelta::FromDays(2)); | |
55 bookmark_model()->AddGroup(other, 1, L"f2"); | |
56 bookmark_model()->AddURLWithCreationTime(other, 2, L"c", url3_, | |
57 t0 + TimeDelta::FromDays(1)); | |
58 } | |
59 | |
60 virtual void TearDown() { | |
61 model_.reset(NULL); | |
62 profile_.reset(NULL); | |
63 } | |
64 | |
65 BookmarkModel* bookmark_model() const { | |
66 return profile_->GetBookmarkModel(); | |
67 } | |
68 | |
69 virtual void OnModelChanged() { | |
70 changed_count_++; | |
71 } | |
72 | |
73 virtual void OnItemsChanged(int start, int length) { | |
74 item_changed_count_++; | |
75 } | |
76 | |
77 virtual void OnItemsAdded(int start, int length) { | |
78 added_count_++; | |
79 } | |
80 | |
81 virtual void OnItemsRemoved(int start, int length) { | |
82 removed_count_++; | |
83 } | |
84 | |
85 void VerifyAndClearOberserverCounts(int changed_count, int item_changed_count, | |
86 int added_count, int removed_count) { | |
87 EXPECT_EQ(changed_count, changed_count_); | |
88 EXPECT_EQ(item_changed_count, item_changed_count_); | |
89 EXPECT_EQ(added_count, added_count_); | |
90 EXPECT_EQ(removed_count, removed_count_); | |
91 ResetCounts(); | |
92 } | |
93 | |
94 void ResetCounts() { | |
95 changed_count_ = item_changed_count_ = removed_count_ = added_count_ = 0; | |
96 } | |
97 | |
98 void SetModel(BookmarkTableModel* model) { | |
99 if (model_.get()) | |
100 model_->SetObserver(NULL); | |
101 model_.reset(model); | |
102 if (model_.get()) | |
103 model_->SetObserver(this); | |
104 } | |
105 | |
106 scoped_ptr<BookmarkTableModel> model_; | |
107 | |
108 const GURL url1_; | |
109 const GURL url2_; | |
110 const GURL url3_; | |
111 | |
112 private: | |
113 int changed_count_; | |
114 int item_changed_count_; | |
115 int added_count_; | |
116 int removed_count_; | |
117 scoped_ptr<TestingProfile> profile_; | |
118 MessageLoop loop_; | |
119 ChromeThread ui_thread_; | |
120 ChromeThread file_thread_; | |
121 }; | |
122 | |
123 // Verifies the count when showing various nodes. | |
124 TEST_F(BookmarkTableModelTest, FolderInitialState) { | |
125 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
126 bookmark_model(), bookmark_model()->GetBookmarkBarNode())); | |
127 ASSERT_EQ(2, model_->RowCount()); | |
128 EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(0) == | |
129 model_->GetNodeForRow(0)); | |
130 EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(1) == | |
131 model_->GetNodeForRow(1)); | |
132 | |
133 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
134 bookmark_model(), bookmark_model()->other_node())); | |
135 EXPECT_EQ(3, model_->RowCount()); | |
136 } | |
137 | |
138 // Verifies adding an item to folder model generates the correct event. | |
139 TEST_F(BookmarkTableModelTest, AddToFolder) { | |
140 const BookmarkNode* other = bookmark_model()->other_node(); | |
141 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
142 bookmark_model(), other)); | |
143 const BookmarkNode* new_node = bookmark_model()->AddURL(other, 0, L"new", | |
144 url1_); | |
145 // Should have gotten notification of the add. | |
146 VerifyAndClearOberserverCounts(0, 0, 1, 0); | |
147 ASSERT_EQ(4, model_->RowCount()); | |
148 EXPECT_TRUE(new_node == model_->GetNodeForRow(0)); | |
149 | |
150 // Add to the bookmark bar, this shouldn't generate an event. | |
151 bookmark_model()->AddURL(bookmark_model()->GetBookmarkBarNode(), 0, L"new", | |
152 url1_); | |
153 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
154 } | |
155 | |
156 // Verifies sort sends out notification and results in a sort. | |
157 TEST_F(BookmarkTableModelTest, SortFolder) { | |
158 const BookmarkNode* other = bookmark_model()->other_node(); | |
159 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
160 bookmark_model(), other)); | |
161 ASSERT_EQ(3, model_->RowCount()); | |
162 bookmark_model()->SortChildren(other); | |
163 | |
164 // Sorting should trigger change notification. | |
165 VerifyAndClearOberserverCounts(1, 0, 0, 0); | |
166 | |
167 // Make sure things reordered. | |
168 EXPECT_TRUE(other->GetChild(0) == model_->GetNodeForRow(0)); | |
169 EXPECT_TRUE(other->GetChild(1) == model_->GetNodeForRow(1)); | |
170 EXPECT_TRUE(other->GetChild(2) == model_->GetNodeForRow(2)); | |
171 } | |
172 | |
173 // Verifies removing an item from folder model generates the correct event. | |
174 TEST_F(BookmarkTableModelTest, RemoveFromFolder) { | |
175 const BookmarkNode* other = bookmark_model()->other_node(); | |
176 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
177 bookmark_model(), other)); | |
178 bookmark_model()->Remove(other, 0); | |
179 | |
180 // Should have gotten notification of the remove. | |
181 VerifyAndClearOberserverCounts(0, 0, 0, 1); | |
182 EXPECT_EQ(2, model_->RowCount()); | |
183 | |
184 // Remove from the bookmark bar, this shouldn't generate an event. | |
185 bookmark_model()->Remove(bookmark_model()->GetBookmarkBarNode(), 0); | |
186 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
187 } | |
188 | |
189 // Verifies changing an item in the folder model generates the correct event. | |
190 TEST_F(BookmarkTableModelTest, ChangeFolder) { | |
191 const BookmarkNode* other = bookmark_model()->other_node(); | |
192 SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( | |
193 bookmark_model(), other)); | |
194 bookmark_model()->SetTitle(other->GetChild(0), L"new"); | |
195 | |
196 // Should have gotten notification of the change. | |
197 VerifyAndClearOberserverCounts(0, 1, 0, 0); | |
198 EXPECT_EQ(3, model_->RowCount()); | |
199 | |
200 // Change a node in the bookmark bar, this shouldn't generate an event. | |
201 bookmark_model()->SetTitle( | |
202 bookmark_model()->GetBookmarkBarNode()->GetChild(0), L"new2"); | |
203 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
204 } | |
205 | |
206 // Verifies show recently added shows the recently added, in order. | |
207 TEST_F(BookmarkTableModelTest, RecentlyBookmarkedOrder) { | |
208 SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); | |
209 EXPECT_EQ(3, model_->RowCount()); | |
210 | |
211 const BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode(); | |
212 const BookmarkNode* other = bookmark_model()->other_node(); | |
213 EXPECT_TRUE(other->GetChild(0) == model_->GetNodeForRow(0)); | |
214 EXPECT_TRUE(other->GetChild(2) == model_->GetNodeForRow(1)); | |
215 EXPECT_TRUE(bb->GetChild(0) == model_->GetNodeForRow(2)); | |
216 } | |
217 | |
218 // Verifies adding an item to recently added notifies observer. | |
219 TEST_F(BookmarkTableModelTest, AddToRecentlyBookmarked) { | |
220 SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); | |
221 bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"new", url1_); | |
222 // Should have gotten notification of the add. | |
223 VerifyAndClearOberserverCounts(1, 0, 0, 0); | |
224 EXPECT_EQ(4, model_->RowCount()); | |
225 | |
226 // Add a folder, this shouldn't change the model. | |
227 bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); | |
228 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
229 } | |
230 | |
231 // Verifies removing an item from recently added notifies observer. | |
232 TEST_F(BookmarkTableModelTest, RemoveFromRecentlyBookmarked) { | |
233 SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); | |
234 bookmark_model()->Remove(bookmark_model()->other_node(), 0); | |
235 // Should have gotten notification of the remove. | |
236 VerifyAndClearOberserverCounts(1, 0, 0, 0); | |
237 EXPECT_EQ(2, model_->RowCount()); | |
238 | |
239 // Remove a folder, this shouldn't change the model. | |
240 bookmark_model()->Remove(bookmark_model()->other_node(), 0); | |
241 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
242 } | |
243 | |
244 // Verifies changing an item in recently added notifies observer. | |
245 TEST_F(BookmarkTableModelTest, ChangeRecentlyBookmarked) { | |
246 SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); | |
247 bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0), | |
248 L"new"); | |
249 // Should have gotten notification of the change. | |
250 VerifyAndClearOberserverCounts(0, 1, 0, 0); | |
251 | |
252 // Change a folder, this shouldn't change the model. | |
253 bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1), | |
254 L"new"); | |
255 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
256 } | |
257 | |
258 // Verifies search finds the correct bookmarks. | |
259 TEST_F(BookmarkTableModelTest, Search) { | |
260 SetModel(BookmarkTableModel::CreateSearchTableModel( | |
261 bookmark_model(), L"c", std::wstring())); | |
262 ASSERT_EQ(1, model_->RowCount()); | |
263 EXPECT_TRUE(bookmark_model()->other_node()->GetChild(2) == | |
264 model_->GetNodeForRow(0)); | |
265 // Make sure IndexOfNode works. | |
266 EXPECT_EQ(0, | |
267 model_->IndexOfNode(bookmark_model()->other_node()->GetChild(2))); | |
268 } | |
269 | |
270 // Verifies adding an item to search notifies observers. | |
271 TEST_F(BookmarkTableModelTest, AddToSearch) { | |
272 SetModel(BookmarkTableModel::CreateSearchTableModel( | |
273 bookmark_model(), L"c", std::wstring())); | |
274 const BookmarkNode* new_node = | |
275 bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"c", url1_); | |
276 // Should have gotten notification of the add. | |
277 VerifyAndClearOberserverCounts(0, 0, 1, 0); | |
278 ASSERT_EQ(2, model_->RowCount()); | |
279 // New node should have gone to end. | |
280 EXPECT_TRUE(model_->GetNodeForRow(1) == new_node); | |
281 | |
282 // Add a folder, this shouldn't change the model. | |
283 bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); | |
284 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
285 EXPECT_EQ(2, model_->RowCount()); | |
286 | |
287 // Add a url that doesn't match search, this shouldn't change the model. | |
288 bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); | |
289 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
290 EXPECT_EQ(2, model_->RowCount()); | |
291 } | |
292 | |
293 // Verifies removing an item updates search. | |
294 TEST_F(BookmarkTableModelTest, RemoveFromSearch) { | |
295 SetModel(BookmarkTableModel::CreateSearchTableModel( | |
296 bookmark_model(), L"c", std::wstring())); | |
297 bookmark_model()->Remove(bookmark_model()->other_node(), 2); | |
298 // Should have gotten notification of the remove. | |
299 VerifyAndClearOberserverCounts(0, 0, 0, 1); | |
300 EXPECT_EQ(0, model_->RowCount()); | |
301 | |
302 // Remove a folder, this shouldn't change the model. | |
303 bookmark_model()->Remove(bookmark_model()->other_node(), 1); | |
304 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
305 | |
306 // Remove another url that isn't in the model, this shouldn't change anything. | |
307 bookmark_model()->Remove(bookmark_model()->other_node(), 0); | |
308 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
309 } | |
310 | |
311 // Verifies changing an item in search notifies observer. | |
312 TEST_F(BookmarkTableModelTest, ChangeSearch) { | |
313 SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), | |
314 L"c", std::wstring())); | |
315 bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(2), | |
316 L"new"); | |
317 // Should have gotten notification of the change. | |
318 VerifyAndClearOberserverCounts(0, 1, 0, 0); | |
319 | |
320 // Change a folder, this shouldn't change the model. | |
321 bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1), | |
322 L"new"); | |
323 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
324 | |
325 // Change a url that isn't in the model, this shouldn't send change. | |
326 bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0), | |
327 L"new"); | |
328 VerifyAndClearOberserverCounts(0, 0, 0, 0); | |
329 } | |
OLD | NEW |