gecko/addon-sdk/source/test/test-content-loader.js
Dave Townsend b015efce49 Bug 935220: Uplift Add-on SDK to Firefox. r=me
https://github.com/mozilla/addon-sdk/compare/firefox27...50697e6182cb4f79f9605b7cd1d1f5084cfaf03f

--HG--
rename : addon-sdk/source/data/index.html => addon-sdk/source/test/addons/addon-page/data/index.html
rename : addon-sdk/source/test/favicon-helpers.js => addon-sdk/source/test/addons/places/favicon-helpers.js
rename : addon-sdk/source/test/places-helper.js => addon-sdk/source/test/addons/places/places-helper.js
rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/addons/symbiont/data/test-trusted-document.html
rename : addon-sdk/source/data/index.html => addon-sdk/source/test/fixtures/index.html
rename : addon-sdk/source/data/mofo_logo.SVG => addon-sdk/source/test/fixtures/mofo_logo.SVG
rename : addon-sdk/source/data/moz_favicon.ico => addon-sdk/source/test/fixtures/moz_favicon.ico
rename : addon-sdk/source/data/pagemod-css-include-file.css => addon-sdk/source/test/fixtures/pagemod-css-include-file.css
rename : addon-sdk/source/data/test-content-symbiont.js => addon-sdk/source/test/fixtures/test-content-symbiont.js
rename : addon-sdk/source/data/test-context-menu.js => addon-sdk/source/test/fixtures/test-context-menu.js
rename : addon-sdk/source/data/test-iframe-postmessage.html => addon-sdk/source/test/fixtures/test-iframe-postmessage.html
rename : addon-sdk/source/data/test-iframe.html => addon-sdk/source/test/fixtures/test-iframe.html
rename : addon-sdk/source/data/test-iframe.js => addon-sdk/source/test/fixtures/test-iframe.js
rename : addon-sdk/source/data/test-message-manager.js => addon-sdk/source/test/fixtures/test-message-manager.js
rename : addon-sdk/source/data/test-net-url.txt => addon-sdk/source/test/fixtures/test-net-url.txt
rename : addon-sdk/source/data/test-page-mod.html => addon-sdk/source/test/fixtures/test-page-mod.html
rename : addon-sdk/source/data/test-page-worker.html => addon-sdk/source/test/fixtures/test-page-worker.html
rename : addon-sdk/source/data/test-page-worker.js => addon-sdk/source/test/fixtures/test-page-worker.js
rename : addon-sdk/source/data/test-sidebar-addon-global.html => addon-sdk/source/test/fixtures/test-sidebar-addon-global.html
rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/fixtures/test-trusted-document.html
rename : addon-sdk/source/data/test.html => addon-sdk/source/test/fixtures/test.html
rename : addon-sdk/source/data/testLocalXhr.json => addon-sdk/source/test/fixtures/testLocalXhr.json
2013-11-05 13:51:58 -08:00

220 lines
5.0 KiB
JavaScript

/* 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";
const { Loader } = require('sdk/content/loader');
const self = require("sdk/self");
const fixtures = require("./fixtures");
exports['test:contentURL'] = function(assert) {
let loader = Loader(),
value, emitted = 0, changes = 0;
assert.throws(
function() loader.contentURL = 4,
/The `contentURL` option must be a valid URL./,
'Must throw an exception if `contentURL` is not URL.'
);
assert.throws(
function() loader.contentURL = { toString: function() 'Oops' },
/The `contentURL` option must be a valid URL./,
'Must throw an exception if `contentURL` is not URL.'
);
function listener(e) {
emitted ++;
assert.ok(
'contentURL' in e,
'emitted event must contain "content" property'
);
assert.ok(
value,
'' + e.contentURL,
'content property of an event must match value'
);
}
loader.on('propertyChange', listener);
assert.equal(
null,
loader.contentURL,
'default value is `null`'
);
loader.contentURL = value = 'data:text/html,<html><body>Hi</body><html>';
assert.equal(
value,
'' + loader.contentURL,
'data uri is ok'
);
assert.equal(
++changes,
emitted,
'had to emit `propertyChange`'
);
loader.contentURL = value;
assert.equal(
changes,
emitted,
'must not emit `propertyChange` if same value is set'
);
loader.contentURL = value = 'http://google.com/';
assert.equal(
value,
'' + loader.contentURL,
'value must be set'
);
assert.equal(
++ changes,
emitted,
'had to emit `propertyChange`'
);
loader.contentURL = value;
assert.equal(
changes,
emitted,
'must not emit `propertyChange` if same value is set'
);
loader.removeListener('propertyChange', listener);
loader.contentURL = value = 'about:blank';
assert.equal(
value,
'' + loader.contentURL,
'contentURL must be an actual value'
);
assert.equal(
changes,
emitted,
'listener had to be romeved'
);
};
exports['test:contentScriptWhen'] = function(assert) {
let loader = Loader();
assert.equal(
'end',
loader.contentScriptWhen,
'`contentScriptWhen` defaults to "end"'
);
loader.contentScriptWhen = "end";
assert.equal(
"end",
loader.contentScriptWhen
);
try {
loader.contentScriptWhen = 'boom';
test.fail('must throw when wrong value is set');
} catch(e) {
assert.equal(
'The `contentScriptWhen` option must be either "start", "ready" or "end".',
e.message
);
}
loader.contentScriptWhen = null;
assert.equal(
'end',
loader.contentScriptWhen,
'`contentScriptWhen` defaults to "end"'
);
loader.contentScriptWhen = "ready";
assert.equal(
"ready",
loader.contentScriptWhen
);
loader.contentScriptWhen = "start";
assert.equal(
'start',
loader.contentScriptWhen
);
};
exports['test:contentScript'] = function(assert) {
let loader = Loader(), value;
assert.equal(
null,
loader.contentScript,
'`contentScript` defaults to `null`'
);
loader.contentScript = value = 'let test = {};';
assert.equal(
value,
loader.contentScript
);
try {
loader.contentScript = { 1: value }
test.fail('must throw when wrong value is set');
} catch(e) {
assert.equal(
'The `contentScript` option must be a string or an array of strings.',
e.message
);
}
try {
loader.contentScript = ['oue', 2]
test.fail('must throw when wrong value is set');
} catch(e) {
assert.equal(
'The `contentScript` option must be a string or an array of strings.',
e.message
);
}
loader.contentScript = undefined;
assert.equal(
null,
loader.contentScript
);
loader.contentScript = value = ["1;", "2;"];
assert.equal(
value,
loader.contentScript
);
};
exports['test:contentScriptFile'] = function(assert) {
let loader = Loader(), value, uri = fixtures.url("test-content-loader.js");
assert.equal(
null,
loader.contentScriptFile,
'`contentScriptFile` defaults to `null`'
);
loader.contentScriptFile = value = uri;
assert.equal(
value,
loader.contentScriptFile
);
try {
loader.contentScriptFile = { 1: uri }
test.fail('must throw when wrong value is set');
} catch(e) {
assert.equal(
'The `contentScriptFile` option must be a local URL or an array of URLs.',
e.message
);
}
try {
loader.contentScriptFile = [ 'oue', uri ]
test.fail('must throw when wrong value is set');
} catch(e) {
assert.equal(
'The `contentScriptFile` option must be a local URL or an array of URLs.',
e.message
);
}
loader.contentScriptFile = undefined;
assert.equal(
null,
loader.contentScriptFile
);
loader.contentScriptFile = value = [uri];
assert.equal(
value,
loader.contentScriptFile
);
};
require('sdk/test').run(exports);