addon-sdk/source/test/test-content-loader.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6 const { Loader } = require('sdk/content/loader');
michael@0 7 const self = require("sdk/self");
michael@0 8 const fixtures = require("./fixtures");
michael@0 9 const { URL } = require('sdk/url');
michael@0 10
michael@0 11 exports['test:contentURL'] = function(assert) {
michael@0 12 let loader = Loader(),
michael@0 13 value, emitted = 0, changes = 0;
michael@0 14
michael@0 15 assert.throws(
michael@0 16 function() loader.contentURL = 4,
michael@0 17 /The `contentURL` option must be a valid URL./,
michael@0 18 'Must throw an exception if `contentURL` is not URL.'
michael@0 19 );
michael@0 20 assert.throws(
michael@0 21 function() loader.contentURL = { toString: function() 'Oops' },
michael@0 22 /The `contentURL` option must be a valid URL./,
michael@0 23 'Must throw an exception if `contentURL` is not URL.'
michael@0 24 );
michael@0 25
michael@0 26 function listener(e) {
michael@0 27 emitted ++;
michael@0 28 assert.ok(
michael@0 29 'contentURL' in e,
michael@0 30 'emitted event must contain "content" property'
michael@0 31 );
michael@0 32 assert.ok(
michael@0 33 value,
michael@0 34 '' + e.contentURL,
michael@0 35 'content property of an event must match value'
michael@0 36 );
michael@0 37 }
michael@0 38 loader.on('propertyChange', listener);
michael@0 39
michael@0 40 assert.equal(
michael@0 41 null,
michael@0 42 loader.contentURL,
michael@0 43 'default value is `null`'
michael@0 44 );
michael@0 45 loader.contentURL = value = 'data:text/html,<html><body>Hi</body><html>';
michael@0 46 assert.equal(
michael@0 47 value,
michael@0 48 '' + loader.contentURL,
michael@0 49 'data uri is ok'
michael@0 50 );
michael@0 51 assert.equal(
michael@0 52 ++changes,
michael@0 53 emitted,
michael@0 54 'had to emit `propertyChange`'
michael@0 55 );
michael@0 56 loader.contentURL = value;
michael@0 57 assert.equal(
michael@0 58 changes,
michael@0 59 emitted,
michael@0 60 'must not emit `propertyChange` if same value is set'
michael@0 61 );
michael@0 62
michael@0 63 loader.contentURL = value = 'http://google.com/';
michael@0 64 assert.equal(
michael@0 65 value,
michael@0 66 '' + loader.contentURL,
michael@0 67 'value must be set'
michael@0 68 );
michael@0 69 assert.equal(
michael@0 70 ++ changes,
michael@0 71 emitted,
michael@0 72 'had to emit `propertyChange`'
michael@0 73 );
michael@0 74 loader.contentURL = value;
michael@0 75 assert.equal(
michael@0 76 changes,
michael@0 77 emitted,
michael@0 78 'must not emit `propertyChange` if same value is set'
michael@0 79 );
michael@0 80
michael@0 81 loader.removeListener('propertyChange', listener);
michael@0 82 loader.contentURL = value = 'about:blank';
michael@0 83 assert.equal(
michael@0 84 value,
michael@0 85 '' + loader.contentURL,
michael@0 86 'contentURL must be an actual value'
michael@0 87 );
michael@0 88 assert.equal(
michael@0 89 changes,
michael@0 90 emitted,
michael@0 91 'listener had to be romeved'
michael@0 92 );
michael@0 93 };
michael@0 94
michael@0 95 exports['test:contentScriptWhen'] = function(assert) {
michael@0 96 let loader = Loader();
michael@0 97 assert.equal(
michael@0 98 'end',
michael@0 99 loader.contentScriptWhen,
michael@0 100 '`contentScriptWhen` defaults to "end"'
michael@0 101 );
michael@0 102 loader.contentScriptWhen = "end";
michael@0 103 assert.equal(
michael@0 104 "end",
michael@0 105 loader.contentScriptWhen
michael@0 106 );
michael@0 107 try {
michael@0 108 loader.contentScriptWhen = 'boom';
michael@0 109 test.fail('must throw when wrong value is set');
michael@0 110 } catch(e) {
michael@0 111 assert.equal(
michael@0 112 'The `contentScriptWhen` option must be either "start", "ready" or "end".',
michael@0 113 e.message
michael@0 114 );
michael@0 115 }
michael@0 116 loader.contentScriptWhen = null;
michael@0 117 assert.equal(
michael@0 118 'end',
michael@0 119 loader.contentScriptWhen,
michael@0 120 '`contentScriptWhen` defaults to "end"'
michael@0 121 );
michael@0 122 loader.contentScriptWhen = "ready";
michael@0 123 assert.equal(
michael@0 124 "ready",
michael@0 125 loader.contentScriptWhen
michael@0 126 );
michael@0 127 loader.contentScriptWhen = "start";
michael@0 128 assert.equal(
michael@0 129 'start',
michael@0 130 loader.contentScriptWhen
michael@0 131 );
michael@0 132 };
michael@0 133
michael@0 134 exports['test:contentScript'] = function(assert) {
michael@0 135 let loader = Loader(), value;
michael@0 136 assert.equal(
michael@0 137 null,
michael@0 138 loader.contentScript,
michael@0 139 '`contentScript` defaults to `null`'
michael@0 140 );
michael@0 141 loader.contentScript = value = 'let test = {};';
michael@0 142 assert.equal(
michael@0 143 value,
michael@0 144 loader.contentScript
michael@0 145 );
michael@0 146 try {
michael@0 147 loader.contentScript = { 1: value }
michael@0 148 test.fail('must throw when wrong value is set');
michael@0 149 } catch(e) {
michael@0 150 assert.equal(
michael@0 151 'The `contentScript` option must be a string or an array of strings.',
michael@0 152 e.message
michael@0 153 );
michael@0 154 }
michael@0 155 try {
michael@0 156 loader.contentScript = ['oue', 2]
michael@0 157 test.fail('must throw when wrong value is set');
michael@0 158 } catch(e) {
michael@0 159 assert.equal(
michael@0 160 'The `contentScript` option must be a string or an array of strings.',
michael@0 161 e.message
michael@0 162 );
michael@0 163 }
michael@0 164 loader.contentScript = undefined;
michael@0 165 assert.equal(
michael@0 166 null,
michael@0 167 loader.contentScript
michael@0 168 );
michael@0 169 loader.contentScript = value = ["1;", "2;"];
michael@0 170 assert.equal(
michael@0 171 value,
michael@0 172 loader.contentScript
michael@0 173 );
michael@0 174 };
michael@0 175
michael@0 176 exports['test:contentScriptFile'] = function(assert) {
michael@0 177 let loader = Loader(), value, uri = fixtures.url("test-content-loader.js");
michael@0 178 assert.equal(
michael@0 179 null,
michael@0 180 loader.contentScriptFile,
michael@0 181 '`contentScriptFile` defaults to `null`'
michael@0 182 );
michael@0 183 loader.contentScriptFile = value = uri;
michael@0 184 assert.equal(
michael@0 185 value,
michael@0 186 loader.contentScriptFile
michael@0 187 );
michael@0 188 try {
michael@0 189 loader.contentScriptFile = { 1: uri }
michael@0 190 test.fail('must throw when wrong value is set');
michael@0 191 } catch(e) {
michael@0 192 assert.equal(
michael@0 193 'The `contentScriptFile` option must be a local URL or an array of URLs.',
michael@0 194 e.message
michael@0 195 );
michael@0 196 }
michael@0 197
michael@0 198 try {
michael@0 199 loader.contentScriptFile = [ 'oue', uri ]
michael@0 200 test.fail('must throw when wrong value is set');
michael@0 201 } catch(e) {
michael@0 202 assert.equal(
michael@0 203 'The `contentScriptFile` option must be a local URL or an array of URLs.',
michael@0 204 e.message
michael@0 205 );
michael@0 206 }
michael@0 207
michael@0 208 let data = 'data:text/html,test';
michael@0 209 try {
michael@0 210 loader.contentScriptFile = [ { toString: () => data } ];
michael@0 211 test.fail('must throw when non-URL object is set');
michael@0 212 } catch(e) {
michael@0 213 assert.equal(
michael@0 214 'The `contentScriptFile` option must be a local URL or an array of URLs.',
michael@0 215 e.message
michael@0 216 );
michael@0 217 }
michael@0 218
michael@0 219 loader.contentScriptFile = new URL(data);
michael@0 220 assert.ok(
michael@0 221 loader.contentScriptFile instanceof URL,
michael@0 222 'must be able to set `contentScriptFile` to an instance of URL'
michael@0 223 );
michael@0 224 assert.equal(
michael@0 225 data,
michael@0 226 loader.contentScriptFile.toString(),
michael@0 227 'setting `contentScriptFile` to an instance of URL should preserve the url'
michael@0 228 );
michael@0 229
michael@0 230 loader.contentScriptFile = undefined;
michael@0 231 assert.equal(
michael@0 232 null,
michael@0 233 loader.contentScriptFile
michael@0 234 );
michael@0 235 loader.contentScriptFile = value = [uri];
michael@0 236 assert.equal(
michael@0 237 value,
michael@0 238 loader.contentScriptFile
michael@0 239 );
michael@0 240 };
michael@0 241
michael@0 242 require('sdk/test').run(exports);

mercurial