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