summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-disposable.js
blob: 3204c2479bacb483674df7c28cad3a8b7824f4b5 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { Loader } = require("sdk/test/loader");
const { Class } = require("sdk/core/heritage");
const { Disposable } = require("sdk/core/disposable");
const { Cc, Ci, Cu } = require("chrome");
const { setTimeout } = require("sdk/timers");

exports["test disposeDisposable"] = assert => {
  let loader = Loader(module);

  const { Disposable, disposeDisposable } = loader.require("sdk/core/disposable");
  const { isWeak, WeakReference } = loader.require("sdk/core/reference");

  let disposals = 0;

  const Foo = Class({
    extends: Disposable,
    implements: [WeakReference],
    dispose(...params) {
      disposeDisposable(this);
      disposals = disposals + 1;
    }
  });

  const f1 = new Foo();
  assert.equal(isWeak(f1), true, "f1 has WeakReference support");

  f1.dispose();
  assert.equal(disposals, 1, "disposed on dispose");

  loader.unload("uninstall");
  assert.equal(disposals, 1, "after disposeDisposable, dispose is not called anymore");
};

exports["test destroy reasons"] = assert => {
  let disposals = 0;

  const Foo = Class({
    extends: Disposable,
    dispose: function() {
      disposals = disposals + 1;
    }
  });

  const f1 = new Foo();

  f1.destroy();
  assert.equal(disposals, 1, "disposed on destroy");
  f1.destroy();
  assert.equal(disposals, 1, "second destroy is ignored");

  disposals = 0;
  const f2 = new Foo();

  f2.destroy("uninstall");
  assert.equal(disposals, 1, "uninstall invokes disposal");
  f2.destroy("uninstall")
  f2.destroy();
  assert.equal(disposals, 1, "disposal happens just once");

  disposals = 0;
  const f3 = new Foo();

  f3.destroy("shutdown");
  assert.equal(disposals, 0, "shutdown invoke disposal");
  f3.destroy("shutdown");
  f3.destroy();
  assert.equal(disposals, 0, "shutdown disposal happens just once");

  disposals = 0;
  const f4 = new Foo();

  f4.destroy("disable");
  assert.equal(disposals, 1, "disable invokes disposal");
  f4.destroy("disable")
  f4.destroy();
  assert.equal(disposals, 1, "destroy happens just once");

  disposals = 0;
  const f5 = new Foo();

  f5.destroy("disable");
  assert.equal(disposals, 1, "disable invokes disposal");
  f5.destroy("disable")
  f5.destroy();
  assert.equal(disposals, 1, "destroy happens just once");

  disposals = 0;
  const f6 = new Foo();

  f6.destroy("upgrade");
  assert.equal(disposals, 1, "upgrade invokes disposal");
  f6.destroy("upgrade")
  f6.destroy();
  assert.equal(disposals, 1, "destroy happens just once");

  disposals = 0;
  const f7 = new Foo();

  f7.destroy("downgrade");
  assert.equal(disposals, 1, "downgrade invokes disposal");
  f7.destroy("downgrade")
  f7.destroy();
  assert.equal(disposals, 1, "destroy happens just once");


  disposals = 0;
  const f8 = new Foo();

  f8.destroy("whatever");
  assert.equal(disposals, 1, "unrecognized reason invokes disposal");
  f8.destroy("meh")
  f8.destroy();
  assert.equal(disposals, 1, "destroy happens just once");
};

exports["test different unload hooks"] = assert => {
  const { uninstall, shutdown, disable, upgrade,
          downgrade, dispose } = require("sdk/core/disposable");
  const UberUnload = Class({
    extends: Disposable,
    setup: function() {
      this.log = [];
    }
  });

  uninstall.define(UberUnload, x => x.log.push("uninstall"));
  shutdown.define(UberUnload, x => x.log.push("shutdown"));
  disable.define(UberUnload, x => x.log.push("disable"));
  upgrade.define(UberUnload, x => x.log.push("upgrade"));
  downgrade.define(UberUnload, x => x.log.push("downgrade"));
  dispose.define(UberUnload, x => x.log.push("dispose"));

  const u1 = new UberUnload();
  u1.destroy("uninstall");
  u1.destroy();
  u1.destroy("shutdown");
  assert.deepEqual(u1.log, ["uninstall"], "uninstall hook invoked");

  const u2 = new UberUnload();
  u2.destroy("shutdown");
  u2.destroy();
  u2.destroy("uninstall");
  assert.deepEqual(u2.log, ["shutdown"], "shutdown hook invoked");

  const u3 = new UberUnload();
  u3.destroy("disable");
  u3.destroy();
  u3.destroy("uninstall");
  assert.deepEqual(u3.log, ["disable"], "disable hook invoked");

  const u4 = new UberUnload();
  u4.destroy("upgrade");
  u4.destroy();
  u4.destroy("uninstall");
  assert.deepEqual(u4.log, ["upgrade"], "upgrade hook invoked");

  const u5 = new UberUnload();
  u5.destroy("downgrade");
  u5.destroy();
  u5.destroy("uninstall");
  assert.deepEqual(u5.log, ["downgrade"], "downgrade hook invoked");

  const u6 = new UberUnload();
  u6.destroy();
  u6.destroy();
  u6.destroy("uninstall");
  assert.deepEqual(u6.log, ["dispose"], "dispose hook invoked");

  const u7 = new UberUnload();
  u7.destroy("whatever");
  u7.destroy();
  u7.destroy("uninstall");
  assert.deepEqual(u7.log, ["dispose"], "dispose hook invoked");
};

