2015-02-23 07:11:22 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2017-10-31 16:25:45 +03:00
|
|
|
/**
|
|
|
|
* Contains a limited number of testing functions that are commonly used in a
|
|
|
|
* wide variety of situations, for example waiting for an event loop tick or an
|
|
|
|
* observer notification.
|
2015-02-23 07:11:22 +03:00
|
|
|
*
|
2017-10-31 16:25:45 +03:00
|
|
|
* More complex functions are likely to belong to a separate test-only module.
|
|
|
|
* Examples include Assert.jsm for generic assertions, FileTestUtils.jsm to work
|
|
|
|
* with local files and their contents, and BrowserTestUtils.jsm to work with
|
|
|
|
* browser windows and tabs.
|
|
|
|
*
|
|
|
|
* Individual components also offer testing functions to other components, for
|
|
|
|
* example LoginTestUtils.jsm.
|
2015-02-23 07:11:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["TestUtils"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2019-11-13 21:24:02 +03:00
|
|
|
const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
|
2015-02-23 07:11:22 +03:00
|
|
|
|
|
|
|
var TestUtils = {
|
|
|
|
executeSoon(callbackFn) {
|
2017-04-15 00:39:23 +03:00
|
|
|
Services.tm.dispatchToMainThread(callbackFn);
|
2015-02-23 07:11:22 +03:00
|
|
|
},
|
|
|
|
|
2017-10-11 12:18:36 +03:00
|
|
|
waitForTick() {
|
|
|
|
return new Promise(resolve => this.executeSoon(resolve));
|
|
|
|
},
|
|
|
|
|
2015-02-23 07:11:22 +03:00
|
|
|
/**
|
|
|
|
* Waits for the specified topic to be observed.
|
|
|
|
*
|
|
|
|
* @param {string} topic
|
|
|
|
* The topic to observe.
|
2015-03-13 18:54:43 +03:00
|
|
|
* @param {function} checkFn [optional]
|
|
|
|
* Called with (subject, data) as arguments, should return true if the
|
|
|
|
* notification is the expected one, or false if it should be ignored
|
|
|
|
* and listening should continue. If not specified, the first
|
|
|
|
* notification for the specified topic resolves the returned promise.
|
|
|
|
*
|
|
|
|
* @note Because this function is intended for testing, any error in checkFn
|
|
|
|
* will cause the returned promise to be rejected instead of waiting for
|
|
|
|
* the next notification, since this is probably a bug in the test.
|
|
|
|
*
|
2015-02-23 07:11:22 +03:00
|
|
|
* @return {Promise}
|
2015-03-13 18:54:43 +03:00
|
|
|
* @resolves The array [subject, data] from the observed notification.
|
2015-02-23 07:11:22 +03:00
|
|
|
*/
|
2015-03-13 18:54:43 +03:00
|
|
|
topicObserved(topic, checkFn) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Services.obs.addObserver(function observer(subject, topic, data) {
|
|
|
|
try {
|
2015-03-20 20:26:01 +03:00
|
|
|
if (checkFn && !checkFn(subject, data)) {
|
|
|
|
return;
|
2015-03-13 18:54:43 +03:00
|
|
|
}
|
2015-03-20 20:26:01 +03:00
|
|
|
Services.obs.removeObserver(observer, topic);
|
2015-03-13 18:54:43 +03:00
|
|
|
resolve([subject, data]);
|
|
|
|
} catch (ex) {
|
2015-03-20 20:26:01 +03:00
|
|
|
Services.obs.removeObserver(observer, topic);
|
2015-03-13 18:54:43 +03:00
|
|
|
reject(ex);
|
|
|
|
}
|
2017-04-15 00:39:22 +03:00
|
|
|
}, topic);
|
2015-02-23 07:11:22 +03:00
|
|
|
});
|
|
|
|
},
|
2017-04-19 12:41:49 +03:00
|
|
|
|
2019-03-14 02:36:54 +03:00
|
|
|
/**
|
|
|
|
* Waits for the specified preference to be change.
|
|
|
|
*
|
|
|
|
* @param {string} prefName
|
|
|
|
* The pref to observe.
|
|
|
|
* @param {function} checkFn [optional]
|
|
|
|
* Called with the new preference value as argument, should return true if the
|
|
|
|
* notification is the expected one, or false if it should be ignored
|
|
|
|
* and listening should continue. If not specified, the first
|
|
|
|
* notification for the specified topic resolves the returned promise.
|
|
|
|
*
|
|
|
|
* @note Because this function is intended for testing, any error in checkFn
|
|
|
|
* will cause the returned promise to be rejected instead of waiting for
|
|
|
|
* the next notification, since this is probably a bug in the test.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves The value of the preference.
|
|
|
|
*/
|
|
|
|
waitForPrefChange(prefName, checkFn) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Services.prefs.addObserver(prefName, function observer(
|
|
|
|
subject,
|
|
|
|
topic,
|
|
|
|
data
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
let prefValue = null;
|
|
|
|
switch (Services.prefs.getPrefType(prefName)) {
|
|
|
|
case Services.prefs.PREF_STRING:
|
|
|
|
prefValue = Services.prefs.getStringPref(prefName);
|
|
|
|
break;
|
|
|
|
case Services.prefs.PREF_INT:
|
|
|
|
prefValue = Services.prefs.getIntPref(prefName);
|
|
|
|
break;
|
|
|
|
case Services.prefs.PREF_BOOL:
|
|
|
|
prefValue = Services.prefs.getBoolPref(prefName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (checkFn && !checkFn(prefValue)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Services.prefs.removeObserver(prefName, observer);
|
|
|
|
resolve(prefValue);
|
|
|
|
} catch (ex) {
|
|
|
|
Services.prefs.removeObserver(prefName, observer);
|
|
|
|
reject(ex);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-04-19 12:41:49 +03:00
|
|
|
/**
|
|
|
|
* Takes a screenshot of an area and returns it as a data URL.
|
|
|
|
*
|
|
|
|
* @param eltOrRect
|
|
|
|
* The DOM node or rect ({left, top, width, height}) to screenshot.
|
|
|
|
* @param win
|
|
|
|
* The current window.
|
|
|
|
*/
|
|
|
|
screenshotArea(eltOrRect, win) {
|
2018-04-27 06:37:34 +03:00
|
|
|
if (Element.isInstance(eltOrRect)) {
|
2017-04-19 12:41:49 +03:00
|
|
|
eltOrRect = eltOrRect.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
let { left, top, width, height } = eltOrRect;
|
|
|
|
let canvas = win.document.createElementNS(
|
|
|
|
"http://www.w3.org/1999/xhtml",
|
|
|
|
"canvas"
|
|
|
|
);
|
|
|
|
let ctx = canvas.getContext("2d");
|
|
|
|
let ratio = win.devicePixelRatio;
|
|
|
|
canvas.width = width * ratio;
|
|
|
|
canvas.height = height * ratio;
|
|
|
|
ctx.scale(ratio, ratio);
|
|
|
|
ctx.drawWindow(win, left, top, width, height, "#fff");
|
|
|
|
return canvas.toDataURL();
|
2017-10-10 02:18:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will poll a condition function until it returns true.
|
|
|
|
*
|
|
|
|
* @param condition
|
|
|
|
* A condition function that must return true or false. If the
|
|
|
|
* condition ever throws, this is also treated as a false. The
|
2019-12-13 02:11:13 +03:00
|
|
|
* function can be an async function.
|
2017-10-10 02:18:23 +03:00
|
|
|
* @param interval
|
|
|
|
* The time interval to poll the condition function. Defaults
|
|
|
|
* to 100ms.
|
|
|
|
* @param attempts
|
|
|
|
* The number of times to poll before giving up and rejecting
|
|
|
|
* if the condition has not yet returned true. Defaults to 50
|
|
|
|
* (~5 seconds for 100ms intervals)
|
|
|
|
* @return Promise
|
2018-01-23 03:50:32 +03:00
|
|
|
* Resolves with the return value of the condition function.
|
2017-10-10 02:18:23 +03:00
|
|
|
* Rejects if timeout is exceeded or condition ever throws.
|
2019-11-13 21:24:02 +03:00
|
|
|
*
|
|
|
|
* NOTE: This is intentionally not using setInterval, using setTimeout
|
|
|
|
* instead. setInterval is not promise-safe.
|
2017-10-10 02:18:23 +03:00
|
|
|
*/
|
|
|
|
waitForCondition(condition, msg, interval = 100, maxTries = 50) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let tries = 0;
|
2019-11-13 21:24:02 +03:00
|
|
|
async function tryOnce() {
|
2017-10-10 02:18:23 +03:00
|
|
|
if (tries >= maxTries) {
|
|
|
|
msg += ` - timed out after ${maxTries} tries.`;
|
|
|
|
reject(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conditionPassed = false;
|
|
|
|
try {
|
|
|
|
conditionPassed = await condition();
|
|
|
|
} catch (e) {
|
|
|
|
msg += ` - threw exception: ${e}`;
|
|
|
|
reject(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conditionPassed) {
|
2018-01-23 03:50:32 +03:00
|
|
|
resolve(conditionPassed);
|
2019-11-13 21:24:02 +03:00
|
|
|
return;
|
2017-10-10 02:18:23 +03:00
|
|
|
}
|
|
|
|
tries++;
|
2019-11-13 21:24:02 +03:00
|
|
|
setTimeout(tryOnce, interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(bug 1596165): This could be a direct call, ideally.
|
|
|
|
setTimeout(tryOnce, interval);
|
2017-10-10 02:18:23 +03:00
|
|
|
});
|
|
|
|
},
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
|
|
|
|
shuffle(array) {
|
|
|
|
let results = [];
|
|
|
|
for (let i = 0; i < array.length; ++i) {
|
|
|
|
let randomIndex = Math.floor(Math.random() * (i + 1));
|
|
|
|
results[i] = results[randomIndex];
|
|
|
|
results[randomIndex] = array[i];
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
2015-02-23 07:11:22 +03:00
|
|
|
};
|