summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/prototype/test/functional/event.html
blob: 526e711f9ac702ab8799daf1b7b811a12f479ce7 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Prototype functional test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../dist/prototype.js"       type="text/javascript"></script>
  
  <style type="text/css" media="screen">
  /* <![CDATA[ */
    body { margin:1em 2em; padding:0; font-size:0.8em }
    hr { width:31.2em; margin:1em 0; text-align:left }
    p { width:30em; margin:0.5em 0; padding:0.3em 0.6em; color:#222; background:#eee; border:1px solid silver; }
    .subtest { margin-top:-0.5em }
    .passed { color:green; border-color:olive }
    .failed { color:firebrick; border-color:firebrick }
    .button { padding:0.2em 0.4em; background:#ccc; border:1px solid #aaa }
    #log { position:absolute; left:35em; top:5em; width:20em; font-size:13px !important }
    h2 { font:normal 1.1em Verdana,Arial,sans-serif; font-style:italic; color:gray; margin-top:-1.2em }
    h2 *, h2 a:visited { color:#444 }
    h2 a:hover { color:blue }
    a:visited { color:blue }
    a:hover { color:red }
  /* ]]> */
  </style>
  
  <script type="text/javascript">
    Element.addMethods({
      passed: function(el, message) {
        el = $(el);
        el.className = 'passed';
        (el.down('span') || el).update(message || 'Test passed!');
      },
      
      failed: function(el, message) {
        el = $(el);
        el.className = 'failed';
        (el.down('span') || el).update(message || 'Test failed');
      }
    });
    
    function log(obj) {
      var line, all = [];
      for (prop in obj) {
        if (typeof obj[prop] == 'function' || /^[A-Z]|[XY]$/.test(prop)) continue;
        line = prop + ": " + Object.inspect(obj[prop]);
        all.push(line.escapeHTML());
      }
      $('log').update(all.join('<br />'));
    }
  </script>
</head>
<body>
  <h1>Prototype functional tests for the Event module</h1>
  
  <div id="log">log empty</div>
  
  <p id="basic">A basic event test - <strong>click here</strong></p>
  <p id="basic_remove" class="subtest"><strong>click</strong> to stop observing the first test</p>
  
  <p id="inline_test" onclick="Event.stop(event); $(this).passed();"><strong>click</strong> to ensure generic Event methods work on inline handlers</p>
  
  <script type="text/javascript">
    var basic_callback = function(e){
      $('basic').passed();
      if ($('basic_remove')) $('basic_remove').show()
      else $('basic').failed()
      log(e);
    }
    $('basic').observe('click', basic_callback)
    $('basic_remove').observe('click', function(e){
      el = $('basic')
      el.passed('This test should now be inactive (try clicking)')
      el.stopObserving('click')
      $('basic_remove').remove()
      log(e);
    }).hide()
  </script>
  
  <p id="basic2"><strong>Scope</strong> test - scope of the handler should be this element</p>
  
  <script type="text/javascript">
    $('basic2').observe('click', function(e) {
      if (this === window) $('basic2').failed('Window scope! (needs scope correction)');
      else this.passed();
      log(e);
    });
  </script>

  <p id="basic3"><strong>Event object</strong> test - should be present as a first argument</p>
  
  <script type="text/javascript">
    $('basic3').observe('click', function(evt) {
      el = $('basic3');
      if (typeof evt != 'object') this.failed('Expected event object for first argument');
      else this.passed('Good first argument');
      log(evt);
    });
  </script>

  <p><a href="#wrong" id="hijack">Hijack link test</a> (preventDefault)</p>
  
  <script type="text/javascript">
    $('hijack').observe('click', function(e){
      el = $(this.parentNode);
      log(e); // this makes it fail?!?
      e.preventDefault();

      setTimeout(function() {
        if (window.location.hash == '#wrong') el.failed('Hijack failed (<a href="' +
            window.location.toString().replace(/#.+$/, '') + '">remove the fragment</a>)')
        else el.passed();
      }, 50)
    })
  </script>
  
  <hr />


  <p>Mouse click:
  <span class="button" id="left">left</span> <span class="button" id="middle">middle</span> <span class="button" id="right">right</span></p>
  
  <script type="text/javascript">
    $w('left middle right').each(function(button) {
      Event.observe(button, 'mousedown', function(e) {
	  if (Event['is' + this.id.capitalize() + 'Click'](e)) this.passed('Squeak!')
	  else this.failed('OH NO!');
	  log(e);
      });
    });
  </script>
  
  <p id="context">Context menu event (tries to prevent default)</p>
  
  <script type="text/javascript">
    $('context').observe('contextmenu', function(e){
      this.passed();
      Event.stop(e);
      log(e);
    })
  </script>

  <p id="target">Event.element() test</p>
  
  <script type="text/javascript">
    $('target').observe('click', function(e) {
      if (e.element() == this && e.target == this) this.passed();
      else this.failed();
      log(e);
    });
  </script>

  <p id="currentTarget"><span>Event.currentTarget test</span></p>
  
  <script type="text/javascript">
    $('currentTarget').observe('click', function(e){
      if (e.currentTarget !== this) this.failed();
      else this.passed();
      log(e);
    })
  </script>
  
  <p id="findElement"><span>Event.findElement() test</span></p>
  
  <script type="text/javascript">
    $('findElement').observe('click', function(e){
      if (e.findElement('p') == this && e.findElement('body') == document.body &&
         e.findElement('foo') == null) this.passed();
      else this.failed();
      log(e);
    })
  </script>
  
  <div id="container"><p id="stop"><strong>Stop propagation</strong> test (bubbling)</p></div>
  
  <script type="text/javascript">
    $('stop').observe('click', function(e){
      e.stop();
      this.passed();
      log(e);
    })
    $('container').observe('click', function(e){
      $('stop').failed();
      log(e);
    })
  </script>
  
  <div>
    <p id="keyup_log"><strong>Keyup</strong> test - focus on the textarea and type</p>
    <textarea id="keyup" class="subtest"></textarea>
  </div>
  
  <script type="text/javascript">
    $('keyup').observe('keyup', function(e){
      el = $('keyup_log');
      el.passed('Key captured: the length is ' + $('keyup').value.length);
      log(e);
    })
  </script>
  
  <p id="bind"><code>bindAsEventListener()</code> test</p>
  
  <script type="text/javascript">
    $('bind').observe('click', function(e, str, arr){
      el = $('bind')
      try {
        if (arguments.length != 3) throw arguments.length + ' arguments: ' + $A(arguments).inspect()
        if (str != 'foo') throw 'wrong string: ' + str
        if (arr.constructor != Array) throw '3rd parameter is not an array'
        el.passed();
      }
      catch (err) { el.failed(err.toString()) }
      log(e);
    }.bindAsEventListener(document.body, 'foo', [1,2,3]))
  </script>
  
  <p id="obj_inspect"><code>Object.inspect(event)</code> test</p>
  
  <script type="text/javascript">
    $('obj_inspect').observe('click', function(e){
      el = $('obj_inspect')
      try { el.passed(Object.inspect(e)) }
      catch (err) { el.failed('Failed! Error thrown') }
      log(e);
    })
  </script>
  
  <p id="addunload">Add unload events</p>
  
  <script type="text/javascript">
    $('addunload').observe('click', function(e){
      if (this._done) return

      window.onunload = function(){ alert('inline unload fired!') }
      Event.observe(window, 'unload', function(){ alert('observed unload fired!') })

      this.update('Registered two unload events, one inline ("onunload") and one regular - try to refresh, both should fire')
      this._done = true
      log(e);
    })
  </script>
</body>
</html>