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