summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/unit_cps2/head.js
blob: b86abe208a3b717bdf78d9523aa217f837a9f4cc (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

var { interfaces: Ci, classes: Cc, results: Cr, utils: Cu } = Components;

Cu.import("resource://gre/modules/Services.jsm");

var cps;
var asyncRunner;
var next;

(function init() {
  // There has to be a profile directory before the CPS service is gotten.
  do_get_profile();
})();

function runAsyncTests(tests, dontResetBefore = false) {
  do_test_pending();

  cps = Cc["@mozilla.org/content-pref/service;1"].
        getService(Ci.nsIContentPrefService2);

  let s = {};
  Cu.import("resource://test/AsyncRunner.jsm", s);
  asyncRunner = new s.AsyncRunner({
    done: do_test_finished,
    error: function (err) {
      // xpcshell test functions like equal throw NS_ERROR_ABORT on
      // failure.  Ignore those and catch only uncaught exceptions.
      if (err !== Cr.NS_ERROR_ABORT) {
        if (err.stack) {
          err = err + "\n\nTraceback (most recent call first):\n" + err.stack +
                      "\nUseless do_throw stack:";
        }
        do_throw(err);
      }
    },
    consoleError: function (scriptErr) {
      // Previously, this code checked for console errors related to the test,
      // and treated them as failures. This was problematic, because our current
      // very-broken exception reporting machinery in XPCWrappedJSClass reports
      // errors to the console even if there's actually JS on the stack above
      // that will catch them. And a lot of the tests here intentionally trigger
      // error conditions on the JS-implemented XPCOM component (see erroneous()
      // in test_getSubdomains.js, for example). In the old world, we got lucky,
      // and the errors were never reported to the console due to happenstantial
      // JSContext reasons that aren't really worth going into.
      //
      // So. We make sure to dump this stuff so that it shows up in the logs, but
      // don't turn them into duplicate failures of the exception that was already
      // propagated to the caller.
      dump("AsyncRunner.jsm observed console error: " +  scriptErr + "\n");
    }
  });

  next = asyncRunner.next.bind(asyncRunner);

  do_register_cleanup(function () {
    asyncRunner.destroy();
    asyncRunner = null;
  });

  tests.forEach(function (test) {
    function* gen() {
      do_print("Running " + test.name);
      yield test();
      yield reset();
    }
    asyncRunner.appendIterator(gen());
  });

  // reset() ends up calling asyncRunner.next(), starting the tests.
  if (dontResetBefore) {
    next();
  } else {
    reset();
  }
}

function makeCallback(callbacks, success = null) {
  callbacks = callbacks || {};
  if (!callbacks.handleError) {
    callbacks.handleError = function (error) {
      do_throw("handleError call was not expected, error: " + error);
    };
  }
  if (!callbacks.handleResult) {
    callbacks.handleResult = function() {
      do_throw("handleResult call was not expected");
    };
  }
  if (!callbacks.handleCompletion)
    callbacks.handleCompletion = function (reason) {
      equal(reason, Ci.nsIContentPrefCallback2.COMPLETE_OK);
      if (success) {
        success();
      } else {
        next();
      }
    };
  return callbacks;
}

function do_check_throws(fn) {
  let threw = false;
  try {
    fn();
  }
  catch (err) {
    threw = true;
  }
  ok(threw);
}

function sendMessage(msg, callback) {
  let obj = callback || {};
  let ref = Cu.getWeakReference(obj);
  cps.QueryInterface(Ci.nsIObserver).observe(ref, "test:" + msg, null);
  return "value" in obj ? obj.value : undefined;
}

function reset() {
  sendMessage("reset", next);
}

function setWithDate(group, name, val, timestamp, context) {
  function updateDate() {
    let db = sendMessage("db");
    let stmt = db.createAsyncStatement(`
      UPDATE prefs SET timestamp = :timestamp
      WHERE
        settingID = (SELECT id FROM settings WHERE name = :name)
        AND groupID = (SELECT id FROM groups WHERE name = :group)
    `);
    stmt.params.timestamp = timestamp / 1000;
    stmt.params.name = name;
    stmt.params.group = group;

    stmt.executeAsync({
      handleCompletion: function (reason) {
        next();
      },
      handleError: function (err) {
        do_throw(err);
      }
    });
    stmt.finalize();
  }

  cps.set(group, name, val, context, makeCallback(null, updateDate));
}

function getDate(group, name, context) {
  let db = sendMessage("db");
  let stmt = db.createAsyncStatement(`
    SELECT timestamp FROM prefs
    WHERE
      settingID = (SELECT id FROM settings WHERE name = :name)
      AND groupID = (SELECT id FROM groups WHERE name = :group)
  `);
  stmt.params.name = name;
  stmt.params.group = group;

  let res;
  stmt.executeAsync({
    handleResult: function (results) {
      let row = results.getNextRow();
      res = row.getResultByName("timestamp");
    },
    handleCompletion: function (reason) {
      next(res * 1000);
    },
    handleError: function (err) {
      do_throw(err);
    }
  });
  stmt.finalize();
}

function set(group, name, val, context) {
  cps.set(group, name, val, context, makeCallback());
}

function setGlobal(name, val, context) {
  cps.setGlobal(name, val, context, makeCallback());
}

function prefOK(actual, expected, strict) {
  ok(actual instanceof Ci.nsIContentPref);
  equal(actual.domain, expected.domain);
  equal(actual.name, expected.name);
  if (strict)
    strictEqual(actual.value, expected.value);
  else
    equal(actual.value, expected.value);
}

function* getOK(args, expectedVal, expectedGroup, strict) {
  if (args.length == 2)
    args.push(undefined);
  let expectedPrefs = expectedVal === undefined ? [] :
                      [{ domain: expectedGroup || args[0],
                         name: args[1],
                         value: expectedVal }];
  yield getOKEx("getByDomainAndName", args, expectedPrefs, strict);
}

function* getSubdomainsOK(args, expectedGroupValPairs) {
  if (args.length == 2)
    args.push(undefined);
  let expectedPrefs = expectedGroupValPairs.map(function ([group, val]) {
    return { domain: group, name: args[1], value: val };
  });
  yield getOKEx("getBySubdomainAndName", args, expectedPrefs);
}

function* getGlobalOK(args, expectedVal) {
  if (args.length == 1)
    args.push(undefined);
  let expectedPrefs = expectedVal === undefined ? [] :
                      [{ domain: null, name: args[0], value: expectedVal }];
  yield getOKEx("getGlobal", args, expectedPrefs);
}

function* getOKEx(methodName, args, expectedPrefs, strict, context) {
  let actualPrefs = [];
  args.push(makeCallback({
    handleResult: pref => actualPrefs.push(pref)
  }));
  yield cps[methodName].apply(cps, args);
  arraysOfArraysOK([actualPrefs], [expectedPrefs], function (actual, expected) {
    prefOK(actual, expected, strict);
  });
}

function getCachedOK(args, expectedIsCached, expectedVal, expectedGroup,
                     strict) {
  if (args.length == 2)
    args.push(undefined);
  let expectedPref = !expectedIsCached ? null : {
    domain: expectedGroup || args[0],
    name: args[1],
    value: expectedVal
  };
  getCachedOKEx("getCachedByDomainAndName", args, expectedPref, strict);
}

function getCachedSubdomainsOK(args, expectedGroupValPairs) {
  if (args.length == 2)
    args.push(undefined);
  let len = {};
  args.push(len);
  let actualPrefs = cps.getCachedBySubdomainAndName.apply(cps, args);
  actualPrefs = actualPrefs.sort(function (a, b) {
    return a.domain.localeCompare(b.domain);
  });
  equal(actualPrefs.length, len.value);
  let expectedPrefs = expectedGroupValPairs.map(function ([group, val]) {
    return { domain: group, name: args[1], value: val };
  });
  arraysOfArraysOK([actualPrefs], [expectedPrefs], prefOK);
}

function getCachedGlobalOK(args, expectedIsCached, expectedVal) {
  if (args.length == 1)
    args.push(undefined);
  let expectedPref = !expectedIsCached ? null : {
    domain: null,
    name: args[0],
    value: expectedVal
  };
  getCachedOKEx("getCachedGlobal", args, expectedPref);
}

function getCachedOKEx(methodName, args, expectedPref, strict) {
  let actualPref = cps[methodName].apply(cps, args);
  if (expectedPref)
    prefOK(actualPref, expectedPref, strict);
  else
    strictEqual(actualPref, null);
}

function arraysOK(actual, expected, cmp) {
  if (actual.length != expected.length) {
    do_throw("Length is not equal: " + JSON.stringify(actual) + "==" + JSON.stringify(expected));
  } else {
    actual.forEach(function (actualElt, j) {
      let expectedElt = expected[j];
      cmp(actualElt, expectedElt);
    });
  }
}

function arraysOfArraysOK(actual, expected, cmp) {
  cmp = cmp || equal;
  arraysOK(actual, expected, function (act, exp) {
    arraysOK(act, exp, cmp)
  });
}

function dbOK(expectedRows) {
  let db = sendMessage("db");
  let stmt = db.createAsyncStatement(`
    SELECT groups.name AS grp, settings.name AS name, prefs.value AS value
    FROM prefs
    LEFT JOIN groups ON groups.id = prefs.groupID
    LEFT JOIN settings ON settings.id = prefs.settingID
    UNION

    /*
      These second two SELECTs get the rows of the groups and settings tables
      that aren't referenced by the prefs table.  Neither should return any
      rows if the component is working properly.
    */
    SELECT groups.name AS grp, NULL AS name, NULL AS value
    FROM groups
    WHERE id NOT IN (
      SELECT DISTINCT groupID
      FROM prefs
      WHERE groupID NOTNULL
    )
    UNION
    SELECT NULL AS grp, settings.name AS name, NULL AS value
    FROM settings
    WHERE id NOT IN (
      SELECT DISTINCT settingID
      FROM prefs
      WHERE settingID NOTNULL
    )

    ORDER BY value ASC, grp ASC, name ASC
  `);

  let actualRows = [];
  let cols = ["grp", "name", "value"];

  db.executeAsync([stmt], 1, {
    handleCompletion: function (reason) {
      arraysOfArraysOK(actualRows, expectedRows);
      next();
    },
    handleResult: function (results) {
      let row = null;
      while (row = results.getNextRow()) {
        actualRows.push(cols.map(c => row.getResultByName(c)));
      }
    },
    handleError: function (err) {
      do_throw(err);
    }
  });
  stmt.finalize();
}

function on(event, names, dontRemove) {
  let args = {
    reset: function () {
      for (let prop in this) {
        if (Array.isArray(this[prop]))
          this[prop].splice(0, this[prop].length);
      }
    },
  };

  let observers = {};

  names.forEach(function (name) {
    let obs = {};
    ["onContentPrefSet", "onContentPrefRemoved"].forEach(function (meth) {
      obs[meth] = () => do_throw(meth + " should not be called");
    });
    obs["onContentPref" + event] = function () {
      args[name].push(Array.slice(arguments));
    };
    observers[name] = obs;
    args[name] = [];
    args[name].observer = obs;
    cps.addObserverForName(name, obs);
  });

  do_execute_soon(function () {
    if (!dontRemove)
      names.forEach(n => cps.removeObserverForName(n, observers[n]));
    next(args);
  });
}

function schemaVersionIs(expectedVersion) {
  let db = sendMessage("db");
  equal(db.schemaVersion, expectedVersion);
}

function wait() {
  do_execute_soon(next);
}

function observerArgsOK(actualArgs, expectedArgs) {
  notEqual(actualArgs, undefined);
  arraysOfArraysOK(actualArgs, expectedArgs);
}