summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/test_datalist_with_caching.html
blob: 8445cb159108a5db3c599ca855e36bcac94da76f (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Form History Autocomplete</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="satchel_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Form History test: form field autocomplete
<p id="display"></p>

<!-- we presumably can't hide the content for this test. -->
<div id="content">

  <!-- normal, basic form -->
  <form id="form1" onsubmit="return false;">
    <input list="suggest" type="text" name="field1">
    <button type="submit">Submit</button>
  </form>

  <datalist id="suggest">
    <option value="First"></option>
    <option value="Second"></option>
    <option value="Secomundo"></option>
  </datalist>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

var input = $_(1, "field1");

function setupFormHistory(aCallback) {
  updateFormHistory([
    { op : "remove" },
    { op : "add", fieldname : "field1", value : "Sec" },
  ], () => {
    spawn_task(aCallback);
  });
}

function setForm(value) {
  input.value = value;
  input.focus();
}

// Restore the form to the default state.
function restoreForm() {
  setForm("");
}

// Check for expected form data.
function checkForm(expectedValue) {
  var formID = input.parentNode.id;
  is(input.value, expectedValue, "Checking " + formID + " input");
}

SimpleTest.waitForExplicitFinish();

var expectingPopup = null;

function expectPopup() {
  info("expecting a popup");
  return new Promise(resolve => {
    expectingPopup = resolve;
  });
}

var testNum = 0;

function popupShownListener() {
  info("popup shown for test " + testNum);
  if (expectingPopup) {
    expectingPopup();
    expectingPopup = null;
  }
  else {
    ok(false, "Autocomplete popup not expected during test " + testNum);
  }
}

function waitForMenuChange(expectedCount) {
  return new Promise(resolve => {
    notifyMenuChanged(expectedCount, null, resolve);
  });
}

registerPopupShownListener(popupShownListener);

function checkMenuEntries(expectedValues) {
  var actualValues = getMenuEntries();
  is(actualValues.length, expectedValues.length, testNum + " Checking length of expected menu");
  for (var i = 0; i < expectedValues.length; i++)
    is(actualValues[i], expectedValues[i], testNum + " Checking menu entry #"+i);
}

function* runTests() {
  testNum++;
  restoreForm();
  doKey("down");
  yield expectPopup();

  checkMenuEntries(["Sec", "First", "Second", "Secomundo"]);
  doKey("down");
  doKey("return");
  checkForm("Sec");

  testNum++;
  restoreForm();
  sendString("Sec");
  doKey("down");
  yield expectPopup();

  testNum++;
  checkMenuEntries(["Sec", "Second", "Secomundo"]);
  sendString("o");
  yield waitForMenuChange(2);

  testNum++;
  checkMenuEntries(["Second", "Secomundo"]);
  doKey("down");
  doKey("return");
  checkForm("Second");
  SimpleTest.finish();
}

function startTest() {
  setupFormHistory(runTests);
}

window.onload = startTest;

</script>
</pre>
</body>
</html>