summaryrefslogtreecommitdiffstats
path: root/devtools/client/jsonview/test/browser_jsonview_row_selection.js
blob: 300ac40a03887d8a36b5841f9f92f2bd3d788bf8 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function () {
  info("Test 1 JSON row selection started");

  // Create a tall JSON so that there is a scrollbar.
  const numRows = 1e3;
  const json = JSON.stringify(
    Array(numRows)
      .fill()
      .map((_, i) => i)
  );
  const tab = await addJsonViewTab("data:application/json," + json);

  is(
    await getElementCount(".treeRow"),
    numRows,
    "Got the expected number of rows."
  );
  await assertRowSelected(null);

  // Focus the tree and select first row.
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const tree = content.document.querySelector(".treeTable");
    tree.focus();
    is(tree, content.document.activeElement, "Tree should be focused");
    content.document.querySelector(".treeRow:nth-child(1)").click();
  });
  await assertRowSelected(1);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    ok(scroller.clientHeight < scroller.scrollHeight, "There is a scrollbar.");
    is(scroller.scrollTop, 0, "Initially scrolled to the top.");
  });

  // Select last row.
  await BrowserTestUtils.synthesizeKey("VK_END", {}, tab.linkedBrowser);
  await assertRowSelected(numRows);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    is(
      scroller.scrollTop + scroller.clientHeight,
      scroller.scrollHeight,
      "Scrolled to the bottom."
    );
    // Click to select 2nd row.
    content.document.querySelector(".treeRow:nth-child(2)").click();
  });
  await assertRowSelected(2);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    ok(scroller.scrollTop > 0, "Not scrolled to the top.");
    // Synthesize up arrow key to select first row.
    content.document.querySelector(".treeTable").focus();
  });
  await BrowserTestUtils.synthesizeKey("VK_UP", {}, tab.linkedBrowser);
  await assertRowSelected(1);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    is(scroller.scrollTop, 0, "Scrolled to the top.");
  });
});

add_task(async function () {
  info("Test 2 JSON row selection started");

  const numRows = 4;
  const tab = await addJsonViewTab("data:application/json,[0,1,2,3]");

  is(
    await getElementCount(".treeRow"),
    numRows,
    "Got the expected number of rows."
  );
  await assertRowSelected(null);

  // Click to select first row.
  await clickJsonNode(".treeRow:first-child");
  await assertRowSelected(1);

  // Synthesize multiple down arrow keydowns to select following rows.
  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    content.document.querySelector(".treeTable").focus();
  });
  for (let i = 2; i < numRows; ++i) {
    await BrowserTestUtils.synthesizeKey(
      "VK_DOWN",
      { type: "keydown" },
      tab.linkedBrowser
    );
    await assertRowSelected(i);
  }

  // Now synthesize the keyup, this shouldn't change selected row.
  await BrowserTestUtils.synthesizeKey(
    "VK_DOWN",
    { type: "keyup" },
    tab.linkedBrowser
  );
  await wait(500);
  await assertRowSelected(numRows - 1);

  // Finally, synthesize keydown with a modifier, this also shouldn't change selected row.
  await BrowserTestUtils.synthesizeKey(
    "VK_DOWN",
    { type: "keydown", shiftKey: true },
    tab.linkedBrowser
  );
  await wait(500);
  await assertRowSelected(numRows - 1);
});

add_task(async function () {
  info("Test 3 JSON row selection started");

  // Create a JSON with a row taller than the panel.
  const json = JSON.stringify([0, "a ".repeat(1e4), 1]);
  const tab = await addJsonViewTab("data:application/json," + encodeURI(json));

  is(await getElementCount(".treeRow"), 3, "Got the expected number of rows.");
  await assertRowSelected(null);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    const row = content.document.querySelector(".treeRow:nth-child(2)");
    ok(
      scroller.clientHeight < row.clientHeight,
      "The row is taller than the scroller."
    );
    is(scroller.scrollTop, 0, "Initially scrolled to the top.");

    // Select the tall row.
    content.document.querySelector(".treeTable").focus();
    row.click();
  });
  await assertRowSelected(2);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    is(
      scroller.scrollTop,
      0,
      "When the row is visible, do not scroll on click."
    );
  });

  // Select the last row.
  await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, tab.linkedBrowser);
  await assertRowSelected(3);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const scroller = content.document.querySelector(
      ".jsonPanelBox .panelContent"
    );
    is(
      scroller.scrollTop + scroller.offsetHeight,
      scroller.scrollHeight,
      "Scrolled to the bottom."
    );

    // Select the tall row.
    const row = content.document.querySelector(".treeRow:nth-child(2)");
    row.click();
  });

  await assertRowSelected(2);
  const scroll = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      const scroller = content.document.querySelector(
        ".jsonPanelBox .panelContent"
      );
      const row = content.document.querySelector(".treeRow:nth-child(2)");
      is(
        scroller.scrollTop + scroller.offsetHeight,
        scroller.scrollHeight,
        "Scrolled to the bottom. When the row is visible, do not scroll on click."
      );

      // Scroll up a bit, so that both the top and bottom of the row are not visible.
      const scrollPos = (scroller.scrollTop = Math.ceil(
        (scroller.scrollTop + row.offsetTop) / 2
      ));
      ok(
        scroller.scrollTop > row.offsetTop,
        "The top of the row is not visible."
      );
      ok(
        scroller.scrollTop + scroller.offsetHeight <
          row.offsetTop + row.offsetHeight,
        "The bottom of the row is not visible."
      );

      // Select the tall row.
      row.click();
      return scrollPos;
    }
  );

  await assertRowSelected(2);
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [scroll],
    function (scrollPos) {
      const scroller = content.document.querySelector(
        ".jsonPanelBox .panelContent"
      );
      is(scroller.scrollTop, scrollPos, "Scroll did not change");
    }
  );
});

async function assertRowSelected(rowNum) {
  const idx = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      return [].indexOf.call(
        content.document.querySelectorAll(".treeRow"),
        content.document.querySelector(".treeRow.selected")
      );
    }
  );
  is(
    idx + 1,
    +rowNum,
    `${rowNum ? "The row #" + rowNum : "No row"} is selected.`
  );
}