michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { Loader } = require('sdk/test/loader'); michael@0: michael@0: exports.testOnClick = function (assert) { michael@0: let [loader, mockAlertServ] = makeLoader(module); michael@0: let notifs = loader.require("sdk/notifications"); michael@0: let data = "test data"; michael@0: let opts = { michael@0: onClick: function (clickedData) { michael@0: assert.equal(this, notifs, "|this| should be notifications module"); michael@0: assert.equal(clickedData, data, michael@0: "data passed to onClick should be correct"); michael@0: }, michael@0: data: data, michael@0: title: "test title", michael@0: text: "test text", michael@0: iconURL: "test icon URL" michael@0: }; michael@0: notifs.notify(opts); michael@0: mockAlertServ.click(); michael@0: loader.unload(); michael@0: }; michael@0: michael@0: exports['test:numbers and URLs in options'] = function(assert) { michael@0: let [loader] = makeLoader(module); michael@0: let notifs = loader.require('sdk/notifications'); michael@0: let opts = { michael@0: title: 123, michael@0: text: 45678, michael@0: // must use in-loader `sdk/url` module for the validation type check to work michael@0: iconURL: loader.require('sdk/url').URL('data:image/png,blah') michael@0: }; michael@0: try { michael@0: notifs.notify(opts); michael@0: assert.pass('using numbers and URLs in options works'); michael@0: } catch (e) { michael@0: assert.fail('using numbers and URLs in options must not throw'); michael@0: } michael@0: loader.unload(); michael@0: } michael@0: michael@0: exports['test:new tag, dir and lang options'] = function(assert) { michael@0: let [loader] = makeLoader(module); michael@0: let notifs = loader.require('sdk/notifications'); michael@0: let opts = { michael@0: title: 'best', michael@0: tag: 'tagging', michael@0: lang: 'en' michael@0: }; michael@0: michael@0: try { michael@0: opts.dir = 'ttb'; michael@0: notifs.notify(opts); michael@0: assert.fail('`dir` option must not accept TopToBottom direction.'); michael@0: } catch (e) { michael@0: assert.equal(e.message, michael@0: '`dir` option must be one of: "auto", "ltr" or "rtl".'); michael@0: } michael@0: michael@0: try { michael@0: opts.dir = 'rtl'; michael@0: notifs.notify(opts); michael@0: assert.pass('`dir` option accepts "rtl" direction.'); michael@0: } catch (e) { michael@0: assert.fail('`dir` option must accept "rtl" direction.'); michael@0: } michael@0: michael@0: loader.unload(); michael@0: } michael@0: michael@0: // Returns [loader, mockAlertService]. michael@0: function makeLoader(module) { michael@0: let loader = Loader(module); michael@0: let mockAlertServ = { michael@0: showAlertNotification: function (imageUrl, title, text, textClickable, michael@0: cookie, alertListener, name) { michael@0: this._cookie = cookie; michael@0: this._alertListener = alertListener; michael@0: }, michael@0: click: function () { michael@0: this._alertListener.observe(null, "alertclickcallback", this._cookie); michael@0: } michael@0: }; michael@0: loader.require("sdk/notifications"); michael@0: let scope = loader.sandbox("sdk/notifications"); michael@0: scope.notify = mockAlertServ.showAlertNotification.bind(mockAlertServ); michael@0: return [loader, mockAlertServ]; michael@0: } michael@0: michael@0: require('sdk/test').run(exports);