main.js 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 'use strict';
  5. const { Cu } = require('chrome');
  6. const sp = require('sdk/simple-prefs');
  7. const app = require('sdk/system/xul-app');
  8. const self = require('sdk/self');
  9. const tabs = require('sdk/tabs');
  10. const { preferencesBranch } = require('sdk/self');
  11. const { AddonManager } = Cu.import('resource://gre/modules/AddonManager.jsm', {});
  12. exports.testRegression = function(assert) {
  13. assert.equal(self.preferencesBranch, self.id, 'preferencesBranch returns id here');
  14. }
  15. exports.testDefaultValues = function (assert) {
  16. assert.equal(sp.prefs.myHiddenInt, 5, 'myHiddenInt default is 5');
  17. assert.equal(sp.prefs.myInteger, 8, 'myInteger default is 8');
  18. assert.equal(sp.prefs.somePreference, 'TEST', 'somePreference default is correct');
  19. }
  20. exports.testOptionsType = function(assert, done) {
  21. AddonManager.getAddonByID(self.id, function(aAddon) {
  22. assert.equal(aAddon.optionsType, AddonManager.OPTIONS_TYPE_INLINE, 'options type is inline');
  23. done();
  24. });
  25. }
  26. if (app.is('Firefox')) {
  27. exports.testAOM = function(assert, done) {
  28. tabs.open({
  29. url: 'about:addons',
  30. onReady: function(tab) {
  31. tab.attach({
  32. contentScriptWhen: 'end',
  33. contentScript: 'function onLoad() {\n' +
  34. 'unsafeWindow.removeEventListener("load", onLoad, false);\n' +
  35. 'AddonManager.getAddonByID("' + self.id + '", function(aAddon) {\n' +
  36. 'unsafeWindow.gViewController.viewObjects.detail.node.addEventListener("ViewChanged", function whenViewChanges() {\n' +
  37. 'unsafeWindow.gViewController.viewObjects.detail.node.removeEventListener("ViewChanged", whenViewChanges, false);\n' +
  38. 'setTimeout(function() {\n' + // TODO: figure out why this is necessary..
  39. 'self.postMessage({\n' +
  40. 'somePreference: getAttributes(unsafeWindow.document.querySelector("setting[title=\'some-title\']")),\n' +
  41. 'myInteger: getAttributes(unsafeWindow.document.querySelector("setting[title=\'my-int\']")),\n' +
  42. 'myHiddenInt: getAttributes(unsafeWindow.document.querySelector("setting[title=\'hidden-int\']"))\n' +
  43. '});\n' +
  44. '}, 250);\n' +
  45. '}, false);\n' +
  46. 'unsafeWindow.gViewController.commands.cmd_showItemDetails.doCommand(aAddon, true);\n' +
  47. '});\n' +
  48. 'function getAttributes(ele) {\n' +
  49. 'if (!ele) return {};\n' +
  50. 'return {\n' +
  51. 'pref: ele.getAttribute("pref"),\n' +
  52. 'type: ele.getAttribute("type"),\n' +
  53. 'title: ele.getAttribute("title"),\n' +
  54. 'desc: ele.getAttribute("desc")\n' +
  55. '}\n' +
  56. '}\n' +
  57. '}\n' +
  58. // Wait for the load event ?
  59. 'if (document.readyState == "complete") {\n' +
  60. 'onLoad()\n' +
  61. '} else {\n' +
  62. 'unsafeWindow.addEventListener("load", onLoad, false);\n' +
  63. '}\n',
  64. onMessage: function(msg) {
  65. // test somePreference
  66. assert.equal(msg.somePreference.type, 'string', 'some pref is a string');
  67. assert.equal(msg.somePreference.pref, 'extensions.'+self.preferencesBranch+'.somePreference', 'somePreference path is correct');
  68. assert.equal(msg.somePreference.title, 'some-title', 'somePreference title is correct');
  69. assert.equal(msg.somePreference.desc, 'Some short description for the preference', 'somePreference description is correct');
  70. // test myInteger
  71. assert.equal(msg.myInteger.type, 'integer', 'myInteger is a int');
  72. assert.equal(msg.myInteger.pref, 'extensions.'+self.preferencesBranch+'.myInteger', 'extensions.test-simple-prefs.myInteger');
  73. assert.equal(msg.myInteger.title, 'my-int', 'myInteger title is correct');
  74. assert.equal(msg.myInteger.desc, 'How many of them we have.', 'myInteger desc is correct');
  75. // test myHiddenInt
  76. assert.equal(msg.myHiddenInt.type, undefined, 'myHiddenInt was not displayed');
  77. assert.equal(msg.myHiddenInt.pref, undefined, 'myHiddenInt was not displayed');
  78. assert.equal(msg.myHiddenInt.title, undefined, 'myHiddenInt was not displayed');
  79. assert.equal(msg.myHiddenInt.desc, undefined, 'myHiddenInt was not displayed');
  80. tab.close(done);
  81. }
  82. });
  83. }
  84. });
  85. }
  86. }
  87. require('sdk/test/runner').runTestsFromModule(module);