test-addon-installer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 { Cc, Ci, Cu } = require("chrome");
  6. const AddonInstaller = require("sdk/addon/installer");
  7. const { on, off } = require("sdk/system/events");
  8. const { setTimeout } = require("sdk/timers");
  9. const tmp = require("sdk/test/tmp-file");
  10. const system = require("sdk/system");
  11. const fixtures = require("./fixtures");
  12. const testFolderURL = module.uri.split('test-addon-installer.js')[0];
  13. const ADDON_URL = testFolderURL + "fixtures/addon-install-unit-test@mozilla.com.xpi";
  14. const ADDON_PATH = tmp.createFromURL(ADDON_URL);
  15. exports["test Install"] = function (assert, done) {
  16. // Save all events distpatched by bootstrap.js of the installed addon
  17. let events = [];
  18. function eventsObserver({ data }) {
  19. events.push(data);
  20. }
  21. on("addon-install-unit-test", eventsObserver);
  22. // Install the test addon
  23. AddonInstaller.install(ADDON_PATH).then(
  24. function onInstalled(id) {
  25. assert.equal(id, "addon-install-unit-test@mozilla.com", "`id` is valid");
  26. // Now uninstall it
  27. AddonInstaller.uninstall(id).then(function () {
  28. // Ensure that bootstrap.js methods of the addon have been called
  29. // successfully and in the right order
  30. let expectedEvents = ["install", "startup", "shutdown", "uninstall"];
  31. assert.equal(JSON.stringify(events),
  32. JSON.stringify(expectedEvents),
  33. "addon's bootstrap.js functions have been called");
  34. off("addon-install-unit-test", eventsObserver);
  35. done();
  36. });
  37. },
  38. function onFailure(code) {
  39. assert.fail("Install failed: "+code);
  40. off("addon-install-unit-test", eventsObserver);
  41. done();
  42. }
  43. );
  44. };
  45. exports["test Failing Install With Invalid Path"] = function (assert, done) {
  46. AddonInstaller.install("invalid-path").then(
  47. function onInstalled(id) {
  48. assert.fail("Unexpected success");
  49. done();
  50. },
  51. function onFailure(code) {
  52. assert.equal(code, AddonInstaller.ERROR_FILE_ACCESS,
  53. "Got expected error code");
  54. done();
  55. }
  56. );
  57. };
  58. exports["test Failing Install With Invalid File"] = function (assert, done) {
  59. let directory = system.pathFor("ProfD");
  60. AddonInstaller.install(directory).then(
  61. function onInstalled(id) {
  62. assert.fail("Unexpected success");
  63. done();
  64. },
  65. function onFailure(code) {
  66. assert.equal(code, AddonInstaller.ERROR_CORRUPT_FILE,
  67. "Got expected error code");
  68. done();
  69. }
  70. );
  71. }
  72. exports["test Update"] = function (assert, done) {
  73. // Save all events distpatched by bootstrap.js of the installed addon
  74. let events = [];
  75. let iteration = 1;
  76. let eventsObserver = ({data}) => events.push(data);
  77. on("addon-install-unit-test", eventsObserver);
  78. function onInstalled(id) {
  79. let prefix = "[" + iteration + "] ";
  80. assert.equal(id, "addon-install-unit-test@mozilla.com",
  81. prefix + "`id` is valid");
  82. // On 2nd and 3rd iteration, we receive uninstall events from the last
  83. // previously installed addon
  84. let expectedEvents =
  85. iteration == 1
  86. ? ["install", "startup"]
  87. : ["shutdown", "uninstall", "install", "startup"];
  88. assert.equal(JSON.stringify(events),
  89. JSON.stringify(expectedEvents),
  90. prefix + "addon's bootstrap.js functions have been called");
  91. if (iteration++ < 3) {
  92. next();
  93. }
  94. else {
  95. events = [];
  96. AddonInstaller.uninstall(id).then(function() {
  97. let expectedEvents = ["shutdown", "uninstall"];
  98. assert.equal(JSON.stringify(events),
  99. JSON.stringify(expectedEvents),
  100. prefix + "addon's bootstrap.js functions have been called");
  101. off("addon-install-unit-test", eventsObserver);
  102. done();
  103. });
  104. }
  105. }
  106. function onFailure(code) {
  107. assert.fail("Install failed: "+code);
  108. off("addon-install-unit-test", eventsObserver);
  109. done();
  110. }
  111. function next() {
  112. events = [];
  113. AddonInstaller.install(ADDON_PATH).then(onInstalled, onFailure);
  114. }
  115. next();
  116. };
  117. exports['test Uninstall failure'] = function (assert, done) {
  118. AddonInstaller.uninstall('invalid-addon-path').then(
  119. () => assert.fail('Addon uninstall should not resolve successfully'),
  120. () => assert.pass('Addon correctly rejected invalid uninstall')
  121. ).then(done, assert.fail);
  122. };
  123. exports['test Addon Disable and Enable'] = function (assert, done) {
  124. let ensureActive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
  125. assert.equal(state, true, 'Addon should be enabled by default');
  126. return addonId;
  127. });
  128. let ensureInactive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
  129. assert.equal(state, false, 'Addon should be disabled after disabling');
  130. return addonId;
  131. });
  132. AddonInstaller.install(ADDON_PATH)
  133. .then(ensureActive)
  134. .then(AddonInstaller.enable) // should do nothing, yet not fail
  135. .then(ensureActive)
  136. .then(AddonInstaller.disable)
  137. .then(ensureInactive)
  138. .then(AddonInstaller.disable) // should do nothing, yet not fail
  139. .then(ensureInactive)
  140. .then(AddonInstaller.enable)
  141. .then(ensureActive)
  142. .then(AddonInstaller.uninstall)
  143. .then(done, assert.fail);
  144. };
  145. exports['test Disable failure'] = function (assert, done) {
  146. AddonInstaller.disable('not-an-id').then(
  147. () => assert.fail('Addon disable should not resolve successfully'),
  148. () => assert.pass('Addon correctly rejected invalid disable')
  149. ).then(done, assert.fail);
  150. };
  151. exports['test Enable failure'] = function (assert, done) {
  152. AddonInstaller.enable('not-an-id').then(
  153. () => assert.fail('Addon enable should not resolve successfully'),
  154. () => assert.pass('Addon correctly rejected invalid enable')
  155. ).then(done, assert.fail);
  156. };
  157. require("test").run(exports);