Skip to content

Commit

Permalink
Bug 909341 - Uplift Addon SDK to Firefox. r=me
Browse files Browse the repository at this point in the history
  • Loading branch information
KWierso committed Aug 26, 2013
1 parent 992f997 commit 4e9e1c3
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 107 deletions.
15 changes: 2 additions & 13 deletions addon-sdk/source/lib/sdk/addon/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const { exit, env, staticArgs } = require('../system');
const { when: unload } = require('../system/unload');
const { loadReason } = require('../self');
const { rootURI } = require("@loader/options");
const cfxArgs = require("@test/options");
const globals = require('../system/globals');
const xulApp = require('../system/xul-app');
const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].
Expand Down Expand Up @@ -102,11 +101,7 @@ function startup(reason, options) {
// Run the addon even in case of error (best effort approach)
require('../l10n/loader').
load(rootURI).
then(function l10nSuccess() {
if (cfxArgs.parseable) {
console.info("localization information has loaded successfully.");
}
}, function l10nFailure(error) {
then(null, function failure(error) {
console.info("Error while loading localization: " + error.message);
}).
then(function onLocalizationReady(data) {
Expand All @@ -115,10 +110,6 @@ function startup(reason, options) {
definePseudo(options.loader, '@l10n/data', data ? data : null);
return ready;
}).then(function() {
if (cfxArgs.parseable) {
console.info("addon window has loaded successfully.");
}

run(options);
}).then(null, console.exception);
}
Expand All @@ -137,7 +128,6 @@ function run(options) {
catch(error) {
console.exception(error);
}

// Initialize inline options localization, without preventing addon to be
// run in case of error
try {
Expand Down Expand Up @@ -167,8 +157,7 @@ function run(options) {
quit: exit
});
}
}
catch (error) {
} catch (error) {
console.exception(error);
throw error;
}
Expand Down
5 changes: 1 addition & 4 deletions addon-sdk/source/lib/sdk/test/runner.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* 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/. */

"use strict";

module.metadata = {
"stability": "experimental"
};

var obsvc = require("../deprecated/observer-service");
var { exit, stdout } = require("../system");
var cfxArgs = require("@test/options");
var { Cc, Ci} = require("chrome");

function runTests(findAndRunTests) {
var harness = require("./harness");
Expand Down Expand Up @@ -62,7 +59,7 @@ function printFailedTests(tests, print) {
iterationNumber++;

if (!singleIteration)
print(" Iteration " + iterationNumber + ":\n");
print(" Iteration " + iterationNumber + ":\n");

for each (let test in testRun) {
if (test.failed > 0) {
Expand Down
37 changes: 29 additions & 8 deletions addon-sdk/source/lib/toolkit/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ const prototypeOf = Object.getPrototypeOf;
const create = Object.create;
const keys = Object.keys;


const COMPONENT_ERROR = '`Components` is not available in this context.\n' +
'Functionality provided by Components may be available in an SDK\n' +
'module: https://jetpack.mozillalabs.com/sdk/latest/docs/ \n\n' +
'However, if you still need to import Components, you may use the\n' +
'`chrome` module\'s properties for shortcuts to Component properties:\n\n' +
'Shortcuts: \n' +
' Cc = Components' + '.classes \n' +
' Ci = Components' + '.interfaces \n' +
' Cu = Components' + '.utils \n' +
' CC = Components' + '.Constructor \n' +
'Example: \n' +
' let { Cc, Ci } = require(\'chrome\');\n';

// Workaround for bug 674195. Freezing objects from other compartments fail,
// so we use `Object.freeze` from the same component instead.
function freeze(object) {
Expand Down Expand Up @@ -216,19 +230,26 @@ const load = iced(function load(loader, module) {
let { sandboxes, globals } = loader;
let require = Require(loader, module);

// We expose set of properties defined by `CommonJS` specification via
// prototype of the sandbox. Also globals are deeper in the prototype
// chain so that each module has access to them as well.
let descriptors = descriptor({
require: require,
module: module,
exports: module.exports,
get Components() {
// Expose `Components` property to throw error on usage with
// additional information
throw new ReferenceError(COMPONENT_ERROR);
}
});

let sandbox = sandboxes[module.uri] = Sandbox({
name: module.uri,
// Get an existing module sandbox, if any, so we can reuse its compartment
// when creating the new one to reduce memory consumption.
sandbox: sandboxes[keys(sandboxes).shift()],
// We expose set of properties defined by `CommonJS` specification via
// prototype of the sandbox. Also globals are deeper in the prototype
// chain so that each module has access to them as well.
prototype: create(globals, descriptor({
require: require,
module: module,
exports: module.exports
})),
prototype: create(globals, descriptors),
wantXrays: false
});

Expand Down
102 changes: 48 additions & 54 deletions addon-sdk/source/test/test-deprecated-list.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,108 @@
/* 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/. */
'use strict';

"use strict";
const { List } = require('sdk/deprecated/list');

function assertList(test, array, list) {
for (let i = 0, ii = array.length; i < ii; i < ii, i++) {
test.assertEqual(
function assertList(assert, array, list) {
for (let i = 0, l = array.length; i < l; i++) {
assert.equal(
array.length,
list.length,
'list must contain same amount of elements as array'
);
test.assertEqual(
assert.equal(
'List(' + array + ')',
list + '',
'toString must output array like result'
);
test.assert(
i in list,
'must contain element with index: ' + i
);
test.assertEqual(
assert.ok(i in list, 'must contain element with index: ' + i);
assert.equal(
array[i],
list[i],
'element with index: ' + i + ' should match'
);
}
}

const { List } = require('sdk/deprecated/list');

exports['test:test for'] = function(test) {
exports['test:test for'] = function(assert) {
let fixture = List(3, 2, 1);

test.assertEqual(3, fixture.length, 'length is 3');
assert.equal(3, fixture.length, 'length is 3');
let i = 0;
for (let key in fixture) {
test.assertEqual(i++, key, 'key should match');
assert.equal(i++, key, 'key should match');
}
};

exports['test:test for each'] = function(test) {
exports['test:test for each'] = function(assert) {
let fixture = new List(3, 2, 1);

test.assertEqual(3, fixture.length, 'length is 3');
assert.equal(3, fixture.length, 'length is 3');
let i = 3;
for each (let value in fixture) {
test.assertEqual(i--, value, 'value should match');
for (let value of fixture) {
assert.equal(i--, value, 'value should match');
}
};

exports['test:test for of'] = function(test) {
exports['test:test for of'] = function(assert) {
let fixture = new List(3, 2, 1);

test.assertEqual(3, fixture.length, 'length is 3');
assert.equal(3, fixture.length, 'length is 3');
let i = 3;
for (let value of fixture) {
test.assertEqual(i--, value, 'value should match');
assert.equal(i--, value, 'value should match');
}
};

exports['test:test toString'] = function(test) {
exports['test:test toString'] = function(assert) {
let fixture = List(3, 2, 1);

test.assertEqual(
assert.equal(
'List(3,2,1)',
fixture + '',
'toString must output array like result'
)
};

exports['test:test constructor with apply'] = function(test) {
exports['test:test constructor with apply'] = function(assert) {
let array = ['a', 'b', 'c'];
let fixture = List.apply(null, array);

test.assertEqual(
assert.equal(
3,
fixture.length,
'should have applied arguments'
);
};

exports['test:direct element access'] = function(test) {
let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, test, 1];
exports['test:direct element access'] = function(assert) {
let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1];
let fixture = List.apply(null, array);
array.splice(5, 1);
array.splice(7, 1);

test.assertEqual(
assert.equal(
array.length,
fixture.length,
'list should omit duplicate elements'
);

test.assertEqual(
assert.equal(
'List(' + array + ')',
fixture.toString(),
'elements should not be rearranged'
);

for (let key in array) {
test.assert(key in fixture,'should contain key for index:' + key);
test.assertEqual(
array[key],
fixture[key],
'values should match for: ' + key
);
assert.ok(key in fixture,'should contain key for index:' + key);
assert.equal(array[key], fixture[key], 'values should match for: ' + key);
}
};

exports['test:removing adding elements'] = function(test) {
let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, test, 1];
exports['test:removing adding elements'] = function(assert) {
let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1];
let fixture = List.compose({
add: function() this._add.apply(this, arguments),
remove: function() this._remove.apply(this, arguments),
Expand All @@ -119,23 +111,23 @@ exports['test:removing adding elements'] = function(test) {
array.splice(5, 1);
array.splice(7, 1);

assertList(test, array, fixture);
assertList(assert, array, fixture);

array.splice(array.indexOf(2), 1);
fixture.remove(2);
assertList(test, array, fixture);
assertList(assert, array, fixture);

array.splice(array.indexOf('foo'), 1);
fixture.remove('foo');
array.splice(array.indexOf(1), 1);
fixture.remove(1);
array.push('foo');
fixture.add('foo');
assertList(test, array, fixture);
assertList(assert, array, fixture);

array.splice(0);
fixture.clear(0);
assertList(test, array, fixture);
assertList(assert, array, fixture);

array.push(1, 'foo', 2, 'bar', 3);
fixture.add(1);
Expand All @@ -144,26 +136,26 @@ exports['test:removing adding elements'] = function(test) {
fixture.add('bar');
fixture.add(3);

assertList(test, array, fixture);
assertList(assert, array, fixture);
};

exports['test: remove does not leave invalid numerical properties'] = function(test) {
exports['test: remove does not leave invalid numerical properties'] = function(assert) {
let fixture = List.compose({
remove: function() this._remove.apply(this, arguments),
}).apply(null, [1, 2, 3]);

fixture.remove(1);
test.assertEqual(fixture[fixture.length], undefined);
assert.equal(fixture[fixture.length], undefined);
}

exports['test:add list item from Iterator'] = function(test) {
exports['test:add list item from Iterator'] = function(assert) {
let array = [1, 2, 3, 4], sum = 0, added = false;

let fixture = List.compose({
add: function() this._add.apply(this, arguments),
}).apply(null, array);

for each (let item in fixture) {
for (let item of fixture) {
sum += item;

if (!added) {
Expand All @@ -172,35 +164,37 @@ exports['test:add list item from Iterator'] = function(test) {
}
}

test.assertEqual(sum, 1 + 2 + 3 + 4);
assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
};

exports['test:remove list item from Iterator'] = function(test) {
exports['test:remove list item from Iterator'] = function(assert) {
let array = [1, 2, 3, 4], sum = 0;

let fixture = List.compose({
remove: function() this._remove.apply(this, arguments),
}).apply(null, array);

for each (let item in fixture) {
for (let item of fixture) {
sum += item;
fixture.remove(item);
}

test.assertEqual(sum, 1 + 2 + 3 + 4);
assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
};

exports['test:clear list from Iterator'] = function(test) {
exports['test:clear list from Iterator'] = function(assert) {
let array = [1, 2, 3, 4], sum = 0;

let fixture = List.compose({
clear: function() this._clear()
}).apply(null, array);

for each (let item in fixture) {
for (let item of fixture) {
sum += item;
fixture.clear();
}

test.assertEqual(sum, 1 + 2 + 3 + 4);
assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
};

require('sdk/test').run(exports);
Loading

0 comments on commit 4e9e1c3

Please sign in to comment.