1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-notifications.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const { Loader } = require('sdk/test/loader'); 1.9 + 1.10 +exports.testOnClick = function (assert) { 1.11 + let [loader, mockAlertServ] = makeLoader(module); 1.12 + let notifs = loader.require("sdk/notifications"); 1.13 + let data = "test data"; 1.14 + let opts = { 1.15 + onClick: function (clickedData) { 1.16 + assert.equal(this, notifs, "|this| should be notifications module"); 1.17 + assert.equal(clickedData, data, 1.18 + "data passed to onClick should be correct"); 1.19 + }, 1.20 + data: data, 1.21 + title: "test title", 1.22 + text: "test text", 1.23 + iconURL: "test icon URL" 1.24 + }; 1.25 + notifs.notify(opts); 1.26 + mockAlertServ.click(); 1.27 + loader.unload(); 1.28 +}; 1.29 + 1.30 +exports['test:numbers and URLs in options'] = function(assert) { 1.31 + let [loader] = makeLoader(module); 1.32 + let notifs = loader.require('sdk/notifications'); 1.33 + let opts = { 1.34 + title: 123, 1.35 + text: 45678, 1.36 + // must use in-loader `sdk/url` module for the validation type check to work 1.37 + iconURL: loader.require('sdk/url').URL('data:image/png,blah') 1.38 + }; 1.39 + try { 1.40 + notifs.notify(opts); 1.41 + assert.pass('using numbers and URLs in options works'); 1.42 + } catch (e) { 1.43 + assert.fail('using numbers and URLs in options must not throw'); 1.44 + } 1.45 + loader.unload(); 1.46 +} 1.47 + 1.48 +exports['test:new tag, dir and lang options'] = function(assert) { 1.49 + let [loader] = makeLoader(module); 1.50 + let notifs = loader.require('sdk/notifications'); 1.51 + let opts = { 1.52 + title: 'best', 1.53 + tag: 'tagging', 1.54 + lang: 'en' 1.55 + }; 1.56 + 1.57 + try { 1.58 + opts.dir = 'ttb'; 1.59 + notifs.notify(opts); 1.60 + assert.fail('`dir` option must not accept TopToBottom direction.'); 1.61 + } catch (e) { 1.62 + assert.equal(e.message, 1.63 + '`dir` option must be one of: "auto", "ltr" or "rtl".'); 1.64 + } 1.65 + 1.66 + try { 1.67 + opts.dir = 'rtl'; 1.68 + notifs.notify(opts); 1.69 + assert.pass('`dir` option accepts "rtl" direction.'); 1.70 + } catch (e) { 1.71 + assert.fail('`dir` option must accept "rtl" direction.'); 1.72 + } 1.73 + 1.74 + loader.unload(); 1.75 +} 1.76 + 1.77 +// Returns [loader, mockAlertService]. 1.78 +function makeLoader(module) { 1.79 + let loader = Loader(module); 1.80 + let mockAlertServ = { 1.81 + showAlertNotification: function (imageUrl, title, text, textClickable, 1.82 + cookie, alertListener, name) { 1.83 + this._cookie = cookie; 1.84 + this._alertListener = alertListener; 1.85 + }, 1.86 + click: function () { 1.87 + this._alertListener.observe(null, "alertclickcallback", this._cookie); 1.88 + } 1.89 + }; 1.90 + loader.require("sdk/notifications"); 1.91 + let scope = loader.sandbox("sdk/notifications"); 1.92 + scope.notify = mockAlertServ.showAlertNotification.bind(mockAlertServ); 1.93 + return [loader, mockAlertServ]; 1.94 +} 1.95 + 1.96 +require('sdk/test').run(exports);