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

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:ba67fad3f4f7
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 { Loader } = require('sdk/test/loader');
6
7 exports.testOnClick = function (assert) {
8 let [loader, mockAlertServ] = makeLoader(module);
9 let notifs = loader.require("sdk/notifications");
10 let data = "test data";
11 let opts = {
12 onClick: function (clickedData) {
13 assert.equal(this, notifs, "|this| should be notifications module");
14 assert.equal(clickedData, data,
15 "data passed to onClick should be correct");
16 },
17 data: data,
18 title: "test title",
19 text: "test text",
20 iconURL: "test icon URL"
21 };
22 notifs.notify(opts);
23 mockAlertServ.click();
24 loader.unload();
25 };
26
27 exports['test:numbers and URLs in options'] = function(assert) {
28 let [loader] = makeLoader(module);
29 let notifs = loader.require('sdk/notifications');
30 let opts = {
31 title: 123,
32 text: 45678,
33 // must use in-loader `sdk/url` module for the validation type check to work
34 iconURL: loader.require('sdk/url').URL('data:image/png,blah')
35 };
36 try {
37 notifs.notify(opts);
38 assert.pass('using numbers and URLs in options works');
39 } catch (e) {
40 assert.fail('using numbers and URLs in options must not throw');
41 }
42 loader.unload();
43 }
44
45 exports['test:new tag, dir and lang options'] = function(assert) {
46 let [loader] = makeLoader(module);
47 let notifs = loader.require('sdk/notifications');
48 let opts = {
49 title: 'best',
50 tag: 'tagging',
51 lang: 'en'
52 };
53
54 try {
55 opts.dir = 'ttb';
56 notifs.notify(opts);
57 assert.fail('`dir` option must not accept TopToBottom direction.');
58 } catch (e) {
59 assert.equal(e.message,
60 '`dir` option must be one of: "auto", "ltr" or "rtl".');
61 }
62
63 try {
64 opts.dir = 'rtl';
65 notifs.notify(opts);
66 assert.pass('`dir` option accepts "rtl" direction.');
67 } catch (e) {
68 assert.fail('`dir` option must accept "rtl" direction.');
69 }
70
71 loader.unload();
72 }
73
74 // Returns [loader, mockAlertService].
75 function makeLoader(module) {
76 let loader = Loader(module);
77 let mockAlertServ = {
78 showAlertNotification: function (imageUrl, title, text, textClickable,
79 cookie, alertListener, name) {
80 this._cookie = cookie;
81 this._alertListener = alertListener;
82 },
83 click: function () {
84 this._alertListener.observe(null, "alertclickcallback", this._cookie);
85 }
86 };
87 loader.require("sdk/notifications");
88 let scope = loader.sandbox("sdk/notifications");
89 scope.notify = mockAlertServ.showAlertNotification.bind(mockAlertServ);
90 return [loader, mockAlertServ];
91 }
92
93 require('sdk/test').run(exports);

mercurial