aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.7/packages/api-utils/tests/test-xpcom.js
blob: eda3565629caa841a513e0e0f37d8559812fba23 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* 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/. */

const xpcom = require("api-utils/xpcom");
const { Cc, Ci, Cm, Cr } = require("chrome");
const { isCIDRegistered } = Cm.QueryInterface(Ci.nsIComponentRegistrar);
const { Loader } = require("./helpers");

exports['test Unknown implements nsISupports'] = function(assert) {
  let actual = xpcom.Unknown.new();
  assert.equal(actual.QueryInterface(Ci.nsISupports),
               actual,
               'component implements nsISupports');
};

exports['test implement xpcom interfaces'] = function(assert) {
  let component = xpcom.Unknown.extend({
    interfaces: [ 'nsIWeakReference' ],
    QueryReferent: function() {}
  })

  assert.equal(component.QueryInterface(Ci.nsISupports),
               component,
               'component implements nsISupports');
  assert.equal(component.QueryInterface(Ci.nsIWeakReference),
               component,
               'component implements specified interface');

  assert.throws(function() {
    component.QueryInterface(Ci.nsIObserver);
  }, "component does not implements interface");

  let actual = component.extend({
    interfaces: [ 'nsIObserver', 'nsIRequestObserver' ],
    observe: function() {},
    onStartRequest: function() {},
    onStopRequest: function() {}
  });

  assert.equal(actual.QueryInterface(Ci.nsISupports),
               actual,
               'derived component implements nsISupports');
  assert.equal(actual.QueryInterface(Ci.nsIWeakReference),
               actual,
               'derived component implements supers interface');
  assert.equal(actual.QueryInterface(Ci.nsIObserver),
               actual.QueryInterface(Ci.nsIRequestObserver),
               'derived component implements specified interfaces');
};

exports['test implement factory without contract'] = function(assert) {
  let actual = xpcom.Factory.new({
    component: xpcom.Unknown.extend({
      get wrappedJSObject() this,
    })
  });

  assert.ok(isCIDRegistered(actual.id), 'factory is regiseterd');
  xpcom.unregister(actual);
  assert.ok(!isCIDRegistered(actual.id), 'factory is unregistered');
};

exports['test implement xpcom factory'] = function(assert) {
  let Component = xpcom.Unknown.extend({
    interfaces: [ 'nsIObserver' ],
    get wrappedJSObject() this,
    observe: function() {}
  });

  let factory = xpcom.Factory.new({
    register: false,
    contract: '@jetpack/test/factory;1',
    component: Component
  });

  assert.ok(!isCIDRegistered(factory.id), 'factory is not registered');
  xpcom.register(factory);
  assert.ok(isCIDRegistered(factory.id), 'factory is registered');

  let actual = Cc[factory.contract].createInstance(Ci.nsIObserver);

  assert.ok(Component.isPrototypeOf(actual.wrappedJSObject),
            "createInstance returnes wrapped factory instances");

  assert.notEqual(Cc[factory.contract].createInstance(Ci.nsIObserver),
                  Cc[factory.contract].createInstance(Ci.nsIObserver),
                  "createInstance returns new instance each time");
};

exports['test implement xpcom service'] = function(assert) {
  let actual = xpcom.Service.new({
    contract: '@jetpack/test/service;1',
    register: false,
    component: xpcom.Unknown.extend({
      get wrappedJSObject() this,
      interfaces: [ 'nsIObserver'],
      observe: function() {},
      name: 'my-service'
    })
  });

  assert.ok(!isCIDRegistered(actual.id), 'component is not registered');
  xpcom.register(actual);
  assert.ok(isCIDRegistered(actual.id), 'service is regiseterd');
  assert.ok(Cc[actual.contract].getService(Ci.nsIObserver).observe,
            'service can be accessed via get service');
  assert.equal(Cc[actual.contract].getService(Ci.nsIObserver).wrappedJSObject,
               actual.component,
               'wrappedJSObject is an actual component');
  xpcom.unregister(actual);
  assert.ok(!isCIDRegistered(actual.id), 'service is unregistered');
};


function testRegister(assert, text) {

  const service = xpcom.Service.new({
    description: 'test about:boop page',
    contract: '@mozilla.org/network/protocol/about;1?what=boop',
    register: false,
    component: xpcom.Unknown.extend({
      get wrappedJSObject() this,
      interfaces: [ 'nsIAboutModule' ],
      newChannel : function(aURI) {
        var ios = Cc["@mozilla.org/network/io-service;1"].
                  getService(Ci.nsIIOService);

        var channel = ios.newChannel(
          "data:text/plain," + text,
          null,
          null
        );

        channel.originalURI = aURI;
        return channel;
      },
      getURIFlags: function(aURI) {
        return Ci.nsIAboutModule.ALLOW_SCRIPT;
      }
    })
  });

  xpcom.register(service);

  assert.equal(isCIDRegistered(service.id), true);

  var aboutFactory = xpcom.factoryByContract(service.contract);
  var about = aboutFactory.createInstance(Ci.nsIAboutModule);

  var ios = Cc["@mozilla.org/network/io-service;1"].
            getService(Ci.nsIIOService);
  assert.equal(
    about.getURIFlags(ios.newURI("http://foo.com", null, null)),
    Ci.nsIAboutModule.ALLOW_SCRIPT
  );

  var aboutURI = ios.newURI("about:boop", null, null);
  var channel = ios.newChannelFromURI(aboutURI);
  var iStream = channel.open();
  var siStream = Cc['@mozilla.org/scriptableinputstream;1']
                 .createInstance(Ci.nsIScriptableInputStream);
  siStream.init(iStream);
  var data = new String();
  data += siStream.read(-1);
  siStream.close();
  iStream.close();
  assert.equal(data, text);

  xpcom.unregister(service);
  assert.equal(isCIDRegistered(service.id), false);
}

exports["test register"] = function(assert) {
  testRegister(assert, "hai2u");
};

exports["test re-register"] = function(assert) {
  testRegister(assert, "hai2u again");
};

exports["test unload"] = function(assert) {
  let loader = Loader(module);
  let sbxpcom = loader.require("xpcom");

  let auto = sbxpcom.Factory.new({
    contract: "@mozilla.org/test/auto-unload;1",
    description: "test auto",
    component: sbxpcom.Unknown.extend({ name: 'auto' })
  });

  let manual = sbxpcom.Factory.new({
    contract: "@mozilla.org/test/manual-unload;1",
    description: "test manual",
    register: false,
    unregister: false,
    component: sbxpcom.Unknown.extend({ name: 'manual' })
  });

  assert.equal(isCIDRegistered(auto.id), true, 'component registered');
  assert.equal(isCIDRegistered(manual.id), false, 'component not registered');

  sbxpcom.register(manual)
  assert.equal(isCIDRegistered(manual.id), true,
                   'component was automatically registered on first instance');
  loader.unload();

  assert.equal(isCIDRegistered(auto.id), false,
                   'component was atumatically unregistered on unload');
  assert.equal(isCIDRegistered(manual.id), true,
                   'component was not automatically unregistered on unload');
  sbxpcom.unregister(manual);
  assert.equal(isCIDRegistered(manual.id), false,
                   'component was manually unregistered on unload');
};

require("test").run(exports)