summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_keywords.js
blob: 57b734c5d85cd029a127f64f79a9cd4a9508558c (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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"use strict"

function* check_keyword(aExpectExists, aHref, aKeyword, aPostData = null) {
  // Check case-insensitivity.
  aKeyword = aKeyword.toUpperCase();

  let entry = yield PlacesUtils.keywords.fetch(aKeyword);

  Assert.deepEqual(entry, yield PlacesUtils.keywords.fetch({ keyword: aKeyword }));

  if (aExpectExists) {
    Assert.ok(!!entry, "A keyword should exist");
    Assert.equal(entry.url.href, aHref);
    Assert.equal(entry.postData, aPostData);
    Assert.deepEqual(entry, yield PlacesUtils.keywords.fetch({ keyword: aKeyword, url: aHref }));
    let entries = [];
    yield PlacesUtils.keywords.fetch({ url: aHref }, e => entries.push(e));
    Assert.ok(entries.some(e => e.url.href == aHref && e.keyword == aKeyword.toLowerCase()));
  } else {
    Assert.ok(!entry || entry.url.href != aHref,
              "The given keyword entry should not exist");
    Assert.equal(null, yield PlacesUtils.keywords.fetch({ keyword: aKeyword, url: aHref }));
  }
}

/**
 * Polls the keywords cache waiting for the given keyword entry.
 */
function* promiseKeyword(keyword, expectedHref) {
  let href = null;
  do {
    yield new Promise(resolve => do_timeout(100, resolve));
    let entry = yield PlacesUtils.keywords.fetch(keyword);
    if (entry)
      href = entry.url.href;
  } while (href != expectedHref);
}

function* check_no_orphans() {
  let db = yield PlacesUtils.promiseDBConnection();
  let rows = yield db.executeCached(
    `SELECT id FROM moz_keywords k
     WHERE NOT EXISTS (SELECT 1 FROM moz_places WHERE id = k.place_id)
    `);
  Assert.equal(rows.length, 0);
}

function expectBookmarkNotifications() {
  let notifications = [];
  let observer = new Proxy(NavBookmarkObserver, {
    get(target, name) {
      if (name == "check") {
        PlacesUtils.bookmarks.removeObserver(observer);
        return expectedNotifications =>
          Assert.deepEqual(notifications, expectedNotifications);
      }

      if (name.startsWith("onItemChanged")) {
        return function(itemId, property) {
          if (property != "keyword")
            return;
          let args = Array.from(arguments, arg => {
            if (arg && arg instanceof Ci.nsIURI)
              return new URL(arg.spec);
            if (arg && typeof(arg) == "number" && arg >= Date.now() * 1000)
              return new Date(parseInt(arg/1000));
            return arg;
          });
          notifications.push({ name: name, arguments: args });
        }
      }

      if (name in target)
        return target[name];
      return undefined;
    }
  });
  PlacesUtils.bookmarks.addObserver(observer, false);
  return observer;
}

add_task(function* test_invalid_input() {
  Assert.throws(() => PlacesUtils.keywords.fetch(null),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch(5),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch(undefined),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ keyword: null }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ keyword: {} }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ keyword: 5 }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.fetch({}),
                /At least keyword or url must be provided/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ keyword: "test" }, "test"),
                /onResult callback must be a valid function/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ url: "test" }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ url: {} }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ url: null }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.fetch({ url: "" }),
                /is not a valid URL/);

  Assert.throws(() => PlacesUtils.keywords.insert(null),
                /Input should be a valid object/);
  Assert.throws(() => PlacesUtils.keywords.insert("test"),
                /Input should be a valid object/);
  Assert.throws(() => PlacesUtils.keywords.insert(undefined),
                /Input should be a valid object/);
  Assert.throws(() => PlacesUtils.keywords.insert({ }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: null }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: 5 }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "" }),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", postData: 5 }),
                /Invalid POST data/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", postData: {} }),
                /Invalid POST data/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test" }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", url: 5 }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", url: "" }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", url: null }),
                /is not a valid URL/);
  Assert.throws(() => PlacesUtils.keywords.insert({ keyword: "test", url: "mozilla" }),
                /is not a valid URL/);

  Assert.throws(() => PlacesUtils.keywords.remove(null),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.remove(""),
                /Invalid keyword/);
  Assert.throws(() => PlacesUtils.keywords.remove(5),
                /Invalid keyword/);
});

