File: bookmark_html_reader_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (515 lines) | stat: -rw-r--r-- 20,510 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/utility/importer/bookmark_html_reader.h"

#include <stddef.h>

#include <array>
#include <string>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/common/chrome_paths.h"
#include "components/user_data_importer/common/imported_bookmark_entry.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;
using base::UTF16ToWide;

namespace bookmark_html_reader {

TEST(BookmarkHTMLReaderTest, ParseTests) {
  bool result;

  // Tests charset.
  std::string charset;
  result = internal::ParseCharsetFromLine(
      "<META HTTP-EQUIV=\"Content-Type\" "
      "CONTENT=\"text/html; charset=UTF-8\">",
      &charset);
  EXPECT_TRUE(result);
  EXPECT_EQ("UTF-8", charset);

  // Escaped characters in name.
  std::u16string folder_name;
  bool is_toolbar_folder;
  base::Time folder_add_date;
  result = internal::ParseFolderNameFromLine(
      "<DT><H3 ADD_DATE=\"1207558707\" >&lt; &gt;"
      " &amp; &quot; &#39; \\ /</H3>",
      charset, &folder_name, &is_toolbar_folder, &folder_add_date);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"< > & \" ' \\ /", folder_name);
  EXPECT_FALSE(is_toolbar_folder);
  EXPECT_TRUE(base::Time::FromTimeT(1207558707) == folder_add_date);

  // Empty name and toolbar folder attribute.
  result = internal::ParseFolderNameFromLine(
      "<DT><H3 PERSONAL_TOOLBAR_FOLDER=\"true\"></H3>",
      charset, &folder_name, &is_toolbar_folder, &folder_add_date);
  EXPECT_TRUE(result);
  EXPECT_EQ(std::u16string(), folder_name);
  EXPECT_TRUE(is_toolbar_folder);

  // Unicode characters in title and shortcut.
  std::u16string title;
  GURL url, favicon;
  std::u16string shortcut;
  std::u16string post_data;
  base::Time add_date;
  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://chinese.site.cn/path?query=1#ref\" "
      "SHORTCUTURL=\"\xE4\xB8\xAD\">\xE4\xB8\xAD\xE6\x96\x87</A>",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_TRUE(result);
  EXPECT_EQ(L"\x4E2D\x6587", UTF16ToWide(title));
  EXPECT_EQ("http://chinese.site.cn/path?query=1#ref", url.spec());
  EXPECT_EQ(L"\x4E2D", UTF16ToWide(shortcut));
  EXPECT_EQ(std::u16string(), post_data);
  EXPECT_TRUE(base::Time() == add_date);

  // No shortcut, and url contains %22 ('"' character).
  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://domain.com/?q=%22<>%22\">name</A>",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"name", title);
  EXPECT_EQ("http://domain.com/?q=%22%3C%3E%22", url.spec());
  EXPECT_EQ(std::u16string(), shortcut);
  EXPECT_EQ(std::u16string(), post_data);
  EXPECT_TRUE(base::Time() == add_date);

  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://domain.com/?g=&quot;\"\">name</A>",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"name", title);
  EXPECT_EQ("http://domain.com/?g=%22", url.spec());
  EXPECT_EQ(std::u16string(), shortcut);
  EXPECT_EQ(std::u16string(), post_data);
  EXPECT_TRUE(base::Time() == add_date);

  // Creation date.
  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://site/\" ADD_DATE=\"1121301154\">name</A>",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"name", title);
  EXPECT_EQ(GURL("http://site/"), url);
  EXPECT_EQ(std::u16string(), shortcut);
  EXPECT_EQ(std::u16string(), post_data);
  EXPECT_TRUE(base::Time::FromTimeT(1121301154) == add_date);

