Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/view/helpers/prototype_engine.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * PrototypeEngine TestCase | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP : Rapid Development Framework <http://www.cakephp.org/> | |
8 * Copyright 2006-2010, Cake Software Foundation, Inc. | |
9 * 1785 E. Sahara Avenue, Suite 490-204 | |
10 * Las Vegas, Nevada 89104 | |
11 * | |
12 * Licensed under The MIT License | |
13 * Redistributions of files must retain the above copyright notice. | |
14 * | |
15 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc. | |
16 * @link http://cakephp.org CakePHP Project | |
17 * @package cake.tests | |
18 * @subpackage cake.tests.cases.views.helpers | |
19 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
20 */ | |
21 App::import('Helper', array('Html', 'Js', 'PrototypeEngine')); | |
22 | |
23 class PrototypeEngineHelperTestCase extends CakeTestCase { | |
24 /** | |
25 * startTest | |
26 * | |
27 * @return void | |
28 */ | |
29 function startTest() { | |
30 $this->Proto =& new PrototypeEngineHelper(); | |
31 } | |
32 | |
33 /** | |
34 * end test | |
35 * | |
36 * @return void | |
37 */ | |
38 function endTest() { | |
39 unset($this->Proto); | |
40 } | |
41 | |
42 /** | |
43 * test selector method | |
44 * | |
45 * @return void | |
46 */ | |
47 function testSelector() { | |
48 $result = $this->Proto->get('#content'); | |
49 $this->assertEqual($result, $this->Proto); | |
50 $this->assertEqual($this->Proto->selection, '$("content")'); | |
51 | |
52 $result = $this->Proto->get('a .remove'); | |
53 $this->assertEqual($result, $this->Proto); | |
54 $this->assertEqual($this->Proto->selection, '$$("a .remove")'); | |
55 | |
56 $result = $this->Proto->get('document'); | |
57 $this->assertEqual($result, $this->Proto); | |
58 $this->assertEqual($this->Proto->selection, "$(document)"); | |
59 | |
60 $result = $this->Proto->get('window'); | |
61 $this->assertEqual($result, $this->Proto); | |
62 $this->assertEqual($this->Proto->selection, "$(window)"); | |
63 | |
64 $result = $this->Proto->get('ul'); | |
65 $this->assertEqual($result, $this->Proto); | |
66 $this->assertEqual($this->Proto->selection, '$$("ul")'); | |
67 | |
68 $result = $this->Proto->get('#some_long-id.class'); | |
69 $this->assertEqual($result, $this->Proto); | |
70 $this->assertEqual($this->Proto->selection, '$$("#some_long-id.class")'); | |
71 } | |
72 | |
73 /** | |
74 * test event binding | |
75 * | |
76 * @return void | |
77 */ | |
78 function testEvent() { | |
79 $this->Proto->get('#myLink'); | |
80 $result = $this->Proto->event('click', 'doClick', array('wrap' => false)); | |
81 $expected = '$("myLink").observe("click", doClick);'; | |
82 $this->assertEqual($result, $expected); | |
83 | |
84 $result = $this->Proto->event('click', 'Element.hide(this);', array('stop' => false)); | |
85 $expected = '$("myLink").observe("click", function (event) {Element.hide(this);});'; | |
86 $this->assertEqual($result, $expected); | |
87 | |
88 $result = $this->Proto->event('click', 'Element.hide(this);'); | |
89 $expected = "\$(\"myLink\").observe(\"click\", function (event) {event.stop();\nElement.hide(this);});"; | |
90 $this->assertEqual($result, $expected); | |
91 } | |
92 | |
93 /** | |
94 * test dom ready event creation | |
95 * | |
96 * @return void | |
97 */ | |
98 function testDomReady() { | |
99 $result = $this->Proto->domReady('foo.name = "bar";'); | |
100 $expected = 'document.observe("dom:loaded", function (event) {foo.name = "bar";});'; | |
101 $this->assertEqual($result, $expected); | |
102 } | |
103 | |
104 /** | |
105 * test Each method | |
106 * | |
107 * @return void | |
108 */ | |
109 function testEach() { | |
110 $this->Proto->get('#foo li'); | |
111 $result = $this->Proto->each('item.hide();'); | |
112 $expected = '$$("#foo li").each(function (item, index) {item.hide();});'; | |
113 $this->assertEqual($result, $expected); | |
114 } | |
115 | |
116 /** | |
117 * test Effect generation | |
118 * | |
119 * @return void | |
120 */ | |
121 function testEffect() { | |
122 $this->Proto->get('#foo'); | |
123 $result = $this->Proto->effect('show'); | |
124 $expected = '$("foo").show();'; | |
125 $this->assertEqual($result, $expected); | |
126 | |
127 $result = $this->Proto->effect('hide'); | |
128 $expected = '$("foo").hide();'; | |
129 $this->assertEqual($result, $expected); | |
130 | |
131 $result = $this->Proto->effect('fadeIn'); | |
132 $expected = '$("foo").appear();'; | |
133 $this->assertEqual($result, $expected); | |
134 | |
135 $result = $this->Proto->effect('fadeIn', array('speed' => 'fast')); | |
136 $expected = '$("foo").appear({duration:0.50000000000});'; | |
137 $this->assertEqual($result, $expected); | |
138 | |
139 $result = $this->Proto->effect('fadeIn', array('speed' => 'slow')); | |
140 $expected = '$("foo").appear({duration:2});'; | |
141 $this->assertEqual($result, $expected); | |
142 | |
143 $result = $this->Proto->effect('fadeOut'); | |
144 $expected = '$("foo").fade();'; | |
145 $this->assertEqual($result, $expected); | |
146 | |
147 $result = $this->Proto->effect('fadeOut', array('speed' => 'fast')); | |
148 $expected = '$("foo").fade({duration:0.50000000000});'; | |
149 $this->assertEqual($result, $expected); | |
150 | |
151 $result = $this->Proto->effect('fadeOut', array('speed' => 'slow')); | |
152 $expected = '$("foo").fade({duration:2});'; | |
153 $this->assertEqual($result, $expected); | |
154 | |
155 $result = $this->Proto->effect('slideIn'); | |
156 $expected = 'Effect.slideDown($("foo"));'; | |
157 $this->assertEqual($result, $expected); | |
158 | |
159 $result = $this->Proto->effect('slideOut'); | |
160 $expected = 'Effect.slideUp($("foo"));'; | |
161 $this->assertEqual($result, $expected); | |
162 | |
163 $result = $this->Proto->effect('slideOut', array('speed' => 'fast')); | |
164 $expected = 'Effect.slideUp($("foo"), {duration:0.50000000000});'; | |
165 $this->assertEqual($result, $expected); | |
166 | |
167 $result = $this->Proto->effect('slideOut', array('speed' => 'slow')); | |
168 $expected = 'Effect.slideUp($("foo"), {duration:2});'; | |
169 $this->assertEqual($result, $expected); | |
170 } | |
171 | |
172 /** | |
173 * Test Request Generation | |
174 * | |
175 * @return void | |
176 */ | |
177 function testRequest() { | |
178 $result = $this->Proto->request(array('controller' => 'posts', 'action' => 'view', 1)); | |
179 $expected = 'var jsRequest = new Ajax.Request("/posts/view/1");'; | |
180 $this->assertEqual($result, $expected); | |
181 | |
182 $result = $this->Proto->request('/posts/view/1', array( | |
183 'method' => 'post', | |
184 'complete' => 'doComplete', | |
185 'before' => 'doBefore', | |
186 'success' => 'doSuccess', | |
187 'error' => 'doError', | |
188 'data' => array('name' => 'jim', 'height' => '185cm'), | |
189 'wrapCallbacks' => false | |
190 )); | |
191 $expected = 'var jsRequest = new Ajax.Request("/posts/view/1", {method:"post", onComplete:doComplete, onCreate:doBefore, onFailure:doError, onSuccess:doSuccess, parameters:{"name":"jim","height":"185cm"}});'; | |
192 $this->assertEqual($result, $expected); | |
193 | |
194 $result = $this->Proto->request('/posts/view/1', array('update' => 'content')); | |
195 $expected = 'var jsRequest = new Ajax.Updater("content", "/posts/view/1");'; | |
196 $this->assertEqual($result, $expected); | |
197 | |
198 $result = $this->Proto->request('/people/edit/1', array( | |
199 'method' => 'post', | |
200 'complete' => 'doSuccess', | |
201 'update' => '#update-zone', | |
202 'wrapCallbacks' => false | |
203 )); | |
204 $expected = 'var jsRequest = new Ajax.Updater("update-zone", "/people/edit/1", {method:"post", onComplete:doSuccess});'; | |
205 $this->assertEqual($result, $expected); | |
206 | |
207 $result = $this->Proto->request('/people/edit/1', array( | |
208 'method' => 'post', | |
209 'complete' => 'doSuccess', | |
210 'error' => 'handleError', | |
211 'type' => 'json', | |
212 'data' => array('name' => 'jim', 'height' => '185cm'), | |
213 'wrapCallbacks' => false | |
214 )); | |
215 $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:doSuccess, onFailure:handleError, parameters:{"name":"jim","height":"185cm"}});'; | |
216 $this->assertEqual($result, $expected); | |
217 | |
218 $result = $this->Proto->request('/people/edit/1', array( | |
219 'method' => 'post', | |
220 'complete' => 'doSuccess', | |
221 'error' => 'handleError', | |
222 'type' => 'json', | |
223 'data' => '$("element").serialize()', | |
224 'dataExpression' => true, | |
225 'wrapCallbacks' => false | |
226 )); | |
227 $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:doSuccess, onFailure:handleError, parameters:$("element").serialize()});'; | |
228 $this->assertEqual($result, $expected); | |
229 | |
230 $result = $this->Proto->request('/people/edit/1', array( | |
231 'method' => 'post', | |
232 'before' => 'doBefore();', | |
233 'success' => 'doSuccess();', | |
234 'complete' => 'doComplete();', | |
235 'error' => 'handleError();', | |
236 )); | |
237 $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});'; | |
238 $this->assertEqual($result, $expected); | |
239 | |
240 $result = $this->Proto->request('/people/edit/1', array( | |
241 'async' => false, | |
242 'method' => 'post', | |
243 'before' => 'doBefore();', | |
244 'success' => 'doSuccess();', | |
245 'complete' => 'doComplete();', | |
246 'error' => 'handleError();', | |
247 )); | |
248 $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {asynchronous:false, method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});'; | |
249 $this->assertEqual($result, $expected); | |
250 | |
251 $this->Proto->get('#submit'); | |
252 $result = $this->Proto->request('/users/login', array( | |
253 'before' => 'login.create(event)', | |
254 'complete' => 'login.complete(event)', | |
255 'update' => 'auth', | |
256 'data' => $this->Proto->serializeForm(array('isForm' => false, 'inline' => true)), | |
257 'dataExpression' => true | |
258 )); | |
259 $this->assertTrue(strpos($result, '$($("submit").form).serialize()') > 0); | |
260 $this->assertFalse(strpos($result, 'parameters:function () {$($("submit").form).serialize()}') > 0); | |
261 } | |
262 | |
263 /** | |
264 * test sortable list generation | |
265 * | |
266 * @return void | |
267 */ | |
268 function testSortable() { | |
269 $this->Proto->get('#myList'); | |
270 $result = $this->Proto->sortable(array( | |
271 'complete' => 'onComplete', | |
272 'sort' => 'onSort', | |
273 'wrapCallbacks' => false | |
274 )); | |
275 $expected = 'var jsSortable = Sortable.create($("myList"), {onChange:onSort, onUpdate:onComplete});'; | |
276 $this->assertEqual($result, $expected); | |
277 } | |
278 | |
279 /** | |
280 * test drag() method. Scriptaculous lacks the ability to take an Array of Elements | |
281 * in new Drag() when selection is a multiple type. Iterate over the array. | |
282 * | |
283 * @return void | |
284 */ | |
285 function testDrag() { | |
286 $this->Proto->get('#element'); | |
287 $result = $this->Proto->drag(array( | |
288 'start' => 'onStart', | |
289 'drag' => 'onDrag', | |
290 'stop' => 'onStop', | |
291 'snapGrid' => array(10, 10), | |
292 'wrapCallbacks' => false | |
293 )); | |
294 $expected = 'var jsDrag = new Draggable($("element"), {onDrag:onDrag, onEnd:onStop, onStart:onStart, snap:[10,10]});'; | |
295 $this->assertEqual($result, $expected); | |
296 | |
297 $this->Proto->get('div.dragger'); | |
298 $result = $this->Proto->drag(array( | |
299 'start' => 'onStart', | |
300 'drag' => 'onDrag', | |
301 'stop' => 'onStop', | |
302 'snapGrid' => array(10, 10), | |
303 'wrapCallbacks' => false | |
304 )); | |
305 $expected = '$$("div.dragger").each(function (item, index) {new Draggable(item, {onDrag:onDrag, onEnd:onStop, onStart:onStart, snap:[10,10]});});'; | |
306 $this->assertEqual($result, $expected); | |
307 } | |
308 | |
309 /** | |
310 * test drop() method | |
311 * | |
312 * @return void | |
313 */ | |
314 function testDrop() { | |
315 $this->Proto->get('#element'); | |
316 $result = $this->Proto->drop(array( | |
317 'hover' => 'onHover', | |
318 'drop' => 'onDrop', | |
319 'accept' => '.drag-me', | |
320 'wrapCallbacks' => false | |
321 )); | |
322 $expected = 'Droppables.add($("element"), {accept:".drag-me", onDrop:onDrop, onHover:onHover});'; | |
323 $this->assertEqual($result, $expected); | |
324 } | |
325 | |
326 /** | |
327 * ensure that slider() method behaves properly | |
328 * | |
329 * @return void | |
330 */ | |
331 function testSlider() { | |
332 $this->Proto->get('#element'); | |
333 $result = $this->Proto->slider(array( | |
334 'handle' => '#handle', | |
335 'direction' => 'horizontal', | |
336 'change' => 'onChange', | |
337 'complete' => 'onComplete', | |
338 'value' => 4, | |
339 'wrapCallbacks' => false | |
340 )); | |
341 $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {axis:"horizontal", onChange:onComplete, onSlide:onChange, sliderValue:4});'; | |
342 $this->assertEqual($result, $expected); | |
343 | |
344 $this->Proto->get('#element'); | |
345 $result = $this->Proto->slider(array( | |
346 'handle' => '#handle', | |
347 'change' => 'change();', | |
348 'complete' => 'complete();', | |
349 'value' => 4, | |
350 'min' => 10, | |
351 'max' => 100 | |
352 )); | |
353 $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {onChange:function (value) {complete();}, onSlide:function (value) {change();}, range:$R(10,100), sliderValue:4});'; | |
354 $this->assertEqual($result, $expected); | |
355 } | |
356 | |
357 /** | |
358 * test the serializeForm implementation. | |
359 * | |
360 * @return void | |
361 */ | |
362 function testSerializeForm() { | |
363 $this->Proto->get('#element'); | |
364 $result = $this->Proto->serializeForm(array('isForm' => true)); | |
365 $expected = '$("element").serialize();'; | |
366 $this->assertEqual($result, $expected); | |
367 | |
368 $result = $this->Proto->serializeForm(array('isForm' => true, 'inline' => true)); | |
369 $expected = '$("element").serialize()'; | |
370 $this->assertEqual($result, $expected); | |
371 | |
372 $result = $this->Proto->serializeForm(array('isForm' => false)); | |
373 $expected = '$($("element").form).serialize();'; | |
374 $this->assertEqual($result, $expected); | |
375 | |
376 $result = $this->Proto->serializeForm(array('isForm' => false, 'inline' => true)); | |
377 $expected = '$($("element").form).serialize()'; | |
378 $this->assertEqual($result, $expected); | |
379 } | |
380 } |