add_task(function* test_addKeyword() {
  yield check_keyword(false, "http://example.com/", "keyword");
  let fc = yield foreign_count("http://example.com/");
  let observer = expectBookmarkNotifications();

  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });
  observer.check([]);

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 1); // +1 keyword

  // Now remove the keyword.
  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");
  observer.check([]);

  yield check_keyword(false, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc); // -1 keyword

  // Check using URL.
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: new URL("http://example.com/") });
  yield check_keyword(true, "http://example.com/", "keyword");
  yield PlacesUtils.keywords.remove("keyword");
  yield check_keyword(false, "http://example.com/", "keyword");

  yield check_no_orphans();
});

add_task(function* test_addBookmarkAndKeyword() {
  yield check_keyword(false, "http://example.com/", "keyword");
  let fc = yield foreign_count("http://example.com/");
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                      parentGuid: PlacesUtils.bookmarks.unfiledGuid });

  let observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });

  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark.guid)),
                                 "keyword", false, "keyword",
                                 bookmark.lastModified, bookmark.type,
                                 (yield PlacesUtils.promiseItemId(bookmark.parentGuid)),
                                 bookmark.guid, bookmark.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 2); // +1 bookmark +1 keyword

  // Now remove the keyword.
  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");

  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark.guid)),
                                 "keyword", false, "",
                                 bookmark.lastModified, bookmark.type,
                                 (yield PlacesUtils.promiseItemId(bookmark.parentGuid)),
                                 bookmark.guid, bookmark.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  yield check_keyword(false, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 1); // -1 keyword

  // Add again the keyword, then remove the bookmark.
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });

  observer = expectBookmarkNotifications();
  yield PlacesUtils.bookmarks.remove(bookmark.guid);
  // the notification is synchronous but the removal process is async.
  // Unfortunately there's nothing explicit we can wait for.
  while ((yield foreign_count("http://example.com/")));
  // We don't get any itemChanged notification since the bookmark has been
  // removed already.
  observer.check([]);

  yield check_keyword(false, "http://example.com/", "keyword");

  yield check_no_orphans();
});

add_task(function* test_addKeywordToURIHavingKeyword() {
  yield check_keyword(false, "http://example.com/", "keyword");
  let fc = yield foreign_count("http://example.com/");

  let observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });
  observer.check([]);

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 1); // +1 keyword

  yield PlacesUtils.keywords.insert({ keyword: "keyword2", url: "http://example.com/" });

  yield check_keyword(true, "http://example.com/", "keyword");
  yield check_keyword(true, "http://example.com/", "keyword2");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 2); // +1 keyword
  let entries = [];
  let entry = yield PlacesUtils.keywords.fetch({ url: "http://example.com/" }, e => entries.push(e));
  Assert.equal(entries.length, 2);
  Assert.deepEqual(entries[0], entry);

  // Now remove the keywords.
  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");
  yield PlacesUtils.keywords.remove("keyword2");
  observer.check([]);

  yield check_keyword(false, "http://example.com/", "keyword");
  yield check_keyword(false, "http://example.com/", "keyword2");
  Assert.equal((yield foreign_count("http://example.com/")), fc); // -1 keyword

  yield check_no_orphans();
});

add_task(function* test_addBookmarkToURIHavingKeyword() {
  yield check_keyword(false, "http://example.com/", "keyword");
  let fc = yield foreign_count("http://example.com/");
  let observer = expectBookmarkNotifications();

  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });
  observer.check([]);

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 1); // +1 keyword

  observer = expectBookmarkNotifications();
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                      parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  Assert.equal((yield foreign_count("http://example.com/")), fc + 2); // +1 bookmark
  observer.check([]);

  observer = expectBookmarkNotifications();
  yield PlacesUtils.bookmarks.remove(bookmark.guid);
  // the notification is synchronous but the removal process is async.
  // Unfortunately there's nothing explicit we can wait for.
  while ((yield foreign_count("http://example.com/")));
  // We don't get any itemChanged notification since the bookmark has been
  // removed already.
  observer.check([]);

  yield check_keyword(false, "http://example.com/", "keyword");

  yield check_no_orphans();
});

