summaryrefslogtreecommitdiffstats
path: root/dom/bindings/test/test_observablearray_helper.html
blob: e2f3e42c45112d20c33eb301616dcb4f8af35205 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>Test Helpers of Observable Array Type</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script>
/* global TestInterfaceObservableArray */

add_task(async function init() {
  await SpecialPowers.pushPrefEnv({set: [["dom.expose_test_interfaces", true]]});
});

add_task(function testObservableArray_helper_elementAt() {
  let m = new TestInterfaceObservableArray();

  [
    // [values, property, helper]
    [[true, false], "observableArrayBoolean", m.booleanElementAtInternal.bind(m)],
    [[new TestInterfaceObservableArray(), new TestInterfaceObservableArray()],
      "observableArrayInterface", m.interfaceElementAtInternal.bind(m)],
    [[{property: "test"}, {property: 2}], "observableArrayObject",
     m.objectElementAtInternal.bind(m)],
  ].forEach(function([values, property, helper]) {
    m[property] = values;

    let t = m[property];
    ok(Array.isArray(t), "observable array should be an array type");
    is(t.length, values.length, "length of observable array");

    for (let i = 0; i < values.length; i++) {
      isDeeply(values[i], helper(i), `check index ${i}`);
    }

    SimpleTest.doesThrow(() => {
      helper(values.length);
    }, `getting element outside the range should throw`);
  });
});

add_task(function testObservableArray_helper_replaceElementAt() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;
  let setCallbackTests = null;
  let deleteCallbackTests = null;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback(value, index) {
      setCallbackCount++;
      if (typeof setCallbackTests === 'function') {
        setCallbackTests(value, index);
      }
    },
    deleteBooleanCallback(value, index) {
      deleteCallbackCount++;
      if (typeof deleteCallbackTests === 'function') {
        deleteCallbackTests(value, index);
      }
    },
  });

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 0, "length of observable array should be 0");

  [
    // [index, value, shouldThrow]
    [b.length + 1, false, true],
    [b.length, false, false],
    [b.length + 1, false, false],
    [b.length + 1, true, false],
  ].forEach(function([index, value, shouldThrow]) {
    // Initialize
    let oldValue = b[index];
    let oldLen = b.length;
    setCallbackCount = 0;
    deleteCallbackCount = 0;
    setCallbackTests = function(_value, _index) {
      info(`set callback for ${_index}`);
      is(_value, value, "setCallbackTests: test value argument");
      is(_index, index, "setCallbackTests: test index argument");
    };
    deleteCallbackTests = function(_value, _index) {
      info(`delete callback for ${_index}`);
      is(_value, oldValue, "deleteCallbackTests: test value argument");
      is(_index, index, "deleteCallbackTests: test index argument");
    };

    // Test
    info(`setting value of property ${index} to ${value}`);
    try {
      m.booleanReplaceElementAtInternal(index, value);
      ok(!shouldThrow, `setting value should not throw`);
    } catch(e) {
      ok(shouldThrow, `setting value throws ${e}`);
    }
    is(setCallbackCount, shouldThrow ? 0 : 1, "setCallback count");
    is(deleteCallbackCount, (oldLen > index) ? 1 : 0, "deleteCallback count");
    is(b[index], shouldThrow ? oldValue : value, `property value`);
    is(b.length, shouldThrow ? oldLen : Math.max(oldLen, index + 1), `length of observable array`);
  });
});

add_task(function testObservableArray_helper_replaceElementAt_callback_throw() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback(value) {
      setCallbackCount++;
      if (value) {
        throw new Error("setBooleanCallback");
      }
    },
    deleteBooleanCallback(value, index) {
      deleteCallbackCount++;
      if (index < 2) {
        throw new Error("deleteBooleanCallback");
      }
    },
  });
  m.observableArrayBoolean = [false, false, false];

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 3, "length of observable array should be 3");

  [
    // [index, value, shouldThrow]
    [b.length, true, true],
    [b.length, false, false],
    [b.length, true, true],
    [0, false, true],
    [0, true, true]
  ].forEach(function([index, value, shouldThrow]) {
    // Initialize
    let oldValue = b[index];
    let oldLen = b.length;
    setCallbackCount = 0;
    deleteCallbackCount = 0;

    // Test
    info(`setting value of property ${index} to ${value}`);
    try {
      m.booleanReplaceElementAtInternal(index, value);
      ok(!shouldThrow, `setting value should not throw`);
    } catch(e) {
      ok(shouldThrow, `setting value throws ${e}`);
    }
    is(setCallbackCount, (shouldThrow && index < 2) ? 0 : 1, "setCallback count");
    is(deleteCallbackCount, (oldLen > index) ? 1 : 0, "deleteCallback count");
    is(b[index], shouldThrow ? oldValue : value, "property value");
    is(b.length, shouldThrow ? oldLen : Math.max(oldLen, index + 1), `length of observable array`);
  });
});

