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_factory.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/importer/importer_unittest_utils.h"
14 #include "chrome/common/importer/imported_bookmark_entry.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/bookmarks/browser/bookmark_match.h"
17 #include "components/bookmarks/browser/bookmark_model.h"
18 #include "components/bookmarks/browser/bookmark_utils.h"
19 #include "components/bookmarks/test/bookmark_test_helpers.h"
20 #include "components/history/core/browser/history_service.h"
21 #include "components/history/core/browser/history_types.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using bookmarks::BookmarkMatch
;
26 using bookmarks::BookmarkModel
;
27 using content::BrowserThread
;
29 class TestProfileWriter
: public ProfileWriter
{
31 explicit TestProfileWriter(Profile
* profile
) : ProfileWriter(profile
) {}
33 ~TestProfileWriter() override
{}
36 class ProfileWriterTest
: public testing::Test
{
39 : ui_thread_(BrowserThread::UI
, &loop_
),
40 file_thread_(BrowserThread::FILE, &loop_
) {
42 ~ProfileWriterTest() override
{}
44 void TearDown() override
{
48 // Create test bookmark entries to be added to ProfileWriter to
49 // simulate bookmark importing.
50 void CreateImportedBookmarksEntries() {
51 AddImportedBookmarkEntry(GURL("http://www.google.com"),
52 base::ASCIIToUTF16("Google"));
53 AddImportedBookmarkEntry(GURL("http://www.yahoo.com"),
54 base::ASCIIToUTF16("Yahoo"));
57 // Helper function to create history entries.
58 history::URLRow
MakeURLRow(const char* url
,
61 int days_since_last_visit
,
63 history::URLRow
row(GURL(url
), 0);
65 row
.set_visit_count(visit_count
);
66 row
.set_typed_count(typed_count
);
67 row
.set_last_visit(base::Time::NowFromSystemTime() -
68 base::TimeDelta::FromDays(days_since_last_visit
));
72 // Create test history entries to be added to ProfileWriter to
73 // simulate history importing.
74 void CreateHistoryPageEntries() {
76 MakeURLRow("http://www.google.com", base::ASCIIToUTF16("Google"),
79 MakeURLRow("http://www.yahoo.com", base::ASCIIToUTF16("Yahoo"),
81 pages_
.push_back(row1
);
82 pages_
.push_back(row2
);
85 void VerifyBookmarksCount(
86 const std::vector
<BookmarkModel::URLAndTitle
>& bookmarks_record
,
87 BookmarkModel
* bookmark_model
,
89 std::vector
<BookmarkMatch
> matches
;
90 for (size_t i
= 0; i
< bookmarks_record
.size(); ++i
) {
91 bookmark_model
->GetBookmarksMatching(
92 bookmarks_record
[i
].title
, 10, &matches
);
93 EXPECT_EQ(expected
, matches
.size());
98 void VerifyHistoryCount(Profile
* profile
) {
99 history::HistoryService
* history_service
=
100 HistoryServiceFactory::GetForProfile(
101 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
102 history::QueryOptions options
;
103 base::CancelableTaskTracker history_task_tracker
;
104 history_service
->QueryHistory(
107 base::Bind(&ProfileWriterTest::HistoryQueryComplete
,
108 base::Unretained(this)),
109 &history_task_tracker
);
110 base::MessageLoop::current()->Run();
113 void HistoryQueryComplete(history::QueryResults
* results
) {
114 base::MessageLoop::current()->Quit();
115 history_count_
= results
->size();
119 std::vector
<ImportedBookmarkEntry
> bookmarks_
;
120 history::URLRows pages_
;
121 size_t history_count_
;
124 void AddImportedBookmarkEntry(const GURL
& url
, const base::string16
& title
) {
126 ImportedBookmarkEntry entry
;
127 entry
.creation_time
= date
;
130 entry
.in_toolbar
= true;
131 entry
.is_folder
= false;
132 bookmarks_
.push_back(entry
);
135 base::MessageLoop loop_
;
136 content::TestBrowserThread ui_thread_
;
137 content::TestBrowserThread file_thread_
;
139 DISALLOW_COPY_AND_ASSIGN(ProfileWriterTest
);
142 // Add bookmarks via ProfileWriter to profile1 when profile2 also exists.
143 TEST_F(ProfileWriterTest
, CheckBookmarksWithMultiProfile
) {
144 TestingProfile profile2
;
145 profile2
.CreateBookmarkModel(true);
147 BookmarkModel
* bookmark_model2
=
148 BookmarkModelFactory::GetForProfile(&profile2
);
149 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model2
);
150 bookmarks::AddIfNotBookmarked(
151 bookmark_model2
, GURL("http://www.bing.com"), base::ASCIIToUTF16("Bing"));
152 TestingProfile profile1
;
153 profile1
.CreateBookmarkModel(true);
155 CreateImportedBookmarksEntries();
156 BookmarkModel
* bookmark_model1
=
157 BookmarkModelFactory::GetForProfile(&profile1
);
158 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model1
);
160 scoped_refptr
<TestProfileWriter
> profile_writer(
161 new TestProfileWriter(&profile1
));
162 profile_writer
->AddBookmarks(bookmarks_
,
163 base::ASCIIToUTF16("Imported from Firefox"));
165 std::vector
<BookmarkModel::URLAndTitle
> url_record1
;
166 bookmark_model1
->GetBookmarks(&url_record1
);
167 EXPECT_EQ(2u, url_record1
.size());
169 std::vector
<BookmarkModel::URLAndTitle
> url_record2
;
170 bookmark_model2
->GetBookmarks(&url_record2
);
171 EXPECT_EQ(1u, url_record2
.size());
174 // Verify that bookmarks are duplicated when added twice.
175 TEST_F(ProfileWriterTest
, CheckBookmarksAfterWritingDataTwice
) {
176 TestingProfile profile
;
177 profile
.CreateBookmarkModel(true);
179 CreateImportedBookmarksEntries();
180 BookmarkModel
* bookmark_model
=
181 BookmarkModelFactory::GetForProfile(&profile
);
182 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model
);
184 scoped_refptr
<TestProfileWriter
> profile_writer(
185 new TestProfileWriter(&profile
));
186 profile_writer
->AddBookmarks(bookmarks_
,
187 base::ASCIIToUTF16("Imported from Firefox"));
188 std::vector
<BookmarkModel::URLAndTitle
> bookmarks_record
;
189 bookmark_model
->GetBookmarks(&bookmarks_record
);
190 EXPECT_EQ(2u, bookmarks_record
.size());
192 VerifyBookmarksCount(bookmarks_record
, bookmark_model
, 1);
194 profile_writer
->AddBookmarks(bookmarks_
,
195 base::ASCIIToUTF16("Imported from Firefox"));
196 // Verify that duplicate bookmarks exist.
197 VerifyBookmarksCount(bookmarks_record
, bookmark_model
, 2);
200 // Verify that history entires are not duplicated when added twice.
201 TEST_F(ProfileWriterTest
, CheckHistoryAfterWritingDataTwice
) {
202 TestingProfile profile
;
203 ASSERT_TRUE(profile
.CreateHistoryService(true, false));
204 profile
.BlockUntilHistoryProcessesPendingRequests();
206 CreateHistoryPageEntries();
207 scoped_refptr
<TestProfileWriter
> profile_writer(
208 new TestProfileWriter(&profile
));
209 profile_writer
->AddHistoryPage(pages_
, history::SOURCE_FIREFOX_IMPORTED
);
210 VerifyHistoryCount(&profile
);
211 size_t original_history_count
= history_count_
;
214 profile_writer
->AddHistoryPage(pages_
, history::SOURCE_FIREFOX_IMPORTED
);
215 VerifyHistoryCount(&profile
);
216 EXPECT_EQ(original_history_count
, history_count_
);