test_devtools_extensions.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <!--
  3. Any copyright is dedicated to the Public Domain.
  4. http://creativecommons.org/publicdomain/zero/1.0/
  5. -->
  6. <html>
  7. <head>
  8. <meta charset="utf8">
  9. <title></title>
  10. <script type="application/javascript"
  11. src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  12. <link rel="stylesheet" type="text/css"
  13. href="chrome://mochikit/content/tests/SimpleTest/test.css">
  14. <script type="application/javascript;version=1.8">
  15. const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  16. let { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
  17. const contentGlobals = require("devtools/server/content-globals");
  18. const Services = require("Services");
  19. const tabs = require('sdk/tabs');
  20. const { getMostRecentBrowserWindow, getInnerId } = require('sdk/window/utils');
  21. const { PageMod } = require('sdk/page-mod');
  22. var _tests = [];
  23. function addTest(test) {
  24. _tests.push(test);
  25. }
  26. function runNextTest() {
  27. if (_tests.length == 0) {
  28. SimpleTest.finish()
  29. return;
  30. }
  31. _tests.shift()();
  32. }
  33. window.onload = function() {
  34. SimpleTest.waitForExplicitFinish();
  35. runNextTest();
  36. }
  37. addTest(function () {
  38. let TEST_URL = 'data:text/html;charset=utf-8,test';
  39. let mod = PageMod({
  40. include: TEST_URL,
  41. contentScriptWhen: 'ready',
  42. contentScript: 'null;'
  43. });
  44. tabs.open({
  45. url: TEST_URL,
  46. onLoad: function(tab) {
  47. let id = getInnerId(getMostRecentBrowserWindow().gBrowser.selectedBrowser.contentWindow);
  48. // getting
  49. is(contentGlobals.getContentGlobals({
  50. 'inner-window-id': id
  51. }).length, 1, 'found a global for inner-id = ' + id);
  52. Services.obs.addObserver(function observer(subject, topic, data) {
  53. if (id == subject.QueryInterface(Components.interfaces.nsISupportsPRUint64).data) {
  54. Services.obs.removeObserver(observer, 'inner-window-destroyed');
  55. setTimeout(function() {
  56. // closing the tab window should have removed the global
  57. is(contentGlobals.getContentGlobals({
  58. 'inner-window-id': id
  59. }).length, 0, 'did not find a global for inner-id = ' + id);
  60. mod.destroy();
  61. runNextTest();
  62. })
  63. }
  64. }, 'inner-window-destroyed', false);
  65. tab.close();
  66. }
  67. });
  68. })
  69. addTest(function testAddRemoveGlobal() {
  70. let global = {};
  71. let globalDetails = {
  72. global: global,
  73. 'inner-window-id': 5
  74. };
  75. // adding
  76. contentGlobals.addContentGlobal(globalDetails);
  77. // getting
  78. is(contentGlobals.getContentGlobals({
  79. 'inner-window-id': 5
  80. }).length, 1, 'found a global for inner-id = 5');
  81. is(contentGlobals.getContentGlobals({
  82. 'inner-window-id': 4
  83. }).length, 0, 'did not find a global for inner-id = 4');
  84. // remove
  85. contentGlobals.removeContentGlobal(globalDetails);
  86. // getting again
  87. is(contentGlobals.getContentGlobals({
  88. 'inner-window-id': 5
  89. }).length, 0, 'did not find a global for inner-id = 5');
  90. runNextTest();
  91. });
  92. </script>
  93. </head>
  94. <body></body>
  95. </html>