test-content-loader.js 5.7 KB

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