Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / importer / profile_writer_unittest.cc
bloba6611eb441744a3a0e691ce529d215d27bf9f819
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"
7 #include <string>
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 {
30 public:
31 explicit TestProfileWriter(Profile* profile) : ProfileWriter(profile) {}
32 protected:
33 ~TestProfileWriter() override {}
36 class ProfileWriterTest : public testing::Test {
37 public:
38 ProfileWriterTest()
39 : ui_thread_(BrowserThread::UI, &loop_),
40 file_thread_(BrowserThread::FILE, &loop_) {
42 ~ProfileWriterTest() override {}
44 // Create test bookmark entries to be added to ProfileWriter to
45 // simulate bookmark importing.
46 void CreateImportedBookmarksEntries() {
47 AddImportedBookmarkEntry(GURL("http://www.google.com"),
48 base::ASCIIToUTF16("Google"));
49 AddImportedBookmarkEntry(GURL("http://www.yahoo.com"),
50 base::ASCIIToUTF16("Yahoo"));
53 // Helper function to create history entries.
54 history::URLRow MakeURLRow(const char* url,
55 base::string16 title,
56 int visit_count,
57 int days_since_last_visit,
58 int typed_count) {
59 history::URLRow row(GURL(url), 0);
60 row.set_title(title);
61 row.set_visit_count(visit_count);
62 row.set_typed_count(typed_count);
63 row.set_last_visit(base::Time::NowFromSystemTime() -
64 base::TimeDelta::FromDays(days_since_last_visit));
65 return row;
68 // Create test history entries to be added to ProfileWriter to
69 // simulate history importing.
70 void CreateHistoryPageEntries() {
71 history::URLRow row1(
72 MakeURLRow("http://www.google.com", base::ASCIIToUTF16("Google"),
73 3, 10, 1));
74 history::URLRow row2(
75 MakeURLRow("http://www.yahoo.com", base::ASCIIToUTF16("Yahoo"),
76 3, 30, 10));
77 pages_.push_back(row1);
78 pages_.push_back(row2);
81 void VerifyBookmarksCount(
82 const std::vector<BookmarkModel::URLAndTitle>& bookmarks_record,
83 BookmarkModel* bookmark_model,
84 size_t expected) {
85 std::vector<BookmarkMatch> matches;
86 for (size_t i = 0; i < bookmarks_record.size(); ++i) {
87 bookmark_model->GetBookmarksMatching(
88 bookmarks_record[i].title, 10, &matches);
89 EXPECT_EQ(expected, matches.size());
90 matches.clear();
94 void VerifyHistoryCount(Profile* profile) {
95 history::HistoryService* history_service =
96 HistoryServiceFactory::GetForProfile(
97 profile, ServiceAccessType::EXPLICIT_ACCESS);
98 history::QueryOptions options;
99 base::CancelableTaskTracker history_task_tracker;
100 history_service->QueryHistory(
101 base::string16(),
102 options,
103 base::Bind(&ProfileWriterTest::HistoryQueryComplete,
104 base::Unretained(this)),
105 &history_task_tracker);
106 base::MessageLoop::current()->Run();
109 void HistoryQueryComplete(history::QueryResults* results) {
110 base::MessageLoop::current()->Quit();
111 history_count_ = results->size();
114 protected:
115 std::vector<ImportedBookmarkEntry> bookmarks_;
116 history::URLRows pages_;
117 size_t history_count_;
119 private:
120 void AddImportedBookmarkEntry(const GURL& url, const base::string16& title) {
121 base::Time date;
122 ImportedBookmarkEntry entry;
123 entry.creation_time = date;
124 entry.url = url;
125 entry.title = title;
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 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model2);
146 bookmarks::AddIfNotBookmarked(
147 bookmark_model2, GURL("http://www.bing.com"), base::ASCIIToUTF16("Bing"));
148 TestingProfile profile1;
149 profile1.CreateBookmarkModel(true);
151 CreateImportedBookmarksEntries();
152 BookmarkModel* bookmark_model1 =
153 BookmarkModelFactory::GetForProfile(&profile1);
154 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model1);
156 scoped_refptr<TestProfileWriter> profile_writer(
157 new TestProfileWriter(&profile1));
158 profile_writer->AddBookmarks(bookmarks_,
159 base::ASCIIToUTF16("Imported from Firefox"));
161 std::vector<BookmarkModel::URLAndTitle> url_record1;
162 bookmark_model1->GetBookmarks(&url_record1);
163 EXPECT_EQ(2u, url_record1.size());
165 std::vector<BookmarkModel::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 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
180 scoped_refptr<TestProfileWriter> profile_writer(
181 new TestProfileWriter(&profile));
182 profile_writer->AddBookmarks(bookmarks_,
183 base::ASCIIToUTF16("Imported from Firefox"));
184 std::vector<BookmarkModel::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 base::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_;
208 history_count_ = 0;
210 profile_writer->AddHistoryPage(pages_, history::SOURCE_FIREFOX_IMPORTED);
211 VerifyHistoryCount(&profile);
212 EXPECT_EQ(original_history_count, history_count_);