michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { Loader } = require("sdk/test/loader"); michael@0: const { Class } = require("sdk/core/heritage"); michael@0: const { Disposable } = require("sdk/core/disposable"); michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const { setTimeout } = require("sdk/timers"); michael@0: michael@0: exports["test destroy reasons"] = assert => { michael@0: const Foo = Class({ michael@0: extends: Disposable, michael@0: dispose: function() { michael@0: disposals = disposals + 1; michael@0: } michael@0: }); michael@0: michael@0: let disposals = 0; michael@0: const f1 = new Foo(); michael@0: michael@0: f1.destroy(); michael@0: assert.equal(disposals, 1, "disposed on destroy"); michael@0: f1.destroy(); michael@0: assert.equal(disposals, 1, "second destroy is ignored"); michael@0: michael@0: disposals = 0; michael@0: const f2 = new Foo(); michael@0: michael@0: f2.destroy("uninstall"); michael@0: assert.equal(disposals, 1, "uninstall invokes disposal"); michael@0: f2.destroy("uninstall") michael@0: f2.destroy(); michael@0: assert.equal(disposals, 1, "disposal happens just once"); michael@0: michael@0: disposals = 0; michael@0: const f3 = new Foo(); michael@0: michael@0: f3.destroy("shutdown"); michael@0: assert.equal(disposals, 0, "shutdown doesn't invoke disposal"); michael@0: f3.destroy(); michael@0: assert.equal(disposals, 0, "shutdown already skipped disposal"); michael@0: michael@0: disposals = 0; michael@0: const f4 = new Foo(); michael@0: michael@0: f4.destroy("disable"); michael@0: assert.equal(disposals, 1, "disable invokes disposal"); michael@0: f4.destroy("disable") michael@0: f4.destroy(); michael@0: assert.equal(disposals, 1, "destroy happens just once"); michael@0: michael@0: disposals = 0; michael@0: const f5 = new Foo(); michael@0: michael@0: f5.destroy("disable"); michael@0: assert.equal(disposals, 1, "disable invokes disposal"); michael@0: f5.destroy("disable") michael@0: f5.destroy(); michael@0: assert.equal(disposals, 1, "destroy happens just once"); michael@0: michael@0: disposals = 0; michael@0: const f6 = new Foo(); michael@0: michael@0: f6.destroy("upgrade"); michael@0: assert.equal(disposals, 1, "upgrade invokes disposal"); michael@0: f6.destroy("upgrade") michael@0: f6.destroy(); michael@0: assert.equal(disposals, 1, "destroy happens just once"); michael@0: michael@0: disposals = 0; michael@0: const f7 = new Foo(); michael@0: michael@0: f7.destroy("downgrade"); michael@0: assert.equal(disposals, 1, "downgrade invokes disposal"); michael@0: f7.destroy("downgrade") michael@0: f7.destroy(); michael@0: assert.equal(disposals, 1, "destroy happens just once"); michael@0: michael@0: michael@0: disposals = 0; michael@0: const f8 = new Foo(); michael@0: michael@0: f8.destroy("whatever"); michael@0: assert.equal(disposals, 1, "unrecognized reason invokes disposal"); michael@0: f8.destroy("meh") michael@0: f8.destroy(); michael@0: assert.equal(disposals, 1, "destroy happens just once"); michael@0: }; michael@0: michael@0: exports["test different unload hooks"] = assert => { michael@0: const { uninstall, shutdown, disable, upgrade, michael@0: downgrade, dispose } = require("sdk/core/disposable"); michael@0: const UberUnload = Class({ michael@0: extends: Disposable, michael@0: setup: function() { michael@0: this.log = []; michael@0: } michael@0: }); michael@0: michael@0: uninstall.define(UberUnload, x => x.log.push("uninstall")); michael@0: shutdown.define(UberUnload, x => x.log.push("shutdown")); michael@0: disable.define(UberUnload, x => x.log.push("disable")); michael@0: upgrade.define(UberUnload, x => x.log.push("upgrade")); michael@0: downgrade.define(UberUnload, x => x.log.push("downgrade")); michael@0: dispose.define(UberUnload, x => x.log.push("dispose")); michael@0: michael@0: const u1 = new UberUnload(); michael@0: u1.destroy("uninstall"); michael@0: u1.destroy(); michael@0: u1.destroy("shutdown"); michael@0: assert.deepEqual(u1.log, ["uninstall"], "uninstall hook invoked"); michael@0: michael@0: const u2 = new UberUnload(); michael@0: u2.destroy("shutdown"); michael@0: u2.destroy(); michael@0: u2.destroy("uninstall"); michael@0: assert.deepEqual(u2.log, ["shutdown"], "shutdown hook invoked"); michael@0: michael@0: const u3 = new UberUnload(); michael@0: u3.destroy("disable"); michael@0: u3.destroy(); michael@0: u3.destroy("uninstall"); michael@0: assert.deepEqual(u3.log, ["disable"], "disable hook invoked"); michael@0: michael@0: const u4 = new UberUnload(); michael@0: u4.destroy("upgrade"); michael@0: u4.destroy(); michael@0: u4.destroy("uninstall"); michael@0: assert.deepEqual(u4.log, ["upgrade"], "upgrade hook invoked"); michael@0: michael@0: const u5 = new UberUnload(); michael@0: u5.destroy("downgrade"); michael@0: u5.destroy(); michael@0: u5.destroy("uninstall"); michael@0: assert.deepEqual(u5.log, ["downgrade"], "downgrade hook invoked"); michael@0: michael@0: const u6 = new UberUnload(); michael@0: u6.destroy(); michael@0: u6.destroy(); michael@0: u6.destroy("uninstall"); michael@0: assert.deepEqual(u6.log, ["dispose"], "dispose hook invoked"); michael@0: michael@0: const u7 = new UberUnload(); michael@0: u7.destroy("whatever"); michael@0: u7.destroy(); michael@0: u7.destroy("uninstall"); michael@0: assert.deepEqual(u7.log, ["dispose"], "dispose hook invoked"); michael@0: }; michael@0: michael@0: exports["test disposables are desposed on unload"] = function(assert) { michael@0: let loader = Loader(module); michael@0: let { Disposable } = loader.require("sdk/core/disposable"); michael@0: michael@0: let arg1 = {} michael@0: let arg2 = 2 michael@0: let disposals = 0 michael@0: michael@0: let Foo = Class({ michael@0: extends: Disposable, michael@0: setup: function setup(a, b) { michael@0: assert.equal(a, arg1, michael@0: "arguments passed to constructur is passed to setup"); michael@0: assert.equal(b, arg2, michael@0: "second argument is also passed to a setup"); michael@0: assert.ok(this instanceof Foo, michael@0: "this is an instance in the scope of the setup method"); michael@0: michael@0: this.fooed = true michael@0: }, michael@0: dispose: function dispose() { michael@0: assert.equal(this.fooed, true, "attribute was set") michael@0: this.fooed = false michael@0: disposals = disposals + 1 michael@0: } michael@0: }) michael@0: michael@0: let foo1 = Foo(arg1, arg2) michael@0: let foo2 = Foo(arg1, arg2) michael@0: michael@0: loader.unload(); michael@0: michael@0: assert.equal(disposals, 2, "both instances were disposed") michael@0: } michael@0: michael@0: exports["test destroyed windows dispose before unload"] = function(assert) { michael@0: let loader = Loader(module); michael@0: let { Disposable } = loader.require("sdk/core/disposable"); michael@0: michael@0: let arg1 = {} michael@0: let arg2 = 2 michael@0: let disposals = 0 michael@0: michael@0: let Foo = Class({ michael@0: extends: Disposable, michael@0: setup: function setup(a, b) { michael@0: assert.equal(a, arg1, michael@0: "arguments passed to constructur is passed to setup"); michael@0: assert.equal(b, arg2, michael@0: "second argument is also passed to a setup"); michael@0: assert.ok(this instanceof Foo, michael@0: "this is an instance in the scope of the setup method"); michael@0: michael@0: this.fooed = true michael@0: }, michael@0: dispose: function dispose() { michael@0: assert.equal(this.fooed, true, "attribute was set") michael@0: this.fooed = false michael@0: disposals = disposals + 1 michael@0: } michael@0: }) michael@0: michael@0: let foo1 = Foo(arg1, arg2) michael@0: let foo2 = Foo(arg1, arg2) michael@0: michael@0: foo1.destroy(); michael@0: assert.equal(disposals, 1, "destroy disposes instance") michael@0: michael@0: loader.unload(); michael@0: michael@0: assert.equal(disposals, 2, "unload disposes alive instances") michael@0: } michael@0: michael@0: exports["test disposables are GC-able"] = function(assert, done) { michael@0: let loader = Loader(module); michael@0: let { Disposable } = loader.require("sdk/core/disposable"); michael@0: let { WeakReference } = loader.require("sdk/core/reference"); michael@0: michael@0: let arg1 = {} michael@0: let arg2 = 2 michael@0: let disposals = 0 michael@0: michael@0: let Foo = Class({ michael@0: extends: Disposable, michael@0: implements: [WeakReference], michael@0: setup: function setup(a, b) { michael@0: assert.equal(a, arg1, michael@0: "arguments passed to constructur is passed to setup"); michael@0: assert.equal(b, arg2, michael@0: "second argument is also passed to a setup"); michael@0: assert.ok(this instanceof Foo, michael@0: "this is an instance in the scope of the setup method"); michael@0: michael@0: this.fooed = true michael@0: }, michael@0: dispose: function dispose() { michael@0: assert.equal(this.fooed, true, "attribute was set") michael@0: this.fooed = false michael@0: disposals = disposals + 1 michael@0: } michael@0: }); michael@0: michael@0: let foo1 = Foo(arg1, arg2) michael@0: let foo2 = Foo(arg1, arg2) michael@0: michael@0: let foo1 = null michael@0: let foo2 = null michael@0: michael@0: Cu.schedulePreciseGC(function() { michael@0: loader.unload(); michael@0: assert.equal(disposals, 0, "GC removed dispose listeners"); michael@0: done(); michael@0: }); michael@0: } michael@0: michael@0: michael@0: exports["test loader unloads do not affect other loaders"] = function(assert) { michael@0: let loader1 = Loader(module); michael@0: let loader2 = Loader(module); michael@0: let { Disposable: Disposable1 } = loader1.require("sdk/core/disposable"); michael@0: let { Disposable: Disposable2 } = loader2.require("sdk/core/disposable"); michael@0: michael@0: let arg1 = {} michael@0: let arg2 = 2 michael@0: let disposals = 0 michael@0: michael@0: let Foo1 = Class({ michael@0: extends: Disposable1, michael@0: dispose: function dispose() { michael@0: disposals = disposals + 1; michael@0: } michael@0: }); michael@0: michael@0: let Foo2 = Class({ michael@0: extends: Disposable2, michael@0: dispose: function dispose() { michael@0: disposals = disposals + 1; michael@0: } michael@0: }); michael@0: michael@0: let foo1 = Foo1(arg1, arg2); michael@0: let foo2 = Foo2(arg1, arg2); michael@0: michael@0: assert.equal(disposals, 0, "no destroy calls"); michael@0: michael@0: loader1.unload(); michael@0: michael@0: assert.equal(disposals, 1, "1 destroy calls"); michael@0: michael@0: loader2.unload(); michael@0: michael@0: assert.equal(disposals, 2, "2 destroy calls"); michael@0: } michael@0: michael@0: exports["test disposables that throw"] = function(assert) { michael@0: let loader = Loader(module); michael@0: let { Disposable } = loader.require("sdk/core/disposable"); michael@0: michael@0: let disposals = 0 michael@0: michael@0: let Foo = Class({ michael@0: extends: Disposable, michael@0: setup: function setup(a, b) { michael@0: throw Error("Boom!") michael@0: }, michael@0: dispose: function dispose() { michael@0: disposals = disposals + 1 michael@0: } michael@0: }) michael@0: michael@0: assert.throws(function() { michael@0: let foo1 = Foo() michael@0: }, /Boom/, "disposable constructors may throw"); michael@0: michael@0: loader.unload(); michael@0: michael@0: assert.equal(disposals, 0, "no disposal if constructor threw"); michael@0: } michael@0: michael@0: exports["test multiple destroy"] = function(assert) { michael@0: let loader = Loader(module); michael@0: let { Disposable } = loader.require("sdk/core/disposable"); michael@0: michael@0: let disposals = 0 michael@0: michael@0: let Foo = Class({ michael@0: extends: Disposable, michael@0: dispose: function dispose() { michael@0: disposals = disposals + 1 michael@0: } michael@0: }) michael@0: michael@0: let foo1 = Foo(); michael@0: let foo2 = Foo(); michael@0: let foo3 = Foo(); michael@0: michael@0: assert.equal(disposals, 0, "no disposals yet"); michael@0: michael@0: foo1.destroy(); michael@0: assert.equal(disposals, 1, "disposed properly"); michael@0: foo1.destroy(); michael@0: assert.equal(disposals, 1, "didn't attempt to dispose twice"); michael@0: michael@0: foo2.destroy(); michael@0: assert.equal(disposals, 2, "other instances still dispose fine"); michael@0: foo2.destroy(); michael@0: assert.equal(disposals, 2, "but not twice"); michael@0: michael@0: loader.unload(); michael@0: michael@0: assert.equal(disposals, 3, "unload only disposed the remaining instance"); michael@0: } michael@0: michael@0: require('test').run(exports);