File: bookmark_undo_service_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (544 lines) | stat: -rw-r--r-- 21,911 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/undo/bookmark_undo_service.h"

#include <stddef.h>

#include <memory>

#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/signin/public/base/signin_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;
using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;

namespace {

// TestBookmarkClient that supports undoing removals.
class TestBookmarkClientWithUndo : public bookmarks::TestBookmarkClient {
 public:
  explicit TestBookmarkClientWithUndo(BookmarkUndoService* undo_service)
      : undo_service_(undo_service) {}

  ~TestBookmarkClientWithUndo() override = default;

  // BookmarkClient overrides.
  void OnBookmarkNodeRemovedUndoable(
      const BookmarkNode* parent,
      size_t index,
      std::unique_ptr<BookmarkNode> node) override {
    undo_service_->AddUndoEntryForRemovedNode(parent, index, std::move(node));
  }

 private:
  const raw_ptr<BookmarkUndoService, DanglingUntriaged> undo_service_;
};

class BookmarkUndoServiceTest : public testing::Test {
 public:
  BookmarkUndoServiceTest();

  BookmarkUndoServiceTest(const BookmarkUndoServiceTest&) = delete;
  BookmarkUndoServiceTest& operator=(const BookmarkUndoServiceTest&) = delete;

  void SetUp() override;
  void TearDown() override;

  BookmarkModel* GetModel();
  BookmarkUndoService* GetUndoService();

 private:
  base::test::ScopedFeatureList features_{
      switches::kSyncEnableBookmarksInTransportMode};
  std::unique_ptr<BookmarkUndoService> bookmark_undo_service_;
  std::unique_ptr<bookmarks::BookmarkModel> bookmark_model_;
};

BookmarkUndoServiceTest::BookmarkUndoServiceTest() = default;

void BookmarkUndoServiceTest::SetUp() {
  DCHECK(!bookmark_model_);
  DCHECK(!bookmark_undo_service_);
  bookmark_undo_service_ = std::make_unique<BookmarkUndoService>();
  bookmark_model_ = bookmarks::TestBookmarkClient::CreateModelWithClient(
      std::make_unique<TestBookmarkClientWithUndo>(
          bookmark_undo_service_.get()));
  bookmark_undo_service_->StartObservingBookmarkModel(bookmark_model_.get());
  bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model_.get());
}

BookmarkModel* BookmarkUndoServiceTest::GetModel() {
  return bookmark_model_.get();
}

BookmarkUndoService* BookmarkUndoServiceTest::GetUndoService() {
  return bookmark_undo_service_.get();
}

void BookmarkUndoServiceTest::TearDown() {
  // Implement two-phase KeyedService shutdown for test KeyedServices.
  bookmark_undo_service_->Shutdown();
  bookmark_model_->Shutdown();
  bookmark_undo_service_.reset();
  bookmark_model_.reset();
}

TEST_F(BookmarkUndoServiceTest, AddBookmark) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* parent = model->other_node();
  model->AddURL(parent, 0, u"foo", GURL("http://www.bar.com"));

  // Undo bookmark creation and test for no bookmarks.
  undo_service->undo_manager()->Undo();
  EXPECT_EQ(0u, model->other_node()->children().size());

  // Redo bookmark creation and ensure bookmark information is valid.
  undo_service->undo_manager()->Redo();
  const BookmarkNode* node = parent->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
}

// Test that a bookmark removal action can be undone and redone.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemove) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* parent = model->other_node();
  model->AddURL(parent, 0, u"foo", GURL("http://www.bar.com"));
  model->Remove(parent->children().front().get(),
                bookmarks::metrics::BookmarkEditSource::kOther, FROM_HERE);

  EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
  EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());

  // Undo the deletion of the only bookmark and check the bookmark values.
  undo_service->undo_manager()->Undo();
  EXPECT_EQ(1u, model->other_node()->children().size());
  const BookmarkNode* node = parent->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  EXPECT_EQ(1U, undo_service->undo_manager()->undo_count());
  EXPECT_EQ(1U, undo_service->undo_manager()->redo_count());

  // Redo the deletion and check that there are no bookmarks left.
  undo_service->undo_manager()->Redo();
  EXPECT_EQ(0u, model->other_node()->children().size());

  EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
  EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());
}

