Bug 980511 - Uplift Add-on SDK to Firefox

This commit is contained in:
Erik Vold 2014-03-06 14:21:49 -08:00
parent eedae70bfb
commit 5f6022aca3
6 changed files with 141 additions and 17 deletions

View File

@ -13,7 +13,7 @@ module.metadata = {
};
const { deprecateFunction } = require("../util/deprecate");
const { setImmediate, setTimeout } = require("../timers");
const { setImmediate, setTimeout, clearTimeout } = require("../timers");
const arity = f => f.arity || f.length;
@ -361,3 +361,40 @@ const debounce = function debounce (fn, wait) {
};
};
exports.debounce = debounce;
/**
* From underscore's `_.throttle`
* http://underscorejs.org
* (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Underscore may be freely distributed under the MIT license.
*/
const throttle = function throttle (func, wait, options) {
let context, args, result;
let timeout = null;
let previous = 0;
options || (options = {});
let later = function() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
let now = Date.now();
if (!previous && options.leading === false) previous = now;
let remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
exports.throttle = throttle;

View File

@ -96,8 +96,8 @@ parser_groups = (
"match FILENAME and optionally "
"match TESTNAME, both regexps"),
metavar="FILENAME[:TESTNAME]",
default=None,
cmds=['test', 'testex', 'testpkgs',
default='',
cmds=['test', 'testex', 'testaddons', 'testpkgs',
'testall'])),
(("-g", "--use-config",), dict(dest="config",
help="use named config from local.json",
@ -421,6 +421,9 @@ def test_all_testaddons(env_root, defaults):
addons.sort()
fail = False
for dirname in addons:
if (not defaults['filter'].split(":")[0] in dirname):
continue
print >>sys.stderr, "Testing %s..." % dirname
sys.stderr.flush()
try:
@ -445,6 +448,9 @@ def test_all_examples(env_root, defaults):
examples.sort()
fail = False
for dirname in examples:
if (not defaults['filter'].split(":")[0] in dirname):
continue
print >>sys.stderr, "Testing %s..." % dirname
sys.stderr.flush()
try:
@ -819,12 +825,12 @@ def run(arguments=sys.argv[1:], target_cfg=None, pkg_cfg=None,
harness_options['manifest'] = manifest.get_harness_options_manifest(False)
# Gives an hint to tell if sdk modules are bundled or not
harness_options['is-sdk-bundled'] = options.bundle_sdk
harness_options['is-sdk-bundled'] = options.bundle_sdk or options.no_strip_xpi
if options.force_use_bundled_sdk:
if not options.bundle_sdk:
print >>sys.stderr, ("--force-use-bundled-sdk and --strip-sdk "
"can't be used at the same time.")
if not harness_options['is-sdk-bundled']:
print >>sys.stderr, ("--force-use-bundled-sdk "
"can't be used if sdk isn't bundled.")
sys.exit(1)
if options.overload_modules:
print >>sys.stderr, ("--force-use-bundled-sdk and --overload-modules "

View File

@ -193,11 +193,16 @@ class RemoteFennecRunner(mozrunner.Runner):
print "Killing running Firefox instance ..."
subprocess.call([self._adb_path, "shell",
"am force-stop " + self._intent_name])
time.sleep(2)
if self.getProcessPID(self._intent_name) != None:
raise Exception("Unable to automatically kill running Firefox" +
" instance. Please close it manually before " +
"executing cfx.")
time.sleep(7)
# It appears recently that the PID still exists even after
# Fennec closes, so removing this error still allows the tests
# to pass as the new Fennec instance is able to start.
# Leaving error in but commented out for now.
#
#if self.getProcessPID(self._intent_name) != None:
# raise Exception("Unable to automatically kill running Firefox" +
# " instance. Please close it manually before " +
# "executing cfx.")
print "Pushing the addon to your device"

View File

@ -16,6 +16,8 @@ const { on, off } = require('sdk/event/core');
const { events } = require('sdk/places/events');
const { setTimeout } = require('sdk/timers');
const { before, after } = require('sdk/test/utils');
const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
getService(Ci.nsINavBookmarksService);
const {
search
} = require('sdk/places/history');
@ -52,6 +54,11 @@ exports['test bookmark-item-changed'] = function (assert, done) {
function handler ({type, data}) {
if (type !== 'bookmark-item-changed') return;
if (data.id !== id) return;
// Abort if the 'bookmark-item-changed' event isn't for the `title` property,
// as sometimes the event can be for the `url` property.
// Intermittent failure, bug 969616
if (data.property !== 'title') return;
assert.equal(type, 'bookmark-item-changed',
'correct type in bookmark-item-changed event');
assert.equal(data.type, 'bookmark',
@ -78,6 +85,8 @@ exports['test bookmark-item-changed'] = function (assert, done) {
exports['test bookmark-item-moved'] = function (assert, done) {
let id;
let complete = makeCompleted(done);
let previousIndex, previousParentId;
function handler ({type, data}) {
if (type !== 'bookmark-item-moved') return;
if (data.id !== id) return;
@ -86,12 +95,12 @@ exports['test bookmark-item-moved'] = function (assert, done) {
assert.equal(data.type, 'bookmark',
'correct data in bookmark-item-moved event');
assert.ok(data.id === id, 'correct id in bookmark-item-moved event');
assert.equal(data.previousParentId, UNSORTED.id,
assert.equal(data.previousParentId, previousParentId,
'correct previousParentId');
assert.equal(data.currentParentId, MENU.id,
assert.equal(data.currentParentId, bmsrv.getFolderIdForItem(id),
'correct currentParentId');
assert.equal(data.previousIndex, 0, 'correct previousIndex');
assert.equal(data.currentIndex, 0, 'correct currentIndex');
assert.equal(data.previousIndex, previousIndex, 'correct previousIndex');
assert.equal(data.currentIndex, bmsrv.getItemIndex(id), 'correct currentIndex');
events.off('data', handler);
complete();
@ -103,6 +112,8 @@ exports['test bookmark-item-moved'] = function (assert, done) {
group: UNSORTED
}).then(item => {
id = item.id;
previousIndex = bmsrv.getItemIndex(id);
previousParentId = bmsrv.getFolderIdForItem(id);
item.group = MENU;
return saveP(item);
}).then(complete);

View File

@ -874,6 +874,47 @@ exports["test:worker events"] = WorkerTest(
}
);
exports["test:onDetach in contentScript on destroy"] = WorkerTest(
"data:text/html;charset=utf-8,foo#detach",
function(assert, browser, done) {
let worker = Worker({
window: browser.contentWindow,
contentScript: 'new ' + function WorkerScope() {
self.port.on('detach', function(reason) {
window.location.hash += '!' + reason;
})
},
});
browser.contentWindow.addEventListener('hashchange', _ => {
assert.equal(browser.contentWindow.location.hash, '#detach!',
"location.href is as expected");
done();
})
worker.destroy();
}
);
exports["test:onDetach in contentScript on unload"] = WorkerTest(
"data:text/html;charset=utf-8,foo#detach",
function(assert, browser, done) {
let { loader } = LoaderWithHookedConsole(module);
let worker = loader.require("sdk/content/worker").Worker({
window: browser.contentWindow,
contentScript: 'new ' + function WorkerScope() {
self.port.on('detach', function(reason) {
window.location.hash += '!' + reason;
})
},
});
browser.contentWindow.addEventListener('hashchange', _ => {
assert.equal(browser.contentWindow.location.hash, '#detach!shutdown',
"location.href is as expected");
done();
})
loader.unload('shutdown');
}
);
exports["test:console method log functions properly"] = WorkerTest(
DEFAULT_CONTENT_URL,
function(assert, browser, done) {

View File

@ -6,7 +6,8 @@
const { setTimeout } = require('sdk/timers');
const utils = require('sdk/lang/functional');
const { invoke, defer, partial, compose, memoize, once, is, isnt,
delay, wrap, curry, chainable, field, query, isInstance, debounce } = utils;
delay, wrap, curry, chainable, field, query, isInstance, debounce, throttle
} = utils;
const { LoaderWithHookedConsole } = require('sdk/test/loader');
exports['test forwardApply'] = function(assert) {
@ -435,4 +436,27 @@ exports["test debounce"] = (assert, done) => {
}, 150);
}, 200);
};
exports["test throttle"] = (assert, done) => {
let called = 0;
let attempt = 0;
let throttledFn = throttle(() => called++, 100);
let fn = () => ++attempt && throttledFn();
new Array(11).join(0).split("").forEach((_, i) => {
setTimeout(fn, 20 * (i+1));
});
setTimeout(() => {
assert.equal(called, 1, "function called atleast once during first throttle period");
assert.ok(attempt >= 2, "function attempted to be called several times during first period");
}, 50);
setTimeout(() => {
assert.equal(called, 3, "function called again during second throttle period");
assert.equal(attempt, 10, "function attempted to be called several times during second period");
done();
}, 300);
};
require('test').run(exports);