|
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 |
|
5 const TEST_PROVIDER_ORIGIN = 'http://example.com'; |
|
6 |
|
7 Cu.import("resource://gre/modules/Services.jsm"); |
|
8 |
|
9 function ensureProvider(workerFunction, cb) { |
|
10 let manifest = { |
|
11 origin: TEST_PROVIDER_ORIGIN, |
|
12 name: "Example Provider", |
|
13 workerURL: "http://example.com/browser/toolkit/components/social/test/browser/echo.sjs?" |
|
14 + encodeURI("let run=" + workerFunction.toSource()) + ";run();" |
|
15 }; |
|
16 |
|
17 SocialService.addProvider(manifest, function (p) { |
|
18 cb(p); |
|
19 }); |
|
20 } |
|
21 |
|
22 function test() { |
|
23 waitForExplicitFinish(); |
|
24 |
|
25 let cbPostTest = function(cb) { |
|
26 SocialService.removeProvider(TEST_PROVIDER_ORIGIN, function() {cb()}); |
|
27 }; |
|
28 replaceAlertsService(); |
|
29 registerCleanupFunction(restoreAlertsService); |
|
30 runTests(tests, undefined, cbPostTest); |
|
31 } |
|
32 |
|
33 let tests = { |
|
34 testNotificationCallback: function(cbnext) { |
|
35 let run = function() { |
|
36 let testPort, apiPort; |
|
37 onconnect = function(e) { |
|
38 let port = e.ports[0]; |
|
39 port.onmessage = function(e) { |
|
40 if (e.data.topic == "social.initialize") { // this is the api port. |
|
41 apiPort = port; |
|
42 } else if (e.data.topic == "test.initialize") { // test suite port. |
|
43 testPort = port; |
|
44 apiPort.postMessage({topic: 'social.notification-create', |
|
45 data: { |
|
46 id: "the id", |
|
47 body: 'test notification', |
|
48 action: 'callback', |
|
49 actionArgs: { data: "something" } |
|
50 } |
|
51 }); |
|
52 } else if (e.data.topic == "social.notification-action") { |
|
53 let data = e.data.data; |
|
54 let ok = data && data.action == "callback" && |
|
55 data.actionArgs && e.data.data.actionArgs.data == "something"; |
|
56 testPort.postMessage({topic: "test.done", data: ok}); |
|
57 } |
|
58 } |
|
59 } |
|
60 } |
|
61 ensureProvider(run, function(provider) { |
|
62 let observer = { |
|
63 observedData: null, |
|
64 observe: function(subject, topic, data) { |
|
65 this.observedData = JSON.parse(data); |
|
66 Services.obs.removeObserver(observer, "social-test:notification-alert"); |
|
67 } |
|
68 } |
|
69 Services.obs.addObserver(observer, "social-test:notification-alert", false); |
|
70 |
|
71 let port = provider.getWorkerPort(); |
|
72 ok(port, "got port from worker"); |
|
73 port.onmessage = function(e) { |
|
74 if (e.data.topic == "test.done") { |
|
75 ok(e.data.data, "check the test worked"); |
|
76 ok(observer.observedData, "test observer fired"); |
|
77 is(observer.observedData.text, "test notification", "check the alert text is correct"); |
|
78 is(observer.observedData.title, "Example Provider", "check the alert title is correct"); |
|
79 is(observer.observedData.textClickable, true, "check the alert is clickable"); |
|
80 port.close(); |
|
81 cbnext(); |
|
82 } |
|
83 } |
|
84 port.postMessage({topic: "test.initialize"}); |
|
85 }); |
|
86 } |
|
87 }; |