summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/history/test_update.js
blob: df5bcc7195cb73eae3a957f1dfeaf9d2f1f090ad (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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests for `History.update` as implemented in History.sys.mjs

"use strict";

add_task(async function test_error_cases() {
  Assert.throws(
    () => PlacesUtils.history.update("not an object"),
    /Error: PageInfo: Input should be a valid object/,
    "passing a string as pageInfo should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.update(null),
    /Error: PageInfo: Input should be/,
    "passing a null as pageInfo should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        description: "Test description",
      }),
    /Error: PageInfo: The following properties were expected: url, guid/,
    "not included a url or a guid should throw"
  );
  Assert.throws(
    () => PlacesUtils.history.update({ url: "not a valid url string" }),
    /Error: PageInfo: Invalid value for property/,
    "passing an invalid url should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        description: 123,
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a non-string description in pageInfo should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        guid: "invalid guid",
        description: "Test description",
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a invalid guid in pageInfo should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        previewImageURL: "not a valid url string",
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing an invlid preview image url in pageInfo should throw an Error"
  );
  Assert.throws(
    () => {
      let imageName = "a-very-long-string".repeat(10000);
      let previewImageURL = `http://valid.uri.com/${imageName}.png`;
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        previewImageURL,
      });
    },
    /Error: PageInfo: Invalid value for property/,
    "passing an oversized previewImageURL in pageInfo should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.update({ url: "http://valid.uri.com" }),
    /TypeError: pageInfo object must at least/,
    "passing a pageInfo with neither description, previewImageURL, nor annotations should throw a TypeError"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        annotations: "asd",
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a pageInfo with incorrect annotations type should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        annotations: new Map(),
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a pageInfo with an empty annotations type should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        annotations: new Map([[1234, "value"]]),
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a pageInfo with an invalid key type should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        annotations: new Map([["test", ["myarray"]]]),
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a pageInfo with an invalid key type should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.update({
        url: "http://valid.uri.com",
        annotations: new Map([["test", { anno: "value" }]]),
      }),
    /Error: PageInfo: Invalid value for property/,
    "passing a pageInfo with an invalid key type should throw an Error"
  );
});

add_task(async function test_description_change_saved() {
  await PlacesUtils.history.clear();

  let TEST_URL = "http://mozilla.org/test_description_change_saved";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(await PlacesTestUtils.isPageInDB(TEST_URL));

  let description = "Test description";
  await PlacesUtils.history.update({ url: TEST_URL, description });
  let descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    { url: TEST_URL }
  );
  Assert.equal(
    description,
    descriptionInDB,
    "description should be updated via URL as expected"
  );

  description = "";
  await PlacesUtils.history.update({ url: TEST_URL, description });
  descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    { url: TEST_URL }
  );
  Assert.strictEqual(
    null,
    descriptionInDB,
    "an empty description should set it to null in the database"
  );

  let guid = await PlacesTestUtils.getDatabaseValue("moz_places", "guid", {
    url: TEST_URL,
  });
  description = "Test description";
  await PlacesUtils.history.update({ url: TEST_URL, guid, description });
  descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    { url: TEST_URL }
  );
  Assert.equal(
    description,
    descriptionInDB,
    "description should be updated via GUID as expected"
  );

  description = "Test descipriton".repeat(1000);
  await PlacesUtils.history.update({ url: TEST_URL, description });
  descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    { url: TEST_URL }
  );
  Assert.ok(
    !!descriptionInDB.length < description.length,
    "a long description should be truncated"
  );

  description = null;
  await PlacesUtils.history.update({ url: TEST_URL, description });
  descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    { url: TEST_URL }
  );
  Assert.strictEqual(
    description,
    descriptionInDB,
    "a null description should set it to null in the database"
  );
});

