test-disposable.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { Loader } = require("sdk/test/loader");
  6. const { Class } = require("sdk/core/heritage");
  7. const { Cc, Ci, Cu } = require("chrome");
  8. const { setTimeout } = require("sdk/timers");
  9. exports["test disposables are desposed on unload"] = function(assert) {
  10. let loader = Loader(module);
  11. let { Disposable } = loader.require("sdk/core/disposable");
  12. let arg1 = {}
  13. let arg2 = 2
  14. let disposals = 0
  15. let Foo = Class({
  16. extends: Disposable,
  17. setup: function setup(a, b) {
  18. assert.equal(a, arg1,
  19. "arguments passed to constructur is passed to setup");
  20. assert.equal(b, arg2,
  21. "second argument is also passed to a setup");
  22. assert.ok(this instanceof Foo,
  23. "this is an instance in the scope of the setup method");
  24. this.fooed = true
  25. },
  26. dispose: function dispose() {
  27. assert.equal(this.fooed, true, "attribute was set")
  28. this.fooed = false
  29. disposals = disposals + 1
  30. }
  31. })
  32. let foo1 = Foo(arg1, arg2)
  33. let foo2 = Foo(arg1, arg2)
  34. loader.unload();
  35. assert.equal(disposals, 2, "both instances were disposed")
  36. }
  37. exports["test destroyed windows dispose before unload"] = function(assert) {
  38. let loader = Loader(module);
  39. let { Disposable } = loader.require("sdk/core/disposable");
  40. let arg1 = {}
  41. let arg2 = 2
  42. let disposals = 0
  43. let Foo = Class({
  44. extends: Disposable,
  45. setup: function setup(a, b) {
  46. assert.equal(a, arg1,
  47. "arguments passed to constructur is passed to setup");
  48. assert.equal(b, arg2,
  49. "second argument is also passed to a setup");
  50. assert.ok(this instanceof Foo,
  51. "this is an instance in the scope of the setup method");
  52. this.fooed = true
  53. },
  54. dispose: function dispose() {
  55. assert.equal(this.fooed, true, "attribute was set")
  56. this.fooed = false
  57. disposals = disposals + 1
  58. }
  59. })
  60. let foo1 = Foo(arg1, arg2)
  61. let foo2 = Foo(arg1, arg2)
  62. foo1.destroy();
  63. assert.equal(disposals, 1, "destroy disposes instance")
  64. loader.unload();
  65. assert.equal(disposals, 2, "unload disposes alive instances")
  66. }
  67. exports["test disposables are GC-able"] = function(assert, done) {
  68. let loader = Loader(module);
  69. let { Disposable } = loader.require("sdk/core/disposable");
  70. let arg1 = {}
  71. let arg2 = 2
  72. let disposals = 0
  73. let Foo = Class({
  74. extends: Disposable,
  75. setup: function setup(a, b) {
  76. assert.equal(a, arg1,
  77. "arguments passed to constructur is passed to setup");
  78. assert.equal(b, arg2,
  79. "second argument is also passed to a setup");
  80. assert.ok(this instanceof Foo,
  81. "this is an instance in the scope of the setup method");
  82. this.fooed = true
  83. },
  84. dispose: function dispose() {
  85. assert.equal(this.fooed, true, "attribute was set")
  86. this.fooed = false
  87. disposals = disposals + 1
  88. }
  89. })
  90. let foo1 = Foo(arg1, arg2)
  91. let foo2 = Foo(arg1, arg2)
  92. let foo1 = null
  93. let foo2 = null
  94. Cu.schedulePreciseGC(function() {
  95. loader.unload();
  96. assert.equal(disposals, 0, "GC removed dispose listeners");
  97. done();
  98. });
  99. }
  100. exports["test loader unloads do not affect other loaders"] = function(assert) {
  101. let loader1 = Loader(module);
  102. let loader2 = Loader(module);
  103. let { Disposable: Disposable1 } = loader1.require("sdk/core/disposable");
  104. let { Disposable: Disposable2 } = loader2.require("sdk/core/disposable");
  105. let arg1 = {}
  106. let arg2 = 2
  107. let disposals = 0
  108. let Foo1 = Class({
  109. extends: Disposable1,
  110. dispose: function dispose() {
  111. disposals = disposals + 1;
  112. }
  113. });
  114. let Foo2 = Class({
  115. extends: Disposable2,
  116. dispose: function dispose() {
  117. disposals = disposals + 1;
  118. }
  119. });
  120. let foo1 = Foo1(arg1, arg2);
  121. let foo2 = Foo2(arg1, arg2);
  122. assert.equal(disposals, 0, "no destroy calls");
  123. loader1.unload();
  124. assert.equal(disposals, 1, "1 destroy calls");
  125. loader2.unload();
  126. assert.equal(disposals, 2, "2 destroy calls");
  127. }
  128. exports["test disposables that throw"] = function(assert) {
  129. let loader = Loader(module);
  130. let { Disposable } = loader.require("sdk/core/disposable");
  131. let disposals = 0
  132. let Foo = Class({
  133. extends: Disposable,
  134. setup: function setup(a, b) {
  135. throw Error("Boom!")
  136. },
  137. dispose: function dispose() {
  138. disposals = disposals + 1
  139. }
  140. })
  141. assert.throws(function() {
  142. let foo1 = Foo()
  143. }, /Boom/, "disposable constructors may throw");
  144. loader.unload();
  145. assert.equal(disposals, 0, "no disposal if constructor threw");
  146. }
  147. exports["test multiple destroy"] = function(assert) {
  148. let loader = Loader(module);
  149. let { Disposable } = loader.require("sdk/core/disposable");
  150. let disposals = 0
  151. let Foo = Class({
  152. extends: Disposable,
  153. dispose: function dispose() {
  154. disposals = disposals + 1
  155. }
  156. })
  157. let foo1 = Foo();
  158. let foo2 = Foo();
  159. let foo3 = Foo();
  160. assert.equal(disposals, 0, "no disposals yet");
  161. foo1.destroy();
  162. assert.equal(disposals, 1, "disposed properly");
  163. foo1.destroy();
  164. assert.equal(disposals, 1, "didn't attempt to dispose twice");
  165. foo2.destroy();
  166. assert.equal(disposals, 2, "other instances still dispose fine");
  167. foo2.destroy();
  168. assert.equal(disposals, 2, "but not twice");
  169. loader.unload();
  170. assert.equal(disposals, 3, "unload only disposed the remaining instance");
  171. }
  172. require('test').run(exports);