add_task(function* test_sameKeywordDifferentURL() {
  let fc1 = yield foreign_count("http://example1.com/");
  let bookmark1 = yield PlacesUtils.bookmarks.insert({ url: "http://example1.com/",
                                                       type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                       parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  let fc2 = yield foreign_count("http://example2.com/");
  let bookmark2 = yield PlacesUtils.bookmarks.insert({ url: "http://example2.com/",
                                                       type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                       parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example1.com/" });

  yield check_keyword(true, "http://example1.com/", "keyword");
  Assert.equal((yield foreign_count("http://example1.com/")), fc1 + 2); // +1 bookmark +1 keyword
  yield check_keyword(false, "http://example2.com/", "keyword");
  Assert.equal((yield foreign_count("http://example2.com/")), fc2 + 1); // +1 bookmark

  // Assign the same keyword to another url.
  let observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example2.com/" });

  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark1.guid)),
                                 "keyword", false, "",
                                 bookmark1.lastModified, bookmark1.type,
                                 (yield PlacesUtils.promiseItemId(bookmark1.parentGuid)),
                                 bookmark1.guid, bookmark1.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] },
                  { name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark2.guid)),
                                 "keyword", false, "keyword",
                                 bookmark2.lastModified, bookmark2.type,
                                 (yield PlacesUtils.promiseItemId(bookmark2.parentGuid)),
                                 bookmark2.guid, bookmark2.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  yield check_keyword(false, "http://example1.com/", "keyword");
  Assert.equal((yield foreign_count("http://example1.com/")), fc1 + 1); // -1 keyword
  yield check_keyword(true, "http://example2.com/", "keyword");
  Assert.equal((yield foreign_count("http://example2.com/")), fc2 + 2); // +1 keyword

  // Now remove the keyword.
  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");
  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark2.guid)),
                                 "keyword", false, "",
                                 bookmark2.lastModified, bookmark2.type,
                                 (yield PlacesUtils.promiseItemId(bookmark2.parentGuid)),
                                 bookmark2.guid, bookmark2.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  yield check_keyword(false, "http://example1.com/", "keyword");
  yield check_keyword(false, "http://example2.com/", "keyword");
  Assert.equal((yield foreign_count("http://example1.com/")), fc1 + 1);
  Assert.equal((yield foreign_count("http://example2.com/")), fc2 + 1); // -1 keyword

  yield PlacesUtils.bookmarks.remove(bookmark1);
  yield PlacesUtils.bookmarks.remove(bookmark2);
  Assert.equal((yield foreign_count("http://example1.com/")), fc1); // -1 bookmark
  while ((yield foreign_count("http://example2.com/"))); // -1 keyword

  yield check_no_orphans();
});

add_task(function* test_sameURIDifferentKeyword() {
  let fc = yield foreign_count("http://example.com/");

  let observer = expectBookmarkNotifications();
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                      parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield PlacesUtils.keywords.insert({keyword: "keyword", url: "http://example.com/" });

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 2); // +1 bookmark +1 keyword

  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark.guid)),
                                 "keyword", false, "keyword",
                                 bookmark.lastModified, bookmark.type,
                                 (yield PlacesUtils.promiseItemId(bookmark.parentGuid)),
                                 bookmark.guid, bookmark.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.insert({ keyword: "keyword2", url: "http://example.com/" });
  yield check_keyword(true, "http://example.com/", "keyword");
  yield check_keyword(true, "http://example.com/", "keyword2");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 3); // +1 keyword
  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark.guid)),
                                 "keyword", false, "keyword2",
                                 bookmark.lastModified, bookmark.type,
                                 (yield PlacesUtils.promiseItemId(bookmark.parentGuid)),
                                 bookmark.guid, bookmark.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  // Add a third keyword.
  yield PlacesUtils.keywords.insert({ keyword: "keyword3", url: "http://example.com/" });
  yield check_keyword(true, "http://example.com/", "keyword");
  yield check_keyword(true, "http://example.com/", "keyword2");
  yield check_keyword(true, "http://example.com/", "keyword3");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 4); // +1 keyword

  // Remove one of the keywords.
  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");
  yield check_keyword(false, "http://example.com/", "keyword");
  yield check_keyword(true, "http://example.com/", "keyword2");
  yield check_keyword(true, "http://example.com/", "keyword3");
  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark.guid)),
                                 "keyword", false, "",
                                 bookmark.lastModified, bookmark.type,
                                 (yield PlacesUtils.promiseItemId(bookmark.parentGuid)),
                                 bookmark.guid, bookmark.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);
  Assert.equal((yield foreign_count("http://example.com/")), fc + 3); // -1 keyword

  // Now remove the bookmark.
  yield PlacesUtils.bookmarks.remove(bookmark);
  while ((yield foreign_count("http://example.com/")));
  yield check_keyword(false, "http://example.com/", "keyword");
  yield check_keyword(false, "http://example.com/", "keyword2");
  yield check_keyword(false, "http://example.com/", "keyword3");

  check_no_orphans();
});

