test-keyboard-observer.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 { keyPress } = require("sdk/dom/events/keys");
  6. const { Loader } = require("sdk/test/loader");
  7. const timer = require("sdk/timers");
  8. exports["test unload keyboard observer"] = function(assert, done) {
  9. let loader = Loader(module);
  10. let element = loader.require("sdk/deprecated/window-utils").
  11. activeBrowserWindow.document.documentElement;
  12. let observer = loader.require("sdk/keyboard/observer").
  13. observer;
  14. let called = 0;
  15. observer.on("keypress", function () { called++; });
  16. // dispatching "keypress" event to trigger observer listeners.
  17. keyPress(element, "accel-%");
  18. // Unload the module.
  19. loader.unload();
  20. // dispatching "keypress" even once again.
  21. keyPress(element, "accel-%");
  22. // Enqueuing asserts to make sure that assertion is not performed early.
  23. timer.setTimeout(function () {
  24. assert.equal(called, 1, "observer was called before unload only.");
  25. done();
  26. }, 0);
  27. };
  28. require("test").run(exports);