// Ensure the undo/redo works for editing of bookmark information grouped into
// one action.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkGroupedAction) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* n1 =
      model->AddURL(model->other_node(), 0, u"foo", GURL("http://www.foo.com"));
  undo_service->undo_manager()->StartGroupingActions();
  model->SetTitle(n1, u"bar", bookmarks::metrics::BookmarkEditSource::kOther);
  model->SetURL(n1, GURL("http://www.bar.com"),
                bookmarks::metrics::BookmarkEditSource::kOther);
  undo_service->undo_manager()->EndGroupingActions();

  EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
  EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());

  // Undo the modification of the bookmark and check for the original values.
  undo_service->undo_manager()->Undo();
  EXPECT_EQ(1u, model->other_node()->children().size());
  const BookmarkNode* node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));

  // Redo the modifications and ensure the newer values are present.
  undo_service->undo_manager()->Redo();
  EXPECT_EQ(1u, model->other_node()->children().size());
  node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
  EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());
}

// Test moving bookmarks within a folder and between folders.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveWithinFolder) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* n1 =
      model->AddURL(model->other_node(), 0, u"foo", GURL("http://www.foo.com"));
  const BookmarkNode* n2 =
      model->AddURL(model->other_node(), 1, u"moo", GURL("http://www.moo.com"));
  const BookmarkNode* n3 =
      model->AddURL(model->other_node(), 2, u"bar", GURL("http://www.bar.com"));
  model->Move(n1, model->other_node(), 3);

  // Undo the move and check that the nodes are in order.
  undo_service->undo_manager()->Undo();
  EXPECT_EQ(model->other_node()->children()[0].get(), n1);
  EXPECT_EQ(model->other_node()->children()[1].get(), n2);
  EXPECT_EQ(model->other_node()->children()[2].get(), n3);

  // Redo the move and check that the first node is in the last position.
  undo_service->undo_manager()->Redo();
  EXPECT_EQ(model->other_node()->children()[0].get(), n2);
  EXPECT_EQ(model->other_node()->children()[1].get(), n3);
  EXPECT_EQ(model->other_node()->children()[2].get(), n1);
}

// Test moving bookmarks across NodeTypeForUuidLookup boundaries.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveAcrossNodeTypeForUuidLookup) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  model->CreateAccountPermanentFolders();
  ASSERT_NE(nullptr, model->account_other_node());
  ASSERT_NE(model->other_node(), model->account_other_node());

  const BookmarkNode* n1 =
      model->AddURL(model->other_node(), 0, u"foo", GURL("http://www.foo.com"));

  ASSERT_EQ(1u, model->other_node()->children().size());
  ASSERT_EQ(0u, model->account_other_node()->children().size());
  ASSERT_EQ(n1,
            model->GetNodeByUuid(
                n1->uuid(),
                BookmarkModel::NodeTypeForUuidLookup::kLocalOrSyncableNodes));
  ASSERT_EQ(nullptr, model->GetNodeByUuid(
                         n1->uuid(),
                         BookmarkModel::NodeTypeForUuidLookup::kAccountNodes));

  // Move from kLocalOrSyncableNodes to kAccountNodes.
  model->Move(n1, model->account_other_node(), 0);

  ASSERT_EQ(0u, model->other_node()->children().size());
  ASSERT_EQ(1u, model->account_other_node()->children().size());
  ASSERT_EQ(nullptr,
            model->GetNodeByUuid(
                n1->uuid(),
                BookmarkModel::NodeTypeForUuidLookup::kLocalOrSyncableNodes));
  ASSERT_EQ(
      n1, model->GetNodeByUuid(
              n1->uuid(), BookmarkModel::NodeTypeForUuidLookup::kAccountNodes));

  // Undo the move and check that it was moved back to kLocalOrSyncableNodes.
  undo_service->undo_manager()->Undo();

  EXPECT_EQ(1u, model->other_node()->children().size());
  EXPECT_EQ(0u, model->account_other_node()->children().size());
  EXPECT_EQ(n1,
            model->GetNodeByUuid(
                n1->uuid(),
                BookmarkModel::NodeTypeForUuidLookup::kLocalOrSyncableNodes));
  EXPECT_EQ(nullptr, model->GetNodeByUuid(
                         n1->uuid(),
                         BookmarkModel::NodeTypeForUuidLookup::kAccountNodes));

  // Redo the move and check that it moves again to kAccountNodes.
  undo_service->undo_manager()->Redo();

  EXPECT_EQ(0u, model->other_node()->children().size());
  EXPECT_EQ(1u, model->account_other_node()->children().size());
  EXPECT_EQ(nullptr,
            model->GetNodeByUuid(
                n1->uuid(),
                BookmarkModel::NodeTypeForUuidLookup::kLocalOrSyncableNodes));
  EXPECT_EQ(
      n1, model->GetNodeByUuid(
              n1->uuid(), BookmarkModel::NodeTypeForUuidLookup::kAccountNodes));
}

