slider_test.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>script.aculo.us Unit test file</title>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. <script src="../../lib/prototype.js" type="text/javascript"></script>
  8. <script src="../../src/scriptaculous.js" type="text/javascript"></script>
  9. <script src="../../src/unittest.js" type="text/javascript"></script>
  10. <link rel="stylesheet" href="../test.css" type="text/css" />
  11. </head>
  12. <body>
  13. <h1>script.aculo.us Unit test file</h1>
  14. <p>
  15. Tests for slider.js
  16. </p>
  17. <div id="track1" style="width:200px;background-color:#aaa;height:5px;">
  18. <div id="handle1" style="width:15px;height:25px;background-color:#f00;"> </div>
  19. </div>
  20. <div id="track2-vertical" style="height:100px;background-color:#aaa;width:5px;">
  21. <div id="handle2-vertical" style="width:25px;height:10px;background-color:#f00;"> </div>
  22. </div>
  23. <div id="track2-horizontal" style="height:5px;background-color:#aaa;width:100px;">
  24. <div id="handle2-horizontal" style="width:10px;height:25px;background-color:#f00;"> </div>
  25. </div>
  26. <div id="track3" style="width:300px;background-color:#cfb;height:30px;">
  27. <span id="handle3-1">1</span>
  28. <span id="handle3-2">2</span>
  29. <span id="handle3-3">3</span>
  30. </div>
  31. <div id="track4" style="width:300px;position:relative;background-color:#cbf;height:30px;">
  32. <span id="handle4-1" style="top:0;left:0;position:absolute;background-color:#f00;">1</span>
  33. <span id="handle4-2" style="top:0;left:0;position:absolute;background-color:#0f0;">2</span>
  34. <span id="handle4-3" style="top:0;left:0;position:absolute;background-color:#00f;">3</span>
  35. </div>
  36. <div id="track5" style="width:300px;background-color:#cbf;height:30px;position:relative;z-index:0;">
  37. <div id="handle5-1" style="top:0;left:0;position:absolute;background-color:#f00;z-index:2">1</div>
  38. <div id="handle5-2" style="top:0;left:0;position:absolute;background-color:#0f0;z-index:2">2</div>
  39. <div id="handle5-3" style="top:0;left:0;position:absolute;background-color:#00f;z-index:2">3</div>
  40. <div id="span5-1" style="top:0;left:0;position:absolute;background-color:#000;height:20px;z-index:1;"> </div>
  41. <div id="span5-2" style="top:0;left:0;position:absolute;background-color:#444;height:20px;z-index:1;"> </div>
  42. </div>
  43. <div id="track6" style="width:20px;background-color:#cbf;height:100px;position:relative;z-index:0;">
  44. <div id="handle6-1" style="top:0;left:0;height:13px;position:absolute;background-color:#f00;z-index:2">1</div>
  45. <div id="handle6-2" style="top:0;left:0;height:13px;position:absolute;background-color:#0f0;z-index:2">2</div>
  46. <div id="handle6-3" style="top:0;left:0;height:13px;position:absolute;background-color:#00f;z-index:2">3</div>
  47. <div id="span6-1" style="top:0;left:0;position:absolute;background-color:#000;width:20px;z-index:1;"> </div>
  48. <div id="span6-2" style="top:0;left:0;position:absolute;background-color:#444;width:20px;z-index:1;"> </div>
  49. </div>
  50. <div id="track7" style="width:200px;background-color:#cbf;height:30px;position:relative;z-index:0;">
  51. <div id="handle7-1" style="top:0;left:0;position:absolute;background-color:#f88;z-index:2">1</div>
  52. <div id="handle7-2" style="top:0;left:0;position:absolute;background-color:#8f8;z-index:2">2</div>
  53. <div id="handle7-3" style="top:0;left:0;position:absolute;background-color:#88f;z-index:2">3</div>
  54. <div id="span7-1" style="top:0;left:0;position:absolute;background-color:#000;height:20px;z-index:1;"> </div>
  55. <div id="span7-2" style="top:0;left:0;position:absolute;background-color:#444;height:20px;z-index:1;"> </div>
  56. <div id="span7-start" style="top:0;left:0;position:absolute;background-color:#f00;height:20px;z-index:1;"> </div>
  57. <div id="span7-end" style="top:0;left:0;position:absolute;background-color:#00f;height:20px;z-index:1;"> </div>
  58. </div>
  59. <div id="debug"> </div>
  60. <!-- Log output -->
  61. <div id="testlog"> </div>
  62. <!-- Tests follow -->
  63. <script type="text/javascript" language="javascript" charset="utf-8">
  64. // <![CDATA[
  65. var globalValue;
  66. new Test.Unit.Runner({
  67. testSliderBasics: function() { with(this) {
  68. var slider = new Control.Slider('handle1', 'track1');
  69. assertInstanceOf(Control.Slider, slider);
  70. assertEqual('horizontal', slider.axis);
  71. assertEqual(false, slider.disabled);
  72. assertEqual(0, slider.value);
  73. slider.dispose();
  74. }},
  75. testSliderValues: function() { with(this) {
  76. ['horizontal', 'vertical'].each( function(axis) {
  77. var slider = new Control.Slider('handle2-'+axis, 'track2-'+axis, {values:[0.2,0.4,0.6,0.8,1],axis:axis});
  78. assertEqual(axis, slider.axis);
  79. assertEqual(0.2, slider.value);
  80. slider.setValue(0.35);
  81. assertEqual(0.4, slider.value);
  82. slider.setValueBy(0.1);
  83. assertEqual(0.6, slider.value);
  84. slider.setValueBy(-0.6);
  85. assertEqual(0.2, slider.value);
  86. slider.setValue(1);
  87. assertEqual(1, slider.value);
  88. slider.setValue(-2);
  89. assertEqual(0.2, slider.value);
  90. slider.setValue(55555);
  91. assertEqual(1, slider.value);
  92. // leave active to be able to play around with the sliders
  93. // slider.dispose();
  94. });
  95. assertEqual("90px", $('handle2-horizontal').style.left);
  96. assertEqual("90px", $('handle2-vertical').style.top);
  97. }},
  98. testSliderInitialValues: function() { with(this) {
  99. var slider = new Control.Slider('handle1', 'track1',{sliderValue:0.5});
  100. assertEqual(0.5, slider.value);
  101. var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', {
  102. sliderValue:[50,145,170],
  103. values:[50,150,160,170,180],
  104. range:$R(50,180)
  105. });
  106. assertEqual(50, slider.value);
  107. assertEqual(50, slider.values[0]);
  108. assertEqual(150, slider.values[1]);
  109. assertEqual(170, slider.values[2]);
  110. slider.dispose();
  111. var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', {
  112. sliderValue:[50,145,170],
  113. values:[50,150,160,170,180]
  114. });
  115. assertEqual(50, slider.value);
  116. assertEqual(50, slider.values[0]);
  117. assertEqual(150, slider.values[1]);
  118. assertEqual(170, slider.values[2]);
  119. slider.dispose();
  120. var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', {
  121. restricted:true,
  122. sliderValue:[50,145,170],
  123. values:[50,150,160,170,180]
  124. });
  125. assertEqual(50, slider.value);
  126. assertEqual(50, slider.values[0]);
  127. assertEqual(150, slider.values[1]);
  128. assertEqual(170, slider.values[2]);
  129. slider.dispose();
  130. }},
  131. testSliderOnChange: function() { with(this) {
  132. var slider = new Control.Slider('handle1', 'track1', { onChange:function(v){ globalValue = v; } });
  133. slider.setValue(1);
  134. assert(1, globalValue);
  135. assert(1, slider.value);
  136. slider.setDisabled();
  137. slider.setValue(0.5);
  138. assert(1, globalValue);
  139. assert(1, slider.value);
  140. slider.setEnabled();
  141. slider.setValue(0.2);
  142. assert(0.2, globalValue);
  143. assert(0.2, slider.value);
  144. // s.event should be null if setValue is called from script
  145. var slider = new Control.Slider(['handle3-1','handle3-2','handle3-3'], 'track3', {
  146. onChange:function(v, s){ if(!s.event) globalValue = v; } });
  147. slider.setValue(0.5,1);
  148. assertEqual([0,0.5,0].inspect(), globalValue.inspect());
  149. assert(!slider.event);
  150. slider.dispose();
  151. }},
  152. testMultipleHandles: function() { with(this) {
  153. var slider = new Control.Slider(['handle3-1','handle3-2','handle3-3'], 'track3', {range:$R(0,300)});
  154. assertInstanceOf(Control.Slider, slider);
  155. slider.setValue(20, 0);
  156. slider.setValue(50, 1);
  157. slider.setValue(70, 2);
  158. assertEqual(20, slider.values[0]);
  159. assertEqual(50, slider.values[1]);
  160. assertEqual(70, slider.values[2]);
  161. assertEqual("20px", slider.handles[0].style.left);
  162. assertEqual("49px", slider.handles[1].style.left);
  163. assertEqual("68px", slider.handles[2].style.left);
  164. // should change last manipulated handled by -10,
  165. // so check for handle with idx 2
  166. slider.setValueBy(-10);
  167. assertEqual(60, slider.values[2]);
  168. slider.setValueBy(10, 0);
  169. assertEqual(20, slider.values[0]);
  170. slider.setValueBy(10, 1);
  171. assertEqual(60, slider.values[1]);
  172. slider.setValueBy(20, slider.activeHandleIdx);
  173. assertEqual(80, slider.values[1]);
  174. }},
  175. testMultipleHandlesValues: function() { with(this) {
  176. var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', {values:[50,150,160,170,180],range:$R(50,180)});
  177. assertInstanceOf(Control.Slider, slider);
  178. slider.setValue(20, 0);
  179. slider.setValue(150, 2);
  180. slider.setValue(179, 1);
  181. assertEqual(50, slider.values[0]);
  182. assertEqual(150, slider.values[2]);
  183. assertEqual(180, slider.values[1]);
  184. assertEqual("0px", slider.handles[0].style.left);
  185. assertEqual("225px", slider.handles[2].style.left);
  186. assertEqual("293px", slider.handles[1].style.left);
  187. assertEqual($R(50,150).inspect(), slider.getRange().inspect());
  188. assertEqual(30, slider.getRange(1).end-slider.getRange(1).start);
  189. }},
  190. testMultipleHandlesSpans: function() { with(this) {
  191. var slider = new Control.Slider(['handle5-1','handle5-2','handle5-3'], 'track5',
  192. {spans:['span5-1','span5-2'],range:$R(0,300)});
  193. assertInstanceOf(Control.Slider, slider);
  194. slider.setValue(20, 0);
  195. slider.setValue(100, 1);
  196. slider.setValue(150, 2);
  197. assertEqual("20px", $('span5-1').style.left);
  198. assertEqual("78px", $('span5-1').style.width);
  199. assertEqual("98px", $('span5-2').style.left);
  200. assertEqual("49px", $('span5-2').style.width);
  201. slider.setValue(30, 0);
  202. slider.setValue(110, 1);
  203. slider.setValue(90, 2);
  204. assertEqual("29px", $('span5-1').style.left);
  205. assertEqual("59px", $('span5-1').style.width);
  206. assertEqual("88px", $('span5-2').style.left);
  207. assertEqual("20px", $('span5-2').style.width);
  208. slider.dispose();
  209. }},
  210. testMultipleHandlesSpansStartEnd: function() { with(this) {
  211. var slider = new Control.Slider(['handle7-1','handle7-2','handle7-3'], 'track7',
  212. { spans:['span7-1','span7-2'],
  213. startSpan:'span7-start',
  214. endSpan:'span7-end',
  215. range:$R(0,200) });
  216. assertInstanceOf(Control.Slider, slider);
  217. slider.setValue(20, 0);
  218. slider.setValue(100, 1);
  219. slider.setValue(150, 2);
  220. assertEqual("0px", $('span7-start').style.left);
  221. assertEqual("19px", $('span7-start').style.width);
  222. assertEqual("145px", $('span7-end').style.left);
  223. assertEqual("48px", $('span7-end').style.width);
  224. slider.dispose();
  225. }},
  226. testSingleHandleSpansStartEnd: function() { with(this) {
  227. var slider = new Control.Slider('handle7-1', 'track7',
  228. { spans:['span7-1','span7-2'],
  229. startSpan:'span7-start',
  230. endSpan:'span7-end',
  231. range:$R(0,200) });
  232. assertInstanceOf(Control.Slider, slider);
  233. slider.setValue(20, 0);
  234. assertEqual("0px", $('span7-start').style.left);
  235. assertEqual("19px", $('span7-start').style.width);
  236. assertEqual("19px", $('span7-end').style.left);
  237. assertEqual("174px", $('span7-end').style.width);
  238. slider.dispose();
  239. }},
  240. testMultipleHandlesStyles: function() { with(this) {
  241. var slider = new Control.Slider(['handle7-1','handle7-2','handle7-3'], 'track7',
  242. { spans:['span7-1','span7-2'],
  243. startSpan:'span7-start',
  244. endSpan:'span7-end',
  245. range:$R(0,200) });
  246. assertInstanceOf(Control.Slider, slider);
  247. assert(Element.hasClassName('handle7-1','selected'));
  248. assert(!Element.hasClassName('handle7-2','selected'));
  249. assert(!Element.hasClassName('handle7-3','selected'));
  250. slider.setValue(20, 0);
  251. assert(Element.hasClassName('handle7-1','selected'));
  252. assert(!Element.hasClassName('handle7-2','selected'));
  253. assert(!Element.hasClassName('handle7-3','selected'));
  254. slider.setValue(100, 1);
  255. assert(!Element.hasClassName('handle7-1','selected'));
  256. assert(Element.hasClassName('handle7-2','selected'));
  257. assert(!Element.hasClassName('handle7-3','selected'));
  258. slider.setValue(150, 2);
  259. assert(!Element.hasClassName('handle7-1','selected'));
  260. assert(!Element.hasClassName('handle7-2','selected'));
  261. assert(Element.hasClassName('handle7-3','selected'));
  262. slider.dispose();
  263. }},
  264. testMultipleHandlesSpansRestricted: function() { with(this) {
  265. var slider = new Control.Slider(['handle5-1','handle5-2','handle5-3'], 'track5',
  266. {restricted:true,spans:['span5-1','span5-2'],range:$R(0,300)});
  267. assertInstanceOf(Control.Slider, slider);
  268. slider.setValue(20, 0);
  269. slider.setValue(100, 1);
  270. slider.setValue(150, 2);
  271. assertEqual(0, slider.values[0]);
  272. assertEqual(0, slider.values[1]);
  273. assertEqual(150, slider.values[2]);
  274. slider.setValue(150, 2);
  275. slider.setValue(100, 1);
  276. slider.setValue(20, 0);
  277. assertEqual(20, slider.values[0]);
  278. assertEqual(100, slider.values[1]);
  279. assertEqual(150, slider.values[2]);
  280. assertEqual("20px", $('span5-1').style.left);
  281. assertEqual("78px", $('span5-1').style.width);
  282. assertEqual("98px", $('span5-2').style.left);
  283. assertEqual("49px", $('span5-2').style.width);
  284. slider.setValue(30, 0);
  285. slider.setValue(110, 1);
  286. slider.setValue(90, 2);
  287. assertEqual(30, slider.values[0]);
  288. assertEqual(110, slider.values[1]);
  289. assertEqual(110, slider.values[2], '???');
  290. assertEqual("29px", $('span5-1').style.left);
  291. assertEqual("78px", $('span5-1').style.width);
  292. assertEqual("107px", $('span5-2').style.left);
  293. assertEqual("0px", $('span5-2').style.width);
  294. slider.dispose();
  295. }},
  296. testMultipleHandlesSpansVertical: function() { with(this) {
  297. var slider = new Control.Slider(['handle6-1','handle6-2','handle6-3'], 'track6', {axis:'vertical',spans:['span6-1','span6-2'],range:$R(0,100)});
  298. assertInstanceOf(Control.Slider, slider);
  299. slider.setValue(20, 0);
  300. slider.setValue(80, 1);
  301. slider.setValue(90, 2);
  302. assertEqual("17px", $('span6-1').style.top);
  303. assertEqual("52px", $('span6-1').style.height);
  304. assertEqual("70px", $('span6-2').style.top);
  305. assertEqual("9px", $('span6-2').style.height);
  306. slider.setValue(30, 0);
  307. slider.setValue(20, 1);
  308. slider.setValue(95, 2);
  309. assertEqual("17px", $('span6-1').style.top);
  310. assertEqual("9px", $('span6-1').style.height);
  311. assertEqual("26px", $('span6-2').style.top);
  312. assertEqual("57px", $('span6-2').style.height);
  313. }},
  314. testRange: function() { with(this) {
  315. var slider = new Control.Slider('handle1','track1');
  316. assertEqual(0, slider.value);
  317. slider.setValue(1);
  318. assertEqual("185px", $('handle1').style.left);
  319. slider.dispose();
  320. var slider = new Control.Slider('handle1','track1',{range:$R(10,20)});
  321. assertEqual(10, slider.value);
  322. assertEqual("0px", $('handle1').style.left);
  323. slider.setValue(15);
  324. assertEqual("93px", $('handle1').style.left);
  325. slider.setValue(20);
  326. assertEqual("185px", $('handle1').style.left);
  327. slider.dispose();
  328. }},
  329. // test for #3732
  330. testRangeValues: function() { with(this) {
  331. // test for non-zero starting range
  332. var slider = new Control.Slider('handle1','track1',{
  333. range:$R(1,3), values:[1,2,3]
  334. });
  335. assertEqual(1, slider.value);
  336. assertEqual("0px", $('handle1').style.left);
  337. slider.setValue(2);
  338. assertEqual("93px", $('handle1').style.left);
  339. slider.setValue(3);
  340. assertEqual("185px", $('handle1').style.left);
  341. slider.dispose();
  342. // test zero-starting range
  343. var slider = new Control.Slider('handle1','track1',{
  344. range:$R(0,2), values:[0,1,2]
  345. });
  346. assertEqual(0, slider.value);
  347. assertEqual("0px", $('handle1').style.left);
  348. slider.setValue(1);
  349. assertEqual("93px", $('handle1').style.left);
  350. slider.setValue(2);
  351. assertEqual("185px", $('handle1').style.left);
  352. slider.dispose();
  353. }}
  354. }, "testlog");
  355. // ]]>
  356. </script>
  357. </body>
  358. </html>