test-tab-observer.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // TODO Fennec support in Bug #894525
  6. module.metadata = {
  7. "engines": {
  8. "Firefox": "*"
  9. }
  10. }
  11. const { openTab, closeTab } = require("sdk/tabs/utils");
  12. const { Loader } = require("sdk/test/loader");
  13. const { setTimeout } = require("sdk/timers");
  14. exports["test unload tab observer"] = function(assert, done) {
  15. let loader = Loader(module);
  16. let window = loader.require("sdk/deprecated/window-utils").activeBrowserWindow;
  17. let observer = loader.require("sdk/tabs/observer").observer;
  18. let opened = 0;
  19. let closed = 0;
  20. observer.on("open", function onOpen(window) { opened++; });
  21. observer.on("close", function onClose(window) { closed++; });
  22. // Open and close tab to trigger observers.
  23. closeTab(openTab(window, "data:text/html;charset=utf-8,tab-1"));
  24. // Unload the module so that all listeners set by observer are removed.
  25. loader.unload();
  26. // Open and close tab once again.
  27. closeTab(openTab(window, "data:text/html;charset=utf-8,tab-2"));
  28. // Enqueuing asserts to make sure that assertion is not performed early.
  29. setTimeout(function () {
  30. assert.equal(1, opened, "observer open was called before unload only");
  31. assert.equal(1, closed, "observer close was called before unload only");
  32. done();
  33. }, 0);
  34. };
  35. require("test").run(exports);