// Test undo of a bookmark moved to a different folder.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveToOtherFolder) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* n1 =
      model->AddURL(model->other_node(), 0, u"foo", GURL("http://www.foo.com"));
  const BookmarkNode* n2 =
      model->AddURL(model->other_node(), 1, u"moo", GURL("http://www.moo.com"));
  const BookmarkNode* n3 =
      model->AddURL(model->other_node(), 2, u"bar", GURL("http://www.bar.com"));
  const BookmarkNode* f1 = model->AddFolder(model->other_node(), 3, u"folder");
  model->Move(n3, f1, 0);

  // Undo the move and check that the bookmark and folder are in place.
  undo_service->undo_manager()->Undo();
  ASSERT_EQ(4u, model->other_node()->children().size());
  EXPECT_EQ(model->other_node()->children()[0].get(), n1);
  EXPECT_EQ(model->other_node()->children()[1].get(), n2);
  EXPECT_EQ(model->other_node()->children()[2].get(), n3);
  EXPECT_EQ(model->other_node()->children()[3].get(), f1);
  EXPECT_EQ(0u, f1->children().size());

  // Redo the move back into the folder and check validity.
  undo_service->undo_manager()->Redo();
  ASSERT_EQ(3u, model->other_node()->children().size());
  EXPECT_EQ(model->other_node()->children()[0].get(), n1);
  EXPECT_EQ(model->other_node()->children()[1].get(), n2);
  EXPECT_EQ(model->other_node()->children()[2].get(), f1);
  ASSERT_EQ(1u, f1->children().size());
  EXPECT_EQ(f1->children().front().get(), n3);
}

// Tests the handling of multiple modifications that include renumbering of the
// bookmark identifiers.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkRenameDelete) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* f1 = model->AddFolder(model->other_node(), 0, u"folder");
  model->AddURL(f1, 0, u"foo", GURL("http://www.foo.com"));
  model->SetTitle(f1, u"Renamed",
                  bookmarks::metrics::BookmarkEditSource::kOther);
  model->Remove(model->other_node()->children().front().get(),
                bookmarks::metrics::BookmarkEditSource::kOther, FROM_HERE);

  // Undo the folder removal and ensure the folder and bookmark were restored.
  undo_service->undo_manager()->Undo();
  ASSERT_EQ(1u, model->other_node()->children().size());
  ASSERT_EQ(1u, model->other_node()->children().front()->children().size());
  const BookmarkNode* node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"Renamed");

  node = model->other_node()->children().front()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));

  // Undo the title change and ensure the folder was updated even though the
  // id has changed.
  undo_service->undo_manager()->Undo();
  node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"folder");

  // Undo bookmark creation and test for removal of bookmark.
  undo_service->undo_manager()->Undo();
  ASSERT_EQ(0u, model->other_node()->children().front()->children().size());

  // Undo folder creation and confirm the bookmark model is empty.
  undo_service->undo_manager()->Undo();
  ASSERT_EQ(0u, model->other_node()->children().size());

  // Redo all the actions and ensure the folder and bookmark are restored.
  undo_service->undo_manager()->Redo(); // folder creation
  undo_service->undo_manager()->Redo(); // bookmark creation
  undo_service->undo_manager()->Redo(); // bookmark title change
  ASSERT_EQ(1u, model->other_node()->children().size());
  ASSERT_EQ(1u, model->other_node()->children().front()->children().size());
  node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"Renamed");
  node = model->other_node()->children().front()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));

  undo_service->undo_manager()->Redo(); // folder deletion
  EXPECT_EQ(0u, model->other_node()->children().size());
}

