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

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 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 base::string16 keyword;
144 const base::string16 title;
145 const bool can_be_imported_as_search_engine;
146 } test_cases[] = {
147 {
148 "http://www.example.%s.com",
149 base::UTF8ToUTF16("keyword1"),
150 base::UTF8ToUTF16("title1"),
Ilya Sherman 2014/11/10 23:00:22 nit: Looks like you could omit the keyword and tit
Ilya Sherman 2014/11/10 23:00:22 nit: Please use ASCIIToUTF16; below as well.
Tapu Ghose 2014/11/15 05:22:20 Removed keyword and title from test cases.
Tapu Ghose 2014/11/15 05:22:20 Acknowledged.
151 true
152 },
153 {
154 "http://www.example.com",
155 base::UTF8ToUTF16("keyword1"),
156 base::UTF8ToUTF16("title1"),
157 false
158 },
Ilya Sherman 2014/11/10 23:00:22 Please also test: (1) and empty URL, (2) a URL tha
Tapu Ghose 2014/11/15 05:22:20 Done.
159 };
160
161 importer::SearchEngineInfo search_engine;
162 for (size_t i = 0; i < arraysize(test_cases); ++i) {
163 EXPECT_EQ(test_cases[i].can_be_imported_as_search_engine,
Ilya Sherman 2014/11/10 23:00:22 nit: Please either wrap the arg, or align the subs
Tapu Ghose 2014/11/15 05:22:20 Done.
164 CanImportURLAsSearchEngine(
165 GURL(test_cases[i].url),
166 test_cases[i].keyword,
167 test_cases[i].title,
168 &search_engine));
169 EXPECT_EQ(test_cases[i].keyword, search_engine.keyword);
170 EXPECT_EQ(test_cases[i].title, search_engine.display_name);
171 }
172 }
173
140 namespace { 174 namespace {
141 175
142 class BookmarkHTMLReaderTestWithData : public testing::Test { 176 class BookmarkHTMLReaderTestWithData : public testing::Test {
143 public: 177 public:
144 void SetUp() override; 178 void SetUp() override;
145 179
146 protected: 180 protected:
147 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry); 181 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry);
148 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry); 182 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry);
149 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry); 183 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry);
150 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry); 184 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry);
151 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry); 185 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry);
152 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry); 186 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry);
153 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry); 187 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry);
154 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry); 188 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry);
189 void ExpectFirstFirefoxBookmarkWithKeyword(
190 const importer::SearchEngineInfo& info);
191 void ExpectSecondFirefoxBookmarkWithKeyword(
192 const importer::SearchEngineInfo& info);
155 193
156 base::FilePath test_data_path_; 194 base::FilePath test_data_path_;
157 }; 195 };
158 196
159 void BookmarkHTMLReaderTestWithData::SetUp() { 197 void BookmarkHTMLReaderTestWithData::SetUp() {
160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_)); 198 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_));
161 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader"); 199 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader");
162 } 200 }
163 201
164 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark( 202 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark(
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark( 267 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark(
230 const ImportedBookmarkEntry& entry) { 268 const ImportedBookmarkEntry& entry) {
231 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title); 269 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title);
232 EXPECT_FALSE(entry.is_folder); 270 EXPECT_FALSE(entry.is_folder);
233 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time); 271 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time);
234 EXPECT_EQ(1U, entry.path.size()); 272 EXPECT_EQ(1U, entry.path.size());
235 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); 273 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front());
236 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec()); 274 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec());
237 } 275 }
238 276
277 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefoxBookmarkWithKeyword(
278 const importer::SearchEngineInfo& info) {
279 EXPECT_EQ(ASCIIToUTF16("http://example.%s.com/"), info.url);
280 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword);
281 EXPECT_EQ(ASCIIToUTF16("Bookmark Keyword"), info.display_name);
282 }
283
284 void BookmarkHTMLReaderTestWithData::ExpectSecondFirefoxBookmarkWithKeyword(
285 const importer::SearchEngineInfo& info) {
286 EXPECT_EQ(ASCIIToUTF16("http://example.com/?q=%s"), info.url);
287 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword);
288 EXPECT_EQ(ASCIIToUTF16("BookmarkName"), info.display_name);
289 }
290
239 } // namespace 291 } // namespace
240 292
241 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) { 293 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) {
242 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 294 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
243 295
244 std::vector<ImportedBookmarkEntry> bookmarks; 296 std::vector<ImportedBookmarkEntry> bookmarks;
245 ImportBookmarksFile(base::Callback<bool(void)>(), 297 ImportBookmarksFile(base::Callback<bool(void)>(),
246 base::Callback<bool(const GURL&)>(), 298 base::Callback<bool(const GURL&)>(),
247 path, &bookmarks, NULL); 299 path, &bookmarks, NULL, NULL);
248 300
249 ASSERT_EQ(3U, bookmarks.size()); 301 ASSERT_EQ(3U, bookmarks.size());
250 ExpectFirstFirefox2Bookmark(bookmarks[0]); 302 ExpectFirstFirefox2Bookmark(bookmarks[0]);
251 ExpectSecondFirefox2Bookmark(bookmarks[1]); 303 ExpectSecondFirefox2Bookmark(bookmarks[1]);
252 ExpectThirdFirefox2Bookmark(bookmarks[2]); 304 ExpectThirdFirefox2Bookmark(bookmarks[2]);
253 } 305 }
254 306
255 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) { 307 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) {
256 base::FilePath path = test_data_path_.AppendASCII("firefox23.html"); 308 base::FilePath path = test_data_path_.AppendASCII("firefox23.html");
257 309
258 std::vector<ImportedBookmarkEntry> bookmarks; 310 std::vector<ImportedBookmarkEntry> bookmarks;
259 ImportBookmarksFile(base::Callback<bool(void)>(), 311 ImportBookmarksFile(base::Callback<bool(void)>(),
260 base::Callback<bool(const GURL&)>(), 312 base::Callback<bool(const GURL&)>(),
261 path, &bookmarks, NULL); 313 path, &bookmarks, NULL, NULL);
262 314
263 ASSERT_EQ(3U, bookmarks.size()); 315 ASSERT_EQ(3U, bookmarks.size());
264 ExpectFirstFirefox23Bookmark(bookmarks[0]); 316 ExpectFirstFirefox23Bookmark(bookmarks[0]);
265 ExpectSecondFirefox23Bookmark(bookmarks[1]); 317 ExpectSecondFirefox23Bookmark(bookmarks[1]);
266 ExpectThirdFirefox23Bookmark(bookmarks[2]); 318 ExpectThirdFirefox23Bookmark(bookmarks[2]);
267 } 319 }
268 320
269 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) { 321 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) {
270 base::FilePath path = test_data_path_.AppendASCII("epiphany.html"); 322 base::FilePath path = test_data_path_.AppendASCII("epiphany.html");
271 323
272 std::vector<ImportedBookmarkEntry> bookmarks; 324 std::vector<ImportedBookmarkEntry> bookmarks;
273 ImportBookmarksFile(base::Callback<bool(void)>(), 325 ImportBookmarksFile(base::Callback<bool(void)>(),
274 base::Callback<bool(const GURL&)>(), 326 base::Callback<bool(const GURL&)>(),
275 path, &bookmarks, NULL); 327 path, &bookmarks, NULL, NULL);
276 328
277 ASSERT_EQ(2U, bookmarks.size()); 329 ASSERT_EQ(2U, bookmarks.size());
278 ExpectFirstEpiphanyBookmark(bookmarks[0]); 330 ExpectFirstEpiphanyBookmark(bookmarks[0]);
279 ExpectSecondEpiphanyBookmark(bookmarks[1]); 331 ExpectSecondEpiphanyBookmark(bookmarks[1]);
280 } 332 }
281 333
334 TEST_F(BookmarkHTMLReaderTestWithData, FirefoxBookmarkFileWithKeywordImport) {
335 base::FilePath path = test_data_path_.AppendASCII(
336 "firefox_bookmark_keyword.html");
337
338 std::vector<importer::SearchEngineInfo> search_engines;
339 ImportBookmarksFile(base::Callback<bool(void)>(),
340 base::Callback<bool(const GURL&)>(),
341 path, NULL, &search_engines, NULL);
342
343 ASSERT_EQ(2U, search_engines.size());
344 ExpectFirstFirefoxBookmarkWithKeyword(search_engines[0]);
345 ExpectSecondFirefoxBookmarkWithKeyword(search_engines[1]);
346 }
347
282 namespace { 348 namespace {
283 349
284 class CancelAfterFifteenCalls { 350 class CancelAfterFifteenCalls {
285 int count; 351 int count;
286 public: 352 public:
287 CancelAfterFifteenCalls() : count(0) { } 353 CancelAfterFifteenCalls() : count(0) { }
288 bool ShouldCancel() { 354 bool ShouldCancel() {
289 return ++count > 16; 355 return ++count > 16;
290 } 356 }
291 }; 357 };
292 358
293 } // namespace 359 } // namespace
294 360
295 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) { 361 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) {
296 // Use a file for testing that has multiple bookmarks. 362 // Use a file for testing that has multiple bookmarks.
297 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 363 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
298 364
299 std::vector<ImportedBookmarkEntry> bookmarks; 365 std::vector<ImportedBookmarkEntry> bookmarks;
300 CancelAfterFifteenCalls cancel_fifteen; 366 CancelAfterFifteenCalls cancel_fifteen;
301 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, 367 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel,
302 base::Unretained(&cancel_fifteen)), 368 base::Unretained(&cancel_fifteen)),
303 base::Callback<bool(const GURL&)>(), 369 base::Callback<bool(const GURL&)>(),
304 path, &bookmarks, NULL); 370 path, &bookmarks, NULL, NULL);
305 371
306 // The cancellation callback is checked before each line is read, so fifteen 372 // 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 373 // lines are imported. The first fifteen lines of firefox2.html include only
308 // one bookmark. 374 // one bookmark.
309 ASSERT_EQ(1U, bookmarks.size()); 375 ASSERT_EQ(1U, bookmarks.size());
310 ExpectFirstFirefox2Bookmark(bookmarks[0]); 376 ExpectFirstFirefox2Bookmark(bookmarks[0]);
311 } 377 }
312 378
313 namespace { 379 namespace {
314 380
315 bool IsURLValid(const GURL& url) { 381 bool IsURLValid(const GURL& url) {
316 // No offense to whomever owns this domain... 382 // No offense to whomever owns this domain...
317 return !url.DomainIs("tamurayukari.com"); 383 return !url.DomainIs("tamurayukari.com");
318 } 384 }
319 385
320 } // namespace 386 } // namespace
321 387
322 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) { 388 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) {
323 // Use a file for testing that has multiple bookmarks. 389 // Use a file for testing that has multiple bookmarks.
324 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); 390 base::FilePath path = test_data_path_.AppendASCII("firefox2.html");
325 391
326 std::vector<ImportedBookmarkEntry> bookmarks; 392 std::vector<ImportedBookmarkEntry> bookmarks;
327 ImportBookmarksFile(base::Callback<bool(void)>(), 393 ImportBookmarksFile(base::Callback<bool(void)>(),
328 base::Bind(&IsURLValid), 394 base::Bind(&IsURLValid),
329 path, &bookmarks, NULL); 395 path, &bookmarks, NULL, NULL);
330 396
331 ASSERT_EQ(2U, bookmarks.size()); 397 ASSERT_EQ(2U, bookmarks.size());
332 ExpectFirstFirefox2Bookmark(bookmarks[0]); 398 ExpectFirstFirefox2Bookmark(bookmarks[0]);
333 ExpectThirdFirefox2Bookmark(bookmarks[1]); 399 ExpectThirdFirefox2Bookmark(bookmarks[1]);
334 } 400 }
335 401
336 } // namespace bookmark_html_reader 402 } // namespace bookmark_html_reader
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698