add_task(async function test_siteName_change_saved() {
  await PlacesUtils.history.clear();

  let TEST_URL = "http://mozilla.org/test_siteName_change_saved";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(await PlacesTestUtils.isPageInDB(TEST_URL));

  let siteName = "Test site name";
  await PlacesUtils.history.update({ url: TEST_URL, siteName });
  let siteNameInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "site_name",
    { url: TEST_URL }
  );
  Assert.equal(
    siteName,
    siteNameInDB,
    "siteName should be updated via URL as expected"
  );

  siteName = "";
  await PlacesUtils.history.update({ url: TEST_URL, siteName });
  siteNameInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "site_name",
    { url: TEST_URL }
  );
  Assert.strictEqual(
    null,
    siteNameInDB,
    "an empty siteName should set it to null in the database"
  );

  let guid = await PlacesTestUtils.getDatabaseValue("moz_places", "guid", {
    url: TEST_URL,
  });
  siteName = "Test site name";
  await PlacesUtils.history.update({ url: TEST_URL, guid, siteName });
  siteNameInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "site_name",
    { url: TEST_URL }
  );
  Assert.equal(
    siteName,
    siteNameInDB,
    "siteName should be updated via GUID as expected"
  );

  siteName = "Test site name".repeat(1000);
  await PlacesUtils.history.update({ url: TEST_URL, siteName });
  siteNameInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "site_name",
    {
      url: TEST_URL,
    }
  );
  Assert.ok(
    !!siteNameInDB.length < siteName.length,
    "a long siteName should be truncated"
  );

  siteName = null;
  await PlacesUtils.history.update({ url: TEST_URL, siteName });
  siteNameInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "site_name",
    {
      url: TEST_URL,
    }
  );
  Assert.strictEqual(
    siteName,
    siteNameInDB,
    "a null siteName should set it to null in the database"
  );
});

add_task(async function test_previewImageURL_change_saved() {
  await PlacesUtils.history.clear();

  let TEST_URL = "http://mozilla.org/test_previewImageURL_change_saved";
  let IMAGE_URL = "http://mozilla.org/test_preview_image.png";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(await PlacesTestUtils.isPageInDB(TEST_URL));

  let previewImageURL = IMAGE_URL;
  await PlacesUtils.history.update({ url: TEST_URL, previewImageURL });
  let previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.equal(
    previewImageURL,
    previewImageURLInDB,
    "previewImageURL should be updated via URL as expected"
  );

  previewImageURL = null;
  await PlacesUtils.history.update({ url: TEST_URL, previewImageURL });
  previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.strictEqual(
    null,
    previewImageURLInDB,
    "a null previewImageURL should set it to null in the database"
  );

  let guid = await PlacesTestUtils.getDatabaseValue("moz_places", "guid", {
    url: TEST_URL,
  });
  previewImageURL = IMAGE_URL;
  await PlacesUtils.history.update({ guid, previewImageURL });
  previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.equal(
    previewImageURL,
    previewImageURLInDB,
    "previewImageURL should be updated via GUID as expected"
  );

  previewImageURL = "";
  await PlacesUtils.history.update({ url: TEST_URL, previewImageURL });
  previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.strictEqual(
    null,
    previewImageURLInDB,
    "an empty previewImageURL should set it to null in the database"
  );
});

add_task(async function test_change_description_and_preview_saved() {
  await PlacesUtils.history.clear();

  let TEST_URL = "http://mozilla.org/test_change_both_saved";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(await PlacesTestUtils.isPageInDB(TEST_URL));

  let description = "Test description";
  let previewImageURL = "http://mozilla.org/test_preview_image.png";

  await PlacesUtils.history.update({
    url: TEST_URL,
    description,
    previewImageURL,
  });
  let descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    {
      url: TEST_URL,
    }
  );
  let previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.equal(
    description,
    descriptionInDB,
    "description should be updated via URL as expected"
  );
  Assert.equal(
    previewImageURL,
    previewImageURLInDB,
    "previewImageURL should be updated via URL as expected"
  );

  // Update description should not touch other fields
  description = null;
  await PlacesUtils.history.update({ url: TEST_URL, description });
  descriptionInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "description",
    {
      url: TEST_URL,
    }
  );
  previewImageURLInDB = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "preview_image_url",
    {
      url: TEST_URL,
    }
  );
  Assert.strictEqual(
    description,
    descriptionInDB,
    "description should be updated via URL as expected"
  );
  Assert.equal(
    previewImageURL,
    previewImageURLInDB,
    "previewImageURL should not be updated"
  );
});

/**
 * Gets annotation information from the database for the specified URL and
 * annotation name.
 *
 * @param {String} pageUrl The URL to search for.
 * @param {String} annoName The name of the annotation to search for.
 * @return {Array} An array of objects containing the annotations found.
 */
