1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-observers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +"use strict"; 1.8 + 1.9 +const { Loader } = require("sdk/test/loader"); 1.10 +const { isWeak, WeakReference } = require("sdk/core/reference"); 1.11 +const { subscribe, unsubscribe, 1.12 + observe, Observer } = require("sdk/core/observer"); 1.13 +const { Class } = require("sdk/core/heritage"); 1.14 + 1.15 +const { Cc, Ci, Cu } = require("chrome"); 1.16 +const { notifyObservers } = Cc["@mozilla.org/observer-service;1"]. 1.17 + getService(Ci.nsIObserverService); 1.18 + 1.19 +const message = x => ({wrappedJSObject: x}); 1.20 + 1.21 +exports["test subscribe unsubscribe"] = assert => { 1.22 + const topic = Date.now().toString(32); 1.23 + const Subscriber = Class({ 1.24 + extends: Observer, 1.25 + initialize: function(observe) { 1.26 + this.observe = observe; 1.27 + } 1.28 + }); 1.29 + observe.define(Subscriber, (x, subject, _, data) => 1.30 + x.observe(subject.wrappedJSObject.x)); 1.31 + 1.32 + let xs = []; 1.33 + const x = Subscriber((...rest) => xs.push(...rest)); 1.34 + 1.35 + let ys = []; 1.36 + const y = Subscriber((...rest) => ys.push(...rest)); 1.37 + 1.38 + const publish = (topic, data) => 1.39 + notifyObservers(message(data), topic, null); 1.40 + 1.41 + publish({x:0}); 1.42 + 1.43 + subscribe(x, topic); 1.44 + 1.45 + publish(topic, {x:1}); 1.46 + 1.47 + subscribe(y, topic); 1.48 + 1.49 + publish(topic, {x:2}); 1.50 + publish(topic + "!", {x: 2.5}); 1.51 + 1.52 + unsubscribe(x, topic); 1.53 + 1.54 + publish(topic, {x:3}); 1.55 + 1.56 + subscribe(y, topic); 1.57 + 1.58 + publish(topic, {x:4}); 1.59 + 1.60 + subscribe(x, topic); 1.61 + 1.62 + publish(topic, {x:5}); 1.63 + 1.64 + unsubscribe(x, topic); 1.65 + unsubscribe(y, topic); 1.66 + 1.67 + publish(topic, {x:6}); 1.68 + 1.69 + assert.deepEqual(xs, [1, 2, 5]); 1.70 + assert.deepEqual(ys, [2, 3, 4, 5]); 1.71 +} 1.72 + 1.73 +exports["test weak observers are GC-ed on unload"] = (assert, end) => { 1.74 + const topic = Date.now().toString(32); 1.75 + const loader = Loader(module); 1.76 + const { Observer, observe, 1.77 + subscribe, unsubscribe } = loader.require("sdk/core/observer"); 1.78 + const { isWeak, WeakReference } = loader.require("sdk/core/reference"); 1.79 + 1.80 + const MyObserver = Class({ 1.81 + extends: Observer, 1.82 + initialize: function(observe) { 1.83 + this.observe = observe; 1.84 + } 1.85 + }); 1.86 + observe.define(MyObserver, (x, ...rest) => x.observe(...rest)); 1.87 + 1.88 + const MyWeakObserver = Class({ 1.89 + extends: MyObserver, 1.90 + implements: [WeakReference] 1.91 + }); 1.92 + 1.93 + let xs = []; 1.94 + let ys = []; 1.95 + let x = new MyObserver((subject, topic, data) => { 1.96 + xs.push(subject.wrappedJSObject, topic, data); 1.97 + }); 1.98 + let y = new MyWeakObserver((subject, topic, data) => { 1.99 + ys.push(subject.wrappedJSObject, topic, data); 1.100 + }); 1.101 + 1.102 + subscribe(x, topic); 1.103 + subscribe(y, topic); 1.104 + 1.105 + 1.106 + notifyObservers(message({ foo: 1 }), topic, null); 1.107 + x = null; 1.108 + y = null; 1.109 + loader.unload(); 1.110 + 1.111 + Cu.schedulePreciseGC(() => { 1.112 + 1.113 + notifyObservers(message({ bar: 2 }), topic, ":)"); 1.114 + 1.115 + assert.deepEqual(xs, [{ foo: 1 }, topic, null, 1.116 + { bar: 2 }, topic, ":)"], 1.117 + "non week observer is kept"); 1.118 + 1.119 + assert.deepEqual(ys, [{ foo: 1 }, topic, null], 1.120 + "week observer was GC-ed"); 1.121 + 1.122 + end(); 1.123 + }); 1.124 +}; 1.125 + 1.126 +require("sdk/test").run(exports); 1.127 \ No newline at end of file