summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_listtabs-02.js
blob: f696b6cb0414664c63c1c22e26d9a921dc4bbce2 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- 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 the root actor's live tab list implementation works as specified.
 */

var { BrowserTabList } = require("devtools/server/actors/webbrowser");

var gTestPage = "data:text/html;charset=utf-8," + encodeURIComponent(
  "<title>JS Debugger BrowserTabList test page</title><body>Yo.</body>");

// The tablist object whose behavior we observe.
var gTabList;
var gFirstActor, gActorA;
var gTabA, gTabB, gTabC;
var gNewWindow;

// Stock onListChanged handler.
var onListChangedCount = 0;
function onListChangedHandler() {
  onListChangedCount++;
}

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

  gTabList = new BrowserTabList("fake DebuggerServerConnection");
  gTabList._testing = true;
  gTabList.onListChanged = onListChangedHandler;

  checkSingleTab()
    .then(addTabA)
    .then(testTabA)
    .then(addTabB)
    .then(testTabB)
    .then(removeTabA)
    .then(testTabClosed)
    .then(addTabC)
    .then(testTabC)
    .then(removeTabC)
    .then(testNewWindow)
    .then(removeNewWindow)
    .then(testWindowClosed)
    .then(removeTabB)
    .then(checkSingleTab)
    .then(finishUp);
}

function checkSingleTab() {
  return gTabList.getList().then(aTabActors => {
    is(aTabActors.length, 1, "initial tab list: contains initial tab");
    gFirstActor = aTabActors[0];
    is(gFirstActor.url, "about:blank", "initial tab list: initial tab URL is 'about:blank'");
    is(gFirstActor.title, "New Tab", "initial tab list: initial tab title is 'New Tab'");
  });
}

function addTabA() {
  return addTab(gTestPage).then(aTab => {
    gTabA = aTab;
  });
}

function testTabA() {
  is(onListChangedCount, 1, "onListChanged handler call count");

  return gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 2, "gTabA opened: two tabs in list");
    ok(tabActors.has(gFirstActor), "gTabA opened: initial tab present");

    info("actors: " + [...tabActors].map(a => a.url));
    gActorA = [...tabActors].filter(a => a !== gFirstActor)[0];
    ok(gActorA.url.match(/^data:text\/html;/), "gTabA opened: new tab URL");
    is(gActorA.title, "JS Debugger BrowserTabList test page", "gTabA opened: new tab title");
  });
}

function addTabB() {
  return addTab(gTestPage).then(aTab => {
    gTabB = aTab;
  });
}

function testTabB() {
  is(onListChangedCount, 2, "onListChanged handler call count");

  return gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 3, "gTabB opened: three tabs in list");
  });
}

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

  once(gBrowser.tabContainer, "TabClose").then(aEvent => {
    ok(!aEvent.detail.adoptedBy, "This was a normal tab close");

    // Let the actor's TabClose handler finish first.
    executeSoon(deferred.resolve);
  }, false);

  removeTab(gTabA);
  return deferred.promise;
}

function testTabClosed() {
  is(onListChangedCount, 3, "onListChanged handler call count");

  gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 2, "gTabA closed: two tabs in list");
    ok(tabActors.has(gFirstActor), "gTabA closed: initial tab present");

    info("actors: " + [...tabActors].map(a => a.url));
    gActorA = [...tabActors].filter(a => a !== gFirstActor)[0];
    ok(gActorA.url.match(/^data:text\/html;/), "gTabA closed: new tab URL");
    is(gActorA.title, "JS Debugger BrowserTabList test page", "gTabA closed: new tab title");
  });
}

function addTabC() {
  return addTab(gTestPage).then(aTab => {
    gTabC = aTab;
  });
}

function testTabC() {
  is(onListChangedCount, 4, "onListChanged handler call count");

  gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 3, "gTabC opened: three tabs in list");
  });
}

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

  once(gBrowser.tabContainer, "TabClose").then(aEvent => {
    ok(aEvent.detail.adoptedBy, "This was a tab closed by moving");

    // Let the actor's TabClose handler finish first.
    executeSoon(deferred.resolve);
  }, false);

  gNewWindow = gBrowser.replaceTabWithWindow(gTabC);
  return deferred.promise;
}

function testNewWindow() {
  is(onListChangedCount, 5, "onListChanged handler call count");

  return gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 3, "gTabC closed: three tabs in list");
    ok(tabActors.has(gFirstActor), "gTabC closed: initial tab present");

    info("actors: " + [...tabActors].map(a => a.url));
    gActorA = [...tabActors].filter(a => a !== gFirstActor)[0];
    ok(gActorA.url.match(/^data:text\/html;/), "gTabC closed: new tab URL");
    is(gActorA.title, "JS Debugger BrowserTabList test page", "gTabC closed: new tab title");
  });
}

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

  once(gNewWindow, "unload").then(aEvent => {
    ok(!aEvent.detail, "This was a normal window close");

    // Let the actor's TabClose handler finish first.
    executeSoon(deferred.resolve);
  }, false);

  gNewWindow.close();
  return deferred.promise;
}

function testWindowClosed() {
  is(onListChangedCount, 6, "onListChanged handler call count");

  return gTabList.getList().then(aTabActors => {
    let tabActors = new Set(aTabActors);
    is(tabActors.size, 2, "gNewWindow closed: two tabs in list");
    ok(tabActors.has(gFirstActor), "gNewWindow closed: initial tab present");

    info("actors: " + [...tabActors].map(a => a.url));
    gActorA = [...tabActors].filter(a => a !== gFirstActor)[0];
    ok(gActorA.url.match(/^data:text\/html;/), "gNewWindow closed: new tab URL");
    is(gActorA.title, "JS Debugger BrowserTabList test page", "gNewWindow closed: new tab title");
  });
}

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

  once(gBrowser.tabContainer, "TabClose").then(aEvent => {
    ok(!aEvent.detail.adoptedBy, "This was a normal tab close");

    // Let the actor's TabClose handler finish first.
    executeSoon(deferred.resolve);
  }, false);

  removeTab(gTabB);
  return deferred.promise;
}

function finishUp() {
  gTabList = gFirstActor = gActorA = gTabA = gTabB = gTabC = gNewWindow = null;
  finish();
}