  // Post-data
  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://localhost:8080/test/hello.html\" ADD_DATE=\""
      "1212447159\" LAST_VISIT=\"1212447251\" LAST_MODIFIED=\"1212447248\""
      "SHORTCUTURL=\"post\" ICON=\"data:\" POST_DATA=\"lname%3D%25s\""
      "LAST_CHARSET=\"UTF-8\" ID=\"rdf:#$weKaR3\">Test Post keyword</A>",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"Test Post keyword", title);
  EXPECT_EQ("http://localhost:8080/test/hello.html", url.spec());
  EXPECT_EQ(u"post", shortcut);
  EXPECT_EQ(u"lname%3D%25s", post_data);
  EXPECT_TRUE(base::Time::FromTimeT(1212447159) == add_date);

  // Invalid case.
  result = internal::ParseBookmarkFromLine(
      "<DT><A HREF=\"http://domain.com/?q=%22",
      charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
  EXPECT_FALSE(result);
  EXPECT_EQ(std::u16string(), title);
  EXPECT_EQ("", url.spec());
  EXPECT_EQ(std::u16string(), shortcut);
  EXPECT_EQ(std::u16string(), post_data);
  EXPECT_TRUE(base::Time() == add_date);

  // Epiphany format.
  result = internal::ParseMinimumBookmarkFromLine(
      "<dt><a href=\"http://www.google.com/\">Google</a></dt>",
      charset, &title, &url);
  EXPECT_TRUE(result);
  EXPECT_EQ(u"Google", title);
  EXPECT_EQ("http://www.google.com/", url.spec());
}

TEST(BookmarkHTMLReaderTest, CanImportURLAsSearchEngineTest) {
  struct TestCase {
    const std::string url;
    const bool can_be_imported_as_search_engine;
    const std::string expected_search_engine_url;
  };
  auto test_cases = std::to_array<TestCase>({
      {"http://www.example.%s.com", true,
       "http://www.example.{searchTerms}.com/"},
      {"http://www.example.%S.com", true,
       "http://www.example.{searchTerms}.com/"},
      {"http://www.example.%x.com", false, "http://www.example.%x.com/"},
      {"http://www.example.com", false, ""},
      {"http://%s.example.com", true, "http://{searchTerms}.example.com/"},
      {"http://www.example.%s.test.%s.com", true,
       "http://www.example.{searchTerms}.test.{searchTerms}.com/"},
      // Illegal characters in the host get escaped.
      {"http://www.test&test.%s.com", true,
       "http://www.test&test.{searchTerms}.com/"},
      {"http://www.example.com?q=%s&foo=bar", true,
       "http://www.example.com/?q={searchTerms}&foo=bar"},
      {"http://www.example.com/%s/?q=%s&foo=bar", true,
       "http://www.example.com/{searchTerms}/?q={searchTerms}&foo=bar"},
      {"http//google.com", false, ""},
      {"path", false, ""},
      {"http:/path/%s/", true, "http://path/{searchTerms}/"},
      {"path", false, ""},
      {"", false, ""},
      // Cases with other percent-encoded characters.
      {"http://www.example.com/search%3Fpage?q=%s&v=foo%26bar", true,
       "http://www.example.com/search%3Fpage?q={searchTerms}&v=foo%26bar"},
      // Encoded percent symbol.
      {"http://www.example.com/search?q=%s&v=foo%25bar", true,
       "http://www.example.com/search?q={searchTerms}&v=foo%25bar"},
      // Literal "%s", escaped so as to not represent a replacement slot.
      // Note: This is buggy due to the fact that the GURL constructor doesn't
      // distinguish "%s" from "%25s", and can't be fixed without changing the
      // interface to this function. https://crbug.com/868214.
      {"http://www.example.com/search?q=%s&v=pepper%25salt", true,
       "http://www.example.com/"
       "search?q={searchTerms}&v=pepper{searchTerms}alt"},
      // Encoded Unicode character (U+2014).
      {"http://www.example.com/search?q=%s&v=foo%E2%80%94bar", true,
       "http://www.example.com/search?q={searchTerms}&v=foo%E2%80%94bar"},
      // Non-encoded Unicode character (U+2014) (should be auto-encoded).
      {"http://www.example.com/search?q=%s&v=foo—bar", true,
       "http://www.example.com/search?q={searchTerms}&v=foo%E2%80%94bar"},
      // Invalid characters that should be auto-encoded.
      {"http://www.example.com/{search}?q=%s", true,
       "http://www.example.com/%7Bsearch%7D?q={searchTerms}"},
  });

  std::string search_engine_url;
  for (size_t i = 0; i < std::size(test_cases); ++i) {
    EXPECT_EQ(test_cases[i].can_be_imported_as_search_engine,
        CanImportURLAsSearchEngine(GURL(test_cases[i].url),
                                   &search_engine_url));
    if (test_cases[i].can_be_imported_as_search_engine)
      EXPECT_EQ(test_cases[i].expected_search_engine_url, search_engine_url);
  }
}

namespace {

class BookmarkHTMLReaderTestWithData : public testing::Test {
 public:
  void SetUp() override;

