addon-sdk/source/test/test-content-symbiont.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-symbiont.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     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 +"use strict";
     1.8 +
     1.9 +const { Cc, Ci } = require('chrome');
    1.10 +const { Symbiont } = require('sdk/deprecated/symbiont');
    1.11 +const self = require('sdk/self');
    1.12 +const fixtures = require("./fixtures");
    1.13 +const { close } = require('sdk/window/helpers');
    1.14 +const app = require("sdk/system/xul-app");
    1.15 +
    1.16 +function makeWindow() {
    1.17 +  let content =
    1.18 +    '<?xml version="1.0"?>' +
    1.19 +    '<window ' +
    1.20 +    'xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' +
    1.21 +    '<iframe id="content" type="content"/>' +
    1.22 +    '</window>';
    1.23 +  var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
    1.24 +            encodeURIComponent(content);
    1.25 +  var features = ["chrome", "width=10", "height=10"];
    1.26 +
    1.27 +  return Cc["@mozilla.org/embedcomp/window-watcher;1"].
    1.28 +         getService(Ci.nsIWindowWatcher).
    1.29 +         openWindow(null, url, null, features.join(","), null);
    1.30 +}
    1.31 +
    1.32 +exports['test:constructing symbiont && validating API'] = function(assert) {
    1.33 +  let contentScript = ["1;", "2;"];
    1.34 +  let contentScriptFile = fixtures.url("test-content-symbiont.js");
    1.35 +
    1.36 +  // We can avoid passing a `frame` argument. Symbiont will create one
    1.37 +  // by using HiddenFrame module
    1.38 +  let contentSymbiont = Symbiont({
    1.39 +    contentScriptFile: contentScriptFile,
    1.40 +    contentScript: contentScript,
    1.41 +    contentScriptWhen: "start"
    1.42 +  });
    1.43 +
    1.44 +  assert.equal(
    1.45 +    contentScriptFile,
    1.46 +    contentSymbiont.contentScriptFile,
    1.47 +    "There is one contentScriptFile, as specified in options."
    1.48 +  );
    1.49 +  assert.equal(
    1.50 +    contentScript.length,
    1.51 +    contentSymbiont.contentScript.length,
    1.52 +    "There are two contentScripts, as specified in options."
    1.53 +  );
    1.54 +  assert.equal(
    1.55 +    contentScript[0],
    1.56 +    contentSymbiont.contentScript[0],
    1.57 +    "There are two contentScripts, as specified in options."
    1.58 +  );
    1.59 +  assert.equal(
    1.60 +    contentScript[1],
    1.61 +    contentSymbiont.contentScript[1],
    1.62 +    "There are two contentScripts, as specified in options."
    1.63 +  )
    1.64 +  assert.equal(
    1.65 +    contentSymbiont.contentScriptWhen,
    1.66 +    "start",
    1.67 +    "contentScriptWhen is as specified in options."
    1.68 +  );
    1.69 +
    1.70 +  contentSymbiont.destroy();
    1.71 +};
    1.72 +
    1.73 +exports["test:communication with worker global scope"] = function(assert, done) {
    1.74 +  if (app.is('Fennec')) {
    1.75 +    assert.pass('Test skipped on Fennec');
    1.76 +    done();
    1.77 +  }
    1.78 +
    1.79 +  let window = makeWindow();
    1.80 +  let contentSymbiont;
    1.81 +
    1.82 +  assert.ok(!!window, 'there is a window');
    1.83 +
    1.84 +  function onMessage1(message) {
    1.85 +    assert.equal(message, 1, "Program gets message via onMessage.");
    1.86 +    contentSymbiont.removeListener('message', onMessage1);
    1.87 +    contentSymbiont.on('message', onMessage2);
    1.88 +    contentSymbiont.postMessage(2);
    1.89 +  };
    1.90 +
    1.91 +  function onMessage2(message) {
    1.92 +    if (5 == message) {
    1.93 +      close(window).then(done);
    1.94 +    }
    1.95 +    else {
    1.96 +      assert.equal(message, 3, "Program gets message via onMessage2.");
    1.97 +      contentSymbiont.postMessage(4)
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +  window.addEventListener("load", function onLoad() {
   1.102 +    window.removeEventListener("load", onLoad, false);
   1.103 +    let frame = window.document.getElementById("content");
   1.104 +    contentSymbiont = Symbiont({
   1.105 +      frame: frame,
   1.106 +      contentScript: 'new ' + function() {
   1.107 +        self.postMessage(1);
   1.108 +        self.on("message", function onMessage(message) {
   1.109 +          if (message === 2)
   1.110 +            self.postMessage(3);
   1.111 +          if (message === 4)
   1.112 +            self.postMessage(5);
   1.113 +        });
   1.114 +      } + '()',
   1.115 +      contentScriptWhen: 'ready',
   1.116 +      onMessage: onMessage1
   1.117 +    });
   1.118 +
   1.119 +    frame.setAttribute("src", "data:text/html;charset=utf-8,<html><body></body></html>");
   1.120 +  }, false);
   1.121 +};
   1.122 +
   1.123 +exports['test:pageWorker'] = function(assert, done) {
   1.124 +  let worker =  Symbiont({
   1.125 +    contentURL: 'about:buildconfig',
   1.126 +    contentScript: 'new ' + function WorkerScope() {
   1.127 +      self.on('message', function(data) {
   1.128 +        if (data.valid)
   1.129 +          self.postMessage('bye!');
   1.130 +      })
   1.131 +      self.postMessage(window.location.toString());
   1.132 +    },
   1.133 +    onMessage: function(msg) {
   1.134 +      if (msg == 'bye!') {
   1.135 +        done()
   1.136 +      } else {
   1.137 +        assert.equal(
   1.138 +          worker.contentURL + '',
   1.139 +          msg
   1.140 +        );
   1.141 +        worker.postMessage({ valid: true });
   1.142 +      }
   1.143 +    }
   1.144 +  });
   1.145 +};
   1.146 +
   1.147 +exports["test:document element present on 'start'"] = function(assert, done) {
   1.148 +  let xulApp = require("sdk/system/xul-app");
   1.149 +  let worker = Symbiont({
   1.150 +    contentURL: "about:buildconfig",
   1.151 +    contentScript: "self.postMessage(!!document.documentElement)",
   1.152 +    contentScriptWhen: "start",
   1.153 +    onMessage: function(message) {
   1.154 +      if (xulApp.versionInRange(xulApp.platformVersion, "2.0b6", "*"))
   1.155 +        assert.ok(message, "document element present on 'start'");
   1.156 +      else
   1.157 +        assert.pass("document element not necessarily present on 'start'");
   1.158 +      done();
   1.159 +    }
   1.160 +  });
   1.161 +};
   1.162 +
   1.163 +require("test").run(exports);

mercurial