summaryrefslogtreecommitdiff
path: root/dom/html/test/test_bug596511.html
blob: e539c5b73ec33b07320eb76eb05818e13d352bcc (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=596511
-->
<head>
  <title>Test for Bug 596511</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <style>
    select:valid { background-color: green; }
    select:invalid { background-color: red; }
  </style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=596511">Mozilla Bug 596511</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 596511 **/

function checkNotSufferingFromBeingMissing(element, aTodo)
{
  if (aTodo) {
    ok = todo;
    is = todo_is;
  }

  ok(!element.validity.valueMissing,
    "Element should not suffer from value missing");
  ok(element.validity.valid, "Element should be valid");
  ok(element.checkValidity(), "Element should be valid");

  is(element.validationMessage, "",
    "Validation message should be the empty string");

  is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
     "rgb(0, 128, 0)", ":valid pseudo-class should apply");

  if (aTodo) {
    ok = SimpleTest.ok;
    is = SimpleTest.is;
  }
}

function checkSufferingFromBeingMissing(element, aTodo)
{
  if (aTodo) {
    ok = todo;
    is = todo_is;
  }

  ok(element.validity.valueMissing, "Element should suffer from value missing");
  ok(!element.validity.valid, "Element should not be valid");
  ok(!element.checkValidity(), "Element should not be valid");

  is(element.validationMessage, "Please select an item in the list.",
     "Validation message is wrong");

  is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
     "rgb(255, 0, 0)", ":invalid pseudo-class should apply");

  if (aTodo) {
    ok = SimpleTest.ok;
    is = SimpleTest.is;
  }
}

function checkRequiredAttribute(element)
{
  ok('required' in element, "select should have a required attribute");

  ok(!element.required, "select required attribute should be disabled");
  is(element.getAttribute('required'), null,
    "select required attribute should be disabled");

  element.required = true;
  ok(element.required, "select required attribute should be enabled");
  isnot(element.getAttribute('required'), null,
    "select required attribute should be enabled");

  element.removeAttribute('required');
  element.setAttribute('required', '');
  ok(element.required, "select required attribute should be enabled");
  isnot(element.getAttribute('required'), null,
    "select required attribute should be enabled");

  element.removeAttribute('required');
  ok(!element.required, "select required attribute should be disabled");
  is(element.getAttribute('required'), null,
    "select required attribute should be disabled");
}

function checkRequiredAndOptionalSelectors(element)
{
  is(document.querySelector("select:optional"), element,
     "select should be optional");
  is(document.querySelector("select:required"), null,
     "select shouldn't be required");

  element.required = true;

  is(document.querySelector("select:optional"), null,
     "select shouldn't be optional");
  is(document.querySelector("select:required"), element,
     "select should be required");

  element.required = false;
}

function checkInvalidWhenValueMissing(element)
{
  checkNotSufferingFromBeingMissing(select);

  element.required = true;
  checkSufferingFromBeingMissing(select);

  /**
   * Non-multiple and size=1.
   */
  select.appendChild(new Option());
  checkSufferingFromBeingMissing(select);

  // When removing the required attribute, element should not be invalid.
  element.required = false;
  checkNotSufferingFromBeingMissing(select);

  element.required = true;
  select.options[0].textContent = "foo";
  // TODO: having that working would require us to add a mutation observer on
  // the select element.
  checkNotSufferingFromBeingMissing(select, true);

  select.remove(0);
  checkSufferingFromBeingMissing(select);

  select.add(new Option("foo", "foo"), null);
  checkNotSufferingFromBeingMissing(select);

  select.add(new Option(), null);
  checkNotSufferingFromBeingMissing(select);

  select.options[1].selected = true;
  checkSufferingFromBeingMissing(select);

  select.selectedIndex = 0;
  checkNotSufferingFromBeingMissing(select);

  select.selectedIndex = 1;
  checkSufferingFromBeingMissing(select);

  select.remove(1);
  checkNotSufferingFromBeingMissing(select);

  select.options[0].disabled = true;
  // TODO: having that working would require us to add a mutation observer on
  // the select element.
  checkSufferingFromBeingMissing(select, true);

  select.options[0].disabled = false
  select.remove(0);
  checkSufferingFromBeingMissing(select);

  var option = new Option("foo", "foo");
  option.disabled = true;
  select.add(option, null);
  select.add(new Option("bar"), null);
  option.selected = true;
  checkSufferingFromBeingMissing(select);

  select.remove(0);
  select.remove(0);

  /**
   * Non-multiple and size > 1.
   * Everything should be the same except moving the selection.
   */
  select.multiple = false;
  select.size = 4;
  checkSufferingFromBeingMissing(select);

  select.add(new Option("", "", true), null);
  checkSufferingFromBeingMissing(select);

  select.add(new Option("foo", "foo"), null);
  select.remove(0);
  checkSufferingFromBeingMissing(select);

  select.options[0].selected = true;
  checkNotSufferingFromBeingMissing(select);

  select.remove(0);

  /**
   * Multiple, any size.
   * We can select more than one element and at least needs a value.
   */
  select.multiple = true;
  select.size = 4;
  checkSufferingFromBeingMissing(select);

  select.add(new Option("", "", true), null);
  checkSufferingFromBeingMissing(select);

  select.add(new Option("", "", true), null);
  checkSufferingFromBeingMissing(select);

  select.add(new Option("foo"), null);
  checkSufferingFromBeingMissing(select);

  select.options[2].selected = true;
  checkNotSufferingFromBeingMissing(select);
}

var select = document.createElement("select");
var content = document.getElementById('content');
content.appendChild(select);

checkRequiredAttribute(select);
checkRequiredAndOptionalSelectors(select);
checkInvalidWhenValueMissing(select);

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