 protected:
  void ExpectFirstFirefox2Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectSecondFirefox2Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectThirdFirefox2Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectFirstEpiphanyBookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectSecondEpiphanyBookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectFirstFirefox23Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectSecondFirefox23Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectThirdFirefox23Bookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectFirstFirefoxBookmarkWithKeyword(
      const user_data_importer::SearchEngineInfo& info);
  void ExpectSecondFirefoxBookmarkWithKeyword(
      const user_data_importer::SearchEngineInfo& info);
  void ExpectFirstEmptyFolderBookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);
  void ExpectSecondEmptyFolderBookmark(
      const user_data_importer::ImportedBookmarkEntry& entry);

  base::FilePath test_data_path_;
};

void BookmarkHTMLReaderTestWithData::SetUp() {
  ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_));
  test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader");
}

void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"Empty", entry.title);
  EXPECT_TRUE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  if (entry.path.size() == 1)
    EXPECT_EQ(u"Empty's Parent", entry.path.front());
}

void BookmarkHTMLReaderTestWithData::ExpectSecondFirefox2Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"[Tamura Yukari.com]", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  if (entry.path.size() == 1)
    EXPECT_EQ(u"Not Empty", entry.path.front());
  EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
}

void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox2Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"Google", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(0000000000), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  if (entry.path.size() == 1)
    EXPECT_EQ(u"Not Empty But Default", entry.path.front());
  EXPECT_EQ("http://www.google.com/", entry.url.spec());
}

void BookmarkHTMLReaderTestWithData::ExpectFirstEpiphanyBookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"[Tamura Yukari.com]", entry.title);
  EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
  EXPECT_EQ(0U, entry.path.size());
}

void BookmarkHTMLReaderTestWithData::ExpectSecondEpiphanyBookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"Google", entry.title);
  EXPECT_EQ("http://www.google.com/", entry.url.spec());
  EXPECT_EQ(0U, entry.path.size());
}

void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox23Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"Google", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1376102167), entry.creation_time);
  EXPECT_EQ(0U, entry.path.size());
  EXPECT_EQ("https://www.google.com/", entry.url.spec());
}

void BookmarkHTMLReaderTestWithData::ExpectSecondFirefox23Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"Issues", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1376102304), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  EXPECT_EQ(u"Chromium", entry.path.front());
  EXPECT_EQ("https://code.google.com/p/chromium/issues/list", entry.url.spec());
}

void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"CodeSearch", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  EXPECT_EQ(u"Chromium", entry.path.front());
  EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec());
}

void BookmarkHTMLReaderTestWithData::ExpectFirstFirefoxBookmarkWithKeyword(
    const user_data_importer::SearchEngineInfo& info) {
  EXPECT_EQ(u"http://example.{searchTerms}.com/", info.url);
  EXPECT_EQ(u"keyword", info.keyword);
  EXPECT_EQ(u"Bookmark Keyword", info.display_name);
}

void BookmarkHTMLReaderTestWithData::ExpectSecondFirefoxBookmarkWithKeyword(
    const user_data_importer::SearchEngineInfo& info) {
  EXPECT_EQ(u"http://example.com/?q={searchTerms}", info.url);
  EXPECT_EQ(u"keyword", info.keyword);
  EXPECT_EQ(u"BookmarkName", info.display_name);
}

void BookmarkHTMLReaderTestWithData::ExpectFirstEmptyFolderBookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(std::u16string(), entry.title);
  EXPECT_TRUE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  if (entry.path.size() == 1)
    EXPECT_EQ(u"Empty's Parent", entry.path.front());
}

void BookmarkHTMLReaderTestWithData::ExpectSecondEmptyFolderBookmark(
    const user_data_importer::ImportedBookmarkEntry& entry) {
  EXPECT_EQ(u"[Tamura Yukari.com]", entry.title);
  EXPECT_FALSE(entry.is_folder);
  EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time);
  EXPECT_EQ(1U, entry.path.size());
  if (entry.path.size() == 1)
    EXPECT_EQ(std::u16string(), entry.path.front());
  EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
}

}  // namespace

TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) {
  base::FilePath path = test_data_path_.AppendASCII("firefox2.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(3U, bookmarks.size());
  ExpectFirstFirefox2Bookmark(bookmarks[0]);
  ExpectSecondFirefox2Bookmark(bookmarks[1]);
  ExpectThirdFirefox2Bookmark(bookmarks[2]);
}

TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) {
  base::FilePath path = test_data_path_.AppendASCII("firefox23.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(3U, bookmarks.size());
  ExpectFirstFirefox23Bookmark(bookmarks[0]);
  ExpectSecondFirefox23Bookmark(bookmarks[1]);
  ExpectThirdFirefox23Bookmark(bookmarks[2]);
}

TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) {
  base::FilePath path = test_data_path_.AppendASCII("epiphany.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(2U, bookmarks.size());
  ExpectFirstEpiphanyBookmark(bookmarks[0]);
  ExpectSecondEpiphanyBookmark(bookmarks[1]);
}

TEST_F(BookmarkHTMLReaderTestWithData, FirefoxBookmarkFileWithKeywordImport) {
  base::FilePath path = test_data_path_.AppendASCII(
      "firefox_bookmark_keyword.html");

  std::vector<user_data_importer::SearchEngineInfo> search_engines;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      nullptr, &search_engines, nullptr);

  ASSERT_EQ(2U, search_engines.size());
  ExpectFirstFirefoxBookmarkWithKeyword(search_engines[0]);
  ExpectSecondFirefoxBookmarkWithKeyword(search_engines[1]);
}

TEST_F(BookmarkHTMLReaderTestWithData, EmptyFolderImport) {
  base::FilePath path = test_data_path_.AppendASCII("empty_folder.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(3U, bookmarks.size());
  ExpectFirstEmptyFolderBookmark(bookmarks[0]);
  ExpectSecondEmptyFolderBookmark(bookmarks[1]);
  ExpectThirdFirefox2Bookmark(bookmarks[2]);
}

TEST_F(BookmarkHTMLReaderTestWithData,
       RedditSaverFileImport) {
  base::FilePath path = test_data_path_.AppendASCII("redditsaver.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(2U, bookmarks.size());
  EXPECT_EQ(u"Google", bookmarks[0].title);
  EXPECT_EQ(u"YouTube", bookmarks[1].title);
}

// Verifies that importing a bookmarks file without a charset specified succeeds
// (by falling back to a default charset). Per [ http://crbug.com/460423 ], this
// sort of bookmarks file is generated by IE.
TEST_F(BookmarkHTMLReaderTestWithData,
       InternetExplorerBookmarkFileWithoutCharsetImport) {
  base::FilePath path = test_data_path_.AppendASCII("ie_sans_charset.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::RepeatingCallback<bool(const GURL&)>(), path,
                      &bookmarks, nullptr, nullptr);

  ASSERT_EQ(3U, bookmarks.size());
  EXPECT_EQ(u"Google", bookmarks[0].title);
  EXPECT_EQ(u"Outlook", bookmarks[1].title);
  EXPECT_EQ(u"Speed Test", bookmarks[2].title);
}

namespace {

class CancelAfterFifteenCalls {
  int count;
 public:
  CancelAfterFifteenCalls() : count(0) { }
  bool ShouldCancel() {
    return ++count > 16;
  }
};

}  // namespace

TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) {
  // Use a file for testing that has multiple bookmarks.
  base::FilePath path = test_data_path_.AppendASCII("firefox2.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  CancelAfterFifteenCalls cancel_fifteen;
  ImportBookmarksFile(
      base::BindRepeating(&CancelAfterFifteenCalls::ShouldCancel,
                          base::Unretained(&cancel_fifteen)),
      base::RepeatingCallback<bool(const GURL&)>(), path, &bookmarks, nullptr,
      nullptr);

  // The cancellation callback is checked before each line is read, so fifteen
  // lines are imported. The first fifteen lines of firefox2.html include only
  // one bookmark.
  ASSERT_EQ(1U, bookmarks.size());
  ExpectFirstFirefox2Bookmark(bookmarks[0]);
}

namespace {

bool IsURLValid(const GURL& url) {
  // No offense to whomever owns this domain...
  return !url.DomainIs("tamurayukari.com");
}

}  // namespace

TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) {
  // Use a file for testing that has multiple bookmarks.
  base::FilePath path = test_data_path_.AppendASCII("firefox2.html");

  std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
  ImportBookmarksFile(base::RepeatingCallback<bool(void)>(),
                      base::BindRepeating(&IsURLValid), path, &bookmarks,
                      nullptr, nullptr);

  ASSERT_EQ(2U, bookmarks.size());
  ExpectFirstFirefox2Bookmark(bookmarks[0]);
  ExpectThirdFirefox2Bookmark(bookmarks[1]);
}

}  // namespace bookmark_html_reader