add_task(function testObservableArray_helper_appendElement() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;
  let setCallbackTests = null;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback(value, index) {
      setCallbackCount++;
      if (typeof setCallbackTests === 'function') {
        setCallbackTests(value, index);
      }
    },
    deleteBooleanCallback() {
      deleteCallbackCount++;
    },
  });

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 0, "length of observable array should be 0");

  [true, false, true, false].forEach(function(value) {
    // Initialize
    let oldLen = b.length;
    let index = oldLen;
    setCallbackCount = 0;
    deleteCallbackCount = 0;
    setCallbackTests = function(_value, _index) {
      info(`set callback for ${_index}`);
      is(_value, value, "setCallbackTests: test value argument");
      is(_index, index, "setCallbackTests: test index argument");
    };

    // Test
    info(`append ${value}`);
    try {
      m.booleanAppendElementInternal(value);
      ok(true, `appending value should not throw`);
    } catch(e) {
      ok(false, `appending value throws ${e}`);
    }
    is(setCallbackCount, 1, "setCallback should be called");
    is(deleteCallbackCount, 0, "deleteCallback should not be called");
    is(b[index], value, `property value`);
    is(b.length, oldLen + 1, `length of observable array`);
  });
});

add_task(function testObservableArray_helper_appendElement_callback_throw() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback(value) {
      setCallbackCount++;
      if (value) {
        throw new Error("setBooleanCallback");
      }
    },
    deleteBooleanCallback() {
      deleteCallbackCount++;
    },
  });
  m.observableArrayBoolean = [false, false, false];

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 3, "length of observable array should be 3");

  [true, false, true, false].forEach(function(value) {
    // Initialize
    let oldLen = b.length;
    let index = oldLen;
    let oldValue = b[index];
    let shouldThrow = value;
    setCallbackCount = 0;
    deleteCallbackCount = 0;

    // Test
    info(`append ${value}`);
    try {
      m.booleanAppendElementInternal(value);
      ok(!shouldThrow, `appending value should not throw`);
    } catch(e) {
      ok(shouldThrow, `appending value throws ${e}`);
    }
    is(setCallbackCount, 1, "setCallback should be called");
    is(deleteCallbackCount, 0, "deleteCallback should not be called");
    is(b[index], shouldThrow ? oldValue : value, "property value");
    is(b.length, shouldThrow ? oldLen : oldLen + 1, `length of observable array`);
  });
});

add_task(function testObservableArray_helper_removeLastElement() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;
  let deleteCallbackTests = null;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback() {
      setCallbackCount++;
    },
    deleteBooleanCallback(value, index) {
      deleteCallbackCount++;
      if (typeof deleteCallbackTests === 'function') {
        deleteCallbackTests(value, index);
      }
    },
  });
  m.observableArrayBoolean = [true, false, true, false];

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 4, "length of observable array should be 4");

  let oldValues = b.slice();
  while (oldValues.length) {
    // Initialize
    let oldValue = oldValues.pop();
    let index = oldValues.length;
    setCallbackCount = 0;
    deleteCallbackCount = 0;
    deleteCallbackTests = function(_value, _index) {
      info(`delete callback for ${_index}`);
      is(_value, oldValue, "deleteCallbackTests: test value argument");
      is(_index, index, "deleteCallbackTests: test index argument");
    };

    // Test
    info(`remove last element`);
    try {
      m.booleanRemoveLastElementInternal();
      ok(true, `removing last element should not throw`);
    } catch(e) {
      ok(false, `removing last element throws ${e}`);
    }
    is(setCallbackCount, 0, "setCallback should not be called");
    is(deleteCallbackCount, 1, "deleteCallback should be called");
    isDeeply(b, oldValues, `property value`);
    is(b.length, oldValues.length, `length of observable array`);
  }

  // test when array is empty
  setCallbackCount = 0;
  deleteCallbackCount = 0;
  SimpleTest.doesThrow(() => {
    m.booleanRemoveLastElementInternal();
  }, `removing last element should throw when array is empty`);
  is(setCallbackCount, 0, "setCallback should not be called");
  is(deleteCallbackCount, 0, "deleteCallback should not be called");
  is(b.length, 0, `length of observable array`);
});

add_task(function testObservableArray_helper_removeLastElement_callback_throw() {
  let setCallbackCount = 0;
  let deleteCallbackCount = 0;

  let m = new TestInterfaceObservableArray({
    setBooleanCallback() {
      setCallbackCount++;
    },
    deleteBooleanCallback(value) {
      deleteCallbackCount++;
      if (value) {
        throw new Error("deleteBooleanCallback");
      }
    },
  });
  m.observableArrayBoolean = [false, true, false, false];

  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 4, "length of observable array should be 4");

  let shouldThrow = false;
  while (!shouldThrow && b.length) {
    // Initialize
    let oldValues = b.slice();
    let oldLen = b.length;
    shouldThrow = oldValues[oldLen - 1];
    setCallbackCount = 0;
    deleteCallbackCount = 0;

    // Test
    info(`remove last element`);
    try {
      m.booleanRemoveLastElementInternal();
      ok(!shouldThrow, `removing last element should not throw`);
    } catch(e) {
      ok(shouldThrow, `removing last element throws ${e}`);
    }
    is(setCallbackCount, 0, "setCallback should not be called");
    is(deleteCallbackCount, 1, "deleteCallback should be called");
    isDeeply(b, shouldThrow ? oldValues : oldValues.slice(0, oldLen - 1), `property value`);
    is(b.length, shouldThrow ? oldLen : oldLen - 1, `length of observable array`);
  }
});

add_task(function testObservableArray_helper_length() {
  let m = new TestInterfaceObservableArray();
  let b = m.observableArrayBoolean;
  ok(Array.isArray(b), "observable array should be an array type");
  is(b.length, 0, "length of observable array");

  [
    [false, true, false, true],
    [true, false],
    [false, true, false],
  ].forEach(function(values) {
    m.observableArrayBoolean = values;
    is(b.length, m.booleanLengthInternal(), "length helper function");
  });
});
</script>
</body>
</html>