summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/test_datalist_dynamic.html
blob: 9fc3ca0006c659718cd69a3a837b52b73449c226 (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
<!DOCTYPE HTML>
<html>
<title>Dynamic change datalist</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="satchel_common.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
<input list="suggest" type="text" name="field1">
<datalist id="suggest">
  <option value="a1">
  <option value="a2">
  <option value="ab1">
  <option value="ab2">
</datalist>
<pre id="test">
<script class="testbody">
const { TestUtils } = SpecialPowers.Cu.import("resource://testing-common/TestUtils.jsm");
const input = document.querySelector("input");
const datalist = document.querySelector("datalist");

const DATALIST_DATA = {
  "a": ["a1", "a2", "ab1", "ab2"],
  "ab": ["ab1", "ab2", "abc1", "abc2"],
  "abc": ["abc1", "abc2", "abcd1", "abcd2"],
  "abcd": ["abcd1", "abcd2", "abcde1", "abcde2", "abcde3"]
};

var resolvePopupShownListener;
registerPopupShownListener(() => resolvePopupShownListener());

function waitPopup() {
  return new Promise(resolve => { resolvePopupShownListener = resolve; });
}

add_task(async function() {
  async function inputHandler() {
    let options = DATALIST_DATA[input.value] || [];

    await TestUtils.waitForTick();

    while (datalist.firstChild) {
      datalist.firstChild.remove();
    }

    for (let option of options) {
      let element = document.createElement("option");
      element.setAttribute("value", option);
      datalist.appendChild(element);
    }
  }

  await SimpleTest.promiseFocus();

  input.addEventListener("input", inputHandler);

  input.focus();
  synthesizeKey("a");
  synthesizeKey("b");
  synthesizeKey("c");
  is(input.value, "abc", "<input>'s value has to be abc for initial data");
  synthesizeKey("KEY_ArrowDown");
  await waitPopup();

  let values = getMenuEntries();
  is(values.length, 4, "expected count of datalist popup");
  for (let i = 0; i < values.length; i++) {
    is(values[i], DATALIST_DATA[input.value][i], "expected data #" + i);
  }

  let promise = notifyMenuChanged(5);
  synthesizeKey("d");
  is(input.value, "abcd", "<input>'s value has to be abcd for next test");
  synthesizeKey("KEY_ArrowDown");
  await promise;

  values = getMenuEntries();
  is(values.length, 5, "expected count of datalist popup");
  for (let i = 0; i < values.length; i++) {
    is(values[i], DATALIST_DATA[input.value][i], "expected data #" + i);
  }

  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  is(input.value, "abcd1", "<input>'s value has to set abcd1");

  input.removeEventListener("input", inputHandler);
});
</script>
</pre>
</html>