This source file includes following definitions.
- SetUp
- GetModel
- GetUndoService
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/undo/bookmark_undo_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
#include "chrome/browser/undo/bookmark_undo_service_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
namespace {
class BookmarkUndoServiceTest : public testing::Test {
public:
BookmarkUndoServiceTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
BookmarkModel* GetModel();
BookmarkUndoService* GetUndoService();
private:
scoped_ptr<TestingProfile> profile_;
content::TestBrowserThreadBundle thread_bundle_;
DISALLOW_COPY_AND_ASSIGN(BookmarkUndoServiceTest);
};
BookmarkUndoServiceTest::BookmarkUndoServiceTest() {}
void BookmarkUndoServiceTest::SetUp() {
profile_.reset(new TestingProfile);
profile_->CreateBookmarkModel(true);
test::WaitForBookmarkModelToLoad(GetModel());
}
BookmarkModel* BookmarkUndoServiceTest::GetModel() {
return BookmarkModelFactory::GetForProfile(profile_.get());
}
BookmarkUndoService* BookmarkUndoServiceTest::GetUndoService() {
return BookmarkUndoServiceFactory::GetForProfile(profile_.get());
}
void BookmarkUndoServiceTest::TearDown() {
profile_.reset(NULL);
}
TEST_F(BookmarkUndoServiceTest, AddBookmark) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* parent = model->other_node();
model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.bar.com"));
undo_service->undo_manager()->Undo();
EXPECT_EQ(0, model->other_node()->child_count());
undo_service->undo_manager()->Redo();
const BookmarkNode* node = parent->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemove) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* parent = model->other_node();
model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.bar.com"));
model->Remove(parent, 0);
EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());
undo_service->undo_manager()->Undo();
EXPECT_EQ(1, model->other_node()->child_count());
const BookmarkNode* node = parent->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("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());
undo_service->undo_manager()->Redo();
EXPECT_EQ(0, model->other_node()->child_count());
EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkGroupedAction) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* n1 = model->AddURL(model->other_node(),
0,
ASCIIToUTF16("foo"),
GURL("http://www.foo.com"));
undo_service->undo_manager()->StartGroupingActions();
model->SetTitle(n1, ASCIIToUTF16("bar"));
model->SetURL(n1, GURL("http://www.bar.com"));
undo_service->undo_manager()->EndGroupingActions();
EXPECT_EQ(2U, undo_service->undo_manager()->undo_count());
EXPECT_EQ(0U, undo_service->undo_manager()->redo_count());
undo_service->undo_manager()->Undo();
EXPECT_EQ(1, model->other_node()->child_count());
const BookmarkNode* node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
undo_service->undo_manager()->Redo();
EXPECT_EQ(1, model->other_node()->child_count());
node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("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_F(BookmarkUndoServiceTest, UndoBookmarkMoveWithinFolder) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* n1 = model->AddURL(model->other_node(),
0,
ASCIIToUTF16("foo"),
GURL("http://www.foo.com"));
const BookmarkNode* n2 = model->AddURL(model->other_node(),
1,
ASCIIToUTF16("moo"),
GURL("http://www.moo.com"));
const BookmarkNode* n3 = model->AddURL(model->other_node(),
2,
ASCIIToUTF16("bar"),
GURL("http://www.bar.com"));
model->Move(n1, model->other_node(), 3);
undo_service->undo_manager()->Undo();
EXPECT_EQ(model->other_node()->GetChild(0), n1);
EXPECT_EQ(model->other_node()->GetChild(1), n2);
EXPECT_EQ(model->other_node()->GetChild(2), n3);
undo_service->undo_manager()->Redo();
EXPECT_EQ(model->other_node()->GetChild(0), n2);
EXPECT_EQ(model->other_node()->GetChild(1), n3);
EXPECT_EQ(model->other_node()->GetChild(2), n1);
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkMoveToOtherFolder) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* n1 = model->AddURL(model->other_node(),
0,
ASCIIToUTF16("foo"),
GURL("http://www.foo.com"));
const BookmarkNode* n2 = model->AddURL(model->other_node(),
1,
ASCIIToUTF16("moo"),
GURL("http://www.moo.com"));
const BookmarkNode* n3 = model->AddURL(model->other_node(),
2,
ASCIIToUTF16("bar"),
GURL("http://www.bar.com"));
const BookmarkNode* f1 =
model->AddFolder(model->other_node(), 3, ASCIIToUTF16("folder"));
model->Move(n3, f1, 0);
undo_service->undo_manager()->Undo();
ASSERT_EQ(4, model->other_node()->child_count());
EXPECT_EQ(model->other_node()->GetChild(0), n1);
EXPECT_EQ(model->other_node()->GetChild(1), n2);
EXPECT_EQ(model->other_node()->GetChild(2), n3);
EXPECT_EQ(model->other_node()->GetChild(3), f1);
EXPECT_EQ(0, f1->child_count());
undo_service->undo_manager()->Redo();
ASSERT_EQ(3, model->other_node()->child_count());
EXPECT_EQ(model->other_node()->GetChild(0), n1);
EXPECT_EQ(model->other_node()->GetChild(1), n2);
EXPECT_EQ(model->other_node()->GetChild(2), f1);
ASSERT_EQ(1, f1->child_count());
EXPECT_EQ(f1->GetChild(0), n3);
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkRenameDelete) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* f1 = model->AddFolder(model->other_node(),
0,
ASCIIToUTF16("folder"));
model->AddURL(f1, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com"));
model->SetTitle(f1, ASCIIToUTF16("Renamed"));
model->Remove(model->other_node(), 0);
undo_service->undo_manager()->Undo();
ASSERT_EQ(1, model->other_node()->child_count());
ASSERT_EQ(1, model->other_node()->GetChild(0)->child_count());
const BookmarkNode* node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("Renamed"));
node = model->other_node()->GetChild(0)->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
undo_service->undo_manager()->Undo();
node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("folder"));
undo_service->undo_manager()->Undo();
ASSERT_EQ(0, model->other_node()->GetChild(0)->child_count());
undo_service->undo_manager()->Undo();
ASSERT_EQ(0, model->other_node()->child_count());
undo_service->undo_manager()->Redo();
undo_service->undo_manager()->Redo();
undo_service->undo_manager()->Redo();
ASSERT_EQ(1, model->other_node()->child_count());
ASSERT_EQ(1, model->other_node()->GetChild(0)->child_count());
node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("Renamed"));
node = model->other_node()->GetChild(0)->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
undo_service->undo_manager()->Redo();
EXPECT_EQ(0, model->other_node()->child_count());
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkReorder) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* parent = model->other_node();
model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com"));
model->AddURL(parent, 1, ASCIIToUTF16("moo"), GURL("http://www.moo.com"));
model->AddURL(parent, 2, ASCIIToUTF16("bar"), GURL("http://www.bar.com"));
model->SortChildren(parent);
undo_service->undo_manager()->Undo();
const BookmarkNode* node = parent->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
node = parent->GetChild(1);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("moo"));
EXPECT_EQ(node->url(), GURL("http://www.moo.com"));
node = parent->GetChild(2);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
undo_service->undo_manager()->Redo();
node = parent->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
node = parent->GetChild(1);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
node = parent->GetChild(2);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("moo"));
EXPECT_EQ(node->url(), GURL("http://www.moo.com"));
}
TEST_F(BookmarkUndoServiceTest, UndoBookmarkRemoveAll) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* new_folder;
const BookmarkNode* parent = model->other_node();
model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.google.com"));
new_folder= model->AddFolder(parent, 1, ASCIIToUTF16("folder"));
model->AddURL(new_folder, 0, ASCIIToUTF16("bar"), GURL("http://www.bar.com"));
parent = model->bookmark_bar_node();
model->AddURL(parent, 0, ASCIIToUTF16("a"), GURL("http://www.a.com"));
new_folder = model->AddFolder(parent, 1, ASCIIToUTF16("folder"));
model->AddURL(new_folder, 0, ASCIIToUTF16("b"), GURL("http://www.b.com"));
model->RemoveAll();
undo_service->undo_manager()->Undo();
ASSERT_EQ(2, model->other_node()->child_count());
EXPECT_EQ(1, model->other_node()->GetChild(1)->child_count());
const BookmarkNode* node = model->other_node()->GetChild(1)->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
ASSERT_EQ(2, model->bookmark_bar_node()->child_count());
EXPECT_EQ(1, model->bookmark_bar_node()->GetChild(1)->child_count());
node = model->bookmark_bar_node()->GetChild(1)->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("b"));
EXPECT_EQ(node->url(), GURL("http://www.b.com"));
undo_service->undo_manager()->Redo();
EXPECT_EQ(0, model->other_node()->child_count());
EXPECT_EQ(0, model->bookmark_bar_node()->child_count());
}
TEST_F(BookmarkUndoServiceTest, UndoRemoveFolderWithBookmarks) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const BookmarkNode* new_folder;
const BookmarkNode* parent = model->other_node();
new_folder = model->AddFolder(parent, 0, ASCIIToUTF16("folder"));
model->AddURL(new_folder, 0, ASCIIToUTF16("bar"), GURL("http://www.bar.com"));
model->Remove(parent, 0);
undo_service->undo_manager()->Undo();
ASSERT_EQ(1, model->other_node()->child_count());
new_folder = model->other_node()->GetChild(0);
EXPECT_EQ(1, new_folder->child_count());
const BookmarkNode* node = new_folder->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
undo_service->undo_manager()->Redo();
ASSERT_EQ(0, model->other_node()->child_count());
undo_service->undo_manager()->Undo();
ASSERT_EQ(1, model->other_node()->child_count());
new_folder = model->other_node()->GetChild(0);
EXPECT_EQ(1, new_folder->child_count());
node = new_folder->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("bar"));
EXPECT_EQ(node->url(), GURL("http://www.bar.com"));
}
TEST_F(BookmarkUndoServiceTest, TestUpperLimit) {
BookmarkModel* model = GetModel();
BookmarkUndoService* undo_service = GetUndoService();
model->AddObserver(undo_service);
const size_t kMaxUndoGroups = 100;
const BookmarkNode* parent = model->other_node();
model->AddURL(parent, 0, ASCIIToUTF16("foo"), GURL("http://www.foo.com"));
for (size_t i = 1; i < kMaxUndoGroups + 1; ++i)
model->AddURL(parent, i, ASCIIToUTF16("bar"), GURL("http://www.bar.com"));
EXPECT_EQ(kMaxUndoGroups, undo_service->undo_manager()->undo_count());
while (undo_service->undo_manager()->undo_count())
undo_service->undo_manager()->Undo();
EXPECT_EQ(1, parent->child_count());
const BookmarkNode* node = model->other_node()->GetChild(0);
EXPECT_EQ(node->GetTitle(), ASCIIToUTF16("foo"));
EXPECT_EQ(node->url(), GURL("http://www.foo.com"));
}
}