This source file includes following definitions.
- file_thread_
- SetUp
- RunMessageLoopForUI
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/history/android/bookmark_model_sql_handler.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.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/history/history_database.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace history {
using content::BrowserThread;
class BookmarkModelSQLHandlerTest : public testing::Test {
public:
BookmarkModelSQLHandlerTest()
: profile_manager_(
TestingBrowserProcess::GetGlobal()),
bookmark_model_(NULL),
ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_) {
}
virtual ~BookmarkModelSQLHandlerTest() {}
protected:
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(profile_manager_.SetUp());
TestingProfile* testing_profile = profile_manager_.CreateTestingProfile(
chrome::kInitialProfile);
testing_profile->CreateBookmarkModel(true);
bookmark_model_ = BookmarkModelFactory::GetForProfile(testing_profile);
test::WaitForBookmarkModelToLoad(bookmark_model_);
ASSERT_TRUE(bookmark_model_);
Profile* profile = ProfileManager::GetLastUsedProfile();
ASSERT_TRUE(profile);
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath history_db_name = temp_dir_.path().AppendASCII(
chrome::kHistoryFilename);
history_db_.Init(history_db_name);
}
void RunMessageLoopForUI() {
content::RunAllPendingInMessageLoop();
}
TestingProfileManager profile_manager_;
BookmarkModel* bookmark_model_;
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
base::ScopedTempDir temp_dir_;
HistoryDatabase history_db_;
};
TEST_F(BookmarkModelSQLHandlerTest, InsertIntoMobileFolder) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://bookmark.com");
row.set_url(GURL("http://bookmark.com"));
row.set_title(base::UTF8ToUTF16("Bookmark Title"));
row.set_is_bookmark(true);
BookmarkModelSQLHandler handler(&history_db_);
ASSERT_TRUE(handler.Insert(&row));
RunMessageLoopForUI();
std::vector<const BookmarkNode*> nodes;
bookmark_model_->GetNodesByURL(row.url(), &nodes);
ASSERT_EQ(1u, nodes.size());
EXPECT_EQ(row.title(), nodes[0]->GetTitle());
const BookmarkNode* parent = nodes[0]->parent();
ASSERT_TRUE(parent);
EXPECT_EQ(bookmark_model_->mobile_node()->id(), parent->id());
}
TEST_F(BookmarkModelSQLHandlerTest, InsertIntoSpecificFolder) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://bookmark.com");
row.set_url(GURL("http://bookmark.com"));
row.set_title(base::UTF8ToUTF16("Bookmark Title"));
row.set_is_bookmark(true);
row.set_parent_id(bookmark_model_->other_node()->id());
BookmarkModelSQLHandler handler(&history_db_);
ASSERT_TRUE(handler.Insert(&row));
RunMessageLoopForUI();
std::vector<const BookmarkNode*> nodes;
bookmark_model_->GetNodesByURL(row.url(), &nodes);
ASSERT_EQ(1u, nodes.size());
EXPECT_EQ(row.title(), nodes[0]->GetTitle());
const BookmarkNode* parent = nodes[0]->parent();
ASSERT_TRUE(parent);
EXPECT_EQ(row.parent_id(), parent->id());
}
TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) {
URLRow url_row(GURL("http://www.google.com"));
url_row.set_title(base::UTF8ToUTF16("Google"));
URLID url_id = history_db_.AddURL(url_row);
ASSERT_TRUE(url_id);
HistoryAndBookmarkRow row;
row.set_url(url_row.url());
row.set_is_bookmark(true);
TableIDRow id_row;
id_row.url_id = url_id;
id_row.url = url_row.url();
TableIDRows id_rows;
id_rows.push_back(id_row);
BookmarkModelSQLHandler handler(&history_db_);
ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI();
std::vector<BookmarkService::URLAndTitle> bookmarks;
bookmark_model_->GetBookmarks(&bookmarks);
ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
std::vector<const BookmarkNode*> nodes;
bookmark_model_->GetNodesByURL(row.url(), &nodes);
ASSERT_EQ(1u, nodes.size());
EXPECT_EQ(url_row.title(), nodes[0]->GetTitle());
const BookmarkNode* parent = nodes[0]->parent();
ASSERT_TRUE(parent);
EXPECT_EQ(bookmark_model_->mobile_node()->id(), parent->id());
row.set_is_bookmark(false);
ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI();
bookmarks.clear();
bookmark_model_->GetBookmarks(&bookmarks);
EXPECT_TRUE(bookmarks.empty());
row.set_parent_id(bookmark_model_->other_node()->id());
row.set_is_bookmark(true);
ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI();
bookmarks.clear();
bookmark_model_->GetBookmarks(&bookmarks);
ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
nodes.clear();
bookmark_model_->GetNodesByURL(row.url(), &nodes);
ASSERT_EQ(1u, nodes.size());
EXPECT_EQ(url_row.title(), nodes[0]->GetTitle());
const BookmarkNode* parent1 = nodes[0]->parent();
ASSERT_TRUE(parent1);
EXPECT_EQ(row.parent_id(), parent1->id());
url_row.set_title(base::UTF8ToUTF16("Google Inc."));
history_db_.UpdateURLRow(url_id, url_row);
HistoryAndBookmarkRow update_title;
update_title.set_title(url_row.title());
ASSERT_TRUE(handler.Update(update_title, id_rows));
RunMessageLoopForUI();
bookmarks.clear();
bookmark_model_->GetBookmarks(&bookmarks);
ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
nodes.clear();
bookmark_model_->GetNodesByURL(row.url(), &nodes);
ASSERT_EQ(1u, nodes.size());
EXPECT_EQ(url_row.title(), nodes[0]->GetTitle());
const BookmarkNode* parent2 = nodes[0]->parent();
ASSERT_TRUE(parent2);
EXPECT_EQ(row.parent_id(), parent2->id());
}
TEST_F(BookmarkModelSQLHandlerTest, Delete) {
HistoryAndBookmarkRow row;
GURL url1 = GURL("http://bookmark.com");
row.set_raw_url("http://bookmark.com");
row.set_url(url1);
row.set_title(base::UTF8ToUTF16("Bookmark Title"));
row.set_is_bookmark(true);
BookmarkModelSQLHandler handler(&history_db_);
ASSERT_TRUE(handler.Insert(&row));
row.set_parent_id(bookmark_model_->other_node()->id());
ASSERT_TRUE(handler.Insert(&row));
row.set_url(GURL("http://google.com"));
ASSERT_TRUE(handler.Insert(&row));
RunMessageLoopForUI();
EXPECT_EQ(1, bookmark_model_->mobile_node()->child_count());
EXPECT_EQ(2, bookmark_model_->other_node()->child_count());
TableIDRow id_row;
id_row.url = row.url();
TableIDRows id_rows;
id_rows.push_back(id_row);
ASSERT_TRUE(handler.Delete(id_rows));
RunMessageLoopForUI();
EXPECT_EQ(1, bookmark_model_->mobile_node()->child_count());
EXPECT_EQ(1, bookmark_model_->other_node()->child_count());
id_row.url = url1;
id_rows.clear();
id_rows.push_back(id_row);
ASSERT_TRUE(handler.Delete(id_rows));
RunMessageLoopForUI();
EXPECT_FALSE(bookmark_model_->HasBookmarks());
}
}