summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser_webconsole_bug_595350_multiple_windows_and_tabs.js
blob: 1951cb36676018c041135c69cbc9bee7bd7848e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the Web Console doesn't leak when multiple tabs and windows are
// opened and then closed.

"use strict";

const TEST_URI = "data:text/html;charset=utf-8,Web Console test for bug 595350";

var win1 = window, win2;
var openTabs = [];
var loadedTabCount = 0;

function test() {
  requestLongerTimeout(3);

  // Add two tabs in the main window.
  addTabs(win1);

  // Open a new window.
  win2 = OpenBrowserWindow();
  win2.addEventListener("load", onWindowLoad, true);
}

function onWindowLoad(aEvent) {
  win2.removeEventListener(aEvent.type, onWindowLoad, true);

  // Add two tabs in the new window.
  addTabs(win2);
}

function addTabs(aWindow) {
  for (let i = 0; i < 2; i++) {
    let tab = aWindow.gBrowser.addTab(TEST_URI);
    openTabs.push(tab);

    tab.linkedBrowser.addEventListener("load", function onLoad(aEvent) {
      tab.linkedBrowser.removeEventListener(aEvent.type, onLoad, true);

      loadedTabCount++;
      info("tabs loaded: " + loadedTabCount);
      if (loadedTabCount >= 4) {
        executeSoon(openConsoles);
      }
    }, true);
  }
}

function openConsoles() {
  function open(i) {
    let tab = openTabs[i];
    openConsole(tab).then(function (hud) {
      ok(hud, "HUD is open for tab " + i);
      let window = hud.target.tab.linkedBrowser.contentWindow;
      window.console.log("message for tab " + i);

      if (i >= openTabs.length - 1) {
        // Use executeSoon() to allow the promise to resolve.
        executeSoon(closeConsoles);
      }
      else {
        executeSoon(() => open(i + 1));
      }
    });
  }

  // open the Web Console for each of the four tabs and log a message.
  open(0);
}

function closeConsoles() {
  let consolesClosed = 0;

  function onWebConsoleClose(aSubject, aTopic) {
    if (aTopic == "web-console-destroyed") {
      consolesClosed++;
      info("consoles destroyed: " + consolesClosed);
      if (consolesClosed == 4) {
        // Use executeSoon() to allow all the observers to execute.
        executeSoon(finishTest);
      }
    }
  }

  Services.obs.addObserver(onWebConsoleClose, "web-console-destroyed", false);

  registerCleanupFunction(() => {
    Services.obs.removeObserver(onWebConsoleClose, "web-console-destroyed");
  });

  win2.close();

  win1.gBrowser.removeTab(openTabs[0]);
  win1.gBrowser.removeTab(openTabs[1]);

  openTabs = win1 = win2 = null;
}