add_task(function* test_deleteKeywordMultipleBookmarks() {
  let fc = yield foreign_count("http://example.com/");

  let observer = expectBookmarkNotifications();
  let bookmark1 = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                       type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                       parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  let bookmark2 = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                       type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                       parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });

  yield check_keyword(true, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 3); // +2 bookmark +1 keyword
  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark2.guid)),
                                 "keyword", false, "keyword",
                                 bookmark2.lastModified, bookmark2.type,
                                 (yield PlacesUtils.promiseItemId(bookmark2.parentGuid)),
                                 bookmark2.guid, bookmark2.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] },
                  { name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark1.guid)),
                                 "keyword", false, "keyword",
                                 bookmark1.lastModified, bookmark1.type,
                                 (yield PlacesUtils.promiseItemId(bookmark1.parentGuid)),
                                 bookmark1.guid, bookmark1.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  observer = expectBookmarkNotifications();
  yield PlacesUtils.keywords.remove("keyword");
  yield check_keyword(false, "http://example.com/", "keyword");
  Assert.equal((yield foreign_count("http://example.com/")), fc + 2); // -1 keyword
  observer.check([{ name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark2.guid)),
                                 "keyword", false, "",
                                 bookmark2.lastModified, bookmark2.type,
                                 (yield PlacesUtils.promiseItemId(bookmark2.parentGuid)),
                                 bookmark2.guid, bookmark2.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] },
                  { name: "onItemChanged",
                    arguments: [ (yield PlacesUtils.promiseItemId(bookmark1.guid)),
                                 "keyword", false, "",
                                 bookmark1.lastModified, bookmark1.type,
                                 (yield PlacesUtils.promiseItemId(bookmark1.parentGuid)),
                                 bookmark1.guid, bookmark1.parentGuid, "",
                                 Ci.nsINavBookmarksService.SOURCE_DEFAULT ] } ]);

  // Now remove the bookmarks.
  yield PlacesUtils.bookmarks.remove(bookmark1);
  yield PlacesUtils.bookmarks.remove(bookmark2);
  Assert.equal((yield foreign_count("http://example.com/")), fc); // -2 bookmarks

  check_no_orphans();
});

add_task(function* test_multipleKeywordsSamePostData() {
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/", postData: "postData1" });
  yield check_keyword(true, "http://example.com/", "keyword", "postData1");
  // Add another keyword with same postData, should fail.
  yield Assert.rejects(PlacesUtils.keywords.insert({ keyword: "keyword2", url: "http://example.com/", postData: "postData1" }),
                       /constraint failed/);
  yield check_keyword(false, "http://example.com/", "keyword2", "postData1");

  yield PlacesUtils.keywords.remove("keyword");

  check_no_orphans();
});

add_task(function* test_oldPostDataAPI() {
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                      parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com/" });
  let itemId = yield PlacesUtils.promiseItemId(bookmark.guid);
  yield PlacesUtils.setPostDataForBookmark(itemId, "postData");
  yield check_keyword(true, "http://example.com/", "keyword", "postData");
  Assert.equal(PlacesUtils.getPostDataForBookmark(itemId), "postData");

  yield PlacesUtils.keywords.remove("keyword");
  yield PlacesUtils.bookmarks.remove(bookmark);

  check_no_orphans();
});

add_task(function* test_oldKeywordsAPI() {
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example.com/",
                                                    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                    parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield check_keyword(false, "http://example.com/", "keyword");
  let itemId = yield PlacesUtils.promiseItemId(bookmark.guid);

  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword");
  yield promiseKeyword("keyword", "http://example.com/");

  // Remove the keyword.
  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "");
  yield promiseKeyword("keyword", null);

  yield PlacesUtils.keywords.insert({ keyword: "keyword", url: "http://example.com" });
  Assert.equal(PlacesUtils.bookmarks.getKeywordForBookmark(itemId), "keyword");
  Assert.equal(PlacesUtils.bookmarks.getURIForKeyword("keyword").spec, "http://example.com/");
  yield PlacesUtils.bookmarks.remove(bookmark);

  check_no_orphans();
});

add_task(function* test_bookmarkURLChange() {
  let fc1 = yield foreign_count("http://example1.com/");
  let fc2 = yield foreign_count("http://example2.com/");
  let bookmark = yield PlacesUtils.bookmarks.insert({ url: "http://example1.com/",
                                                      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                      parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  yield PlacesUtils.keywords.insert({ keyword: "keyword",
                                      url: "http://example1.com/" });

  yield check_keyword(true, "http://example1.com/", "keyword");
  Assert.equal((yield foreign_count("http://example1.com/")), fc1 + 2); // +1 bookmark +1 keyword

  yield PlacesUtils.bookmarks.update({ guid: bookmark.guid,
                                       url: "http://example2.com/"});
  yield promiseKeyword("keyword", "http://example2.com/");

  yield check_keyword(false, "http://example1.com/", "keyword");
  yield check_keyword(true, "http://example2.com/", "keyword");
  Assert.equal((yield foreign_count("http://example1.com/")), fc1); // -1 bookmark -1 keyword
  Assert.equal((yield foreign_count("http://example2.com/")), fc2 + 2); // +1 bookmark +1 keyword
});