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

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

mercurial