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.
5 #include "chrome/browser/importer/profile_writer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
13 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
14 #include "chrome/browser/bookmarks/bookmark_title_match.h"
15 #include "chrome/browser/bookmarks/bookmark_utils.h"
16 #include "chrome/browser/history/history_service.h"
17 #include "chrome/browser/history/history_service_factory.h"
18 #include "chrome/browser/history/history_types.h"
19 #include "chrome/browser/importer/importer_unittest_utils.h"
20 #include "chrome/common/importer/imported_bookmark_entry.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using content::BrowserThread;
27 class TestProfileWriter : public ProfileWriter {
29 explicit TestProfileWriter(Profile* profile) : ProfileWriter(profile) {}
31 virtual ~TestProfileWriter() {}
34 class ProfileWriterTest : public testing::Test {
37 : loop_(base::MessageLoop::TYPE_DEFAULT),
38 ui_thread_(BrowserThread::UI, &loop_),
39 file_thread_(BrowserThread::FILE, &loop_) {
41 virtual ~ProfileWriterTest() {}
43 // Create test bookmark entries to be added to ProfileWriter to
44 // simulate bookmark importing.
45 void CreateImportedBookmarksEntries() {
46 AddImportedBookmarkEntry(GURL("http://www.google.com"),
47 ASCIIToUTF16("Google"));
48 AddImportedBookmarkEntry(GURL("http://www.yahoo.com"),
49 ASCIIToUTF16("Yahoo"));
52 // Helper function to create history entries.
53 history::URLRow MakeURLRow(const char* url,
56 int days_since_last_visit,
58 history::URLRow row(GURL(url), 0);
60 row.set_visit_count(visit_count);
61 row.set_typed_count(typed_count);
62 row.set_last_visit(base::Time::NowFromSystemTime() -
63 base::TimeDelta::FromDays(days_since_last_visit));
67 // Create test history entries to be added to ProfileWriter to
68 // simulate history importing.
69 void CreateHistoryPageEntries() {
71 MakeURLRow("http://www.google.com", ASCIIToUTF16("Google"), 3, 10, 1));
73 MakeURLRow("http://www.yahoo.com", ASCIIToUTF16("Yahoo"), 3, 30, 10));
74 pages_.push_back(row1);
75 pages_.push_back(row2);
78 void VerifyBookmarksCount(
79 const std::vector<BookmarkService::URLAndTitle>& bookmarks_record,
80 BookmarkModel* bookmark_model,
82 std::vector<BookmarkTitleMatch> matches;
83 for (size_t i = 0; i < bookmarks_record.size(); ++i) {
84 bookmark_model->GetBookmarksWithTitlesMatching(bookmarks_record[i].title,
87 EXPECT_EQ(expected, matches.size());
92 void VerifyHistoryCount(Profile* profile) {
93 HistoryService* history_service =
94 HistoryServiceFactory::GetForProfile(profile,
95 Profile::EXPLICIT_ACCESS);
96 history::QueryOptions options;
97 CancelableRequestConsumer history_request_consumer;
98 history_service->QueryHistory(
101 &history_request_consumer,
102 base::Bind(&ProfileWriterTest::HistoryQueryComplete,
103 base::Unretained(this)));
104 base::MessageLoop::current()->Run();
107 void HistoryQueryComplete(HistoryService::Handle handle,
108 history::QueryResults* results) {
109 base::MessageLoop::current()->Quit();
110 history_count_ = results->size();
114 std::vector<ImportedBookmarkEntry> bookmarks_;
115 history::URLRows pages_;
116 size_t history_count_;
119 void AddImportedBookmarkEntry(const GURL& url, const string16& title) {
121 ImportedBookmarkEntry entry;
122 entry.creation_time = date;
125 entry.in_toolbar = true;
126 entry.is_folder = false;
127 bookmarks_.push_back(entry);
130 base::MessageLoop loop_;
131 content::TestBrowserThread ui_thread_;
132 content::TestBrowserThread file_thread_;
134 DISALLOW_COPY_AND_ASSIGN(ProfileWriterTest);
137 // Add bookmarks via ProfileWriter to profile1 when profile2 also exists.
138 TEST_F(ProfileWriterTest, CheckBookmarksWithMultiProfile) {
139 TestingProfile profile2;
140 profile2.CreateBookmarkModel(true);
142 BookmarkModel* bookmark_model2 =
143 BookmarkModelFactory::GetForProfile(&profile2);
144 test::WaitForBookmarkModelToLoad(bookmark_model2);
145 bookmark_utils::AddIfNotBookmarked(bookmark_model2,
146 GURL("http://www.bing.com"),
147 ASCIIToUTF16("Bing"));
148 TestingProfile profile1;
149 profile1.CreateBookmarkModel(true);
151 CreateImportedBookmarksEntries();
152 BookmarkModel* bookmark_model1 =
153 BookmarkModelFactory::GetForProfile(&profile1);
154 test::WaitForBookmarkModelToLoad(bookmark_model1);
156 scoped_refptr<TestProfileWriter> profile_writer(
157 new TestProfileWriter(&profile1));
158 profile_writer->AddBookmarks(bookmarks_,
159 ASCIIToUTF16("Imported from Firefox"));
161 std::vector<BookmarkService::URLAndTitle> url_record1;
162 bookmark_model1->GetBookmarks(&url_record1);
163 EXPECT_EQ(2u, url_record1.size());
165 std::vector<BookmarkService::URLAndTitle> url_record2;
166 bookmark_model2->GetBookmarks(&url_record2);
167 EXPECT_EQ(1u, url_record2.size());
170 // Verify that bookmarks are duplicated when added twice.
171 TEST_F(ProfileWriterTest, CheckBookmarksAfterWritingDataTwice) {
172 TestingProfile profile;
173 profile.CreateBookmarkModel(true);
175 CreateImportedBookmarksEntries();
176 BookmarkModel* bookmark_model =
177 BookmarkModelFactory::GetForProfile(&profile);
178 test::WaitForBookmarkModelToLoad(bookmark_model);
180 scoped_refptr<TestProfileWriter> profile_writer(
181 new TestProfileWriter(&profile));
182 profile_writer->AddBookmarks(bookmarks_,
183 ASCIIToUTF16("Imported from Firefox"));
184 std::vector<BookmarkService::URLAndTitle> bookmarks_record;
185 bookmark_model->GetBookmarks(&bookmarks_record);
186 EXPECT_EQ(2u, bookmarks_record.size());
188 VerifyBookmarksCount(bookmarks_record, bookmark_model, 1);
190 profile_writer->AddBookmarks(bookmarks_,
191 ASCIIToUTF16("Imported from Firefox"));
192 // Verify that duplicate bookmarks exist.
193 VerifyBookmarksCount(bookmarks_record, bookmark_model, 2);
196 // Verify that history entires are not duplicated when added twice.
197 TEST_F(ProfileWriterTest, CheckHistoryAfterWritingDataTwice) {
198 TestingProfile profile;
199 ASSERT_TRUE(profile.CreateHistoryService(true, false));
200 profile.BlockUntilHistoryProcessesPendingRequests();
202 CreateHistoryPageEntries();
203 scoped_refptr<TestProfileWriter> profile_writer(
204 new TestProfileWriter(&profile));
205 profile_writer->AddHistoryPage(pages_, history::SOURCE_FIREFOX_IMPORTED);
206 VerifyHistoryCount(&profile);
207 size_t original_history_count = history_count_;
210 profile_writer->AddHistoryPage(pages_, history::SOURCE_FIREFOX_IMPORTED);
211 VerifyHistoryCount(&profile);
212 EXPECT_EQ(original_history_count, history_count_);