summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_multiple-windows.js
blob: b0bb1834c1a4127aa638f779ae29eee6fd47e748 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- 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/ */

/**
 * Make sure that the debugger attaches to the right tab when multiple windows
 * are open.
 */

const TAB1_URL = EXAMPLE_URL + "doc_script-switching-01.html";
const TAB2_URL = EXAMPLE_URL + "doc_script-switching-02.html";

var gNewTab, gNewWindow;
var gClient;

function test() {
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }

  let transport = DebuggerServer.connectPipe();
  gClient = new DebuggerClient(transport);
  gClient.connect().then(([aType, aTraits]) => {
    is(aType, "browser",
      "Root actor should identify itself as a browser.");

    promise.resolve(null)
      .then(() => addTab(TAB1_URL))
      .then(testFirstTab)
      .then(() => addWindow(TAB2_URL))
      .then(testNewWindow)
      .then(testFocusFirst)
      .then(testRemoveTab)
      .then(() => gClient.close())
      .then(finish)
      .then(null, aError => {
        ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
      });
  });
}

function testFirstTab(aTab) {
  let deferred = promise.defer();

  gNewTab = aTab;
  ok(!!gNewTab, "Second tab created.");

  gClient.listTabs(aResponse => {
    let tabActor = aResponse.tabs.filter(aGrip => aGrip.url == TAB1_URL).pop();
    ok(tabActor,
      "Should find a tab actor for the first tab.");

    is(aResponse.selected, 1,
      "The first tab is selected.");

    deferred.resolve();
  });

  return deferred.promise;
}

function testNewWindow(aWindow) {
  let deferred = promise.defer();

  gNewWindow = aWindow;
  ok(!!gNewWindow, "Second window created.");

  gNewWindow.focus();

  let topWindow = Services.wm.getMostRecentWindow("navigator:browser");
  is(topWindow, gNewWindow,
    "The second window is on top.");

  let isActive = promise.defer();
  let isLoaded = promise.defer();

  promise.all([isActive.promise, isLoaded.promise]).then(() => {
    gClient.listTabs(aResponse => {
      is(aResponse.selected, 2,
        "The second tab is selected.");

      deferred.resolve();
    });
  });

  if (Services.focus.activeWindow != gNewWindow) {
    gNewWindow.addEventListener("activate", function onActivate(aEvent) {
      if (aEvent.target != gNewWindow) {
        return;
      }
      gNewWindow.removeEventListener("activate", onActivate, true);
      isActive.resolve();
    }, true);
  } else {
    isActive.resolve();
  }

  let contentLocation = gNewWindow.content.location.href;
  if (contentLocation != TAB2_URL) {
    gNewWindow.document.addEventListener("load", function onLoad(aEvent) {
      if (aEvent.target.documentURI != TAB2_URL) {
        return;
      }
      gNewWindow.document.removeEventListener("load", onLoad, true);
      isLoaded.resolve();
    }, true);
  } else {
    isLoaded.resolve();
  }

  return deferred.promise;
}

function testFocusFirst() {
  let deferred = promise.defer();

  once(window.content, "focus").then(() => {
    gClient.listTabs(aResponse => {
      is(aResponse.selected, 1,
        "The first tab is selected after focusing on it.");

      deferred.resolve();
    });
  });

  window.content.focus();

  return deferred.promise;
}

function testRemoveTab() {
  let deferred = promise.defer();

  gNewWindow.close();

  // give it time to close
  executeSoon(function () { continue_remove_tab(deferred); });
  return deferred.promise;
}

function continue_remove_tab(deferred)
{
  removeTab(gNewTab);

  gClient.listTabs(aResponse => {
    // Verify that tabs are no longer included in listTabs.
    let foundTab1 = aResponse.tabs.some(aGrip => aGrip.url == TAB1_URL);
    let foundTab2 = aResponse.tabs.some(aGrip => aGrip.url == TAB2_URL);
    ok(!foundTab1, "Tab1 should be gone.");
    ok(!foundTab2, "Tab2 should be gone.");

    is(aResponse.selected, 0,
      "The original tab is selected.");

    deferred.resolve();
  });
}

registerCleanupFunction(function () {
  gNewTab = null;
  gNewWindow = null;
  gClient = null;
});