summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-observers.js
blob: 4f15a87f1212f4fddb7e3e230acc514061ba1d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { Loader } = require("sdk/test/loader");
const { isWeak, WeakReference } = require("sdk/core/reference");
const { subscribe, unsubscribe,
        observe, Observer } = require("sdk/core/observer");
const { Class } = require("sdk/core/heritage");

const { Cc, Ci, Cu } = require("chrome");
const { notifyObservers } = Cc["@mozilla.org/observer-service;1"].
                              getService(Ci.nsIObserverService);
const { defer } = require("sdk/core/promise");

const message = x => ({wrappedJSObject: x});

exports["test subscribe unsubscribe"] = assert => {
  const topic = Date.now().toString(32);
  const Subscriber = Class({
    extends: Observer,
    initialize: function(observe) {
      this.observe = observe;
    }
  });
  observe.define(Subscriber, (x, subject, _, data) =>
                                x.observe(subject.wrappedJSObject.x));

  let xs = [];
  const x = Subscriber((...rest) => xs.push(...rest));

  let ys = [];
  const y = Subscriber((...rest) => ys.push(...rest));

  const publish = (topic, data) =>
    notifyObservers(message(data), topic, null);

  publish({x:0});

  subscribe(x, topic);

  publish(topic, {x:1});

  subscribe(y, topic);

  publish(topic, {x:2});
  publish(topic + "!", {x: 2.5});

  unsubscribe(x, topic);

  publish(topic, {x:3});

  subscribe(y, topic);

  publish(topic, {x:4});

  subscribe(x, topic);

  publish(topic, {x:5});

  unsubscribe(x, topic);
  unsubscribe(y, topic);

  publish(topic, {x:6});

  assert.deepEqual(xs, [1, 2, 5]);
  assert.deepEqual(ys, [2, 3, 4, 5]);
}

exports["test weak observers are GC-ed on unload"] = (assert, end) => {
  const topic = Date.now().toString(32);
  const loader = Loader(module);
  const { Observer, observe,
          subscribe, unsubscribe } = loader.require("sdk/core/observer");
  const { isWeak, WeakReference } = loader.require("sdk/core/reference");

  const MyObserver = Class({
    extends: Observer,
    initialize: function(observe) {
      this.observe = observe;
    }
  });
  observe.define(MyObserver, (x, ...rest) => x.observe(...rest));

  const MyWeakObserver = Class({
    extends: MyObserver,
    implements: [WeakReference]
  });

  let xs = [];
  let ys = [];
  let x = new MyObserver((subject, topic, data) => {
    xs.push(subject.wrappedJSObject, topic, data);
  });
  let y = new MyWeakObserver((subject, topic, data) => {
    ys.push(subject.wrappedJSObject, topic, data);
  });

  subscribe(x, topic);
  subscribe(y, topic);


  notifyObservers(message({ foo: 1 }), topic, null);
  x = null;
  y = null;
  loader.unload();

  Cu.schedulePreciseGC(() => {

    notifyObservers(message({ bar: 2 }), topic, ":)");

    assert.deepEqual(xs, [{ foo: 1 }, topic, null,
                          { bar: 2 }, topic, ":)"],
                     "non week observer is kept");

    assert.deepEqual(ys, [{ foo: 1 }, topic, null],
                     "week observer was GC-ed");

    end();
  });
};

exports["test weak observer unsubscribe"] = function*(assert) {
  const loader = Loader(module);
  const { Observer, observe, subscribe, unsubscribe } = loader.require("sdk/core/observer");
  const { WeakReference } = loader.require("sdk/core/reference");

  let sawNotification = false;
  let firstWait = defer();
  let secondWait = defer();

  const WeakObserver = Class({
    extends: Observer,
    implements: [WeakReference],
    observe: function() {
      sawNotification = true;
      firstWait.resolve();
    }
  });

  const StrongObserver = Class({
    extends: Observer,
    observe: function() {
      secondWait.resolve();
    }
  });

  observe.define(Observer, (x, ...rest) => x.observe(...rest));

  let weakObserver = new WeakObserver;
  let strongObserver = new StrongObserver();
  subscribe(weakObserver, "test-topic");
  subscribe(strongObserver, "test-wait");

  notifyObservers(null, "test-topic", null);
  yield firstWait.promise;

  assert.ok(sawNotification, "Should have seen notification before GC");
  sawNotification = false;

  yield loader.require("sdk/test/memory").gc();

  notifyObservers(null, "test-topic", null);
  notifyObservers(null, "test-wait", null);
  yield secondWait.promise;

  assert.ok(sawNotification, "Should have seen notification after GC");
  sawNotification = false;

  try {
    unsubscribe(weakObserver, "test-topic");
    unsubscribe(strongObserver, "test-wait");
    assert.pass("Should not have seen an exception");
  }
  catch (e) {
    assert.fail("Should not have seen an exception");
  }

  loader.unload();
};

require("sdk/test").run(exports);