addon-sdk/source/test/test-disposable.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 const { Loader } = require("sdk/test/loader");
michael@0 7 const { Class } = require("sdk/core/heritage");
michael@0 8 const { Disposable } = require("sdk/core/disposable");
michael@0 9 const { Cc, Ci, Cu } = require("chrome");
michael@0 10 const { setTimeout } = require("sdk/timers");
michael@0 11
michael@0 12 exports["test destroy reasons"] = assert => {
michael@0 13 const Foo = Class({
michael@0 14 extends: Disposable,
michael@0 15 dispose: function() {
michael@0 16 disposals = disposals + 1;
michael@0 17 }
michael@0 18 });
michael@0 19
michael@0 20 let disposals = 0;
michael@0 21 const f1 = new Foo();
michael@0 22
michael@0 23 f1.destroy();
michael@0 24 assert.equal(disposals, 1, "disposed on destroy");
michael@0 25 f1.destroy();
michael@0 26 assert.equal(disposals, 1, "second destroy is ignored");
michael@0 27
michael@0 28 disposals = 0;
michael@0 29 const f2 = new Foo();
michael@0 30
michael@0 31 f2.destroy("uninstall");
michael@0 32 assert.equal(disposals, 1, "uninstall invokes disposal");
michael@0 33 f2.destroy("uninstall")
michael@0 34 f2.destroy();
michael@0 35 assert.equal(disposals, 1, "disposal happens just once");
michael@0 36
michael@0 37 disposals = 0;
michael@0 38 const f3 = new Foo();
michael@0 39
michael@0 40 f3.destroy("shutdown");
michael@0 41 assert.equal(disposals, 0, "shutdown doesn't invoke disposal");
michael@0 42 f3.destroy();
michael@0 43 assert.equal(disposals, 0, "shutdown already skipped disposal");
michael@0 44
michael@0 45 disposals = 0;
michael@0 46 const f4 = new Foo();
michael@0 47
michael@0 48 f4.destroy("disable");
michael@0 49 assert.equal(disposals, 1, "disable invokes disposal");
michael@0 50 f4.destroy("disable")
michael@0 51 f4.destroy();
michael@0 52 assert.equal(disposals, 1, "destroy happens just once");
michael@0 53
michael@0 54 disposals = 0;
michael@0 55 const f5 = new Foo();
michael@0 56
michael@0 57 f5.destroy("disable");
michael@0 58 assert.equal(disposals, 1, "disable invokes disposal");
michael@0 59 f5.destroy("disable")
michael@0 60 f5.destroy();
michael@0 61 assert.equal(disposals, 1, "destroy happens just once");
michael@0 62
michael@0 63 disposals = 0;
michael@0 64 const f6 = new Foo();
michael@0 65
michael@0 66 f6.destroy("upgrade");
michael@0 67 assert.equal(disposals, 1, "upgrade invokes disposal");
michael@0 68 f6.destroy("upgrade")
michael@0 69 f6.destroy();
michael@0 70 assert.equal(disposals, 1, "destroy happens just once");
michael@0 71
michael@0 72 disposals = 0;
michael@0 73 const f7 = new Foo();
michael@0 74
michael@0 75 f7.destroy("downgrade");
michael@0 76 assert.equal(disposals, 1, "downgrade invokes disposal");
michael@0 77 f7.destroy("downgrade")
michael@0 78 f7.destroy();
michael@0 79 assert.equal(disposals, 1, "destroy happens just once");
michael@0 80
michael@0 81
michael@0 82 disposals = 0;
michael@0 83 const f8 = new Foo();
michael@0 84
michael@0 85 f8.destroy("whatever");
michael@0 86 assert.equal(disposals, 1, "unrecognized reason invokes disposal");
michael@0 87 f8.destroy("meh")
michael@0 88 f8.destroy();
michael@0 89 assert.equal(disposals, 1, "destroy happens just once");
michael@0 90 };
michael@0 91
michael@0 92 exports["test different unload hooks"] = assert => {
michael@0 93 const { uninstall, shutdown, disable, upgrade,
michael@0 94 downgrade, dispose } = require("sdk/core/disposable");
michael@0 95 const UberUnload = Class({
michael@0 96 extends: Disposable,
michael@0 97 setup: function() {
michael@0 98 this.log = [];
michael@0 99 }
michael@0 100 });
michael@0 101
michael@0 102 uninstall.define(UberUnload, x => x.log.push("uninstall"));
michael@0 103 shutdown.define(UberUnload, x => x.log.push("shutdown"));
michael@0 104 disable.define(UberUnload, x => x.log.push("disable"));
michael@0 105 upgrade.define(UberUnload, x => x.log.push("upgrade"));
michael@0 106 downgrade.define(UberUnload, x => x.log.push("downgrade"));
michael@0 107 dispose.define(UberUnload, x => x.log.push("dispose"));
michael@0 108
michael@0 109 const u1 = new UberUnload();
michael@0 110 u1.destroy("uninstall");
michael@0 111 u1.destroy();
michael@0 112 u1.destroy("shutdown");
michael@0 113 assert.deepEqual(u1.log, ["uninstall"], "uninstall hook invoked");
michael@0 114
michael@0 115 const u2 = new UberUnload();
michael@0 116 u2.destroy("shutdown");
michael@0 117 u2.destroy();
michael@0 118 u2.destroy("uninstall");
michael@0 119 assert.deepEqual(u2.log, ["shutdown"], "shutdown hook invoked");
michael@0 120
michael@0 121 const u3 = new UberUnload();
michael@0 122 u3.destroy("disable");
michael@0 123 u3.destroy();
michael@0 124 u3.destroy("uninstall");
michael@0 125 assert.deepEqual(u3.log, ["disable"], "disable hook invoked");
michael@0 126
michael@0 127 const u4 = new UberUnload();
michael@0 128 u4.destroy("upgrade");
michael@0 129 u4.destroy();
michael@0 130 u4.destroy("uninstall");
michael@0 131 assert.deepEqual(u4.log, ["upgrade"], "upgrade hook invoked");
michael@0 132
michael@0 133 const u5 = new UberUnload();
michael@0 134 u5.destroy("downgrade");
michael@0 135 u5.destroy();
michael@0 136 u5.destroy("uninstall");
michael@0 137 assert.deepEqual(u5.log, ["downgrade"], "downgrade hook invoked");
michael@0 138
michael@0 139 const u6 = new UberUnload();
michael@0 140 u6.destroy();
michael@0 141 u6.destroy();
michael@0 142 u6.destroy("uninstall");
michael@0 143 assert.deepEqual(u6.log, ["dispose"], "dispose hook invoked");
michael@0 144
michael@0 145 const u7 = new UberUnload();
michael@0 146 u7.destroy("whatever");
michael@0 147 u7.destroy();
michael@0 148 u7.destroy("uninstall");
michael@0 149 assert.deepEqual(u7.log, ["dispose"], "dispose hook invoked");
michael@0 150 };
michael@0 151
michael@0 152 exports["test disposables are desposed on unload"] = function(assert) {
michael@0 153 let loader = Loader(module);
michael@0 154 let { Disposable } = loader.require("sdk/core/disposable");
michael@0 155
michael@0 156 let arg1 = {}
michael@0 157 let arg2 = 2
michael@0 158 let disposals = 0
michael@0 159
michael@0 160 let Foo = Class({
michael@0 161 extends: Disposable,
michael@0 162 setup: function setup(a, b) {
michael@0 163 assert.equal(a, arg1,
michael@0 164 "arguments passed to constructur is passed to setup");
michael@0 165 assert.equal(b, arg2,
michael@0 166 "second argument is also passed to a setup");
michael@0 167 assert.ok(this instanceof Foo,
michael@0 168 "this is an instance in the scope of the setup method");
michael@0 169
michael@0 170 this.fooed = true
michael@0 171 },
michael@0 172 dispose: function dispose() {
michael@0 173 assert.equal(this.fooed, true, "attribute was set")
michael@0 174 this.fooed = false
michael@0 175 disposals = disposals + 1
michael@0 176 }
michael@0 177 })
michael@0 178
michael@0 179 let foo1 = Foo(arg1, arg2)
michael@0 180 let foo2 = Foo(arg1, arg2)
michael@0 181
michael@0 182 loader.unload();
michael@0 183
michael@0 184 assert.equal(disposals, 2, "both instances were disposed")
michael@0 185 }
michael@0 186
michael@0 187 exports["test destroyed windows dispose before unload"] = function(assert) {
michael@0 188 let loader = Loader(module);
michael@0 189 let { Disposable } = loader.require("sdk/core/disposable");
michael@0 190
michael@0 191 let arg1 = {}
michael@0 192 let arg2 = 2
michael@0 193 let disposals = 0
michael@0 194
michael@0 195 let Foo = Class({
michael@0 196 extends: Disposable,
michael@0 197 setup: function setup(a, b) {
michael@0 198 assert.equal(a, arg1,
michael@0 199 "arguments passed to constructur is passed to setup");
michael@0 200 assert.equal(b, arg2,
michael@0 201 "second argument is also passed to a setup");
michael@0 202 assert.ok(this instanceof Foo,
michael@0 203 "this is an instance in the scope of the setup method");
michael@0 204
michael@0 205 this.fooed = true
michael@0 206 },
michael@0 207 dispose: function dispose() {
michael@0 208 assert.equal(this.fooed, true, "attribute was set")
michael@0 209 this.fooed = false
michael@0 210 disposals = disposals + 1
michael@0 211 }
michael@0 212 })
michael@0 213
michael@0 214 let foo1 = Foo(arg1, arg2)
michael@0 215 let foo2 = Foo(arg1, arg2)
michael@0 216
michael@0 217 foo1.destroy();
michael@0 218 assert.equal(disposals, 1, "destroy disposes instance")
michael@0 219
michael@0 220 loader.unload();
michael@0 221
michael@0 222 assert.equal(disposals, 2, "unload disposes alive instances")
michael@0 223 }
michael@0 224
michael@0 225 exports["test disposables are GC-able"] = function(assert, done) {
michael@0 226 let loader = Loader(module);
michael@0 227 let { Disposable } = loader.require("sdk/core/disposable");
michael@0 228 let { WeakReference } = loader.require("sdk/core/reference");
michael@0 229
michael@0 230 let arg1 = {}
michael@0 231 let arg2 = 2
michael@0 232 let disposals = 0
michael@0 233
michael@0 234 let Foo = Class({
michael@0 235 extends: Disposable,
michael@0 236 implements: [WeakReference],
michael@0 237 setup: function setup(a, b) {
michael@0 238 assert.equal(a, arg1,
michael@0 239 "arguments passed to constructur is passed to setup");
michael@0 240 assert.equal(b, arg2,
michael@0 241 "second argument is also passed to a setup");
michael@0 242 assert.ok(this instanceof Foo,
michael@0 243 "this is an instance in the scope of the setup method");
michael@0 244
michael@0 245 this.fooed = true
michael@0 246 },
michael@0 247 dispose: function dispose() {
michael@0 248 assert.equal(this.fooed, true, "attribute was set")
michael@0 249 this.fooed = false
michael@0 250 disposals = disposals + 1
michael@0 251 }
michael@0 252 });
michael@0 253
michael@0 254 let foo1 = Foo(arg1, arg2)
michael@0 255 let foo2 = Foo(arg1, arg2)
michael@0 256
michael@0 257 let foo1 = null
michael@0 258 let foo2 = null
michael@0 259
michael@0 260 Cu.schedulePreciseGC(function() {
michael@0 261 loader.unload();
michael@0 262 assert.equal(disposals, 0, "GC removed dispose listeners");
michael@0 263 done();
michael@0 264 });
michael@0 265 }
michael@0 266
michael@0 267
michael@0 268 exports["test loader unloads do not affect other loaders"] = function(assert) {
michael@0 269 let loader1 = Loader(module);
michael@0 270 let loader2 = Loader(module);
michael@0 271 let { Disposable: Disposable1 } = loader1.require("sdk/core/disposable");
michael@0 272 let { Disposable: Disposable2 } = loader2.require("sdk/core/disposable");
michael@0 273
michael@0 274 let arg1 = {}
michael@0 275 let arg2 = 2
michael@0 276 let disposals = 0
michael@0 277
michael@0 278 let Foo1 = Class({
michael@0 279 extends: Disposable1,
michael@0 280 dispose: function dispose() {
michael@0 281 disposals = disposals + 1;
michael@0 282 }
michael@0 283 });
michael@0 284
michael@0 285 let Foo2 = Class({
michael@0 286 extends: Disposable2,
michael@0 287 dispose: function dispose() {
michael@0 288 disposals = disposals + 1;
michael@0 289 }
michael@0 290 });
michael@0 291
michael@0 292 let foo1 = Foo1(arg1, arg2);
michael@0 293 let foo2 = Foo2(arg1, arg2);
michael@0 294
michael@0 295 assert.equal(disposals, 0, "no destroy calls");
michael@0 296
michael@0 297 loader1.unload();
michael@0 298
michael@0 299 assert.equal(disposals, 1, "1 destroy calls");
michael@0 300
michael@0 301 loader2.unload();
michael@0 302
michael@0 303 assert.equal(disposals, 2, "2 destroy calls");
michael@0 304 }
michael@0 305
michael@0 306 exports["test disposables that throw"] = function(assert) {
michael@0 307 let loader = Loader(module);
michael@0 308 let { Disposable } = loader.require("sdk/core/disposable");
michael@0 309
michael@0 310 let disposals = 0
michael@0 311
michael@0 312 let Foo = Class({
michael@0 313 extends: Disposable,
michael@0 314 setup: function setup(a, b) {
michael@0 315 throw Error("Boom!")
michael@0 316 },
michael@0 317 dispose: function dispose() {
michael@0 318 disposals = disposals + 1
michael@0 319 }
michael@0 320 })
michael@0 321
michael@0 322 assert.throws(function() {
michael@0 323 let foo1 = Foo()
michael@0 324 }, /Boom/, "disposable constructors may throw");
michael@0 325
michael@0 326 loader.unload();
michael@0 327
michael@0 328 assert.equal(disposals, 0, "no disposal if constructor threw");
michael@0 329 }
michael@0 330
michael@0 331 exports["test multiple destroy"] = function(assert) {
michael@0 332 let loader = Loader(module);
michael@0 333 let { Disposable } = loader.require("sdk/core/disposable");
michael@0 334
michael@0 335 let disposals = 0
michael@0 336
michael@0 337 let Foo = Class({
michael@0 338 extends: Disposable,
michael@0 339 dispose: function dispose() {
michael@0 340 disposals = disposals + 1
michael@0 341 }
michael@0 342 })
michael@0 343
michael@0 344 let foo1 = Foo();
michael@0 345 let foo2 = Foo();
michael@0 346 let foo3 = Foo();
michael@0 347
michael@0 348 assert.equal(disposals, 0, "no disposals yet");
michael@0 349
michael@0 350 foo1.destroy();
michael@0 351 assert.equal(disposals, 1, "disposed properly");
michael@0 352 foo1.destroy();
michael@0 353 assert.equal(disposals, 1, "didn't attempt to dispose twice");
michael@0 354
michael@0 355 foo2.destroy();
michael@0 356 assert.equal(disposals, 2, "other instances still dispose fine");
michael@0 357 foo2.destroy();
michael@0 358 assert.equal(disposals, 2, "but not twice");
michael@0 359
michael@0 360 loader.unload();
michael@0 361
michael@0 362 assert.equal(disposals, 3, "unload only disposed the remaining instance");
michael@0 363 }
michael@0 364
michael@0 365 require('test').run(exports);

mercurial