// Test the undo of SortChildren and ReorderChildren.
TEST_F(BookmarkUndoServiceTest, UndoBookmarkReorder) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  const BookmarkNode* parent = model->other_node();
  model->AddURL(parent, 0, u"foo", GURL("http://www.foo.com"));
  model->AddURL(parent, 1, u"moo", GURL("http://www.moo.com"));
  model->AddURL(parent, 2, u"bar", GURL("http://www.bar.com"));
  model->SortChildren(parent);

  // Test the undo of SortChildren.
  undo_service->undo_manager()->Undo();
  const BookmarkNode* node = parent->children()[0].get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));

  node = parent->children()[1].get();
  EXPECT_EQ(node->GetTitle(), u"moo");
  EXPECT_EQ(node->url(), GURL("http://www.moo.com"));

  node = parent->children()[2].get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  // Test the redo of SortChildren.
  undo_service->undo_manager()->Redo();
  node = parent->children()[0].get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  node = parent->children()[1].get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));

  node = parent->children()[2].get();
  EXPECT_EQ(node->GetTitle(), u"moo");
  EXPECT_EQ(node->url(), GURL("http://www.moo.com"));

}

TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemoveAll) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  // Setup bookmarks in the Other Bookmarks and the Bookmark Bar.
  const BookmarkNode* new_folder;
  const BookmarkNode* parent = model->other_node();
  model->AddURL(parent, 0, u"foo", GURL("http://www.google.com"));
  new_folder = model->AddFolder(parent, 1, u"folder");
  model->AddURL(new_folder, 0, u"bar", GURL("http://www.bar.com"));

  parent = model->bookmark_bar_node();
  model->AddURL(parent, 0, u"a", GURL("http://www.a.com"));
  new_folder = model->AddFolder(parent, 1, u"folder");
  model->AddURL(new_folder, 0, u"b", GURL("http://www.b.com"));

  model->RemoveAllUserBookmarks(FROM_HERE);

  // Test that the undo of RemoveAllUserBookmarks restores all folders and
  // bookmarks.
  undo_service->undo_manager()->Undo();

  ASSERT_EQ(2u, model->other_node()->children().size());
  EXPECT_EQ(1u, model->other_node()->children()[1]->children().size());
  const BookmarkNode* node =
      model->other_node()->children()[1]->children()[0].get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  ASSERT_EQ(2u, model->bookmark_bar_node()->children().size());
  EXPECT_EQ(1u, model->bookmark_bar_node()->children()[1]->children().size());
  node = model->bookmark_bar_node()->children()[1]->children()[0].get();
  EXPECT_EQ(node->GetTitle(), u"b");
  EXPECT_EQ(node->url(), GURL("http://www.b.com"));

  // Test that the redo removes all folders and bookmarks.
  undo_service->undo_manager()->Redo();
  EXPECT_EQ(0u, model->other_node()->children().size());
  EXPECT_EQ(0u, model->bookmark_bar_node()->children().size());
}

