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