Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/utility/importer/bookmark_html_reader_unittest.cc

Issue 616763002: Importing certain bookmarks from firefox and HTML file as search engines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed more feedback. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/utility/importer/bookmark_html_reader.h" 5 #include "chrome/utility/importer/bookmark_html_reader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 // Epiphany format. 131 // Epiphany format.
132 result = internal::ParseMinimumBookmarkFromLine( 132 result = internal::ParseMinimumBookmarkFromLine(
133 "<dt><a href=\"http://www.google.com/\">Google</a></dt>", 133 "<dt><a href=\"http://www.google.com/\">Google</a></dt>",
134 charset, &title, &url); 134 charset, &title, &url);
135 EXPECT_TRUE(result); 135 EXPECT_TRUE(result);
136 EXPECT_EQ(ASCIIToUTF16("Google"), title); 136 EXPECT_EQ(ASCIIToUTF16("Google"), title);
137 EXPECT_EQ("http://www.google.com/", url.spec()); 137 EXPECT_EQ("http://www.google.com/", url.spec());
138 } 138 }
139 139
140 TEST(BookmarkHTMLReaderTest, CanImportURLAsSearchEngineTest) {
141 struct TestCase {
142 const std::string url;
143 const bool can_be_imported_as_search_engine;
144 } test_cases[] = {
145 { "http://www.example.%s.com", true },
146 { "http://www.example.com", false },
147 { "http://%s.example.com", true },
148 { "http://www.example.%s.test.%s.com", true },
149 { "http://www.test&test.%s.com", true },
Peter Kasting 2014/11/17 21:30:36 These cases all test %s in the hostname. Shouldn'
Tapu Ghose 2014/12/07 02:49:55 Added few more test cases.
150 { "", false },
151 };
152
153 std::string search_engine_url;
154 for (size_t i = 0; i < arraysize(test_cases); ++i) {
155 EXPECT_EQ(test_cases[i].can_be_imported_as_search_engine,
156 CanImportURLAsSearchEngine(GURL(test_cases[i].url),
157 &search_engine_url));
158 }
159 }
160
140 namespace { 161 namespace {
141 162
142 class BookmarkHTMLReaderTestWithData : public testing::Test { 163 class BookmarkHTMLReaderTestWithData : public testing::Test {
143 public: 164 public:
144 void SetUp() override; 165 void SetUp() override;
145 166
146 protected: 167 protected:
147 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry); 168 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry);
148 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry); 169 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry);
149 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry); 170 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry);
150 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry); 171 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry);
151 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry); 172 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry);
152 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry); 173 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry);
153 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry); 174 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry);
154 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry); 175 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry);
176 void ExpectFirstFirefoxBookmarkWithKeyword(
177 const importer::SearchEngineInfo& info);
178 void ExpectSecondFirefoxBookmarkWithKeyword(
179 const importer::SearchEngineInfo& info);
155 180
156 base::FilePath test_data_path_; 181 base::FilePath test_data_path_;
157 }; 182 };
158 183
159 void BookmarkHTMLReaderTestWithData::SetUp() { 184 void BookmarkHTMLReaderTestWithData::SetUp() {
160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_)); 185 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_));
161 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader"); 186 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader");
162 } 187 }
163 188
164 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark( 189 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark(
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark( 254 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark(
230 const ImportedBookmarkEntry& entry) { 255 const ImportedBookmarkEntry& entry) {
231 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title); 256 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title);
232 EXPECT_FALSE(entry.is_folder); 257 EXPECT_FALSE(entry.is_folder);
233 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time); 258 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time);
234 EXPECT_EQ(1U, entry.path.size()); 259 EXPECT_EQ(1U, entry.path.size());
235 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); 260 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front());
236 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec()); 261 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec());
237 } 262 }
238 263
264 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefoxBookmarkWithKeyword(
265 const importer::SearchEngineInfo& info) {
266 EXPECT_EQ(ASCIIToUTF16("http://example.%s.com/"), info.url);
267 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword);
268 EXPECT_EQ(ASCIIToUTF16("Bookmark Keyword"), info.display_name);
269 }
270
271 void BookmarkHTMLReaderTestWithData::ExpectSecondFirefoxBookmarkWithKeyword(
272 const importer::SearchEngineInfo& info) {
273 EXPECT_EQ(ASCIIToUTF16("http://example.com/?q=%s"), info.url);
274 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword);
275 EXPECT_EQ(ASCIIToUTF16("BookmarkName"), info.display_name);
276 }
277
239 } // namespace 278 } // namespace
240 279
241 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) { 280 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) {
242 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 281 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
243 282
244 std::vector<ImportedBookmarkEntry> bookmarks; 283 std::vector<ImportedBookmarkEntry> bookmarks;
245 ImportBookmarksFile(base::Callback<bool(void)>(), 284 ImportBookmarksFile(base::Callback<bool(void)>(),
246 base::Callback<bool(const GURL&)>(), 285 base::Callback<bool(const GURL&)>(),
247 path, &bookmarks, NULL); 286 path, &bookmarks, NULL, NULL);
248 287
249 ASSERT_EQ(3U, bookmarks.size()); 288 ASSERT_EQ(3U, bookmarks.size());
250 ExpectFirstFirefox2Bookmark(bookmarks[0]); 289 ExpectFirstFirefox2Bookmark(bookmarks[0]);
251 ExpectSecondFirefox2Bookmark(bookmarks[1]); 290 ExpectSecondFirefox2Bookmark(bookmarks[1]);
252 ExpectThirdFirefox2Bookmark(bookmarks[2]); 291 ExpectThirdFirefox2Bookmark(bookmarks[2]);
253 } 292 }
254 293
255 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) { 294 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) {
256 base::FilePath path = test_data_path_.AppendASCII("firefox23.html"); 295 base::FilePath path = test_data_path_.AppendASCII("firefox23.html");
257 296
258 std::vector<ImportedBookmarkEntry> bookmarks; 297 std::vector<ImportedBookmarkEntry> bookmarks;
259 ImportBookmarksFile(base::Callback<bool(void)>(), 298 ImportBookmarksFile(base::Callback<bool(void)>(),
260 base::Callback<bool(const GURL&)>(), 299 base::Callback<bool(const GURL&)>(),
261 path, &bookmarks, NULL); 300 path, &bookmarks, NULL, NULL);
262 301
263 ASSERT_EQ(3U, bookmarks.size()); 302 ASSERT_EQ(3U, bookmarks.size());
264 ExpectFirstFirefox23Bookmark(bookmarks[0]); 303 ExpectFirstFirefox23Bookmark(bookmarks[0]);
265 ExpectSecondFirefox23Bookmark(bookmarks[1]); 304 ExpectSecondFirefox23Bookmark(bookmarks[1]);
266 ExpectThirdFirefox23Bookmark(bookmarks[2]); 305 ExpectThirdFirefox23Bookmark(bookmarks[2]);
267 } 306 }
268 307
269 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) { 308 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) {
270 base::FilePath path = test_data_path_.AppendASCII("epiphany.html"); 309 base::FilePath path = test_data_path_.AppendASCII("epiphany.html");
271 310
272 std::vector<ImportedBookmarkEntry> bookmarks; 311 std::vector<ImportedBookmarkEntry> bookmarks;
273 ImportBookmarksFile(base::Callback<bool(void)>(), 312 ImportBookmarksFile(base::Callback<bool(void)>(),
274 base::Callback<bool(const GURL&)>(), 313 base::Callback<bool(const GURL&)>(),
275 path, &bookmarks, NULL); 314 path, &bookmarks, NULL, NULL);
276 315
277 ASSERT_EQ(2U, bookmarks.size()); 316 ASSERT_EQ(2U, bookmarks.size());
278 ExpectFirstEpiphanyBookmark(bookmarks[0]); 317 ExpectFirstEpiphanyBookmark(bookmarks[0]);
279 ExpectSecondEpiphanyBookmark(bookmarks[1]); 318 ExpectSecondEpiphanyBookmark(bookmarks[1]);
280 } 319 }
281 320
321 TEST_F(BookmarkHTMLReaderTestWithData, FirefoxBookmarkFileWithKeywordImport) {
322 base::FilePath path = test_data_path_.AppendASCII(
323 "firefox_bookmark_keyword.html");
324
325 std::vector<importer::SearchEngineInfo> search_engines;
326 ImportBookmarksFile(base::Callback<bool(void)>(),
327 base::Callback<bool(const GURL&)>(),
328 path, NULL, &search_engines, NULL);
329
330 ASSERT_EQ(2U, search_engines.size());
331 ExpectFirstFirefoxBookmarkWithKeyword(search_engines[0]);
332 ExpectSecondFirefoxBookmarkWithKeyword(search_engines[1]);
333 }
334
282 namespace { 335 namespace {
283 336
284 class CancelAfterFifteenCalls { 337 class CancelAfterFifteenCalls {
285 int count; 338 int count;
286 public: 339 public:
287 CancelAfterFifteenCalls() : count(0) { } 340 CancelAfterFifteenCalls() : count(0) { }
288 bool ShouldCancel() { 341 bool ShouldCancel() {
289 return ++count > 16; 342 return ++count > 16;
290 } 343 }
291 }; 344 };
292 345
293 } // namespace 346 } // namespace
294 347
295 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) { 348 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) {
296 // Use a file for testing that has multiple bookmarks. 349 // Use a file for testing that has multiple bookmarks.
297 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 350 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
298 351
299 std::vector<ImportedBookmarkEntry> bookmarks; 352 std::vector<ImportedBookmarkEntry> bookmarks;
300 CancelAfterFifteenCalls cancel_fifteen; 353 CancelAfterFifteenCalls cancel_fifteen;
301 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, 354 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel,
302 base::Unretained(&cancel_fifteen)), 355 base::Unretained(&cancel_fifteen)),
303 base::Callback<bool(const GURL&)>(), 356 base::Callback<bool(const GURL&)>(),
304 path, &bookmarks, NULL); 357 path, &bookmarks, NULL, NULL);
305 358
306 // The cancellation callback is checked before each line is read, so fifteen 359 // The cancellation callback is checked before each line is read, so fifteen
307 // lines are imported. The first fifteen lines of firefox2.html include only 360 // lines are imported. The first fifteen lines of firefox2.html include only
308 // one bookmark. 361 // one bookmark.
309 ASSERT_EQ(1U, bookmarks.size()); 362 ASSERT_EQ(1U, bookmarks.size());
310 ExpectFirstFirefox2Bookmark(bookmarks[0]); 363 ExpectFirstFirefox2Bookmark(bookmarks[0]);
311 } 364 }
312 365
313 namespace { 366 namespace {
314 367
315 bool IsURLValid(const GURL& url) { 368 bool IsURLValid(const GURL& url) {
316 // No offense to whomever owns this domain... 369 // No offense to whomever owns this domain...
317 return !url.DomainIs("tamurayukari.com"); 370 return !url.DomainIs("tamurayukari.com");
318 } 371 }
319 372
320 } // namespace 373 } // namespace
321 374
322 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) { 375 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) {
323 // Use a file for testing that has multiple bookmarks. 376 // Use a file for testing that has multiple bookmarks.
324 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 377 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
325 378
326 std::vector<ImportedBookmarkEntry> bookmarks; 379 std::vector<ImportedBookmarkEntry> bookmarks;
327 ImportBookmarksFile(base::Callback<bool(void)>(), 380 ImportBookmarksFile(base::Callback<bool(void)>(),
328 base::Bind(&IsURLValid), 381 base::Bind(&IsURLValid),
329 path, &bookmarks, NULL); 382 path, &bookmarks, NULL, NULL);
330 383
331 ASSERT_EQ(2U, bookmarks.size()); 384 ASSERT_EQ(2U, bookmarks.size());
332 ExpectFirstFirefox2Bookmark(bookmarks[0]); 385 ExpectFirstFirefox2Bookmark(bookmarks[0]);
333 ExpectThirdFirefox2Bookmark(bookmarks[1]); 386 ExpectThirdFirefox2Bookmark(bookmarks[1]);
334 } 387 }
335 388
336 } // namespace bookmark_html_reader 389 } // namespace bookmark_html_reader
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698