summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_tableWidget_mouse_interaction.js
blob: 91b93ac6d5f64b4da15435950d2daf2d0dad965b (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that mosue interaction works fine with the table widget

"use strict";

const TEST_URI = CHROME_URL_ROOT + "doc_tableWidget_mouse_interaction.xhtml";
const TEST_OPT = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";

const {
  TableWidget,
} = require("resource://devtools/client/shared/widgets/TableWidget.js");

var doc, table;

function test() {
  waitForExplicitFinish();
  const win = Services.ww.openWindow(null, TEST_URI, "_blank", TEST_OPT, null);

  win.addEventListener(
    "load",
    function () {
      waitForFocus(function () {
        doc = win.document;
        table = new TableWidget(doc.querySelector("box"), {
          initialColumns: {
            col1: "Column 1",
            col2: "Column 2",
            col3: "Column 3",
            col4: "Column 4",
          },
          uniqueId: "col1",
          emptyText: "This is dummy empty text",
          highlightUpdated: true,
          removableColumns: true,
          wrapTextInElements: true,
          l10n: {
            setAttributes() {},
          },
        });
        startTests();
      });
    },
    { once: true }
  );
}

function endTests() {
  table.destroy();
  doc.defaultView.close();
  doc = table = null;
  finish();
}

var startTests = async function () {
  populateTable();
  await testMouseInteraction();
  endTests();
};

function populateTable() {
  table.push({
    col1: "id1",
    col2: "value10",
    col3: "value20",
    col4: "value30",
  });
  table.push({
    col1: "id2",
    col2: "value14",
    col3: "value29",
    col4: "value32",
  });
  table.push({
    col1: "id3",
    col2: "value17",
    col3: "value21",
    col4: "value31",
    extraData: "foobar",
    extraData2: 42,
  });
  table.push({
    col1: "id4",
    col2: "value12",
    col3: "value26",
    col4: "value33",
  });
  table.push({
    col1: "id5",
    col2: "value19",
    col3: "value26",
    col4: "value37",
  });
  table.push({
    col1: "id6",
    col2: "value15",
    col3: "value25",
    col4: "value37",
  });
  table.push({
    col1: "id7",
    col2: "value18",
    col3: "value21",
    col4: "value36",
    somethingExtra: "Hello World!",
  });
  table.push({
    col1: "id8",
    col2: "value11",
    col3: "value27",
    col4: "value34",
  });
  table.push({
    col1: "id9",
    col2: "value11",
    col3: "value23",
    col4: "value38",
  });
}

// Sends a click event on the passed DOM node in an async manner
function click(node, button = 0) {
  if (button == 0) {
    executeSoon(() =>
      EventUtils.synthesizeMouseAtCenter(node, {}, doc.defaultView)
    );
  } else {
    executeSoon(() =>
      EventUtils.synthesizeMouseAtCenter(
        node,
        {
          button,
          type: "contextmenu",
        },
        doc.defaultView
      )
    );
  }
}

async function showCol(id) {
  const onPopupHidden = once(table.menupopup, "popuphidden");
  const event = table.once(TableWidget.EVENTS.HEADER_CONTEXT_MENU);
  const menuItem = table.menupopup.querySelector(`[data-id='${id}']`);
  const column = table.tbody.querySelector(`#${id}`);

  info(`Showing ${id}`);
  ok(BrowserTestUtils.isHidden(column), "Column is hidden before showing it");

  table.menupopup.activateItem(menuItem);
  const toShow = await event;
  await onPopupHidden;

  is(toShow, id, `#${id} was selected to be shown`);
  ok(
    BrowserTestUtils.isVisible(column),
    "Column is not hidden after showing it"
  );
}

async function hideCol(id) {
  const onPopupHidden = once(table.menupopup, "popuphidden");
  const event = table.once(TableWidget.EVENTS.HEADER_CONTEXT_MENU);
  const menuItem = table.menupopup.querySelector(`[data-id='${id}']`);
  const column = table.tbody.querySelector(`#${id}`);

  info(`selecting to hide #${id}`);
  ok(
    BrowserTestUtils.isVisible(column),
    `Column #${id} is not hidden before hiding it`
  );
  table.menupopup.activateItem(menuItem);
  const toHide = await event;
  await onPopupHidden;
  is(toHide, id, `#${id} was selected to be hidden`);
  ok(
    BrowserTestUtils.isHidden(column),
    `Column #${id} is hidden after hiding it`
  );
}

/**
 * Tests if clicking the table items does the expected behavior
 */
var testMouseInteraction = async function () {
  info("Testing mouse interaction with the table");
  ok(!table.selectedRow, "Nothing should be selected beforehand");

  let event = table.once(TableWidget.EVENTS.ROW_SELECTED);
  const firstColumnFirstRowCell = table.tbody.querySelector("[data-id=id1]");
  info("clicking on the first row");
  ok(
    !firstColumnFirstRowCell.classList.contains("theme-selected"),
    "Node should not have selected class before clicking"
  );
  click(firstColumnFirstRowCell);
  let id = await event;
  ok(
    firstColumnFirstRowCell.classList.contains("theme-selected"),
    "Node has selected class after click"
  );
  is(id, "id1", "Correct row was selected");

  info("clicking on second row to select it");
  event = table.once(TableWidget.EVENTS.ROW_SELECTED);
  const firstColumnSecondRowCell = table.tbody.firstChild.children[2];
  // node should not have selected class
  ok(
    !firstColumnSecondRowCell.classList.contains("theme-selected"),
    "New node should not have selected class before clicking"
  );
  click(firstColumnSecondRowCell);
  id = await event;
  ok(
    firstColumnSecondRowCell.classList.contains("theme-selected"),
    "New node has selected class after clicking"
  );
  is(id, "id2", "Correct table path is emitted for new node");
  isnot(
    firstColumnFirstRowCell,
    firstColumnSecondRowCell,
    "Old and new node are different"
  );
  ok(
    !firstColumnFirstRowCell.classList.contains("theme-selected"),
    "Old node should not have selected class after the click on new node"
  );

  info("clicking on the third row cell content to select third row");
  event = table.once(TableWidget.EVENTS.ROW_SELECTED);
  const firstColumnThirdRowCell = table.tbody.firstChild.children[3];
  const firstColumnThirdRowCellInnerNode =
    firstColumnThirdRowCell.querySelector("span");
  // node should not have selected class
  ok(
    !firstColumnThirdRowCell.classList.contains("theme-selected"),
    "New node should not have selected class before clicking"
  );
  click(firstColumnThirdRowCellInnerNode);
  id = await event;
  ok(
    firstColumnThirdRowCell.classList.contains("theme-selected"),
    "New node has selected class after clicking the cell content"
  );
  is(id, "id3", "Correct table path is emitted for new node");

  // clicking on table header to sort by it
  event = table.once(TableWidget.EVENTS.COLUMN_SORTED);
  let node = table.tbody.children[6].children[0];
  info("clicking on the 4th coulmn header to sort the table by it");
  ok(
    !node.hasAttribute("sorted"),
    "Node should not have sorted attribute before clicking"
  );
  ok(
    doc.querySelector("[sorted]"),
    "Although, something else should be sorted on"
  );
  isnot(doc.querySelector("[sorted]"), node, "Which is not equal to this node");
  click(node);
  id = await event;
  is(id, "col4", "Correct column was sorted on");
  ok(
    node.hasAttribute("sorted"),
    "Node should now have sorted attribute after clicking"
  );
  is(
    doc.querySelectorAll("[sorted]").length,
    1,
    "Now only one column should be sorted on"
  );
  is(doc.querySelector("[sorted]"), node, "Which should be this column");

  // test context menu opening.
  // hiding second column
  // event listener for popupshown
  info("right click on the first column header");
  node = table.tbody.firstChild.firstChild;
  let onPopupShown = once(table.menupopup, "popupshown");
  click(node, 2);
  await onPopupShown;

  is(
    table.menupopup.querySelectorAll("menuitem[disabled]").length,
    1,
    "Only 1 menuitem is disabled"
  );
  is(
    table.menupopup.querySelector("menuitem[disabled]"),
    table.menupopup.querySelector("[data-id='col1']"),
    "Which is the unique column"
  );

  // popup should be open now
  // clicking on second column label
  await hideCol("col2");

  // hiding third column
  // event listener for popupshown
  info("right clicking on the first column header");
  node = table.tbody.firstChild.firstChild;
  onPopupShown = once(table.menupopup, "popupshown");
  click(node, 2);
  await onPopupShown;

  is(
    table.menupopup.querySelectorAll("menuitem[disabled]").length,
    1,
    "Only 1 menuitem is disabled"
  );

  await hideCol("col3");

  // opening again to see if 2 items are disabled now
  // event listener for popupshown
  info("right clicking on the first column header");
  node = table.tbody.firstChild.firstChild;
  onPopupShown = once(table.menupopup, "popupshown");
  click(node, 2);
  await onPopupShown;

  is(
    table.menupopup.querySelectorAll("menuitem[disabled]").length,
    2,
    "2 menuitems are disabled now as only 2 columns remain visible"
  );
  is(
    table.menupopup.querySelectorAll("menuitem[disabled]")[0],
    table.menupopup.querySelector("[data-id='col1']"),
    "First is the unique column"
  );
  is(
    table.menupopup.querySelectorAll("menuitem[disabled]")[1],
    table.menupopup.querySelector("[data-id='col4']"),
    "Second is the last column"
  );

  // showing back 2nd column
  // popup should be open now
  // clicking on second column label
  await showCol("col2");

  // showing back 3rd column
  // event listener for popupshown
  info("right clicking on the first column header");
  node = table.tbody.firstChild.firstChild;
  onPopupShown = once(table.menupopup, "popupshown");
  click(node, 2);
  await onPopupShown;

  // popup should be open now
  // clicking on second column label
  await showCol("col3");

  // reset table state
  table.clearSelection();
  table.sortBy("col1");
};