summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_bookmarks_html_import_tags.js
blob: f01048e1a5a3b7070534fcc94897150d9910620e (plain)
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
var bookmarkData = [
  {
    uri: uri("http://www.toastytech.com"),
    title: "Nathan's Toasty Technology Page",
    tags: ["technology", "personal", "retro"],
  },
  {
    uri: uri("http://www.reddit.com"),
    title: "reddit: the front page of the internet",
    tags: ["social media", "news", "humour"],
  },
  {
    uri: uri("http://www.4chan.org"),
    title: "4chan",
    tags: ["discussion", "imageboard", "anime"],
  },
];

/*
  TEST SUMMARY
  - Add bookmarks with tags
  - Export tagged bookmarks as HTML file
  - Delete bookmarks
  - Import bookmarks from HTML file
  - Check that all bookmarks are successfully imported with tags
*/

add_task(async function test_import_tags() {
  // Removes bookmarks.html if the file already exists.
  let HTMLFile = PathUtils.join(PathUtils.profileDir, "bookmarks.html");
  await IOUtils.remove(HTMLFile, { ignoreAbsent: true });

  // Adds bookmarks and tags to the database.
  let bookmarkList = new Set();
  for (let { uri, title, tags } of bookmarkData) {
    bookmarkList.add(
      await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: uri,
        title,
      })
    );
    PlacesUtils.tagging.tagURI(uri, tags);
  }

  // Exports the bookmarks as a HTML file.
  await BookmarkHTMLUtils.exportToFile(HTMLFile);

  // Deletes bookmarks and tags from the database.
  for (let bookmark of bookmarkList) {
    await PlacesUtils.bookmarks.remove(bookmark.guid);
  }

  // Re-imports the bookmarks from the HTML file.
  await BookmarkHTMLUtils.importFromFile(HTMLFile, { replace: true });

  // Tests to ensure that the tags are still present for each bookmark URI.
  for (let { uri, tags } of bookmarkData) {
    info("Test tags for " + uri.spec + ": " + tags + "\n");
    let foundTags = PlacesUtils.tagging.getTagsForURI(uri);
    Assert.equal(foundTags.length, tags.length);
    Assert.ok(tags.every(tag => foundTags.includes(tag)));
  }
});