Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 145 |
| 146 protected: | 146 protected: |
| 147 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry); | 147 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry); |
| 148 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry); | 148 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry); |
| 149 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry); | 149 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry); |
| 150 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry); | 150 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry); |
| 151 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry); | 151 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry); |
| 152 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry); | 152 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry); |
| 153 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry); | 153 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry); |
| 154 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry); | 154 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry); |
| 155 void ExpectFirstFirefoxBookmarkWithKeyword( | |
| 156 const importer::SearchEngineInfo& info); | |
| 157 void ExpectSecondFirefoxBookmarkWithKeyword( | |
| 158 const importer::SearchEngineInfo& info); | |
| 155 | 159 |
| 156 base::FilePath test_data_path_; | 160 base::FilePath test_data_path_; |
| 157 }; | 161 }; |
| 158 | 162 |
| 159 void BookmarkHTMLReaderTestWithData::SetUp() { | 163 void BookmarkHTMLReaderTestWithData::SetUp() { |
| 160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_)); | 164 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_)); |
| 161 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader"); | 165 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader"); |
| 162 } | 166 } |
| 163 | 167 |
| 164 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark( | 168 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark( | 233 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark( |
| 230 const ImportedBookmarkEntry& entry) { | 234 const ImportedBookmarkEntry& entry) { |
| 231 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title); | 235 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title); |
| 232 EXPECT_FALSE(entry.is_folder); | 236 EXPECT_FALSE(entry.is_folder); |
| 233 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time); | 237 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time); |
| 234 EXPECT_EQ(1U, entry.path.size()); | 238 EXPECT_EQ(1U, entry.path.size()); |
| 235 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); | 239 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); |
| 236 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec()); | 240 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec()); |
| 237 } | 241 } |
| 238 | 242 |
| 243 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefoxBookmarkWithKeyword( | |
| 244 const importer::SearchEngineInfo& info) { | |
| 245 EXPECT_EQ(ASCIIToUTF16("http://example.%s.com/"), info.url); | |
| 246 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword); | |
| 247 EXPECT_EQ(ASCIIToUTF16("Bookmark Keyword"), info.display_name); | |
| 248 } | |
| 249 | |
| 250 void BookmarkHTMLReaderTestWithData::ExpectSecondFirefoxBookmarkWithKeyword( | |
| 251 const importer::SearchEngineInfo& info) { | |
| 252 EXPECT_EQ(ASCIIToUTF16("http://example.com/?q=%s"), info.url); | |
| 253 EXPECT_EQ(ASCIIToUTF16("keyword"), info.keyword); | |
| 254 EXPECT_EQ(ASCIIToUTF16("BookmarkName"), info.display_name); | |
| 255 } | |
| 256 | |
| 239 } // namespace | 257 } // namespace |
| 240 | 258 |
| 241 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) { | 259 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) { |
| 242 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); | 260 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); |
| 243 | 261 |
| 244 std::vector<ImportedBookmarkEntry> bookmarks; | 262 std::vector<ImportedBookmarkEntry> bookmarks; |
| 245 ImportBookmarksFile(base::Callback<bool(void)>(), | 263 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 246 base::Callback<bool(const GURL&)>(), | 264 base::Callback<bool(const GURL&)>(), |
| 247 path, &bookmarks, NULL); | 265 path, &bookmarks, NULL, NULL); |
| 248 | 266 |
| 249 ASSERT_EQ(3U, bookmarks.size()); | 267 ASSERT_EQ(3U, bookmarks.size()); |
| 250 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 268 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 251 ExpectSecondFirefox2Bookmark(bookmarks[1]); | 269 ExpectSecondFirefox2Bookmark(bookmarks[1]); |
| 252 ExpectThirdFirefox2Bookmark(bookmarks[2]); | 270 ExpectThirdFirefox2Bookmark(bookmarks[2]); |
| 253 } | 271 } |
| 254 | 272 |
| 255 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) { | 273 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) { |
| 256 base::FilePath path = test_data_path_.AppendASCII("firefox23.html"); | 274 base::FilePath path = test_data_path_.AppendASCII("firefox23.html"); |
| 257 | 275 |
| 258 std::vector<ImportedBookmarkEntry> bookmarks; | 276 std::vector<ImportedBookmarkEntry> bookmarks; |
| 259 ImportBookmarksFile(base::Callback<bool(void)>(), | 277 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 260 base::Callback<bool(const GURL&)>(), | 278 base::Callback<bool(const GURL&)>(), |
| 261 path, &bookmarks, NULL); | 279 path, &bookmarks, NULL, NULL); |
| 262 | 280 |
| 263 ASSERT_EQ(3U, bookmarks.size()); | 281 ASSERT_EQ(3U, bookmarks.size()); |
| 264 ExpectFirstFirefox23Bookmark(bookmarks[0]); | 282 ExpectFirstFirefox23Bookmark(bookmarks[0]); |
| 265 ExpectSecondFirefox23Bookmark(bookmarks[1]); | 283 ExpectSecondFirefox23Bookmark(bookmarks[1]); |
| 266 ExpectThirdFirefox23Bookmark(bookmarks[2]); | 284 ExpectThirdFirefox23Bookmark(bookmarks[2]); |
| 267 } | 285 } |
| 268 | 286 |
| 269 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) { | 287 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) { |
| 270 base::FilePath path = test_data_path_.AppendASCII("epiphany.html"); | 288 base::FilePath path = test_data_path_.AppendASCII("epiphany.html"); |
| 271 | 289 |
| 272 std::vector<ImportedBookmarkEntry> bookmarks; | 290 std::vector<ImportedBookmarkEntry> bookmarks; |
| 273 ImportBookmarksFile(base::Callback<bool(void)>(), | 291 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 274 base::Callback<bool(const GURL&)>(), | 292 base::Callback<bool(const GURL&)>(), |
| 275 path, &bookmarks, NULL); | 293 path, &bookmarks, NULL, NULL); |
| 276 | 294 |
| 277 ASSERT_EQ(2U, bookmarks.size()); | 295 ASSERT_EQ(2U, bookmarks.size()); |
| 278 ExpectFirstEpiphanyBookmark(bookmarks[0]); | 296 ExpectFirstEpiphanyBookmark(bookmarks[0]); |
| 279 ExpectSecondEpiphanyBookmark(bookmarks[1]); | 297 ExpectSecondEpiphanyBookmark(bookmarks[1]); |
| 280 } | 298 } |
| 281 | 299 |
| 300 TEST_F(BookmarkHTMLReaderTestWithData, FirefoxBookmarkFileWithKeywordImport) { | |
| 301 base::FilePath path = test_data_path_.AppendASCII( | |
| 302 "firefox_bookmark_keyword.html"); | |
| 303 | |
| 304 std::vector<importer::SearchEngineInfo> search_engines; | |
| 305 ImportBookmarksFile(base::Callback<bool(void)>(), | |
| 306 base::Callback<bool(const GURL&)>(), | |
| 307 path, NULL, &search_engines, NULL); | |
| 308 | |
| 309 ASSERT_EQ(2U, search_engines.size()); | |
| 310 ExpectFirstFirefoxBookmarkWithKeyword(search_engines[0]); | |
| 311 ExpectSecondFirefoxBookmarkWithKeyword(search_engines[1]); | |
| 312 } | |
| 313 | |
| 282 namespace { | 314 namespace { |
| 283 | 315 |
| 284 class CancelAfterFifteenCalls { | 316 class CancelAfterFifteenCalls { |
| 285 int count; | 317 int count; |
| 286 public: | 318 public: |
| 287 CancelAfterFifteenCalls() : count(0) { } | 319 CancelAfterFifteenCalls() : count(0) { } |
| 288 bool ShouldCancel() { | 320 bool ShouldCancel() { |
| 289 return ++count > 16; | 321 return ++count > 16; |
| 290 } | 322 } |
| 291 }; | 323 }; |
| 292 | 324 |
| 293 } // namespace | 325 } // namespace |
| 294 | 326 |
| 295 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) { | 327 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) { |
| 296 // Use a file for testing that has multiple bookmarks. | 328 // Use a file for testing that has multiple bookmarks. |
| 297 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); | 329 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); |
| 298 | 330 |
| 299 std::vector<ImportedBookmarkEntry> bookmarks; | 331 std::vector<ImportedBookmarkEntry> bookmarks; |
| 300 CancelAfterFifteenCalls cancel_fifteen; | 332 CancelAfterFifteenCalls cancel_fifteen; |
| 301 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, | 333 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, |
| 302 base::Unretained(&cancel_fifteen)), | 334 base::Unretained(&cancel_fifteen)), |
| 303 base::Callback<bool(const GURL&)>(), | 335 base::Callback<bool(const GURL&)>(), |
| 304 path, &bookmarks, NULL); | 336 path, &bookmarks, NULL, NULL); |
| 305 | 337 |
| 306 // The cancellation callback is checked before each line is read, so fifteen | 338 // 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 | 339 // lines are imported. The first fifteen lines of firefox2.html include only |
| 308 // one bookmark. | 340 // one bookmark. |
| 309 ASSERT_EQ(1U, bookmarks.size()); | 341 ASSERT_EQ(1U, bookmarks.size()); |
| 310 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 342 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 311 } | 343 } |
| 312 | 344 |
| 313 namespace { | 345 namespace { |
|
Ilya Sherman
2014/11/07 00:22:34
nit: Please restore the newline after this line.
Tapu Ghose
2014/11/09 14:03:07
Done.
| |
| 314 | |
| 315 bool IsURLValid(const GURL& url) { | 346 bool IsURLValid(const GURL& url) { |
| 316 // No offense to whomever owns this domain... | 347 // No offense to whomever owns this domain... |
| 317 return !url.DomainIs("tamurayukari.com"); | 348 return !url.DomainIs("tamurayukari.com"); |
| 318 } | 349 } |
| 319 | 350 |
| 320 } // namespace | 351 } // namespace |
| 321 | 352 |
| 322 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) { | 353 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) { |
| 323 // Use a file for testing that has multiple bookmarks. | 354 // Use a file for testing that has multiple bookmarks. |
| 324 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); | 355 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); |
| 325 | 356 |
| 326 std::vector<ImportedBookmarkEntry> bookmarks; | 357 std::vector<ImportedBookmarkEntry> bookmarks; |
| 327 ImportBookmarksFile(base::Callback<bool(void)>(), | 358 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 328 base::Bind(&IsURLValid), | 359 base::Bind(&IsURLValid), |
| 329 path, &bookmarks, NULL); | 360 path, &bookmarks, NULL, NULL); |
| 330 | 361 |
| 331 ASSERT_EQ(2U, bookmarks.size()); | 362 ASSERT_EQ(2U, bookmarks.size()); |
| 332 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 363 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 333 ExpectThirdFirefox2Bookmark(bookmarks[1]); | 364 ExpectThirdFirefox2Bookmark(bookmarks[1]); |
| 334 } | 365 } |
| 335 | 366 |
| 336 } // namespace bookmark_html_reader | 367 } // namespace bookmark_html_reader |
|
Ilya Sherman
2014/11/07 00:22:34
Please add test coverage specifically for CanImpor
Tapu Ghose
2014/11/09 14:03:07
Added.
| |
| OLD | NEW |