TEST_F(BookmarkUndoServiceTest, UndoRemoveFolderWithBookmarks) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  // Setup bookmarks in the Other Bookmarks.
  const BookmarkNode* new_folder;
  const BookmarkNode* parent = model->other_node();
  new_folder = model->AddFolder(parent, 0, u"folder");
  model->AddURL(new_folder, 0, u"bar", GURL("http://www.bar.com"));

  model->Remove(parent->children().front().get(),
                bookmarks::metrics::BookmarkEditSource::kOther, FROM_HERE);

  // Test that the undo restores the bookmark and folder.
  undo_service->undo_manager()->Undo();

  ASSERT_EQ(1u, model->other_node()->children().size());
  new_folder = model->other_node()->children().front().get();
  EXPECT_EQ(1u, new_folder->children().size());
  const BookmarkNode* node = new_folder->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));

  // Test that the redo restores the bookmark and folder.
  undo_service->undo_manager()->Redo();

  ASSERT_EQ(0u, model->other_node()->children().size());

  // Test that the undo after a redo restores the bookmark and folder.
  undo_service->undo_manager()->Undo();

  ASSERT_EQ(1u, model->other_node()->children().size());
  new_folder = model->other_node()->children().front().get();
  EXPECT_EQ(1u, new_folder->children().size());
  node = new_folder->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
}

TEST_F(BookmarkUndoServiceTest, UndoRemoveFolderWithSubfolders) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  // Setup bookmarks in the Other Bookmarks with the following structure:
  // folder
  //    subfolder1
  //    subfolder2
  //        bar - http://www.bar.com
  // This setup of multiple subfolders where the first subfolder has 0 children
  // is designed specifically to ensure we do not crash in this scenario and
  // that bookmarks are restored to the proper subfolder. See crbug.com/474123.
  const BookmarkNode* parent = model->other_node();
  const BookmarkNode* new_folder = model->AddFolder(parent, 0, u"folder");
  model->AddFolder(new_folder, 0, u"subfolder1");
  const BookmarkNode* sub_folder2 =
      model->AddFolder(new_folder, 1, u"subfolder2");
  model->AddURL(sub_folder2, 0, u"bar", GURL("http://www.bar.com"));

  model->Remove(parent->children()[0].get(),
                bookmarks::metrics::BookmarkEditSource::kOther, FROM_HERE);

  // Test that the undo restores the subfolders and their contents.
  undo_service->undo_manager()->Undo();

  ASSERT_EQ(1u, model->other_node()->children().size());
  const BookmarkNode* restored_new_folder =
      model->other_node()->children()[0].get();
  EXPECT_EQ(2u, restored_new_folder->children().size());

  const BookmarkNode* restored_sub_folder1 =
      restored_new_folder->children()[0].get();
  EXPECT_EQ(u"subfolder1", restored_sub_folder1->GetTitle());
  EXPECT_EQ(0u, restored_sub_folder1->children().size());

  const BookmarkNode* restored_sub_folder2 =
      restored_new_folder->children()[1].get();
  EXPECT_EQ(u"subfolder2", restored_sub_folder2->GetTitle());
  EXPECT_EQ(1u, restored_sub_folder2->children().size());

  const BookmarkNode* node = restored_sub_folder2->children()[0].get();
  EXPECT_EQ(node->GetTitle(), u"bar");
  EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
}

TEST_F(BookmarkUndoServiceTest, TestUpperLimit) {
  BookmarkModel* model = GetModel();
  BookmarkUndoService* undo_service = GetUndoService();

  // This maximum is set in undo_manager.cc
  const size_t kMaxUndoGroups = 100;

  const BookmarkNode* parent = model->other_node();
  model->AddURL(parent, 0, u"foo", GURL("http://www.foo.com"));
  for (size_t i = 1; i < kMaxUndoGroups + 1; ++i)
    model->AddURL(parent, i, u"bar", GURL("http://www.bar.com"));

  EXPECT_EQ(kMaxUndoGroups, undo_service->undo_manager()->undo_count());

  // Undo as many operations as possible.
  while (undo_service->undo_manager()->undo_count())
    undo_service->undo_manager()->Undo();

  EXPECT_EQ(1u, parent->children().size());
  const BookmarkNode* node = model->other_node()->children().front().get();
  EXPECT_EQ(node->GetTitle(), u"foo");
  EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
}

} // namespace