-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_update.js
400 lines (342 loc) · 15.4 KB
/
test_update.js
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
/* -*- 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.jsm
"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.fieldInDB(TEST_URL, "description");
Assert.equal(description, descriptionInDB, "description should be updated via URL as expected");
description = "";
await PlacesUtils.history.update({ url: TEST_URL, description });
descriptionInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "description");
Assert.strictEqual(null, descriptionInDB, "an empty description should set it to null in the database");
let guid = await PlacesTestUtils.fieldInDB(TEST_URL, "guid");
description = "Test description";
await PlacesUtils.history.update({ url: TEST_URL, guid, description });
descriptionInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "description");
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.fieldInDB(TEST_URL, "description");
Assert.ok(0 < descriptionInDB.length < description.length, "a long description should be truncated");
description = null;
await PlacesUtils.history.update({ url: TEST_URL, description});
descriptionInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "description");
Assert.strictEqual(description, descriptionInDB, "a null description 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.fieldInDB(TEST_URL, "preview_image_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.fieldInDB(TEST_URL, "preview_image_url");
Assert.strictEqual(null, previewImageURLInDB, "a null previewImageURL should set it to null in the database");
let guid = await PlacesTestUtils.fieldInDB(TEST_URL, "guid");
previewImageURL = IMAGE_URL;
await PlacesUtils.history.update({ guid, previewImageURL });
previewImageURLInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "preview_image_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.fieldInDB(TEST_URL, "preview_image_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.fieldInDB(TEST_URL, "description");
let previewImageURLInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "preview_image_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.fieldInDB(TEST_URL, "description");
previewImageURLInDB = await PlacesTestUtils.fieldInDB(TEST_URL, "preview_image_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: Ci.nsIAnnotationService.TYPE_STRING,
expiration: Ci.nsIAnnotationService.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: Ci.nsIAnnotationService.TYPE_INT64,
expiration: Ci.nsIAnnotationService.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],
]),
});
});