exports["test disposables are disposed on unload"] = function(assert) {
  let loader = Loader(module);
  let { Disposable } = loader.require("sdk/core/disposable");

  let arg1 = {}
  let arg2 = 2
  let disposals = 0

  let Foo = Class({
    extends: Disposable,
    setup: function setup(a, b) {
      assert.equal(a, arg1,
                   "arguments passed to constructur is passed to setup");
      assert.equal(b, arg2,
                   "second argument is also passed to a setup");
      assert.ok(this instanceof Foo,
                "this is an instance in the scope of the setup method");

      this.fooed = true
    },
    dispose: function dispose() {
      assert.equal(this.fooed, true, "attribute was set")
      this.fooed = false
      disposals = disposals + 1
    }
  })

  let foo1 = Foo(arg1, arg2)
  let foo2 = Foo(arg1, arg2)

  loader.unload();

  assert.equal(disposals, 2, "both instances were disposed")
}

exports["test destroyed windows dispose before unload"] = function(assert) {
  let loader = Loader(module);
  let { Disposable } = loader.require("sdk/core/disposable");

  let arg1 = {}
  let arg2 = 2
  let disposals = 0

  let Foo = Class({
    extends: Disposable,
    setup: function setup(a, b) {
      assert.equal(a, arg1,
                   "arguments passed to constructur is passed to setup");
      assert.equal(b, arg2,
                   "second argument is also passed to a setup");
      assert.ok(this instanceof Foo,
                "this is an instance in the scope of the setup method");

      this.fooed = true
    },
    dispose: function dispose() {
      assert.equal(this.fooed, true, "attribute was set")
      this.fooed = false
      disposals = disposals + 1
    }
  })

  let foo1 = Foo(arg1, arg2)
  let foo2 = Foo(arg1, arg2)

  foo1.destroy();
  assert.equal(disposals, 1, "destroy disposes instance")

  loader.unload();

  assert.equal(disposals, 2, "unload disposes alive instances")
}

exports["test disposables are GC-able"] = function(assert, done) {
  let loader = Loader(module);
  let { Disposable } = loader.require("sdk/core/disposable");
  let { WeakReference } = loader.require("sdk/core/reference");

  let arg1 = {}
  let arg2 = 2
  let disposals = 0

  let Foo = Class({
    extends: Disposable,
    implements: [WeakReference],
    setup: function setup(a, b) {
      assert.equal(a, arg1,
                   "arguments passed to constructur is passed to setup");
      assert.equal(b, arg2,
                   "second argument is also passed to a setup");
      assert.ok(this instanceof Foo,
                "this is an instance in the scope of the setup method");

      this.fooed = true
    },
    dispose: function dispose() {
      assert.equal(this.fooed, true, "attribute was set")
      this.fooed = false
      disposals = disposals + 1
    }
  });

  let foo1 = Foo(arg1, arg2)
  let foo2 = Foo(arg1, arg2)

  foo1 = foo2 = null;

  Cu.schedulePreciseGC(function() {
    loader.unload();
    assert.equal(disposals, 0, "GC removed dispose listeners");
    done();
  });
}


exports["test loader unloads do not affect other loaders"] = function(assert) {
  let loader1 = Loader(module);
  let loader2 = Loader(module);
  let { Disposable: Disposable1 } = loader1.require("sdk/core/disposable");
  let { Disposable: Disposable2 } = loader2.require("sdk/core/disposable");

  let arg1 = {}
  let arg2 = 2
  let disposals = 0

  let Foo1 = Class({
    extends: Disposable1,
    dispose: function dispose() {
      disposals = disposals + 1;
    }
  });

  let Foo2 = Class({
    extends: Disposable2,
    dispose: function dispose() {
      disposals = disposals + 1;
    }
  });

  let foo1 = Foo1(arg1, arg2);
  let foo2 = Foo2(arg1, arg2);

  assert.equal(disposals, 0, "no destroy calls");

  loader1.unload();

  assert.equal(disposals, 1, "1 destroy calls");

  loader2.unload();

  assert.equal(disposals, 2, "2 destroy calls");
}

exports["test disposables that throw"] = function(assert) {
  let loader = Loader(module);
  let { Disposable } = loader.require("sdk/core/disposable");

  let disposals = 0

  let Foo = Class({
    extends: Disposable,
    setup: function setup(a, b) {
      throw Error("Boom!")
    },
    dispose: function dispose() {
      disposals = disposals + 1
    }
  })

  assert.throws(function() {
    let foo1 = Foo()
  }, /Boom/, "disposable constructors may throw");

  loader.unload();

  assert.equal(disposals, 0, "no disposal if constructor threw");
}

exports["test multiple destroy"] = function(assert) {
  let loader = Loader(module);
  let { Disposable } = loader.require("sdk/core/disposable");

  let disposals = 0

  let Foo = Class({
    extends: Disposable,
    dispose: function dispose() {
      disposals = disposals + 1
    }
  })

  let foo1 = Foo();
  let foo2 = Foo();
  let foo3 = Foo();

  assert.equal(disposals, 0, "no disposals yet");

  foo1.destroy();
  assert.equal(disposals, 1, "disposed properly");
  foo1.destroy();
  assert.equal(disposals, 1, "didn't attempt to dispose twice");

  foo2.destroy();
  assert.equal(disposals, 2, "other instances still dispose fine");
  foo2.destroy();
  assert.equal(disposals, 2, "but not twice");

  loader.unload();

  assert.equal(disposals, 3, "unload only disposed the remaining instance");
}

require('sdk/test').run(exports);