Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 { isWeak, WeakReference } = require("sdk/core/reference"); |
michael@0 | 8 | const { subscribe, unsubscribe, |
michael@0 | 9 | observe, Observer } = require("sdk/core/observer"); |
michael@0 | 10 | const { Class } = require("sdk/core/heritage"); |
michael@0 | 11 | |
michael@0 | 12 | const { Cc, Ci, Cu } = require("chrome"); |
michael@0 | 13 | const { notifyObservers } = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 14 | getService(Ci.nsIObserverService); |
michael@0 | 15 | |
michael@0 | 16 | const message = x => ({wrappedJSObject: x}); |
michael@0 | 17 | |
michael@0 | 18 | exports["test subscribe unsubscribe"] = assert => { |
michael@0 | 19 | const topic = Date.now().toString(32); |
michael@0 | 20 | const Subscriber = Class({ |
michael@0 | 21 | extends: Observer, |
michael@0 | 22 | initialize: function(observe) { |
michael@0 | 23 | this.observe = observe; |
michael@0 | 24 | } |
michael@0 | 25 | }); |
michael@0 | 26 | observe.define(Subscriber, (x, subject, _, data) => |
michael@0 | 27 | x.observe(subject.wrappedJSObject.x)); |
michael@0 | 28 | |
michael@0 | 29 | let xs = []; |
michael@0 | 30 | const x = Subscriber((...rest) => xs.push(...rest)); |
michael@0 | 31 | |
michael@0 | 32 | let ys = []; |
michael@0 | 33 | const y = Subscriber((...rest) => ys.push(...rest)); |
michael@0 | 34 | |
michael@0 | 35 | const publish = (topic, data) => |
michael@0 | 36 | notifyObservers(message(data), topic, null); |
michael@0 | 37 | |
michael@0 | 38 | publish({x:0}); |
michael@0 | 39 | |
michael@0 | 40 | subscribe(x, topic); |
michael@0 | 41 | |
michael@0 | 42 | publish(topic, {x:1}); |
michael@0 | 43 | |
michael@0 | 44 | subscribe(y, topic); |
michael@0 | 45 | |
michael@0 | 46 | publish(topic, {x:2}); |
michael@0 | 47 | publish(topic + "!", {x: 2.5}); |
michael@0 | 48 | |
michael@0 | 49 | unsubscribe(x, topic); |
michael@0 | 50 | |
michael@0 | 51 | publish(topic, {x:3}); |
michael@0 | 52 | |
michael@0 | 53 | subscribe(y, topic); |
michael@0 | 54 | |
michael@0 | 55 | publish(topic, {x:4}); |
michael@0 | 56 | |
michael@0 | 57 | subscribe(x, topic); |
michael@0 | 58 | |
michael@0 | 59 | publish(topic, {x:5}); |
michael@0 | 60 | |
michael@0 | 61 | unsubscribe(x, topic); |
michael@0 | 62 | unsubscribe(y, topic); |
michael@0 | 63 | |
michael@0 | 64 | publish(topic, {x:6}); |
michael@0 | 65 | |
michael@0 | 66 | assert.deepEqual(xs, [1, 2, 5]); |
michael@0 | 67 | assert.deepEqual(ys, [2, 3, 4, 5]); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | exports["test weak observers are GC-ed on unload"] = (assert, end) => { |
michael@0 | 71 | const topic = Date.now().toString(32); |
michael@0 | 72 | const loader = Loader(module); |
michael@0 | 73 | const { Observer, observe, |
michael@0 | 74 | subscribe, unsubscribe } = loader.require("sdk/core/observer"); |
michael@0 | 75 | const { isWeak, WeakReference } = loader.require("sdk/core/reference"); |
michael@0 | 76 | |
michael@0 | 77 | const MyObserver = Class({ |
michael@0 | 78 | extends: Observer, |
michael@0 | 79 | initialize: function(observe) { |
michael@0 | 80 | this.observe = observe; |
michael@0 | 81 | } |
michael@0 | 82 | }); |
michael@0 | 83 | observe.define(MyObserver, (x, ...rest) => x.observe(...rest)); |
michael@0 | 84 | |
michael@0 | 85 | const MyWeakObserver = Class({ |
michael@0 | 86 | extends: MyObserver, |
michael@0 | 87 | implements: [WeakReference] |
michael@0 | 88 | }); |
michael@0 | 89 | |
michael@0 | 90 | let xs = []; |
michael@0 | 91 | let ys = []; |
michael@0 | 92 | let x = new MyObserver((subject, topic, data) => { |
michael@0 | 93 | xs.push(subject.wrappedJSObject, topic, data); |
michael@0 | 94 | }); |
michael@0 | 95 | let y = new MyWeakObserver((subject, topic, data) => { |
michael@0 | 96 | ys.push(subject.wrappedJSObject, topic, data); |
michael@0 | 97 | }); |
michael@0 | 98 | |
michael@0 | 99 | subscribe(x, topic); |
michael@0 | 100 | subscribe(y, topic); |
michael@0 | 101 | |
michael@0 | 102 | |
michael@0 | 103 | notifyObservers(message({ foo: 1 }), topic, null); |
michael@0 | 104 | x = null; |
michael@0 | 105 | y = null; |
michael@0 | 106 | loader.unload(); |
michael@0 | 107 | |
michael@0 | 108 | Cu.schedulePreciseGC(() => { |
michael@0 | 109 | |
michael@0 | 110 | notifyObservers(message({ bar: 2 }), topic, ":)"); |
michael@0 | 111 | |
michael@0 | 112 | assert.deepEqual(xs, [{ foo: 1 }, topic, null, |
michael@0 | 113 | { bar: 2 }, topic, ":)"], |
michael@0 | 114 | "non week observer is kept"); |
michael@0 | 115 | |
michael@0 | 116 | assert.deepEqual(ys, [{ foo: 1 }, topic, null], |
michael@0 | 117 | "week observer was GC-ed"); |
michael@0 | 118 | |
michael@0 | 119 | end(); |
michael@0 | 120 | }); |
michael@0 | 121 | }; |
michael@0 | 122 | |
michael@0 | 123 | require("sdk/test").run(exports); |