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";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = [
|
2015-02-23 07:11:22 +03:00
|
|
|
"TestUtils",
|
|
|
|
];
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Timer.jsm");
|
2015-02-23 07:11:22 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var TestUtils = {
|
2015-02-23 07:11:22 +03:00
|
|
|
executeSoon(callbackFn) {
|
2017-04-14 19:29:12 +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-14 22:51:38 +03:00
|
|
|
}, topic);
|
2015-02-23 07:11:22 +03:00
|
|
|
});
|
|
|
|
},
|
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) {
|
|
|
|
if (eltOrRect instanceof Ci.nsIDOMElement) {
|
|
|
|
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
|
|
|
|
* function can be a generator.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
waitForCondition(condition, msg, interval = 100, maxTries = 50) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let tries = 0;
|
|
|
|
let intervalID = setInterval(async function() {
|
|
|
|
if (tries >= maxTries) {
|
|
|
|
clearInterval(intervalID);
|
|
|
|
msg += ` - timed out after ${maxTries} tries.`;
|
|
|
|
reject(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conditionPassed = false;
|
|
|
|
try {
|
|
|
|
conditionPassed = await condition();
|
|
|
|
} catch (e) {
|
|
|
|
msg += ` - threw exception: ${e}`;
|
|
|
|
clearInterval(intervalID);
|
|
|
|
reject(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conditionPassed) {
|
|
|
|
clearInterval(intervalID);
|
2018-01-23 03:50:32 +03:00
|
|
|
resolve(conditionPassed);
|
2017-10-10 02:18:23 +03:00
|
|
|
}
|
|
|
|
tries++;
|
|
|
|
}, interval);
|
|
|
|
});
|
|
|
|
},
|
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
|
|
|
};
|