test-content-symbiont.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { Cc, Ci } = require('chrome');
  6. const { Symbiont } = require('sdk/deprecated/symbiont');
  7. const self = require('sdk/self');
  8. const fixtures = require("./fixtures");
  9. const { close } = require('sdk/window/helpers');
  10. const app = require("sdk/system/xul-app");
  11. function makeWindow() {
  12. let content =
  13. '<?xml version="1.0"?>' +
  14. '<window ' +
  15. 'xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' +
  16. '<iframe id="content" type="content"/>' +
  17. '</window>';
  18. var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
  19. encodeURIComponent(content);
  20. var features = ["chrome", "width=10", "height=10"];
  21. return Cc["@mozilla.org/embedcomp/window-watcher;1"].
  22. getService(Ci.nsIWindowWatcher).
  23. openWindow(null, url, null, features.join(","), null);
  24. }
  25. exports['test:constructing symbiont && validating API'] = function(assert) {
  26. let contentScript = ["1;", "2;"];
  27. let contentScriptFile = fixtures.url("test-content-symbiont.js");
  28. // We can avoid passing a `frame` argument. Symbiont will create one
  29. // by using HiddenFrame module
  30. let contentSymbiont = Symbiont({
  31. contentScriptFile: contentScriptFile,
  32. contentScript: contentScript,
  33. contentScriptWhen: "start"
  34. });
  35. assert.equal(
  36. contentScriptFile,
  37. contentSymbiont.contentScriptFile,
  38. "There is one contentScriptFile, as specified in options."
  39. );
  40. assert.equal(
  41. contentScript.length,
  42. contentSymbiont.contentScript.length,
  43. "There are two contentScripts, as specified in options."
  44. );
  45. assert.equal(
  46. contentScript[0],
  47. contentSymbiont.contentScript[0],
  48. "There are two contentScripts, as specified in options."
  49. );
  50. assert.equal(
  51. contentScript[1],
  52. contentSymbiont.contentScript[1],
  53. "There are two contentScripts, as specified in options."
  54. )
  55. assert.equal(
  56. contentSymbiont.contentScriptWhen,
  57. "start",
  58. "contentScriptWhen is as specified in options."
  59. );
  60. contentSymbiont.destroy();
  61. };
  62. exports["test:communication with worker global scope"] = function(assert, done) {
  63. if (app.is('Fennec')) {
  64. assert.pass('Test skipped on Fennec');
  65. done();
  66. }
  67. let window = makeWindow();
  68. let contentSymbiont;
  69. console.log(window)
  70. function onMessage1(message) {
  71. assert.equal(message, 1, "Program gets message via onMessage.");
  72. contentSymbiont.removeListener('message', onMessage1);
  73. contentSymbiont.on('message', onMessage2);
  74. contentSymbiont.postMessage(2);
  75. };
  76. function onMessage2(message) {
  77. if (5 == message) {
  78. close(window).then(done);
  79. }
  80. else {
  81. assert.equal(message, 3, "Program gets message via onMessage2.");
  82. contentSymbiont.postMessage(4)
  83. }
  84. }
  85. window.addEventListener("load", function onLoad() {
  86. window.removeEventListener("load", onLoad, false);
  87. let frame = window.document.getElementById("content");
  88. contentSymbiont = Symbiont({
  89. frame: frame,
  90. contentScript: 'new ' + function() {
  91. self.postMessage(1);
  92. self.on("message", function onMessage(message) {
  93. if (message === 2)
  94. self.postMessage(3);
  95. if (message === 4)
  96. self.postMessage(5);
  97. });
  98. } + '()',
  99. contentScriptWhen: 'ready',
  100. onMessage: onMessage1
  101. });
  102. frame.setAttribute("src", "data:text/html;charset=utf-8,<html><body></body></html>");
  103. }, false);
  104. };
  105. exports['test:pageWorker'] = function(assert, done) {
  106. let worker = Symbiont({
  107. contentURL: 'about:buildconfig',
  108. contentScript: 'new ' + function WorkerScope() {
  109. self.on('message', function(data) {
  110. if (data.valid)
  111. self.postMessage('bye!');
  112. })
  113. self.postMessage(window.location.toString());
  114. },
  115. onMessage: function(msg) {
  116. if (msg == 'bye!') {
  117. done()
  118. } else {
  119. assert.equal(
  120. worker.contentURL + '',
  121. msg
  122. );
  123. worker.postMessage({ valid: true });
  124. }
  125. }
  126. });
  127. };
  128. exports["test:document element present on 'start'"] = function(assert, done) {
  129. let xulApp = require("sdk/system/xul-app");
  130. let worker = Symbiont({
  131. contentURL: "about:buildconfig",
  132. contentScript: "self.postMessage(!!document.documentElement)",
  133. contentScriptWhen: "start",
  134. onMessage: function(message) {
  135. if (xulApp.versionInRange(xulApp.platformVersion, "2.0b6", "*"))
  136. assert.ok(message, "document element present on 'start'");
  137. else
  138. assert.pass("document element not necessarily present on 'start'");
  139. done();
  140. }
  141. });
  142. };
  143. require("test").run(exports);