|
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 let SocialService = Components.utils.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
6 |
|
7 // A helper to run a suite of tests. |
|
8 // The "test object" should be an object with function names as keys and a |
|
9 // function as the value. The functions will be called with a "cbnext" param |
|
10 // which should be called when the test is complete. |
|
11 // eg: |
|
12 // test = { |
|
13 // foo: function(cbnext) {... cbnext();} |
|
14 // } |
|
15 function runTests(tests, cbPreTest, cbPostTest, cbFinish) { |
|
16 let testIter = Iterator(tests); |
|
17 |
|
18 if (cbPreTest === undefined) { |
|
19 cbPreTest = function(cb) {cb()}; |
|
20 } |
|
21 if (cbPostTest === undefined) { |
|
22 cbPostTest = function(cb) {cb()}; |
|
23 } |
|
24 |
|
25 function runNextTest() { |
|
26 let name, func; |
|
27 try { |
|
28 [name, func] = testIter.next(); |
|
29 } catch (err if err instanceof StopIteration) { |
|
30 // out of items: |
|
31 (cbFinish || finish)(); |
|
32 return; |
|
33 } |
|
34 // We run on a timeout as the frameworker also makes use of timeouts, so |
|
35 // this helps keep the debug messages sane. |
|
36 executeSoon(function() { |
|
37 function cleanupAndRunNextTest() { |
|
38 info("sub-test " + name + " complete"); |
|
39 cbPostTest(runNextTest); |
|
40 } |
|
41 cbPreTest(function() { |
|
42 info("sub-test " + name + " starting"); |
|
43 try { |
|
44 func.call(tests, cleanupAndRunNextTest); |
|
45 } catch (ex) { |
|
46 ok(false, "sub-test " + name + " failed: " + ex.toString() +"\n"+ex.stack); |
|
47 cleanupAndRunNextTest(); |
|
48 } |
|
49 }) |
|
50 }); |
|
51 } |
|
52 runNextTest(); |
|
53 } |
|
54 |
|
55 // A mock notifications server. Based on: |
|
56 // dom/tests/mochitest/notification/notification_common.js |
|
57 const FAKE_CID = Cc["@mozilla.org/uuid-generator;1"]. |
|
58 getService(Ci.nsIUUIDGenerator).generateUUID(); |
|
59 |
|
60 const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1"; |
|
61 const ALERTS_SERVICE_CID = Components.ID(Cc[ALERTS_SERVICE_CONTRACT_ID].number); |
|
62 |
|
63 function MockAlertsService() {} |
|
64 |
|
65 MockAlertsService.prototype = { |
|
66 |
|
67 showAlertNotification: function(imageUrl, title, text, textClickable, |
|
68 cookie, alertListener, name) { |
|
69 let obData = JSON.stringify({ |
|
70 imageUrl: imageUrl, |
|
71 title: title, |
|
72 text:text, |
|
73 textClickable: textClickable, |
|
74 cookie: cookie, |
|
75 name: name |
|
76 }); |
|
77 Services.obs.notifyObservers(null, "social-test:notification-alert", obData); |
|
78 |
|
79 if (textClickable) { |
|
80 // probably should do this async.... |
|
81 alertListener.observe(null, "alertclickcallback", cookie); |
|
82 } |
|
83 |
|
84 alertListener.observe(null, "alertfinished", cookie); |
|
85 }, |
|
86 |
|
87 QueryInterface: function(aIID) { |
|
88 if (aIID.equals(Ci.nsISupports) || |
|
89 aIID.equals(Ci.nsIAlertsService)) |
|
90 return this; |
|
91 throw Cr.NS_ERROR_NO_INTERFACE; |
|
92 } |
|
93 }; |
|
94 |
|
95 var factory = { |
|
96 createInstance: function(aOuter, aIID) { |
|
97 if (aOuter != null) |
|
98 throw Cr.NS_ERROR_NO_AGGREGATION; |
|
99 return new MockAlertsService().QueryInterface(aIID); |
|
100 } |
|
101 }; |
|
102 |
|
103 function replaceAlertsService() { |
|
104 Components.manager.QueryInterface(Ci.nsIComponentRegistrar) |
|
105 .registerFactory(FAKE_CID, "", |
|
106 ALERTS_SERVICE_CONTRACT_ID, |
|
107 factory) |
|
108 } |
|
109 |
|
110 function restoreAlertsService() { |
|
111 Components.manager.QueryInterface(Ci.nsIComponentRegistrar) |
|
112 .registerFactory(ALERTS_SERVICE_CID, "", |
|
113 ALERTS_SERVICE_CONTRACT_ID, |
|
114 null); |
|
115 } |
|
116 // end of alerts service mock. |