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

"use strict";

// Test that the autocomplete popup is resized when needed.

const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
<head>
  <script>
    /* Create prototype-less object so popup does not contain native
     * Object prototype properties.
     */
    window.xx = Object.create(null, Object.getOwnPropertyDescriptors({
      ["y".repeat(10)]: 1,
      ["z".repeat(20)]: 2
    }));
    window.xxx = 1;
  </script>
</head>
<body>Test</body>`;

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);
  const { jsterm } = hud;
  const { autocompletePopup: popup } = jsterm;

  info(`wait for completion suggestions for "xx"`);
  await setInputValueForAutocompletion(hud, "xx");
  ok(popup.isOpen, "popup is open");

  const expectedPopupItems = ["xx", "xxx"];
  ok(
    hasExactPopupLabels(popup, expectedPopupItems),
    "popup has expected items"
  );

  const originalWidth = popup._tooltip.container.clientWidth;
  Assert.greaterOrEqual(
    originalWidth,
    getLongestLabelWidth(jsterm),
    `popup (${originalWidth}px) is at least wider than the width of the longest list item (${getLongestLabelWidth(
      jsterm
    )}px)`
  );

  info(`wait for completion suggestions for "xx."`);
  let onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString(".");
  await onAutocompleteUpdated;

  ok(
    hasExactPopupLabels(popup, ["y".repeat(10), "z".repeat(20)]),
    "popup has expected items"
  );
  const newPopupWidth = popup._tooltip.container.clientWidth;
  Assert.greaterOrEqual(
    newPopupWidth,
    originalWidth,
    `The popup width was updated (${originalWidth}px -> ${newPopupWidth}px)`
  );
  Assert.greaterOrEqual(
    newPopupWidth,
    getLongestLabelWidth(jsterm),
    `popup (${newPopupWidth}px) is at least wider than the width of the longest list item (${getLongestLabelWidth(
      jsterm
    )}px)`
  );

  info(`wait for completion suggestions for "xx"`);
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.synthesizeKey("KEY_Backspace");
  await onAutocompleteUpdated;

  is(
    popup._tooltip.container.clientWidth,
    originalWidth,
    "popup is back to its original width"
  );

  info("Close autocomplete popup");
  await closeAutocompletePopup(hud);
});

function getLongestLabelWidth(jsterm) {
  return (
    jsterm._inputCharWidth *
    getAutocompletePopupLabels(jsterm.autocompletePopup).sort(
      (a, b) => a < b
    )[0].length
  );
}