OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/strings/utf_string_conversions.h" | |
6 #include "chrome/browser/bookmarks/bookmark_model.h" | |
7 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
8 #include "chrome/browser/undo/bookmark_undo_service.h" | |
sky
2013/08/27 22:24:18
nit: this should be your first include (test files
Tom Cassiotis
2013/09/04 13:44:43
Done.
| |
9 #include "chrome/browser/undo/bookmark_undo_service_factory.h" | |
10 #include "chrome/test/base/testing_profile.h" | |
11 #include "chrome/test/base/ui_test_utils.h" | |
12 #include "content/public/test/test_browser_thread_bundle.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 class BookmarkUndoServiceTest : public testing::Test { | |
18 public: | |
19 BookmarkUndoServiceTest(); | |
20 | |
21 virtual void SetUp() OVERRIDE; | |
22 virtual void TearDown() OVERRIDE; | |
23 | |
24 BookmarkModel* GetModel(); | |
25 BookmarkUndoService* GetUndoService(); | |
26 | |
27 private: | |
28 scoped_ptr<TestingProfile> profile_; | |
29 content::TestBrowserThreadBundle thread_bundle_; | |
30 | |
31 DISALLOW_COPY_AND_ASSIGN(BookmarkUndoServiceTest); | |
32 }; | |
33 | |
34 BookmarkUndoServiceTest::BookmarkUndoServiceTest() {} | |
35 | |
36 void BookmarkUndoServiceTest::SetUp() { | |
37 profile_.reset(new TestingProfile); | |
38 profile_->CreateBookmarkModel(true); | |
39 ui_test_utils::WaitForBookmarkModelToLoad(GetModel()); | |
40 } | |
41 | |
42 BookmarkModel* BookmarkUndoServiceTest::GetModel() { | |
43 return BookmarkModelFactory::GetForProfile(profile_.get()); | |
44 } | |
45 | |
46 BookmarkUndoService* BookmarkUndoServiceTest::GetUndoService() { | |
47 return BookmarkUndoServiceFactory::GetForProfile(profile_.get()); | |
48 } | |
49 | |
50 void BookmarkUndoServiceTest::TearDown() { | |
51 profile_.reset(NULL); | |
52 } | |
53 | |
54 TEST_F(BookmarkUndoServiceTest, AddBookmark) { | |
55 BookmarkModel* model = GetModel(); | |
56 BookmarkUndoService* undo_service = GetUndoService(); | |
57 | |
58 const BookmarkNode* parent = model->other_node(); | |
59 model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.bar.com")); | |
60 | |
61 // Undo bookmark creation and test for no bookmarks. | |
62 undo_service->undo_manager()->Undo(); | |
63 EXPECT_EQ(0, model->other_node()->child_count()); | |
64 | |
65 // Redo bookmark creation and ensure bookmark information is valid. | |
66 undo_service->undo_manager()->Redo(); | |
67 const BookmarkNode* node = parent->GetChild(0); | |
68 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
69 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
70 } | |
71 | |
72 // Test that a bookmark removal action can be undone and redone. | |
73 TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemove) { | |
74 BookmarkModel* model = GetModel(); | |
75 BookmarkUndoService* undo_service = GetUndoService(); | |
76 | |
77 const BookmarkNode* parent = model->other_node(); | |
78 model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.bar.com")); | |
79 model->Remove(parent, 0); | |
80 | |
81 EXPECT_EQ(2U, undo_service->undo_manager()->undo_count()); | |
82 EXPECT_EQ(0U, undo_service->undo_manager()->redo_count()); | |
83 | |
84 // Undo the deletion of the only bookmark and check the bookmark values. | |
85 undo_service->undo_manager()->Undo(); | |
86 EXPECT_EQ(1, model->other_node()->child_count()); | |
87 const BookmarkNode* node = parent->GetChild(0); | |
88 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
89 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
90 | |
91 EXPECT_EQ(1U, undo_service->undo_manager()->undo_count()); | |
92 EXPECT_EQ(1U, undo_service->undo_manager()->redo_count()); | |
93 | |
94 // Redo the deletion and check that there are no bookmarks left. | |
95 undo_service->undo_manager()->Redo(); | |
96 EXPECT_EQ(0, model->other_node()->child_count()); | |
97 | |
98 EXPECT_EQ(2U, undo_service->undo_manager()->undo_count()); | |
99 EXPECT_EQ(0U, undo_service->undo_manager()->redo_count()); | |
100 } | |
101 | |
102 // Ensure the undo/redo works for editing of bookmark information grouped into | |
103 // one action. | |
104 TEST_F(BookmarkUndoServiceTest, UndoBookmarkGroupedAction) { | |
105 BookmarkModel* model = GetModel(); | |
106 BookmarkUndoService* undo_service = GetUndoService(); | |
107 | |
108 const BookmarkNode* n1 = model->AddURL(model->other_node(), | |
109 0, | |
110 ASCIIToUTF16("foo"), | |
111 GURL("http://www.foo.com")); | |
112 undo_service->undo_manager()->StartGroupingActions(); | |
113 model->SetTitle(n1, ASCIIToUTF16("bar")); | |
114 model->SetURL(n1, GURL("http://www.bar.com")); | |
115 undo_service->undo_manager()->EndGroupingActions(); | |
116 | |
117 EXPECT_EQ(2U, undo_service->undo_manager()->undo_count()); | |
118 EXPECT_EQ(0U, undo_service->undo_manager()->redo_count()); | |
119 | |
120 // Undo the modification of the bookmark and check for the original values. | |
121 undo_service->undo_manager()->Undo(); | |
122 EXPECT_EQ(1, model->other_node()->child_count()); | |
123 const BookmarkNode* node = model->other_node()->GetChild(0); | |
124 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
125 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
126 | |
127 // Redo the modifications and ensure the newer values are present. | |
128 undo_service->undo_manager()->Redo(); | |
129 EXPECT_EQ(1, model->other_node()->child_count()); | |
130 node = model->other_node()->GetChild(0); | |
131 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar")); | |
132 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
133 | |
134 EXPECT_EQ(2U, undo_service->undo_manager()->undo_count()); | |
135 EXPECT_EQ(0U, undo_service->undo_manager()->redo_count()); | |
136 } | |
137 | |
138 // Test moving bookmarks within a folder and between folders. | |
139 TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveWithinFolder) { | |
140 BookmarkModel* model = GetModel(); | |
141 BookmarkUndoService* undo_service = GetUndoService(); | |
142 | |
143 const BookmarkNode* n1 = model->AddURL(model->other_node(), | |
144 0, | |
145 ASCIIToUTF16("foo"), | |
146 GURL("http://www.foo.com")); | |
147 const BookmarkNode* n2 = model->AddURL(model->other_node(), | |
148 1, | |
149 ASCIIToUTF16("moo"), | |
150 GURL("http://www.moo.com")); | |
151 const BookmarkNode* n3 = model->AddURL(model->other_node(), | |
152 2, | |
153 ASCIIToUTF16("bar"), | |
154 GURL("http://www.bar.com")); | |
155 model->Move(n1, model->other_node(), 3); | |
156 | |
157 // Undo the move and check that the nodes are in order. | |
158 undo_service->undo_manager()->Undo(); | |
159 EXPECT_EQ(model->other_node()->GetChild(0), n1); | |
160 EXPECT_EQ(model->other_node()->GetChild(1), n2); | |
161 EXPECT_EQ(model->other_node()->GetChild(2), n3); | |
162 | |
163 // Redo the move and check that the first node is in the last position. | |
164 undo_service->undo_manager()->Redo(); | |
165 EXPECT_EQ(model->other_node()->GetChild(0), n2); | |
166 EXPECT_EQ(model->other_node()->GetChild(1), n3); | |
167 EXPECT_EQ(model->other_node()->GetChild(2), n1); | |
168 } | |
169 | |
170 // Test undo of a bookmark moved to a different folder. | |
171 TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveToOtherFolder) { | |
172 BookmarkModel* model = GetModel(); | |
173 BookmarkUndoService* undo_service = GetUndoService(); | |
174 | |
175 const BookmarkNode* n1 = model->AddURL(model->other_node(), | |
176 0, | |
177 ASCIIToUTF16("foo"), | |
178 GURL("http://www.foo.com")); | |
179 const BookmarkNode* n2 = model->AddURL(model->other_node(), | |
180 1, | |
181 ASCIIToUTF16("moo"), | |
182 GURL("http://www.moo.com")); | |
183 const BookmarkNode* n3 = model->AddURL(model->other_node(), | |
184 2, | |
185 ASCIIToUTF16("bar"), | |
186 GURL("http://www.bar.com")); | |
187 const BookmarkNode* f1 = | |
188 model->AddFolder(model->other_node(), 3, ASCIIToUTF16("folder")); | |
189 model->Move(n3, f1, 0); | |
190 | |
191 // Undo the move and check that the bookmark and folder are in place. | |
192 undo_service->undo_manager()->Undo(); | |
193 ASSERT_EQ(4, model->other_node()->child_count()); | |
194 EXPECT_EQ(model->other_node()->GetChild(0), n1); | |
195 EXPECT_EQ(model->other_node()->GetChild(1), n2); | |
196 EXPECT_EQ(model->other_node()->GetChild(2), n3); | |
197 EXPECT_EQ(model->other_node()->GetChild(3), f1); | |
198 EXPECT_EQ(0, f1->child_count()); | |
199 | |
200 // Redo the move back into the folder and check validity. | |
201 undo_service->undo_manager()->Redo(); | |
202 ASSERT_EQ(3, model->other_node()->child_count()); | |
203 EXPECT_EQ(model->other_node()->GetChild(0), n1); | |
204 EXPECT_EQ(model->other_node()->GetChild(1), n2); | |
205 EXPECT_EQ(model->other_node()->GetChild(2), f1); | |
206 ASSERT_EQ(1, f1->child_count()); | |
207 EXPECT_EQ(f1->GetChild(0), n3); | |
208 } | |
209 | |
210 // Tests the handling of multiple modifications that include renumbering of the | |
211 // bookmark identifiers. | |
212 TEST_F(BookmarkUndoServiceTest, UndoBookmarkRenameDelete) { | |
213 BookmarkModel* model = GetModel(); | |
214 BookmarkUndoService* undo_service = GetUndoService(); | |
215 | |
216 const BookmarkNode* f1 = model->AddFolder(model->other_node(), | |
217 0, | |
218 ASCIIToUTF16("folder")); | |
219 model->AddURL(f1, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com")); | |
220 model->SetTitle(f1, ASCIIToUTF16("Renamed")); | |
221 model->Remove(model->other_node(), 0); | |
222 | |
223 // Undo the folder removal and ensure the folder and bookmark were restored. | |
224 undo_service->undo_manager()->Undo(); | |
225 ASSERT_EQ(1, model->other_node()->child_count()); | |
226 ASSERT_EQ(1, model->other_node()->GetChild(0)->child_count()); | |
227 const BookmarkNode* node = model->other_node()->GetChild(0); | |
228 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("Renamed")); | |
229 | |
230 node = model->other_node()->GetChild(0)->GetChild(0); | |
231 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
232 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
233 | |
234 // Undo the title change and ensure the folder was updated even though the | |
235 // id has changed. | |
236 undo_service->undo_manager()->Undo(); | |
237 node = model->other_node()->GetChild(0); | |
238 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("folder")); | |
239 | |
240 // Undo bookmark creation and test for removal of bookmark. | |
241 undo_service->undo_manager()->Undo(); | |
242 ASSERT_EQ(0, model->other_node()->GetChild(0)->child_count()); | |
243 | |
244 // Undo folder creation and confirm the bookmark model is empty. | |
245 undo_service->undo_manager()->Undo(); | |
246 ASSERT_EQ(0, model->other_node()->child_count()); | |
247 | |
248 // Redo all the actions and ensure the folder and bookmark are restored. | |
249 undo_service->undo_manager()->Redo(); // folder creation | |
250 undo_service->undo_manager()->Redo(); // bookmark creation | |
251 undo_service->undo_manager()->Redo(); // bookmark title change | |
252 ASSERT_EQ(1, model->other_node()->child_count()); | |
253 ASSERT_EQ(1, model->other_node()->GetChild(0)->child_count()); | |
254 node = model->other_node()->GetChild(0); | |
255 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("Renamed")); | |
256 node = model->other_node()->GetChild(0)->GetChild(0); | |
257 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
258 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
259 | |
260 undo_service->undo_manager()->Redo(); // folder deletion | |
261 EXPECT_EQ(0, model->other_node()->child_count()); | |
262 } | |
263 | |
264 // Test the undo of SortChildren and ReorderChildren. | |
265 TEST_F(BookmarkUndoServiceTest, UndoBookmarkReorder) { | |
266 BookmarkModel* model = GetModel(); | |
267 BookmarkUndoService* undo_service = GetUndoService(); | |
268 | |
269 const BookmarkNode* parent = model->other_node(); | |
270 model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com")); | |
271 model->AddURL(parent, 1, ASCIIToUTF16("moo"), GURL("http://www.moo.com")); | |
272 model->AddURL(parent, 2, ASCIIToUTF16("bar"), GURL("http://www.bar.com")); | |
273 model->SortChildren(parent); | |
274 | |
275 // Test the undo of SortChildren. | |
276 undo_service->undo_manager()->Undo(); | |
277 const BookmarkNode* node = parent->GetChild(0); | |
278 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
279 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
280 | |
281 node = parent->GetChild(1); | |
282 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("moo")); | |
283 EXPECT_EQ(node->url(), GURL("http://www.moo.com")); | |
284 | |
285 node = parent->GetChild(2); | |
286 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar")); | |
287 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
288 | |
289 // Test the redo of SortChildren. | |
290 undo_service->undo_manager()->Redo(); | |
291 node = parent->GetChild(0); | |
292 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar")); | |
293 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
294 | |
295 node = parent->GetChild(1); | |
296 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
297 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
298 | |
299 node = parent->GetChild(2); | |
300 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("moo")); | |
301 EXPECT_EQ(node->url(), GURL("http://www.moo.com")); | |
302 | |
303 } | |
304 | |
305 TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemoveAll) { | |
306 BookmarkModel* model = GetModel(); | |
307 BookmarkUndoService* undo_service = GetUndoService(); | |
308 | |
309 // Setup bookmarks in the Other Bookmarks and the Bookmark Bar. | |
310 const BookmarkNode* new_folder; | |
311 const BookmarkNode* parent = model->other_node(); | |
312 model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.google.com")); | |
313 new_folder= model->AddFolder(parent, 1, ASCIIToUTF16("folder")); | |
314 model->AddURL(new_folder, 0, ASCIIToUTF16("bar"), GURL("http://www.bar.com")); | |
315 | |
316 parent = model->bookmark_bar_node(); | |
317 model->AddURL(parent, 0, ASCIIToUTF16("a"), GURL("http://www.a.com")); | |
318 new_folder = model->AddFolder(parent, 1, ASCIIToUTF16("folder")); | |
319 model->AddURL(new_folder, 0, ASCIIToUTF16("b"), GURL("http://www.b.com")); | |
320 | |
321 model->RemoveAll(); | |
322 | |
323 // Test that the undo of RemoveAll restores all folders and bookmarks. | |
324 undo_service->undo_manager()->Undo(); | |
325 | |
326 ASSERT_EQ(2, model->other_node()->child_count()); | |
327 EXPECT_EQ(1, model->other_node()->GetChild(1)->child_count()); | |
328 const BookmarkNode* node = model->other_node()->GetChild(1)->GetChild(0); | |
329 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar")); | |
330 EXPECT_EQ(node->url(), GURL("http://www.bar.com")); | |
331 | |
332 ASSERT_EQ(2, model->bookmark_bar_node()->child_count()); | |
333 EXPECT_EQ(1, model->bookmark_bar_node()->GetChild(1)->child_count()); | |
334 node = model->bookmark_bar_node()->GetChild(1)->GetChild(0); | |
335 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("b")); | |
336 EXPECT_EQ(node->url(), GURL("http://www.b.com")); | |
337 | |
338 // Test that the redo removes all folders and bookmarks. | |
339 undo_service->undo_manager()->Redo(); | |
340 EXPECT_EQ(0, model->other_node()->child_count()); | |
341 EXPECT_EQ(0, model->bookmark_bar_node()->child_count()); | |
342 } | |
343 | |
344 TEST_F(BookmarkUndoServiceTest, TestUpperLimit) { | |
345 BookmarkModel* model = GetModel(); | |
346 BookmarkUndoService* undo_service = GetUndoService(); | |
347 | |
348 // This maximum is set in undo_manager.cc | |
349 const size_t kMaxUndoGroups = 100; | |
350 | |
351 const BookmarkNode* parent = model->other_node(); | |
352 model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com")); | |
353 for (int i = 1; i < kMaxUndoGroups + 1; ++i) | |
354 model->AddURL(parent, i, ASCIIToUTF16("bar"), GURL("http://www.bar.com")); | |
355 | |
356 EXPECT_EQ(kMaxUndoGroups, undo_service->undo_manager()->undo_count()); | |
357 | |
358 // Undo as many operations as possible. | |
359 while (undo_service->undo_manager()->undo_count()) | |
360 undo_service->undo_manager()->Undo(); | |
361 | |
362 EXPECT_EQ(1, parent->child_count()); | |
363 const BookmarkNode* node = model->other_node()->GetChild(0); | |
364 EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo")); | |
365 EXPECT_EQ(node->url(), GURL("http://www.foo.com")); | |
366 } | |
367 | |
368 } // namespace | |
OLD | NEW |