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 : ui_thread_(BrowserThread::UI
, &loop_
),
38 file_thread_(BrowserThread::FILE, &loop_
) {
40 virtual ~ProfileWriterTest() {}
42 // Create test bookmark entries to be added to ProfileWriter to
43 // simulate bookmark importing.
44 void CreateImportedBookmarksEntries() {
45 AddImportedBookmarkEntry(GURL("http://www.google.com"),
46 base::ASCIIToUTF16("Google"));
47 AddImportedBookmarkEntry(GURL("http://www.yahoo.com"),
48 base::ASCIIToUTF16("Yahoo"));
51 // Helper function to create history entries.
52 history::URLRow
MakeURLRow(const char* url
,
55 int days_since_last_visit
,
57 history::URLRow
row(GURL(url
), 0);
59 row
.set_visit_count(visit_count
);
60 row
.set_typed_count(typed_count
);
61 row
.set_last_visit(base::Time::NowFromSystemTime() -
62 base::TimeDelta::FromDays(days_since_last_visit
));
66 // Create test history entries to be added to ProfileWriter to
67 // simulate history importing.
68 void CreateHistoryPageEntries() {
70 MakeURLRow("http://www.google.com", base::ASCIIToUTF16("Google"),
73 MakeURLRow("http://www.yahoo.com", base::ASCIIToUTF16("Yahoo"),
75 pages_
.push_back(row1
);
76 pages_
.push_back(row2
);
79 void VerifyBookmarksCount(
80 const std::vector
<BookmarkService::URLAndTitle
>& bookmarks_record
,
81 BookmarkModel
* bookmark_model
,
83 std::vector
<BookmarkTitleMatch
> matches
;
84 for (size_t i
= 0; i
< bookmarks_record
.size(); ++i
) {
85 bookmark_model
->GetBookmarksWithTitlesMatching(bookmarks_record
[i
].title
,
88 EXPECT_EQ(expected
, matches
.size());
93 void VerifyHistoryCount(Profile
* profile
) {
94 HistoryService
* history_service
=
95 HistoryServiceFactory::GetForProfile(profile
,
96 Profile::EXPLICIT_ACCESS
);
97 history::QueryOptions options
;
98 CancelableRequestConsumer history_request_consumer
;
99 history_service
->QueryHistory(
102 &history_request_consumer
,
103 base::Bind(&ProfileWriterTest::HistoryQueryComplete
,
104 base::Unretained(this)));
105 base::MessageLoop::current()->Run();
108 void HistoryQueryComplete(HistoryService::Handle handle
,
109 history::QueryResults
* results
) {
110 base::MessageLoop::current()->Quit();
111 history_count_
= results
->size();
115 std::vector
<ImportedBookmarkEntry
> bookmarks_
;
116 history::URLRows pages_
;
117 size_t history_count_
;
120 void AddImportedBookmarkEntry(const GURL
& url
, const base::string16
& title
) {
122 ImportedBookmarkEntry entry
;
123 entry
.creation_time
= date
;
126 entry
.in_toolbar
= true;
127 entry
.is_folder
= false;
128 bookmarks_
.push_back(entry
);
131 base::MessageLoop loop_
;
132 content::TestBrowserThread ui_thread_
;
133 content::TestBrowserThread file_thread_
;
135 DISALLOW_COPY_AND_ASSIGN(ProfileWriterTest
);
138 // Add bookmarks via ProfileWriter to profile1 when profile2 also exists.
139 TEST_F(ProfileWriterTest
, CheckBookmarksWithMultiProfile
) {
140 TestingProfile profile2
;
141 profile2
.CreateBookmarkModel(true);
143 BookmarkModel
* bookmark_model2
=
144 BookmarkModelFactory::GetForProfile(&profile2
);
145 test::WaitForBookmarkModelToLoad(bookmark_model2
);
146 bookmark_utils::AddIfNotBookmarked(bookmark_model2
,
147 GURL("http://www.bing.com"),
148 base::ASCIIToUTF16("Bing"));
149 TestingProfile profile1
;
150 profile1
.CreateBookmarkModel(true);
152 CreateImportedBookmarksEntries();
153 BookmarkModel
* bookmark_model1
=
154 BookmarkModelFactory::GetForProfile(&profile1
);
155 test::WaitForBookmarkModelToLoad(bookmark_model1
);
157 scoped_refptr
<TestProfileWriter
> profile_writer(
158 new TestProfileWriter(&profile1
));
159 profile_writer
->AddBookmarks(bookmarks_
,
160 base::ASCIIToUTF16("Imported from Firefox"));
162 std::vector
<BookmarkService::URLAndTitle
> url_record1
;
163 bookmark_model1
->GetBookmarks(&url_record1
);
164 EXPECT_EQ(2u, url_record1
.size());
166 std::vector
<BookmarkService::URLAndTitle
> url_record2
;
167 bookmark_model2
->GetBookmarks(&url_record2
);
168 EXPECT_EQ(1u, url_record2
.size());
171 // Verify that bookmarks are duplicated when added twice.
172 TEST_F(ProfileWriterTest
, CheckBookmarksAfterWritingDataTwice
) {
173 TestingProfile profile
;
174 profile
.CreateBookmarkModel(true);
176 CreateImportedBookmarksEntries();
177 BookmarkModel
* bookmark_model
=
178 BookmarkModelFactory::GetForProfile(&profile
);
179 test::WaitForBookmarkModelToLoad(bookmark_model
);
181 scoped_refptr
<TestProfileWriter
> profile_writer(
182 new TestProfileWriter(&profile
));
183 profile_writer
->AddBookmarks(bookmarks_
,
184 base::ASCIIToUTF16("Imported from Firefox"));
185 std::vector
<BookmarkService::URLAndTitle
> bookmarks_record
;
186 bookmark_model
->GetBookmarks(&bookmarks_record
);
187 EXPECT_EQ(2u, bookmarks_record
.size());
189 VerifyBookmarksCount(bookmarks_record
, bookmark_model
, 1);
191 profile_writer
->AddBookmarks(bookmarks_
,
192 base::ASCIIToUTF16("Imported from Firefox"));
193 // Verify that duplicate bookmarks exist.
194 VerifyBookmarksCount(bookmarks_record
, bookmark_model
, 2);
197 // Verify that history entires are not duplicated when added twice.
198 TEST_F(ProfileWriterTest
, CheckHistoryAfterWritingDataTwice
) {
199 TestingProfile profile
;
200 ASSERT_TRUE(profile
.CreateHistoryService(true, false));
201 profile
.BlockUntilHistoryProcessesPendingRequests();
203 CreateHistoryPageEntries();
204 scoped_refptr
<TestProfileWriter
> profile_writer(
205 new TestProfileWriter(&profile
));
206 profile_writer
->AddHistoryPage(pages_
, history::SOURCE_FIREFOX_IMPORTED
);
207 VerifyHistoryCount(&profile
);
208 size_t original_history_count
= history_count_
;
211 profile_writer
->AddHistoryPage(pages_
, history::SOURCE_FIREFOX_IMPORTED
);
212 VerifyHistoryCount(&profile
);
213 EXPECT_EQ(original_history_count
, history_count_
);