async function getAnnotationInfoFromDB(pageUrl, annoName) {
  let db = await PlacesUtils.promiseDBConnection();

  let rows = await db.execute(
    `
    SELECT a.content, a.flags, a.expiration, a.type FROM moz_anno_attributes n
    JOIN moz_annos a ON n.id = a.anno_attribute_id
    JOIN moz_places h ON h.id = a.place_id
    WHERE h.url_hash = hash(:pageUrl) AND h.url = :pageUrl
      AND n.name = :annoName
  `,
    { annoName, pageUrl }
  );

  let result = rows.map(row => {
    return {
      content: row.getResultByName("content"),
      flags: row.getResultByName("flags"),
      expiration: row.getResultByName("expiration"),
      type: row.getResultByName("type"),
    };
  });

  return result;
}

add_task(async function test_simple_change_annotations() {
  await PlacesUtils.history.clear();

  const TEST_URL = "http://mozilla.org/test_change_both_saved";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(
    await PlacesTestUtils.isPageInDB(TEST_URL),
    "Should have inserted the page into the database."
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([["test/annotation", "testContent"]]),
  });

  let pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    1,
    "Should have one annotation for the page"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    "testContent",
    "Should have the correct annotation"
  );

  let annotationInfo = await getAnnotationInfoFromDB(
    TEST_URL,
    "test/annotation"
  );
  Assert.deepEqual(
    {
      content: "testContent",
      flags: 0,
      type: PlacesUtils.history.ANNOTATION_TYPE_STRING,
      expiration: PlacesUtils.history.ANNOTATION_EXPIRE_NEVER,
    },
    annotationInfo[0],
    "Should have stored the correct annotation data in the db"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([["test/annotation2", "testAnno"]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    2,
    "Should have two annotations for the page"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    "testContent",
    "Should have the correct value for the first annotation"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    "testAnno",
    "Should have the correct value for the second annotation"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([["test/annotation", 1234]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    2,
    "Should still have two annotations for the page"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    1234,
    "Should have the updated the first annotation value"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    "testAnno",
    "Should have kept the value for the second annotation"
  );

  annotationInfo = await getAnnotationInfoFromDB(TEST_URL, "test/annotation");
  Assert.deepEqual(
    {
      content: 1234,
      flags: 0,
      type: PlacesUtils.history.ANNOTATION_TYPE_INT64,
      expiration: PlacesUtils.history.ANNOTATION_EXPIRE_NEVER,
    },
    annotationInfo[0],
    "Should have updated the annotation data in the db"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([["test/annotation", null]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    1,
    "Should have removed only the first annotation"
  );
  Assert.strictEqual(
    pageInfo.annotations.get("test/annotation"),
    undefined,
    "Should have removed only the first annotation"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    "testAnno",
    "Should have kept the value for the second annotation"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([["test/annotation2", null]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(pageInfo.annotations.size, 0, "Should have no annotations left");

  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.execute(`
    SELECT * FROM moz_annos
  `);
  Assert.equal(rows.length, 0, "Should be no annotations left in the db");
});

add_task(async function test_change_multiple_annotations() {
  await PlacesUtils.history.clear();

  const TEST_URL = "http://mozilla.org/test_change_both_saved";
  await PlacesTestUtils.addVisits(TEST_URL);
  Assert.ok(
    await PlacesTestUtils.isPageInDB(TEST_URL),
    "Should have inserted the page into the database."
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([
      ["test/annotation", "testContent"],
      ["test/annotation2", "testAnno"],
    ]),
  });

  let pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    2,
    "Should have inserted the two annotations for the page."
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    "testContent",
    "Should have the correct value for the first annotation"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    "testAnno",
    "Should have the correct value for the second annotation"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([
      ["test/annotation", 123456],
      ["test/annotation2", 135246],
    ]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(
    pageInfo.annotations.size,
    2,
    "Should have two annotations for the page"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    123456,
    "Should have the correct value for the first annotation"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    135246,
    "Should have the correct value for the second annotation"
  );

  await PlacesUtils.history.update({
    url: TEST_URL,
    annotations: new Map([
      ["test/annotation", null],
      ["test/annotation2", null],
    ]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
    includeAnnotations: true,
  });

  Assert.equal(pageInfo.annotations.size, 0, "Should have no annotations left");
});

add_task(async function test_annotations_nonexisting_page() {
  info("Adding annotations to a non existing page should be silent");
  await PlacesUtils.history.update({
    url: "http://nonexisting.moz/",
    annotations: new Map([["test/annotation", null]]),
  });
});

add_task(async function test_annotations_nonexisting_page() {
  info("Adding annotations to a non existing page should be silent");
  await PlacesUtils.history.update({
    url: "http://nonexisting.moz/",
    annotations: new Map([["test/annotation", null]]),
  });
});