summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_autocompleteinfo.html
blob: 310b7c9f3d18d939621a363abed3307ffadcc2c0 (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
<!DOCTYPE html>
<html>
<!--
Test getAutocompleteInfo() on <input>
-->
<head>
  <title>Test for getAutocompleteInfo()</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>

<body>
<p id="display"></p>
<div id="content" style="display: none">
  <form>
    <input id="input"/>
  </form>
</div>
<pre id="test">
<script>
"use strict";

var values = [
  // Missing or empty attribute
  [undefined, {}],
  ["", {}],

  // One token
  ["on", {fieldName: "on" }],
  ["On", {fieldName: "on" }],
  ["off", {fieldName: "off" } ],
  ["username", {fieldName: "username" }],
  [" username ", {fieldName: "username" }],
  ["foobar", {}],

  // Two tokens
  ["on off", {}],
  ["off on", {}],
  ["username tel", {}],
  ["tel username ", {}],
  [" username tel ", {}],
  ["tel mobile", {}],
  ["tel shipping", {}],
  ["shipping tel", {addressType: "shipping", fieldName: "tel"}],
  ["shipPING tel", {addressType: "shipping", fieldName: "tel"}],
  ["mobile tel", {contactType: "mobile", fieldName: "tel"}],
  ["  MoBiLe  TeL  ", {contactType: "mobile", fieldName: "tel"}],
  ["XXX tel", {}],
  ["XXX username", {}],

  // Three tokens
  ["billing invalid tel", {}],
  ["___ mobile tel", {}],
  ["mobile foo tel", {}],
  ["mobile tel foo", {}],
  ["tel mobile billing", {}],
  ["billing mobile tel", {addressType: "billing", contactType: "mobile", fieldName: "tel"}],
  ["  BILLing   MoBiLE   tEl  ", {addressType: "billing", contactType: "mobile", fieldName: "tel"}],
  ["billing home tel", {addressType: "billing", contactType: "home", fieldName: "tel"}],

  // Four tokens (invalid)
  ["billing billing mobile tel", {}],

  // Five tokens (invalid)
  ["billing billing billing mobile tel", {}],
];

var autocompleteEnabledTypes = ["hidden", "text", "search", "url", "tel",
                                "email", "password", "date", "time", "number",
                                "range", "color"];
var autocompleteDisabledTypes = ["reset", "submit", "image", "button", "radio",
                                 "checkbox", "file"];

function start() {
  const fieldid = "input";
  var field = document.getElementById(fieldid);
  for (var test of values) {
    if (typeof(test[0]) === "undefined")
      field.removeAttribute("autocomplete");
    else
      field.setAttribute("autocomplete", test[0]);

    var info = field.getAutocompleteInfo();

    is(info.section, "section" in test[1] ? test[1].section : "",
       "Checking autocompleteInfo.section for " + fieldid + ": " + test[0]);
    is(info.addressType, "addressType" in test[1] ? test[1].addressType : "",
       "Checking autocompleteInfo.addressType for " + fieldid + ": " + test[0]);
    is(info.contactType, "contactType" in test[1] ? test[1].contactType : "",
       "Checking autocompleteInfo.contactType for " + fieldid + ": " + test[0]);
    is(info.fieldName, "fieldName" in test[1] ? test[1].fieldName : "",
       "Checking autocompleteInfo.fieldName for " + fieldid + ": " + test[0]);

  }

  for (var type of autocompleteEnabledTypes) {
    testAutocomplete(field, type, true);
  }

  for (var type of autocompleteDisabledTypes) {
    testAutocomplete(field, type, false);
  }
  SimpleTest.finish();
}

function testAutocomplete(aField, aType, aEnabled) {
  aField.type = aType;
  if (aEnabled) {
    ok(aField.getAutocompleteInfo() !== null, "getAutocompleteInfo shouldn't return null");
  } else {
    is(aField.getAutocompleteInfo(), null, "getAutocompleteInfo should return null");
  }
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.forms.autocomplete.experimental", true]]}, start);

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