browser_two_tabs.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Check regression when opening two tabs
  6. */
  7. var { DebuggerServer } = require("devtools/server/main");
  8. var { DebuggerClient } = require("devtools/shared/client/main");
  9. const TAB_URL_1 = "data:text/html;charset=utf-8,foo";
  10. const TAB_URL_2 = "data:text/html;charset=utf-8,bar";
  11. var gClient;
  12. var gTab1, gTab2;
  13. var gTabActor1, gTabActor2;
  14. function test() {
  15. waitForExplicitFinish();
  16. if (!DebuggerServer.initialized) {
  17. DebuggerServer.init();
  18. DebuggerServer.addBrowserActors();
  19. }
  20. openTabs();
  21. }
  22. function openTabs() {
  23. // Open two tabs, select the second
  24. addTab(TAB_URL_1).then(tab1 => {
  25. gTab1 = tab1;
  26. addTab(TAB_URL_2).then(tab2 => {
  27. gTab2 = tab2;
  28. connect();
  29. });
  30. });
  31. }
  32. function connect() {
  33. // Connect to debugger server to fetch the two tab actors
  34. gClient = new DebuggerClient(DebuggerServer.connectPipe());
  35. gClient.connect()
  36. .then(() => gClient.listTabs())
  37. .then(response => {
  38. // Fetch the tab actors for each tab
  39. gTabActor1 = response.tabs.filter(a => a.url === TAB_URL_1)[0];
  40. gTabActor2 = response.tabs.filter(a => a.url === TAB_URL_2)[0];
  41. checkGetTab();
  42. });
  43. }
  44. function checkGetTab() {
  45. gClient.getTab({tab: gTab1})
  46. .then(response => {
  47. is(JSON.stringify(gTabActor1), JSON.stringify(response.tab),
  48. "getTab returns the same tab grip for first tab");
  49. })
  50. .then(() => {
  51. let filter = {};
  52. // Filter either by tabId or outerWindowID,
  53. // if we are running tests OOP or not.
  54. if (gTab1.linkedBrowser.frameLoader.tabParent) {
  55. filter.tabId = gTab1.linkedBrowser.frameLoader.tabParent.tabId;
  56. } else {
  57. let windowUtils = gTab1.linkedBrowser.contentWindow
  58. .QueryInterface(Ci.nsIInterfaceRequestor)
  59. .getInterface(Ci.nsIDOMWindowUtils);
  60. filter.outerWindowID = windowUtils.outerWindowID;
  61. }
  62. return gClient.getTab(filter);
  63. })
  64. .then(response => {
  65. is(JSON.stringify(gTabActor1), JSON.stringify(response.tab),
  66. "getTab returns the same tab grip when filtering by tabId/outerWindowID");
  67. })
  68. .then(() => gClient.getTab({tab: gTab2}))
  69. .then(response => {
  70. is(JSON.stringify(gTabActor2), JSON.stringify(response.tab),
  71. "getTab returns the same tab grip for second tab");
  72. })
  73. .then(checkGetTabFailures);
  74. }
  75. function checkGetTabFailures() {
  76. gClient.getTab({ tabId: -999 })
  77. .then(
  78. response => ok(false, "getTab unexpectedly succeed with a wrong tabId"),
  79. response => {
  80. is(response.error, "noTab");
  81. is(response.message, "Unable to find tab with tabId '-999'");
  82. }
  83. )
  84. .then(() => gClient.getTab({ outerWindowID: -999 }))
  85. .then(
  86. response => ok(false, "getTab unexpectedly succeed with a wrong outerWindowID"),
  87. response => {
  88. is(response.error, "noTab");
  89. is(response.message, "Unable to find tab with outerWindowID '-999'");
  90. }
  91. )
  92. .then(checkSelectedTabActor);
  93. }
  94. function checkSelectedTabActor() {
  95. // Send a naive request to the second tab actor
  96. // to check if it works
  97. gClient.request({ to: gTabActor2.consoleActor, type: "startListeners", listeners: [] }, aResponse => {
  98. ok("startedListeners" in aResponse, "Actor from the selected tab should respond to the request.");
  99. closeSecondTab();
  100. });
  101. }
  102. function closeSecondTab() {
  103. // Close the second tab, currently selected
  104. let container = gBrowser.tabContainer;
  105. container.addEventListener("TabClose", function onTabClose() {
  106. container.removeEventListener("TabClose", onTabClose);
  107. checkFirstTabActor();
  108. });
  109. gBrowser.removeTab(gTab2);
  110. }
  111. function checkFirstTabActor() {
  112. // then send a request to the first tab actor
  113. // to check if it still works
  114. gClient.request({ to: gTabActor1.consoleActor, type: "startListeners", listeners: [] }, aResponse => {
  115. ok("startedListeners" in aResponse, "Actor from the first tab should still respond.");
  116. cleanup();
  117. });
  118. }
  119. function cleanup() {
  120. let container = gBrowser.tabContainer;
  121. container.addEventListener("TabClose", function onTabClose() {
  122. container.removeEventListener("TabClose", onTabClose);
  123. gClient.close().then(finish);
  124. });
  125. gBrowser.removeTab(gTab1);
  126. }