test-notifications.js 2.7 KB

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