summaryrefslogtreecommitdiff
path: root/devtools/client/scratchpad/test/browser_scratchpad_autocomplete.js
blob: ccc84b609f7dfbcbc7af1bccbdd75c13dbc9062f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
/* Bug 968896 */

// Test the completions using numbers.
const source = "0x1.";
const completions = ["toExponential", "toFixed", "toString"];
const { Task } = require("devtools/shared/task");

function test() {
  const options = { tabContent: "test scratchpad autocomplete" };
  openTabAndScratchpad(options)
    .then(Task.async(runTests))
    .then(finish, console.error);
}


function* runTests([win, sp]) {
  const {editor} = sp;
  const editorWin = editor.container.contentWindow;

  // Show the completions popup.
  sp.setText(source);
  sp.editor.setCursor({ line: 0, ch: source.length });
  yield keyOnce("suggestion-entered", " ", { ctrlKey: true });

  // Get the hints popup container.
  const hints = editorWin.document.querySelector(".CodeMirror-hints");

  ok(hints,
     "The hint container should exist.");
  is(hints.childNodes.length, 3,
     "The hint container should have the completions.");

  let i = 0;
  for (let completion of completions) {
    let active = hints.querySelector(".CodeMirror-hint-active");
    is(active.textContent, completion,
       "Check that completion " + i++ + " is what is expected.");
    yield keyOnce("suggestion-entered", "VK_DOWN");
  }

  // We should have looped around to the first suggestion again. Accept it.
  yield keyOnce("after-suggest", "VK_RETURN");

  is(sp.getText(), source + completions[0],
     "Autocompletion should work and select the right element.");

  // Check that the information tooltips work.
  sp.setText("5");
  yield keyOnce("show-information", " ", { ctrlKey: true, shiftKey: true });

  // Get the information container.
  const info = editorWin.document.querySelector(".CodeMirror-Tern-information");
  ok(info,
     "Info tooltip should appear.");
  is(info.textContent.slice(0, 6), "number",
     "Info tooltip should have expected contents.");

  function keyOnce(event, key, opts = {}) {
    const p = editor.once(event);
    EventUtils.synthesizeKey(key, opts, editorWin);
    return p;
  }
}