michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: const { Loader } = require('sdk/content/loader'); michael@0: const self = require("sdk/self"); michael@0: const fixtures = require("./fixtures"); michael@0: const { URL } = require('sdk/url'); michael@0: michael@0: exports['test:contentURL'] = function(assert) { michael@0: let loader = Loader(), michael@0: value, emitted = 0, changes = 0; michael@0: michael@0: assert.throws( michael@0: function() loader.contentURL = 4, michael@0: /The `contentURL` option must be a valid URL./, michael@0: 'Must throw an exception if `contentURL` is not URL.' michael@0: ); michael@0: assert.throws( michael@0: function() loader.contentURL = { toString: function() 'Oops' }, michael@0: /The `contentURL` option must be a valid URL./, michael@0: 'Must throw an exception if `contentURL` is not URL.' michael@0: ); michael@0: michael@0: function listener(e) { michael@0: emitted ++; michael@0: assert.ok( michael@0: 'contentURL' in e, michael@0: 'emitted event must contain "content" property' michael@0: ); michael@0: assert.ok( michael@0: value, michael@0: '' + e.contentURL, michael@0: 'content property of an event must match value' michael@0: ); michael@0: } michael@0: loader.on('propertyChange', listener); michael@0: michael@0: assert.equal( michael@0: null, michael@0: loader.contentURL, michael@0: 'default value is `null`' michael@0: ); michael@0: loader.contentURL = value = 'data:text/html,Hi'; michael@0: assert.equal( michael@0: value, michael@0: '' + loader.contentURL, michael@0: 'data uri is ok' michael@0: ); michael@0: assert.equal( michael@0: ++changes, michael@0: emitted, michael@0: 'had to emit `propertyChange`' michael@0: ); michael@0: loader.contentURL = value; michael@0: assert.equal( michael@0: changes, michael@0: emitted, michael@0: 'must not emit `propertyChange` if same value is set' michael@0: ); michael@0: michael@0: loader.contentURL = value = 'http://google.com/'; michael@0: assert.equal( michael@0: value, michael@0: '' + loader.contentURL, michael@0: 'value must be set' michael@0: ); michael@0: assert.equal( michael@0: ++ changes, michael@0: emitted, michael@0: 'had to emit `propertyChange`' michael@0: ); michael@0: loader.contentURL = value; michael@0: assert.equal( michael@0: changes, michael@0: emitted, michael@0: 'must not emit `propertyChange` if same value is set' michael@0: ); michael@0: michael@0: loader.removeListener('propertyChange', listener); michael@0: loader.contentURL = value = 'about:blank'; michael@0: assert.equal( michael@0: value, michael@0: '' + loader.contentURL, michael@0: 'contentURL must be an actual value' michael@0: ); michael@0: assert.equal( michael@0: changes, michael@0: emitted, michael@0: 'listener had to be romeved' michael@0: ); michael@0: }; michael@0: michael@0: exports['test:contentScriptWhen'] = function(assert) { michael@0: let loader = Loader(); michael@0: assert.equal( michael@0: 'end', michael@0: loader.contentScriptWhen, michael@0: '`contentScriptWhen` defaults to "end"' michael@0: ); michael@0: loader.contentScriptWhen = "end"; michael@0: assert.equal( michael@0: "end", michael@0: loader.contentScriptWhen michael@0: ); michael@0: try { michael@0: loader.contentScriptWhen = 'boom'; michael@0: test.fail('must throw when wrong value is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScriptWhen` option must be either "start", "ready" or "end".', michael@0: e.message michael@0: ); michael@0: } michael@0: loader.contentScriptWhen = null; michael@0: assert.equal( michael@0: 'end', michael@0: loader.contentScriptWhen, michael@0: '`contentScriptWhen` defaults to "end"' michael@0: ); michael@0: loader.contentScriptWhen = "ready"; michael@0: assert.equal( michael@0: "ready", michael@0: loader.contentScriptWhen michael@0: ); michael@0: loader.contentScriptWhen = "start"; michael@0: assert.equal( michael@0: 'start', michael@0: loader.contentScriptWhen michael@0: ); michael@0: }; michael@0: michael@0: exports['test:contentScript'] = function(assert) { michael@0: let loader = Loader(), value; michael@0: assert.equal( michael@0: null, michael@0: loader.contentScript, michael@0: '`contentScript` defaults to `null`' michael@0: ); michael@0: loader.contentScript = value = 'let test = {};'; michael@0: assert.equal( michael@0: value, michael@0: loader.contentScript michael@0: ); michael@0: try { michael@0: loader.contentScript = { 1: value } michael@0: test.fail('must throw when wrong value is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScript` option must be a string or an array of strings.', michael@0: e.message michael@0: ); michael@0: } michael@0: try { michael@0: loader.contentScript = ['oue', 2] michael@0: test.fail('must throw when wrong value is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScript` option must be a string or an array of strings.', michael@0: e.message michael@0: ); michael@0: } michael@0: loader.contentScript = undefined; michael@0: assert.equal( michael@0: null, michael@0: loader.contentScript michael@0: ); michael@0: loader.contentScript = value = ["1;", "2;"]; michael@0: assert.equal( michael@0: value, michael@0: loader.contentScript michael@0: ); michael@0: }; michael@0: michael@0: exports['test:contentScriptFile'] = function(assert) { michael@0: let loader = Loader(), value, uri = fixtures.url("test-content-loader.js"); michael@0: assert.equal( michael@0: null, michael@0: loader.contentScriptFile, michael@0: '`contentScriptFile` defaults to `null`' michael@0: ); michael@0: loader.contentScriptFile = value = uri; michael@0: assert.equal( michael@0: value, michael@0: loader.contentScriptFile michael@0: ); michael@0: try { michael@0: loader.contentScriptFile = { 1: uri } michael@0: test.fail('must throw when wrong value is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScriptFile` option must be a local URL or an array of URLs.', michael@0: e.message michael@0: ); michael@0: } michael@0: michael@0: try { michael@0: loader.contentScriptFile = [ 'oue', uri ] michael@0: test.fail('must throw when wrong value is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScriptFile` option must be a local URL or an array of URLs.', michael@0: e.message michael@0: ); michael@0: } michael@0: michael@0: let data = 'data:text/html,test'; michael@0: try { michael@0: loader.contentScriptFile = [ { toString: () => data } ]; michael@0: test.fail('must throw when non-URL object is set'); michael@0: } catch(e) { michael@0: assert.equal( michael@0: 'The `contentScriptFile` option must be a local URL or an array of URLs.', michael@0: e.message michael@0: ); michael@0: } michael@0: michael@0: loader.contentScriptFile = new URL(data); michael@0: assert.ok( michael@0: loader.contentScriptFile instanceof URL, michael@0: 'must be able to set `contentScriptFile` to an instance of URL' michael@0: ); michael@0: assert.equal( michael@0: data, michael@0: loader.contentScriptFile.toString(), michael@0: 'setting `contentScriptFile` to an instance of URL should preserve the url' michael@0: ); michael@0: michael@0: loader.contentScriptFile = undefined; michael@0: assert.equal( michael@0: null, michael@0: loader.contentScriptFile michael@0: ); michael@0: loader.contentScriptFile = value = [uri]; michael@0: assert.equal( michael@0: value, michael@0: loader.contentScriptFile michael@0: ); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);