This source file includes following definitions.
- grouped_changes_ended_count_
- TearDown
- ExpectGroupedChangeCount
- BookmarkModelChanged
- GroupedBookmarkChangesBeginning
- GroupedBookmarkChangesEnded
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include <vector>
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_node_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
using base::ASCIIToUTF16;
using std::string;
namespace bookmark_utils {
namespace {
class BookmarkUtilsTest : public testing::Test,
public BaseBookmarkModelObserver {
public:
BookmarkUtilsTest()
: grouped_changes_beginning_count_(0),
grouped_changes_ended_count_(0) {}
virtual ~BookmarkUtilsTest() {}
virtual void TearDown() OVERRIDE {
ui::Clipboard::DestroyClipboardForCurrentThread();
}
void ExpectGroupedChangeCount(int expected_beginning_count,
int expected_ended_count) {
#if !defined(OS_ANDROID)
EXPECT_EQ(grouped_changes_beginning_count_, expected_beginning_count);
EXPECT_EQ(grouped_changes_ended_count_, expected_ended_count);
#endif
}
private:
virtual void BookmarkModelChanged() OVERRIDE {}
virtual void GroupedBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE {
++grouped_changes_beginning_count_;
}
virtual void GroupedBookmarkChangesEnded(BookmarkModel* model) OVERRIDE {
++grouped_changes_ended_count_;
}
int grouped_changes_beginning_count_;
int grouped_changes_ended_count_;
base::MessageLoopForUI loop_;
DISALLOW_COPY_AND_ASSIGN(BookmarkUtilsTest);
};
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) {
BookmarkModel model(NULL);
const BookmarkNode* node1 = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("foo bar"),
GURL("http://www.google.com"));
const BookmarkNode* node2 = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("baz buz"),
GURL("http://www.cnn.com"));
const BookmarkNode* folder1 = model.AddFolder(model.other_node(),
0,
ASCIIToUTF16("foo"));
std::vector<const BookmarkNode*> nodes;
QueryFields query;
query.word_phrase_query.reset(new base::string16);
*query.word_phrase_query = ASCIIToUTF16("");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
EXPECT_TRUE(nodes.empty());
nodes.clear();
*query.word_phrase_query = ASCIIToUTF16(" ");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
EXPECT_TRUE(nodes.empty());
nodes.clear();
*query.word_phrase_query = ASCIIToUTF16("foo");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(2U, nodes.size());
EXPECT_TRUE(nodes[0] == folder1);
EXPECT_TRUE(nodes[1] == node1);
nodes.clear();
*query.word_phrase_query = ASCIIToUTF16("cnn");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node2);
nodes.clear();
*query.word_phrase_query = ASCIIToUTF16("foo bar");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node1);
nodes.clear();
*query.word_phrase_query = ASCIIToUTF16("Bookmark");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(0U, nodes.size());
nodes.clear();
}
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesUrl) {
BookmarkModel model(NULL);
const BookmarkNode* node1 = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google"),
GURL("https://www.google.com/"));
model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google Calendar"),
GURL("https://www.google.com/calendar"));
model.AddFolder(model.other_node(), 0, ASCIIToUTF16("Folder"));
std::vector<const BookmarkNode*> nodes;
QueryFields query;
query.url.reset(new base::string16);
*query.url = ASCIIToUTF16("https://www.google.com/");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node1);
nodes.clear();
*query.url = ASCIIToUTF16("calendar");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(0U, nodes.size());
nodes.clear();
*query.url = ASCIIToUTF16("");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(0U, nodes.size());
nodes.clear();
}
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesTitle) {
BookmarkModel model(NULL);
const BookmarkNode* node1 = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google"),
GURL("https://www.google.com/"));
model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google Calendar"),
GURL("https://www.google.com/calendar"));
const BookmarkNode* folder1 =
model.AddFolder(model.other_node(), 0, ASCIIToUTF16("Folder"));
std::vector<const BookmarkNode*> nodes;
QueryFields query;
query.title.reset(new base::string16);
*query.title = ASCIIToUTF16("Google");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node1);
nodes.clear();
*query.title = ASCIIToUTF16("Calendar");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(0U, nodes.size());
nodes.clear();
*query.title = ASCIIToUTF16("Folder");
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == folder1);
nodes.clear();
}
TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesConjunction) {
BookmarkModel model(NULL);
const BookmarkNode* node1 = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google"),
GURL("https://www.google.com/"));
model.AddURL(model.other_node(),
0,
ASCIIToUTF16("Google Calendar"),
GURL("https://www.google.com/calendar"));
model.AddFolder(model.other_node(), 0, ASCIIToUTF16("Folder"));
std::vector<const BookmarkNode*> nodes;
QueryFields query;
query.word_phrase_query.reset(new base::string16(ASCIIToUTF16("www")));
query.url.reset(new base::string16(ASCIIToUTF16("https://www.google.com/")));
query.title.reset(new base::string16(ASCIIToUTF16("Google")));
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node1);
nodes.clear();
scoped_ptr<base::string16>* fields[] = {
&query.word_phrase_query, &query.url, &query.title };
for (size_t i = 0; i < arraysize(fields); i++) {
scoped_ptr<base::string16> original_value(fields[i]->release());
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(1U, nodes.size());
EXPECT_TRUE(nodes[0] == node1);
nodes.clear();
fields[i]->reset(original_value.release());
}
for (size_t i = 0; i < arraysize(fields); i++) {
scoped_ptr<base::string16> original_value(fields[i]->release());
fields[i]->reset(new base::string16(ASCIIToUTF16("fjdkslafjkldsa")));
GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
ASSERT_EQ(0U, nodes.size());
nodes.clear();
fields[i]->reset(original_value.release());
}
}
TEST_F(BookmarkUtilsTest, CopyPaste) {
BookmarkModel model(NULL);
const BookmarkNode* node = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("foo bar"),
GURL("http://www.google.com"));
std::vector<const BookmarkNode*> nodes;
nodes.push_back(node);
CopyToClipboard(&model, nodes, false);
EXPECT_TRUE(CanPasteFromClipboard(model.bookmark_bar_node()));
{
ui::ScopedClipboardWriter clipboard_writer(
ui::Clipboard::GetForCurrentThread(),
ui::CLIPBOARD_TYPE_COPY_PASTE);
clipboard_writer.WriteText(ASCIIToUTF16("foo"));
}
EXPECT_FALSE(CanPasteFromClipboard(model.bookmark_bar_node()));
}
TEST_F(BookmarkUtilsTest, CutToClipboard) {
BookmarkModel model(NULL);
model.AddObserver(this);
base::string16 title(ASCIIToUTF16("foo"));
GURL url("http://foo.com");
const BookmarkNode* n1 = model.AddURL(model.other_node(), 0, title, url);
const BookmarkNode* n2 = model.AddURL(model.other_node(), 1, title, url);
std::vector<const BookmarkNode*> nodes;
nodes.push_back(n1);
nodes.push_back(n2);
CopyToClipboard(&model, nodes, true);
EXPECT_EQ(0, model.other_node()->child_count());
ExpectGroupedChangeCount(1, 1);
EXPECT_TRUE(CanPasteFromClipboard(model.other_node()));
}
TEST_F(BookmarkUtilsTest, GetParentForNewNodes) {
BookmarkModel model(NULL);
std::vector<const BookmarkNode*> nodes;
nodes.push_back(model.bookmark_bar_node());
int index = -1;
const BookmarkNode* real_parent = GetParentForNewNodes(
model.bookmark_bar_node(), nodes, &index);
EXPECT_EQ(real_parent, model.bookmark_bar_node());
EXPECT_EQ(0, index);
nodes.clear();
const BookmarkNode* page1 = model.AddURL(model.bookmark_bar_node(),
0,
ASCIIToUTF16("Google"),
GURL("http://google.com"));
nodes.push_back(page1);
real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
EXPECT_EQ(real_parent, model.bookmark_bar_node());
EXPECT_EQ(1, index);
const BookmarkNode* folder1 = model.AddFolder(model.bookmark_bar_node(), 1,
ASCIIToUTF16("Folder 1"));
nodes.push_back(folder1);
real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
EXPECT_EQ(real_parent, model.bookmark_bar_node());
EXPECT_EQ(2, index);
nodes.clear();
real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
EXPECT_EQ(real_parent, model.bookmark_bar_node());
EXPECT_EQ(2, index);
}
TEST_F(BookmarkUtilsTest, CloneMetaInfo) {
BookmarkModel model(NULL);
const BookmarkNode* node = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("foo bar"),
GURL("http://www.google.com"));
model.SetNodeMetaInfo(node, "somekey", "somevalue");
model.SetNodeMetaInfo(node, "someotherkey", "someothervalue");
const BookmarkNode* folder = model.AddFolder(model.bookmark_bar_node(), 0,
ASCIIToUTF16("Folder"));
std::vector<BookmarkNodeData::Element> elements;
BookmarkNodeData::Element node_data(node);
elements.push_back(node_data);
EXPECT_EQ(0, folder->child_count());
CloneBookmarkNode(&model, elements, folder, 0, false);
ASSERT_EQ(1, folder->child_count());
const BookmarkNode* clone = folder->GetChild(0);
ASSERT_TRUE(clone->GetMetaInfoMap());
EXPECT_EQ(2u, clone->GetMetaInfoMap()->size());
std::string value;
EXPECT_TRUE(clone->GetMetaInfo("somekey", &value));
EXPECT_EQ("somevalue", value);
EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value));
EXPECT_EQ("someothervalue", value);
}
}
}