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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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";
     6 const { Loader } = require("sdk/test/loader");
     7 const { isWeak, WeakReference } = require("sdk/core/reference");
     8 const { subscribe, unsubscribe,
     9         observe, Observer } = require("sdk/core/observer");
    10 const { Class } = require("sdk/core/heritage");
    12 const { Cc, Ci, Cu } = require("chrome");
    13 const { notifyObservers } = Cc["@mozilla.org/observer-service;1"].
    14                               getService(Ci.nsIObserverService);
    16 const message = x => ({wrappedJSObject: x});
    18 exports["test subscribe unsubscribe"] = assert => {
    19   const topic = Date.now().toString(32);
    20   const Subscriber = Class({
    21     extends: Observer,
    22     initialize: function(observe) {
    23       this.observe = observe;
    24     }
    25   });
    26   observe.define(Subscriber, (x, subject, _, data) =>
    27                                 x.observe(subject.wrappedJSObject.x));
    29   let xs = [];
    30   const x = Subscriber((...rest) => xs.push(...rest));
    32   let ys = [];
    33   const y = Subscriber((...rest) => ys.push(...rest));
    35   const publish = (topic, data) =>
    36     notifyObservers(message(data), topic, null);
    38   publish({x:0});
    40   subscribe(x, topic);
    42   publish(topic, {x:1});
    44   subscribe(y, topic);
    46   publish(topic, {x:2});
    47   publish(topic + "!", {x: 2.5});
    49   unsubscribe(x, topic);
    51   publish(topic, {x:3});
    53   subscribe(y, topic);
    55   publish(topic, {x:4});
    57   subscribe(x, topic);
    59   publish(topic, {x:5});
    61   unsubscribe(x, topic);
    62   unsubscribe(y, topic);
    64   publish(topic, {x:6});
    66   assert.deepEqual(xs, [1, 2, 5]);
    67   assert.deepEqual(ys, [2, 3, 4, 5]);
    68 }
    70 exports["test weak observers are GC-ed on unload"] = (assert, end) => {
    71   const topic = Date.now().toString(32);
    72   const loader = Loader(module);
    73   const { Observer, observe,
    74           subscribe, unsubscribe } = loader.require("sdk/core/observer");
    75   const { isWeak, WeakReference } = loader.require("sdk/core/reference");
    77   const MyObserver = Class({
    78     extends: Observer,
    79     initialize: function(observe) {
    80       this.observe = observe;
    81     }
    82   });
    83   observe.define(MyObserver, (x, ...rest) => x.observe(...rest));
    85   const MyWeakObserver = Class({
    86     extends: MyObserver,
    87     implements: [WeakReference]
    88   });
    90   let xs = [];
    91   let ys = [];
    92   let x = new MyObserver((subject, topic, data) => {
    93     xs.push(subject.wrappedJSObject, topic, data);
    94   });
    95   let y = new MyWeakObserver((subject, topic, data) => {
    96     ys.push(subject.wrappedJSObject, topic, data);
    97   });
    99   subscribe(x, topic);
   100   subscribe(y, topic);
   103   notifyObservers(message({ foo: 1 }), topic, null);
   104   x = null;
   105   y = null;
   106   loader.unload();
   108   Cu.schedulePreciseGC(() => {
   110     notifyObservers(message({ bar: 2 }), topic, ":)");
   112     assert.deepEqual(xs, [{ foo: 1 }, topic, null,
   113                           { bar: 2 }, topic, ":)"],
   114                      "non week observer is kept");
   116     assert.deepEqual(ys, [{ foo: 1 }, topic, null],
   117                      "week observer was GC-ed");
   119     end();
   120   });
   121 };
   123 require("sdk/test").run(exports);

mercurial