summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_384370.js
blob: cc760e32768f4f23e26f2d650da94f1a94c859e0 (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
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
var tagData = [
  { uri: uri("http://slint.us"), tags: ["indie", "kentucky", "music"] },
  {
    uri: uri("http://en.wikipedia.org/wiki/Diplodocus"),
    tags: ["dinosaur", "dj", "rad word"],
  },
];

var bookmarkData = [
  { uri: uri("http://slint.us"), title: "indie,kentucky,music" },
  {
    uri: uri("http://en.wikipedia.org/wiki/Diplodocus"),
    title: "dinosaur,dj,rad word",
  },
];

/*
  HTML+FEATURES SUMMARY:
  - import legacy bookmarks
  - export as json, import, test (tests integrity of html > json)
  - export as html, import, test (tests integrity of json > html)

  BACKUP/RESTORE SUMMARY:
  - create a bookmark in each root
  - tag multiple URIs with multiple tags
  - export as json, import, test
*/
add_task(async function () {
  // Remove eventual bookmarks.exported.json.
  let jsonFile = PathUtils.join(
    PathUtils.profileDir,
    "bookmarks.exported.json"
  );
  await IOUtils.remove(jsonFile, { ignoreAbsent: true });

  // Test importing a pre-Places canonical bookmarks file.
  // Note: we do not empty the db before this import to catch bugs like 380999
  let htmlFile = PathUtils.join(do_get_cwd().path, "bookmarks.preplaces.html");
  await BookmarkHTMLUtils.importFromFile(htmlFile, { replace: true });

  // Populate the database.
  for (let { uri, tags } of tagData) {
    PlacesUtils.tagging.tagURI(uri, tags);
  }
  for (let { uri, title } of bookmarkData) {
    await PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url: uri,
      title,
    });
  }
  for (let { uri, title } of bookmarkData) {
    await PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
      url: uri,
      title,
    });
  }

  await validate("initial database");

  // Test exporting a Places canonical json file.
  // 1. export to bookmarks.exported.json
  await BookmarkJSONUtils.exportToFile(jsonFile);
  info("exported json");

  // 2. empty bookmarks db
  // 3. import bookmarks.exported.json
  await BookmarkJSONUtils.importFromFile(jsonFile, { replace: true });
  info("imported json");

  // 4. run the test-suite
  await validate("re-imported json");
  info("validated import");
});

async function validate(infoMsg) {
  info(`Validating ${infoMsg}: testMenuBookmarks`);
  await testMenuBookmarks();
  info(`Validating ${infoMsg}: testToolbarBookmarks`);
  await testToolbarBookmarks();
  info(`Validating ${infoMsg}: testUnfiledBookmarks`);
  testUnfiledBookmarks();
  info(`Validating ${infoMsg}: testTags`);
  testTags();
  await PlacesTestUtils.promiseAsyncUpdates();
}

// Tests a bookmarks datastore that has a set of bookmarks, etc
// that flex each supported field and feature.
async function testMenuBookmarks() {
  let root = PlacesUtils.getFolderContents(PlacesUtils.bookmarks.menuGuid).root;
  Assert.equal(root.childCount, 3);

  let separatorNode = root.getChild(1);
  Assert.equal(separatorNode.type, separatorNode.RESULT_TYPE_SEPARATOR);

  let folderNode = root.getChild(2);
  Assert.equal(folderNode.type, folderNode.RESULT_TYPE_FOLDER);
  Assert.equal(folderNode.title, "test");
  let folder = await PlacesUtils.bookmarks.fetch(folderNode.bookmarkGuid);
  Assert.equal(folder.dateAdded.getTime(), 1177541020000);

  Assert.equal(PlacesUtils.asQuery(folderNode).hasChildren, true);

  // open test folder, and test the children
  folderNode.containerOpen = true;
  Assert.equal(folderNode.childCount, 1);

  let bookmarkNode = folderNode.getChild(0);
  Assert.equal("http://test/post", bookmarkNode.uri);
  Assert.equal("test post keyword", bookmarkNode.title);
  Assert.equal(bookmarkNode.dateAdded, 1177375336000000);

  let entry = await PlacesUtils.keywords.fetch({ url: bookmarkNode.uri });
  Assert.equal("test", entry.keyword);
  Assert.equal("hidden1%3Dbar&text1%3D%25s", entry.postData);

  let pageInfo = await PlacesUtils.history.fetch(bookmarkNode.uri, {
    includeAnnotations: true,
  });
  Assert.equal(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    "ISO-8859-1",
    "Should have the correct charset"
  );

  folderNode.containerOpen = false;
  root.containerOpen = false;
}

async function testToolbarBookmarks() {
  let root = PlacesUtils.getFolderContents(
    PlacesUtils.bookmarks.toolbarGuid
  ).root;

  // child count (add 2 for pre-existing items, one of the feeds is skipped
  // because it doesn't have href)
  Assert.equal(root.childCount, bookmarkData.length + 2);

  // Livemarks are no more supported but may still exist in old html files.
  let legacyLivemarkNode = root.getChild(1);
  Assert.equal("Latest Headlines", legacyLivemarkNode.title);
  Assert.equal(
    "http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
    legacyLivemarkNode.uri
  );
  Assert.equal(
    legacyLivemarkNode.type,
    Ci.nsINavHistoryResultNode.RESULT_TYPE_URI
  );

  // test added bookmark data
  let bookmarkNode = root.getChild(2);
  Assert.equal(bookmarkNode.uri, bookmarkData[0].uri.spec);
  Assert.equal(bookmarkNode.title, bookmarkData[0].title);
  bookmarkNode = root.getChild(3);
  Assert.equal(bookmarkNode.uri, bookmarkData[1].uri.spec);
  Assert.equal(bookmarkNode.title, bookmarkData[1].title);

  root.containerOpen = false;
}

function testUnfiledBookmarks() {
  let root = PlacesUtils.getFolderContents(
    PlacesUtils.bookmarks.unfiledGuid
  ).root;
  // child count (add 1 for pre-existing item)
  Assert.equal(root.childCount, bookmarkData.length + 1);
  for (let i = 1; i < root.childCount; ++i) {
    let child = root.getChild(i);
    Assert.equal(child.uri, bookmarkData[i - 1].uri.spec);
    Assert.equal(child.title, bookmarkData[i - 1].title);
    if (child.tags) {
      Assert.equal(child.tags, bookmarkData[i - 1].title);
    }
  }
  root.containerOpen = false;
}

function testTags() {
  for (let { uri, tags } of tagData) {
    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)));
  }
}