summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_bug558788-1.html
blob: 94b7a5f00ea558f9b7e2d7ac97e1a6c195adeac5 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=558788
-->
<head>
  <title>Test for Bug 558788</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <style>
    input, textarea { background-color: rgb(0,0,0) !important; }
    :-moz-any(input,textarea):valid   { background-color: rgb(0,255,0) !important; }
    :-moz-any(input,textarea):invalid { background-color: rgb(255,0,0) !important; }
  </style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=558788">Mozilla Bug 558788</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 558788 **/

/**
 * This test checks the behavior of :valid and :invalid pseudo-classes
 * when the user is typing/interacting with the element.
 * Only <input> and <textarea> elements can have there validity changed by an
 * user input.
 */

var gContent = document.getElementById('content');

function checkValidApplies(elmt)
{
  is(window.getComputedStyle(elmt, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid pseudo-class should apply");
}

function checkInvalidApplies(elmt, aTodo)
{
  if (aTodo) {
    todo_is(window.getComputedStyle(elmt, null).getPropertyValue('background-color'),
            "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
    return;
  }
  is(window.getComputedStyle(elmt, null).getPropertyValue('background-color'),
     "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
}

function checkMissing(elementName)
{
  var element = document.createElement(elementName);
  element.required = true;
  gContent.appendChild(element);
  checkInvalidApplies(element);

  element.focus();

  synthesizeKey("a", {});
  checkValidApplies(element);

  synthesizeKey("VK_BACK_SPACE", {});
  checkInvalidApplies(element);

  gContent.removeChild(element);
}

function checkTooLong(elementName)
{
  var element = document.createElement(elementName);
  element.value = "foo";
  element.maxLength = 2;
  gContent.appendChild(element);
  checkInvalidApplies(element, true);

  element.focus();

  synthesizeKey("VK_BACK_SPACE", {});
  checkValidApplies(element);
  gContent.removeChild(element);
}

function checkTextAreaMissing()
{
  checkMissing('textarea');
}

function checkTextAreaTooLong()
{
  checkTooLong('textarea');
}

function checkTextArea()
{
  checkTextAreaMissing();
  checkTextAreaTooLong();
}

function checkInputMissing()
{
  checkMissing('input');
}

function checkInputTooLong()
{
  checkTooLong('input');
}

function checkInputEmail()
{
  var element = document.createElement('input');
  element.type = 'email';
  gContent.appendChild(element);
  checkValidApplies(element);

  element.focus();

  synthesizeKey("a", {});
  checkInvalidApplies(element);

  sendString("@b.c");
  checkValidApplies(element);

  synthesizeKey("VK_BACK_SPACE", {});
  for (var i=0; i<4; ++i) {
    if (i == 1) {
      // a@b is a valid value.
      checkValidApplies(element);
    } else {
      checkInvalidApplies(element);
    }
    synthesizeKey("VK_BACK_SPACE", {});
  }
  checkValidApplies(element);

  gContent.removeChild(element);
}

function checkInputURL()
{
  var element = document.createElement('input');
  element.type = 'url';
  gContent.appendChild(element);
  checkValidApplies(element);

  element.focus();

  synthesizeKey("h", {});
  checkInvalidApplies(element);

  sendString("ttp://mozilla.org");
  checkValidApplies(element);

  for (var i=0; i<13; ++i) {
    synthesizeKey("VK_BACK_SPACE", {});
    checkValidApplies(element);
  }

  synthesizeKey("VK_BACK_SPACE", {});
  for (var i=0; i<4; ++i) {
    checkInvalidApplies(element);
    synthesizeKey("VK_BACK_SPACE", {});
  }
  checkValidApplies(element);

  gContent.removeChild(element);
}

function checkInputPattern()
{
  var element = document.createElement('input');
  element.pattern = "[0-9]*"
  gContent.appendChild(element);
  checkValidApplies(element);

  element.focus();

  synthesizeKey("0", {});
  checkValidApplies(element);

  synthesizeKey("a", {});
  checkInvalidApplies(element);

  synthesizeKey("VK_BACK_SPACE", {});
  checkValidApplies(element);

  synthesizeKey("VK_BACK_SPACE", {});
  checkValidApplies(element);

  gContent.removeChild(element);
}

function checkInput()
{
  checkInputMissing();
  checkInputTooLong();
  checkInputEmail();
  checkInputURL();
  checkInputPattern();
}

checkTextArea();
checkInput();

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