123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /* 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 { Cc, Ci, Cu } = require("chrome");
- const { setTimeout } = require("sdk/timers");
- exports["test disposables are desposed 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 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)
- let foo1 = null
- let 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('test').run(exports);
|