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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-content-loader.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,242 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +const { Loader } = require('sdk/content/loader');
    1.10 +const self = require("sdk/self");
    1.11 +const fixtures = require("./fixtures");
    1.12 +const { URL } = require('sdk/url');
    1.13 +
    1.14 +exports['test:contentURL'] = function(assert) {
    1.15 +  let loader = Loader(),
    1.16 +      value, emitted = 0, changes = 0;
    1.17 +
    1.18 +  assert.throws(
    1.19 +    function() loader.contentURL = 4,
    1.20 +    /The `contentURL` option must be a valid URL./,
    1.21 +    'Must throw an exception if `contentURL` is not URL.'
    1.22 +  );
    1.23 + assert.throws(
    1.24 +    function() loader.contentURL = { toString: function() 'Oops' },
    1.25 +    /The `contentURL` option must be a valid URL./,
    1.26 +    'Must throw an exception if `contentURL` is not URL.'
    1.27 +  );
    1.28 +
    1.29 +  function listener(e) {
    1.30 +    emitted ++;
    1.31 +    assert.ok(
    1.32 +      'contentURL' in e,
    1.33 +      'emitted event must contain "content" property'
    1.34 +    );
    1.35 +    assert.ok(
    1.36 +      value,
    1.37 +      '' + e.contentURL,
    1.38 +      'content property of an event must match value'
    1.39 +    );
    1.40 +  }
    1.41 +  loader.on('propertyChange', listener);
    1.42 +
    1.43 +  assert.equal(
    1.44 +    null,
    1.45 +    loader.contentURL,
    1.46 +    'default value is `null`'
    1.47 +  );
    1.48 +  loader.contentURL = value = 'data:text/html,<html><body>Hi</body><html>';
    1.49 +  assert.equal(
    1.50 +    value,
    1.51 +    '' + loader.contentURL,
    1.52 +    'data uri is ok'
    1.53 +  );
    1.54 +  assert.equal(
    1.55 +    ++changes,
    1.56 +    emitted,
    1.57 +    'had to emit `propertyChange`'
    1.58 +  );
    1.59 +  loader.contentURL = value;
    1.60 +  assert.equal(
    1.61 +    changes,
    1.62 +    emitted,
    1.63 +    'must not emit `propertyChange` if same value is set'
    1.64 +  );
    1.65 +
    1.66 +  loader.contentURL = value = 'http://google.com/';
    1.67 +  assert.equal(
    1.68 +    value,
    1.69 +    '' + loader.contentURL,
    1.70 +    'value must be set'
    1.71 +  );
    1.72 +  assert.equal(
    1.73 +    ++ changes,
    1.74 +    emitted,
    1.75 +    'had to emit `propertyChange`'
    1.76 +  );
    1.77 +  loader.contentURL = value;
    1.78 +  assert.equal(
    1.79 +    changes,
    1.80 +    emitted,
    1.81 +    'must not emit `propertyChange` if same value is set'
    1.82 +  );
    1.83 +
    1.84 +  loader.removeListener('propertyChange', listener);
    1.85 +  loader.contentURL = value = 'about:blank';
    1.86 +  assert.equal(
    1.87 +    value,
    1.88 +    '' + loader.contentURL,
    1.89 +    'contentURL must be an actual value'
    1.90 +  );
    1.91 +  assert.equal(
    1.92 +    changes,
    1.93 +    emitted,
    1.94 +    'listener had to be romeved'
    1.95 +  );
    1.96 +};
    1.97 +
    1.98 +exports['test:contentScriptWhen'] = function(assert) {
    1.99 +  let loader = Loader();
   1.100 +  assert.equal(
   1.101 +    'end',
   1.102 +    loader.contentScriptWhen,
   1.103 +    '`contentScriptWhen` defaults to "end"'
   1.104 +  );
   1.105 +  loader.contentScriptWhen = "end";
   1.106 +  assert.equal(
   1.107 +    "end",
   1.108 +    loader.contentScriptWhen
   1.109 +  );
   1.110 +  try {
   1.111 +    loader.contentScriptWhen = 'boom';
   1.112 +    test.fail('must throw when wrong value is set');
   1.113 +  } catch(e) {
   1.114 +    assert.equal(
   1.115 +      'The `contentScriptWhen` option must be either "start", "ready" or "end".',
   1.116 +      e.message
   1.117 +    );
   1.118 +  }
   1.119 +  loader.contentScriptWhen = null;
   1.120 +  assert.equal(
   1.121 +    'end',
   1.122 +    loader.contentScriptWhen,
   1.123 +    '`contentScriptWhen` defaults to "end"'
   1.124 +  );
   1.125 +  loader.contentScriptWhen = "ready";
   1.126 +  assert.equal(
   1.127 +    "ready",
   1.128 +    loader.contentScriptWhen
   1.129 +  );
   1.130 +  loader.contentScriptWhen = "start";
   1.131 +  assert.equal(
   1.132 +    'start',
   1.133 +    loader.contentScriptWhen
   1.134 +  );
   1.135 +};
   1.136 +
   1.137 +exports['test:contentScript'] = function(assert) {
   1.138 +  let loader = Loader(), value;
   1.139 +  assert.equal(
   1.140 +    null,
   1.141 +    loader.contentScript,
   1.142 +    '`contentScript` defaults to `null`'
   1.143 +  );
   1.144 +  loader.contentScript = value = 'let test = {};';
   1.145 +  assert.equal(
   1.146 +    value,
   1.147 +    loader.contentScript
   1.148 +  );
   1.149 +  try {
   1.150 +    loader.contentScript = { 1: value }
   1.151 +    test.fail('must throw when wrong value is set');
   1.152 +  } catch(e) {
   1.153 +    assert.equal(
   1.154 +      'The `contentScript` option must be a string or an array of strings.',
   1.155 +      e.message
   1.156 +    );
   1.157 +  }
   1.158 +  try {
   1.159 +    loader.contentScript = ['oue', 2]
   1.160 +    test.fail('must throw when wrong value is set');
   1.161 +  } catch(e) {
   1.162 +    assert.equal(
   1.163 +      'The `contentScript` option must be a string or an array of strings.',
   1.164 +      e.message
   1.165 +    );
   1.166 +  }
   1.167 +  loader.contentScript = undefined;
   1.168 +  assert.equal(
   1.169 +    null,
   1.170 +    loader.contentScript
   1.171 +  );
   1.172 +  loader.contentScript = value = ["1;", "2;"];
   1.173 +  assert.equal(
   1.174 +    value,
   1.175 +    loader.contentScript
   1.176 +  );
   1.177 +};
   1.178 +
   1.179 +exports['test:contentScriptFile'] = function(assert) {
   1.180 +  let loader = Loader(), value, uri = fixtures.url("test-content-loader.js");
   1.181 +  assert.equal(
   1.182 +    null,
   1.183 +    loader.contentScriptFile,
   1.184 +    '`contentScriptFile` defaults to `null`'
   1.185 +  );
   1.186 +  loader.contentScriptFile = value = uri;
   1.187 +  assert.equal(
   1.188 +    value,
   1.189 +    loader.contentScriptFile
   1.190 +  );
   1.191 +  try {
   1.192 +    loader.contentScriptFile = { 1: uri }
   1.193 +    test.fail('must throw when wrong value is set');
   1.194 +  } catch(e) {
   1.195 +    assert.equal(
   1.196 +      'The `contentScriptFile` option must be a local URL or an array of URLs.',
   1.197 +      e.message
   1.198 +    );
   1.199 +  }
   1.200 +
   1.201 +  try {
   1.202 +    loader.contentScriptFile = [ 'oue', uri ]
   1.203 +    test.fail('must throw when wrong value is set');
   1.204 +  } catch(e) {
   1.205 +    assert.equal(
   1.206 +      'The `contentScriptFile` option must be a local URL or an array of URLs.',
   1.207 +      e.message
   1.208 +    );
   1.209 +  }
   1.210 +
   1.211 +  let data = 'data:text/html,test';
   1.212 +  try {
   1.213 +    loader.contentScriptFile = [ { toString: () => data } ];
   1.214 +    test.fail('must throw when non-URL object is set');
   1.215 +  } catch(e) {
   1.216 +    assert.equal(
   1.217 +      'The `contentScriptFile` option must be a local URL or an array of URLs.',
   1.218 +      e.message
   1.219 +    );
   1.220 +  }
   1.221 +
   1.222 +  loader.contentScriptFile = new URL(data);
   1.223 +  assert.ok(
   1.224 +    loader.contentScriptFile instanceof URL,
   1.225 +    'must be able to set `contentScriptFile` to an instance of URL'
   1.226 +  );
   1.227 +  assert.equal(
   1.228 +    data, 
   1.229 +    loader.contentScriptFile.toString(),
   1.230 +    'setting `contentScriptFile` to an instance of URL should preserve the url'
   1.231 +  );
   1.232 +
   1.233 +  loader.contentScriptFile = undefined;
   1.234 +  assert.equal(
   1.235 +    null,
   1.236 +    loader.contentScriptFile
   1.237 +  );
   1.238 +  loader.contentScriptFile = value = [uri];
   1.239 +  assert.equal(
   1.240 +    value,
   1.241 +    loader.contentScriptFile
   1.242 +  );
   1.243 +};
   1.244 +
   1.245 +require('sdk/test').run(exports);

mercurial