aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.3/packages/api-utils/tests/test-content-loader.js
blob: 0e91ee51e1d20d9d6d99ac82bda452a656426a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"use strict";
const { Loader } = require('content/loader');
const self = require("self");

exports['test:contentURL'] = function(test) {
  let loader = Loader(),
      value, emitted = 0, changes = 0;

  test.assertRaises(
    function() loader.contentURL = undefined,
    'The `contentURL` option must be a valid URL.',
    'Must throw an exception if `contentURL` is not URL.'
  );
   test.assertRaises(
    function() loader.contentURL = null,
    'The `contentURL` option must be a valid URL.',
    'Must throw an exception if `contentURL` is not URL.'
  );
  test.assertRaises(
    function() loader.contentURL = 4,
    'The `contentURL` option must be a valid URL.',
    'Must throw an exception if `contentURL` is not URL.'
  );
 test.assertRaises(
    function() loader.contentURL = { toString: function() 'Oops' },
    'The `contentURL` option must be a valid URL.',
    'Must throw an exception if `contentURL` is not URL.'
  );

  function listener(e) {
    emitted ++;
    test.assert(
      'contentURL' in e,
      'emitted event must contain "content" property'
    );
    test.assert(
      value,
      '' + e.contentURL,
      'content property of an event must match value'
    );
  }
  loader.on('propertyChange', listener);

  test.assertEqual(
    null,
    loader.contentURL,
    'default value is `null`'
  );
  loader.contentURL = value = 'data:text/html,<html><body>Hi</body><html>';
  test.assertEqual(
    value,
    '' + loader.contentURL,
    'data uri is ok'
  );
  test.assertEqual(
    ++changes,
    emitted,
    'had to emit `propertyChange`'
  );
  loader.contentURL = value;
  test.assertEqual(
    changes,
    emitted,
    'must not emit `propertyChange` if same value is set'
  );

  loader.contentURL = value = 'http://google.com/';
  test.assertEqual(
    value,
    '' + loader.contentURL,
    'value must be set'
  );
  test.assertEqual(
    ++ changes,
    emitted,
    'had to emit `propertyChange`'
  );
  loader.contentURL = value;
  test.assertEqual(
    changes,
    emitted,
    'must not emit `propertyChange` if same value is set'
  );
  
  loader.removeListener('propertyChange', listener);
  loader.contentURL = value = 'about:blank';
  test.assertEqual(
    value,
    '' + loader.contentURL,
    'contentURL must be an actual value'
  );
  test.assertEqual(
    changes,
    emitted,
    'listener had to be romeved'
  );
};

exports['test:contentScriptWhen'] = function(test) {
  let loader = Loader();
  test.assertEqual(
    'end',
    loader.contentScriptWhen,
    '`contentScriptWhen` defaults to "end"'
  );
  loader.contentScriptWhen = "end";
  test.assertEqual(
    "end",
    loader.contentScriptWhen
  );
  try {
    loader.contentScriptWhen = 'boom';
    test.fail('must throw when wrong value is set');
  } catch(e) {
    test.assertEqual(
      'The `contentScriptWhen` option must be either "start", "ready" or "end".',
      e.message
    );
  }
  loader.contentScriptWhen = null;
  test.assertEqual(
    'end',
    loader.contentScriptWhen,
    '`contentScriptWhen` defaults to "end"'
  );
  loader.contentScriptWhen = "ready";
  test.assertEqual(
    "ready",
    loader.contentScriptWhen
  );
  loader.contentScriptWhen = "start";
  test.assertEqual(
    'start',
    loader.contentScriptWhen
  );
};

exports['test:contentScript'] = function(test) {
  let loader = Loader(), value;
  test.assertEqual(
    null,
    loader.contentScript,
    '`contentScript` defaults to `null`'
  );
  loader.contentScript = value = 'let test = {};';
  test.assertEqual(
    value,
    loader.contentScript
  );
  try {
    loader.contentScript = { 1: value }
    test.fail('must throw when wrong value is set');
  } catch(e) {
    test.assertEqual(
      'The script option must be a string or an array of strings.',
      e.message
    );
  }
  try {
    loader.contentScript = ['oue', 2]
    test.fail('must throw when wrong value is set');
  } catch(e) {
    test.assertEqual(
      'The script option must be a string or an array of strings.',
      e.message
    );
  }
  loader.contentScript = undefined;
  test.assertEqual(
    null,
    loader.contentScript
  );
  loader.contentScript = value = ["1;", "2;"];
  test.assertEqual(
    value,
    loader.contentScript
  );
};

exports['test:contentScriptFile'] = function(test) {
  let loader = Loader(), value, uri = self.data.url("test-content-loader.js");
  test.assertEqual(
    null,
    loader.contentScriptFile,
    '`contentScriptFile` defaults to `null`'
  );
  loader.contentScriptFile = value = uri;
  test.assertEqual(
    value,
    loader.contentScriptFile
  );
  try {
    loader.contentScriptFile = { 1: uri }
    test.fail('must throw when wrong value is set');
  } catch(e) {
    test.assertEqual(
      'The `contentScriptFile` option must be a local file URL or an array of'
          + 'URLs.',
      e.message
    );
  }
  try {
    loader.contentScriptFile = ['oue', uri]
    test.fail('must throw when wrong value is set');
  } catch(e) {
    test.assertEqual(
      'The `contentScriptFile` option must be a local file URL or an array of'
          + 'URLs.',
      e.message
    );
  }
  loader.contentScriptFile = undefined;
  test.assertEqual(
    null,
    loader.contentScriptFile
  );
  loader.contentScriptFile = value = [uri];
  test.assertEqual(
    value,
    loader.contentScriptFile
  );
};