PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/index.html

https://code.google.com/
HTML | 1420 lines | 979 code | 441 blank | 0 comment | 0 complexity | 7291da8faf65f732cf2ad0414623ae33 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <link rel="stylesheet" href="qunit.css" type="text/css" media="screen"/>
  6. <script type="text/javascript" src="qunit.js"></script>
  7. <script type="text/javascript" src="jquery.js"></script>
  8. <script type="text/javascript" src="../build/dat.gui.min.js"></script>
  9. <script type="text/javascript">
  10. $.noConflict();
  11. jQuery(document).ready(function($) {
  12. var math = dat.color.math;
  13. var interpret = dat.color.interpret;
  14. var Color = dat.color.Color;
  15. var dom = dat.dom.dom;
  16. var Controller = dat.controllers.Controller;
  17. var BooleanController = dat.controllers.BooleanController;
  18. var OptionController = dat.controllers.OptionController;
  19. var StringController = dat.controllers.StringController;
  20. var NumberController = dat.controllers.NumberController;
  21. var NumberControllerBox = dat.controllers.NumberControllerBox;
  22. var NumberControllerSlider = dat.controllers.NumberControllerSlider;
  23. var FunctionController = dat.controllers.FunctionController;
  24. var ColorController = dat.controllers.ColorController;
  25. var GUI = dat.gui.GUI;
  26. module("Color Math");
  27. test("rgb_to_hex", function() {
  28. equal(
  29. math.rgb_to_hex(100, 255, 20),
  30. 0x64ff14
  31. );
  32. });
  33. test("component_from_hex", function() {
  34. equal(
  35. math.component_from_hex(0xff3366, 0),
  36. 0x66,
  37. 'get blue'
  38. );
  39. equal(
  40. math.component_from_hex(0xff3366, 1),
  41. 0x33,
  42. 'get green'
  43. );
  44. equal(
  45. math.component_from_hex(0xff3366, 2),
  46. 0xff,
  47. 'get red'
  48. );
  49. });
  50. test("hex_with_component", function() {
  51. equal(
  52. math.hex_with_component(0x002203, 0, 0x32),
  53. 0x002232,
  54. 'replace blue'
  55. );
  56. equal(
  57. math.hex_with_component(0x002203, 1, 0x32),
  58. 0x003203,
  59. 'replace green'
  60. );
  61. equal(
  62. math.hex_with_component(0x002203, 2, 0x32),
  63. 0x322203,
  64. 'replace red'
  65. );
  66. });
  67. test("rgb_to_hsv", function() {
  68. match(
  69. math.rgb_to_hsv(173, 52, 141),
  70. {
  71. h: 315.86776859504135,
  72. s: 0.6994219653179191,
  73. v: 0.6784313725490196
  74. });
  75. match(
  76. math.rgb_to_hsv(10, 10, 10),
  77. {
  78. h: NaN,
  79. s: 0,
  80. v: 0.0392156862745098
  81. }, 'grayscale');
  82. match(
  83. math.rgb_to_hsv(0, 0, 0),
  84. {
  85. h: NaN,
  86. s: 0,
  87. v: 0
  88. }, 'black');
  89. });
  90. test("hsv_to_rgb", function() {
  91. match(
  92. math.hsv_to_rgb(255, 0.85, 0.46),
  93. {
  94. r: 42.52125000000001,
  95. g: 17.595000000000006,
  96. b: 117.30000000000001
  97. });
  98. match(
  99. math.hsv_to_rgb(0, 0, 0.46),
  100. {
  101. r: 117.30000000000001,
  102. g: 117.30000000000001,
  103. b: 117.30000000000001
  104. }, 'grayscale');
  105. });
  106. module("Color Interpretations");
  107. test("CSS Strings", function() {
  108. match(
  109. interpret('#ccc'),
  110. {
  111. hex: 0xcccccc,
  112. space: 'HEX',
  113. conversionName: 'THREE_CHAR_HEX'
  114. },
  115. 'THREE_CHAR_HEX'
  116. );
  117. match(
  118. interpret('#f09'),
  119. {
  120. hex: 0xff0099,
  121. space: 'HEX',
  122. conversionName: 'THREE_CHAR_HEX'
  123. },
  124. 'THREE_CHAR_HEX'
  125. );
  126. match(
  127. interpret('#0f93cd'),
  128. {
  129. hex: 0x0f93cd,
  130. space: 'HEX',
  131. conversionName: 'SIX_CHAR_HEX'
  132. },
  133. 'SIX_CHAR_HEX'
  134. );
  135. match(
  136. interpret('rgba(255,10,200,0.3)'),
  137. {
  138. r: 255,
  139. g: 10,
  140. b: 200,
  141. a: 0.3,
  142. space: 'RGB',
  143. conversionName: 'CSS_RGBA'
  144. },
  145. 'CSS_RGBA'
  146. );
  147. match(
  148. interpret('rgb(255,10,200)'),
  149. {
  150. r: 255,
  151. g: 10,
  152. b: 200,
  153. space: 'RGB',
  154. conversionName: 'CSS_RGB'
  155. },
  156. 'CSS_RGB'
  157. );
  158. });
  159. test("Other", function() {
  160. match(
  161. interpret(0xff3322),
  162. {
  163. hex: 0xff3322,
  164. space: 'HEX',
  165. conversionName: 'HEX'
  166. },
  167. 'HEX'
  168. );
  169. match(
  170. interpret([255, 255, 0]),
  171. {
  172. r: 255,
  173. g: 255,
  174. b: 0,
  175. space: 'RGB',
  176. conversionName: 'RGB_ARRAY'
  177. },
  178. 'RGB_ARRAY'
  179. );
  180. match(
  181. interpret([0, 110, 255, 0.3]),
  182. {
  183. r: 0,
  184. g: 110,
  185. b: 255,
  186. a: 0.3,
  187. space: 'RGB',
  188. conversionName: 'RGBA_ARRAY'
  189. },
  190. 'RGBA_ARRAY'
  191. );
  192. match(
  193. interpret({
  194. r: 255,
  195. g: 255,
  196. b: 200
  197. }),
  198. {
  199. r: 255,
  200. g: 255,
  201. b: 200,
  202. space: 'RGB',
  203. conversionName: 'RGB_OBJ'
  204. },
  205. 'RGB_OBJ'
  206. );
  207. match(
  208. interpret({
  209. r: 255,
  210. g: 255,
  211. b: 200,
  212. a: 0.2
  213. }),
  214. {
  215. r: 255,
  216. g: 255,
  217. b: 200,
  218. a: 0.2,
  219. space: 'RGB',
  220. conversionName: 'RGBA_OBJ'
  221. },
  222. 'RGBA_OBJ'
  223. );
  224. match(
  225. interpret({
  226. h: 360,
  227. s: 1,
  228. v: 0.5
  229. }),
  230. {
  231. h: 360,
  232. s: 1,
  233. v: 0.5,
  234. space: 'HSV',
  235. conversionName: 'HSV_OBJ'
  236. },
  237. 'HSV_OBJ'
  238. );
  239. match(
  240. interpret({
  241. h: 360,
  242. s: 1,
  243. v: 0.5,
  244. a: 0.8
  245. }),
  246. {
  247. h: 360,
  248. s: 1,
  249. v: 0.5,
  250. a: 0.8,
  251. space: 'HSV',
  252. conversionName: 'HSVA_OBJ'
  253. },
  254. 'HSVA_OBJ'
  255. );
  256. match(
  257. interpret('Failuuureeee'),
  258. false,
  259. 'FAIL'
  260. );
  261. });
  262. module("Color Objects");
  263. test("Creation", function() {
  264. var c = new Color(255, 100, 20, 0.3);
  265. equal(c.r, 255, 'red');
  266. equal(c.g, 100, 'green');
  267. equal(c.b, 20, 'blue');
  268. equal(c.a, 0.3, 'alpha');
  269. equal(c.hex, 0xff6414, 'hex');
  270. equal(Math.round(c.h), 20, 'hue');
  271. equal(Math.round(c.s * 100), 92, 'saturation');
  272. equal(Math.round(c.v * 100), 100, 'value');
  273. });
  274. test("RGB Modification", function() {
  275. var c = new Color(255, 100, 20, 0.3);
  276. c.r -= 100;
  277. equal(c.r, 155, 'green');
  278. equal(c.g, 100, 'green');
  279. equal(c.b, 20, 'blue');
  280. equal(c.a, 0.3, 'alpha');
  281. equal(c.hex, 0x9b6414, 'hex');
  282. equal(Math.round(c.h), 36, 'hue');
  283. equal(Math.round(c.s * 100), 87, 'saturation');
  284. equal(Math.round(c.v * 100), 61, 'value');
  285. });
  286. test("RGB Modification", function() {
  287. var c = new Color(255, 100, 20, 0.3);
  288. c.r -= 100;
  289. equal(c.r, 155, 'green');
  290. equal(c.g, 100, 'green');
  291. equal(c.b, 20, 'blue');
  292. equal(c.a, 0.3, 'alpha');
  293. equal(c.hex, 0x9b6414, 'hex');
  294. equal(Math.round(c.h), 36, 'hue');
  295. equal(Math.round(c.s * 100), 87, 'saturation');
  296. equal(Math.round(c.v * 100), 61, 'value');
  297. });
  298. test("Setting RGB, Modifying HSV", function() {
  299. var c = new Color(255, 0, 100);
  300. c.s = 1;
  301. equal(c.r, 255);
  302. equal(c.g, 0);
  303. equalish(c.b, 100, 0.00001);
  304. equal(c.a, 1);
  305. });
  306. test("Setting HSV, Modifying RGB", function() {
  307. var c = new Color({ h: 340, s: 0.3, v: 0.9 });
  308. c.g = 0;
  309. equal(c.h, 312);
  310. equal(c.s, 1);
  311. equal(c.v, 0.9);
  312. equal(c.a, 1);
  313. });
  314. test("Grayscale Hue", function() {
  315. var c = new Color(120, 100, 20, 0.3);
  316. var prevHue = c.h;
  317. equal(typeof c.h, 'number');
  318. // Make graysale
  319. c.g = c.b = c.r;
  320. equal(c.h, prevHue, 'grayscale, hue intact');
  321. });
  322. test("Black Hue", function() {
  323. var c = new Color(120, 100, 20, 0.3);
  324. var prevHue = c.h;
  325. console.log('heh?');
  326. c.r = 0;
  327. c.b = 0;
  328. c.g = 0;
  329. equal(c.h, prevHue, 'black, hue intact');
  330. });
  331. function match(a, b, msg) {
  332. for (var i in b) {
  333. if (b[i] !== b[i]) {
  334. ok(a[i] !== a[i], msg)
  335. } else {
  336. equal(b[i], a[i], msg);
  337. }
  338. }
  339. }
  340. function equalish(a, b, tolerance, label) {
  341. return ok(Math.abs(a - b) < tolerance, label);
  342. }
  343. function initObject() {
  344. return {
  345. numberProperty: 10,
  346. stringProperty: 'foo',
  347. booleanProperty: false,
  348. functionProperty: function() {
  349. // do something
  350. },
  351. anotherBooleanProperty: true,
  352. colorProperty: "#ffffff"
  353. };
  354. }
  355. var hidden = document.createElement('div');
  356. hidden.style.display = 'none';
  357. document.body.appendChild(hidden);
  358. module("Controller");
  359. test("Retrieves values", function() {
  360. var object = initObject();
  361. var c1 = new Controller(object, 'numberProperty');
  362. var c2 = new Controller(object, 'stringProperty');
  363. var c3 = new Controller(object, 'booleanProperty');
  364. var c4 = new Controller(object, 'functionProperty');
  365. equal(c1.getValue(), object.numberProperty, "Number property");
  366. equal(c2.getValue(), object.stringProperty, "String property");
  367. equal(c3.getValue(), object.booleanProperty, "Boolean property");
  368. equal(c4.getValue(), object.functionProperty, "Function property");
  369. });
  370. test("Sets values", function() {
  371. var object = initObject();
  372. var c1 = new Controller(object, 'numberProperty');
  373. c1.setValue(40);
  374. equal(40, object.numberProperty);
  375. });
  376. module("BooleanController");
  377. test("Acknowledges original values", function() {
  378. var object = initObject();
  379. var c1 = new BooleanController(object, 'booleanProperty');
  380. var c2 = new BooleanController(object, 'anotherBooleanProperty');
  381. equal(c1.__checkbox.checked,
  382. object.booleanProperty);
  383. console.log(c2.__checkbox.getAttribute('checked'));
  384. equal(c2.__checkbox.getAttribute('checked') === 'checked',
  385. object.anotherBooleanProperty);
  386. });
  387. test("Modifies values (starting true)", function() {
  388. var object = { booleanProperty: true };
  389. var c1 = new BooleanController(object, 'booleanProperty');
  390. // Must append this to body for click to work
  391. hidden.appendChild(c1.domElement);
  392. dom.fakeEvent(c1.__checkbox, 'click');
  393. equal(false, object.booleanProperty, 'changes');
  394. equal(false, c1.__checkbox.checked, 'checkbox valid');
  395. dom.fakeEvent(c1.__checkbox, 'click');
  396. equal(true, object.booleanProperty, 'changes back');
  397. equal(true, c1.__checkbox.checked, 'checkbox valid');
  398. object.booleanProperty = false;
  399. dom.fakeEvent(c1.__checkbox, 'click');
  400. equal(false, object.booleanProperty, 'maintains sync');
  401. equal(false, c1.__checkbox.checked, 'checkbox valid');
  402. });
  403. test("Modifies values (starting false)", function() {
  404. var object = { booleanProperty: false };
  405. var c1 = new BooleanController(object, 'booleanProperty');
  406. // Must append this to body for click to work
  407. hidden.appendChild(c1.domElement);
  408. dom.fakeEvent(c1.__checkbox, 'click');
  409. equal(true, object.booleanProperty, 'changes');
  410. equal(true, c1.__checkbox.checked, 'checkbox valid');
  411. dom.fakeEvent(c1.__checkbox, 'click');
  412. equal(false, object.booleanProperty, 'changes back');
  413. equal(false, c1.__checkbox.checked, 'checkbox valid');
  414. object.booleanProperty = true;
  415. dom.fakeEvent(c1.__checkbox, 'click');
  416. equal(true, object.booleanProperty, 'maintains sync');
  417. equal(true, c1.__checkbox.checked, 'checkbox valid');
  418. });
  419. module("OptionController");
  420. test("Populates with string array", function() {
  421. var object = initObject();
  422. var options = ['Jono', 'Doug', 'George'];
  423. var c1 = new OptionController(object, 'stringProperty',
  424. options);
  425. // hidden.appendChild(c1.domElement);
  426. $(c1.__select).children().each(function(index) {
  427. equal(options[index], this.innerHTML);
  428. equal(options[index], $(this).attr('value'));
  429. });
  430. });
  431. test("Populates with map", function() {
  432. var object = initObject();
  433. var options = {
  434. 'Small': 0,
  435. 'Medium': 2,
  436. 'Large': 10
  437. };
  438. var c1 = new OptionController(object, 'stringProperty',
  439. options);
  440. // hidden.appendChild(c1.domElement);
  441. $(c1.__select).children().each(function(index) {
  442. equal(options[this.innerHTML], $(this).attr('value'));
  443. });
  444. });
  445. test("Acknowledges original value", function() {
  446. var object = initObject();
  447. var options = {
  448. 'Small': 0,
  449. 'Medium': 2,
  450. 'Large': object.numberProperty
  451. };
  452. var c1 = new OptionController(object, 'numberProperty',
  453. options);
  454. // hidden.appendChild(c1.domElement);
  455. equal($(c1.__select).val(), object.numberProperty);
  456. });
  457. test("Modifies values", function() {
  458. var object = initObject();
  459. var options = {
  460. 'Small': 0,
  461. 'Medium': 2,
  462. 'Large': 10
  463. };
  464. var c1 = new OptionController(object, 'numberProperty',
  465. options);
  466. var c2 = new OptionController(object, 'stringProperty', ['a', 'b', 'c']);
  467. // hidden.appendChild(c1.domElement);
  468. var elem = $(c1.__select).val(options['Medium'])[0];
  469. dom.fakeEvent(elem, 'change');
  470. equal(options['Medium'], object.numberProperty, 'Number property');
  471. elem = $(c2.__select).val('b')[0];
  472. dom.fakeEvent(elem, 'change');
  473. equal('b', object.stringProperty, 'String property');
  474. });
  475. module("StringController");
  476. test("Acknowledges original value", function() {
  477. var object = initObject();
  478. var c1 = new StringController(object, 'stringProperty');
  479. equal($(c1.__input).val(), object.stringProperty);
  480. });
  481. test("Modifies values", function() {
  482. var object = initObject();
  483. var c1 = new StringController(object, 'stringProperty');
  484. var newVal = (new Date()).toJSON();
  485. var elem = $(c1.__input).val(newVal)[0];
  486. dom.fakeEvent(elem, 'change');
  487. equal(newVal, object.stringProperty);
  488. });
  489. module("NumberController");
  490. test("Constraints", function() {
  491. var object = initObject();
  492. var params = { min: 0, max: 10, step: 2 };
  493. var c1 = new NumberController(object,
  494. 'numberProperty', params);
  495. c1.setValue(12);
  496. equal(object.numberProperty, params.max, "Maximum values");
  497. c1.setValue(-20);
  498. equal(object.numberProperty, params.min, "Minimum values");
  499. c1.setValue(1);
  500. equal(object.numberProperty, 2, "Steps");
  501. });
  502. module("NumberControllerBox");
  503. test("Acknowledges original value", function() {
  504. var object = initObject();
  505. var c1 = new NumberControllerBox(object,
  506. 'numberProperty');
  507. // var newVal = Date.now();
  508. //
  509. // $(c1.__input).val(newVal).trigger('change');
  510. //
  511. equal($(c1.__input).val(), object.numberProperty.toString());
  512. });
  513. test("Modifies value", function() {
  514. var object = initObject();
  515. var c1 = new NumberControllerBox(object,
  516. 'numberProperty');
  517. var newVal = Date.now();
  518. var elem = $(c1.__input).val(newVal)[0];
  519. dom.fakeEvent(elem, 'change');
  520. equal(typeof object.numberProperty, 'number');
  521. equal(object.numberProperty, newVal);
  522. });
  523. test("Handles invalid input", function() {
  524. var object = initObject();
  525. var c1 = new NumberControllerBox(object,
  526. 'numberProperty');
  527. var newVal = '~! I98* omg this is not a N&^&*^umber.e-083.9';
  528. var prevVal = object.numberProperty;
  529. var elem = $(c1.__input).val(newVal)[0];
  530. dom.fakeEvent(elem, 'change');
  531. equal(typeof object.numberProperty, 'number');
  532. equal(object.numberProperty, prevVal);
  533. });
  534. test("Handles drag", function() {
  535. var object = { numberProperty: 0 };
  536. var params = { step: Math.random() * 21 };
  537. var c1 = new NumberControllerBox(object,
  538. 'numberProperty', params);
  539. var prevVal = object.numberProperty;
  540. var disp = Math.round(Math.random() * 100);
  541. var elem = c1.__input;
  542. dom.fakeEvent(elem, 'mousedown', {
  543. x: 0,
  544. y: 0
  545. });
  546. dom.fakeEvent(window, 'mousemove', {
  547. x: 0,
  548. y: disp
  549. });
  550. dom.fakeEvent(window, 'mouseup');
  551. equal(object.numberProperty, prevVal + params.step * -disp);
  552. });
  553. module("NumberControllerSlider");
  554. test("Acknowledges original value", function() {
  555. var object = initObject();
  556. var min = 0, max = 50;
  557. var c1 = new NumberControllerSlider(object, 'numberProperty', min, max);
  558. document.body.appendChild(c1.domElement);
  559. var bw = dom.getWidth(c1.__background);
  560. var fw = dom.getWidth(c1.__foreground);
  561. document.body.removeChild(c1.domElement);
  562. equalish(fw/bw, (object.numberProperty - min) / (max - min), 0.01, 'Slider width indicative of value.');
  563. });
  564. test("Modifies values", function() {
  565. var object = initObject();
  566. var min = 0, max = 50;
  567. var c1 = new NumberControllerSlider(object, 'numberProperty', min, max, 1);
  568. document.body.appendChild(c1.domElement);
  569. var o = dom.getOffset(c1.domElement);
  570. var w = dom.getWidth(c1.domElement);
  571. dom.fakeEvent(c1.__background, 'mousedown', {
  572. x: o.left + w/2,
  573. y: o.top
  574. });
  575. var bw = dom.getWidth(c1.__background);
  576. var fw = dom.getWidth(c1.__foreground);
  577. equal(object.numberProperty, (min+max)/2, 'Mouse down');
  578. equalish(fw/bw, (object.numberProperty - min) / (max - min), 0.01, 'Slider width still indicative of value.');
  579. dom.fakeEvent(window, 'mousemove', {
  580. x: o.left,
  581. y: o.top
  582. });
  583. fw = dom.getWidth(c1.__foreground);
  584. equal(object.numberProperty, min, 'Mouse move');
  585. equal(fw/bw, (object.numberProperty - min) / (max - min), 'Slider width still indicative of value.');
  586. dom.fakeEvent(window, 'mouseup');
  587. dom.fakeEvent(window, 'mousemove', {
  588. x: o.left+w,
  589. y: o.top
  590. });
  591. equal(object.numberProperty, min, 'Mouse releases drag');
  592. document.body.removeChild(c1.domElement);
  593. });
  594. module("ColorController");
  595. test("Get Color", function() {
  596. var object = initObject();
  597. var c1 = new ColorController(object, 'colorProperty');
  598. document.body.appendChild(c1.domElement);
  599. var input = c1.domElement.getElementsByTagName("input")[0];
  600. equal(input.value, object.colorProperty, "Input value is the same as the colorProperty");
  601. document.body.removeChild(c1.domElement);
  602. });
  603. /*
  604. test("Set Color", function() {
  605. // get from click, get from hover
  606. var object = initObject();
  607. var c1 = new ColorController(object, 'colorProperty');
  608. document.body.appendChild(c1.domElement);
  609. var input = c1.domElement.getElementsByTagName("input")[0];
  610. // type in color
  611. input.value = "#ff0";
  612. // TODO fake events for keys not working
  613. dom.fakeEvent(input, 'keydown', { keyCode: 13 });
  614. // click sv field
  615. // click hue slider
  616. equal(1,0, "TODO: add set color tests.");
  617. document.body.removeChild(c1.domElement);
  618. });
  619. */
  620. module("Controller Events");
  621. test("onChange", function() {
  622. var object = initObject();
  623. var c0 = new NumberControllerSlider(object, 'numberProperty');
  624. var c1 = new NumberControllerBox(object, 'numberProperty');
  625. var c2 = new StringController(object, 'stringProperty');
  626. var c3 = new BooleanController(object, 'booleanProperty');
  627. var c4 = new FunctionController(object, 'functionProperty');
  628. var c5 = new OptionController(object, 'stringProperty', [0,1,2]);
  629. var c0_changed = false;
  630. var c1_changed = false;
  631. var c2_changed = false;
  632. var c3_changed = false;
  633. var c4_changed = false;
  634. var c5_changed = false;
  635. c0.onChange(function() {
  636. c0_changed = true;
  637. });
  638. c1.onChange(function() {
  639. c1_changed = true;
  640. });
  641. c2.onChange(function() {
  642. c2_changed = true;
  643. });
  644. c3.onChange(function() {
  645. c3_changed = true;
  646. });
  647. c4.onChange(function() {
  648. c4_changed = true;
  649. });
  650. c5.onChange(function() {
  651. c5_changed = true;
  652. });
  653. hidden.appendChild(c3.domElement);
  654. c0.setValue(0.5);
  655. c1.setValue(10);
  656. c2.setValue('hey');
  657. c3.setValue(false);
  658. c4.fire();
  659. c5.setValue('yo');
  660. ok(c1_changed, 'NumberControllerSlider');
  661. ok(c1_changed, 'NumberControllerBox');
  662. ok(c2_changed, 'StringController');
  663. ok(c3_changed, 'BooleanController');
  664. ok(c4_changed, 'FunctionController');
  665. ok(c5_changed, 'OptionController');
  666. });
  667. test("onFinishChange", function() {
  668. var object = initObject();
  669. var min = 0, max = 100;
  670. var c0 = new NumberControllerSlider(object, 'numberProperty', min, max);
  671. var c1 = new NumberControllerBox(object, 'numberProperty');
  672. var c2 = new StringController(object, 'stringProperty');
  673. var c0_changed = false;
  674. var c1_changed = false;
  675. var c2_changed = false;
  676. c0.onFinishChange(function() {
  677. c0_changed = true;
  678. });
  679. document.body.appendChild(c0.domElement);
  680. var o = dom.getOffset(c0.domElement);
  681. var w = dom.getWidth(c0.domElement);
  682. dom.fakeEvent(c0.__background, 'mousedown', {
  683. x: o.left + w/2,
  684. y: o.top
  685. });
  686. ok(!c0_changed, 'NumberControllerSlider didn\'t jump the gun ...');
  687. dom.fakeEvent(window, 'mousemove', {
  688. x: o.left,
  689. y: o.top
  690. });
  691. dom.fakeEvent(window, 'mouseup', {
  692. x: o.left,
  693. y: o.top
  694. });
  695. ok(c0_changed, 'NumberControllerSlider fires when needed.');
  696. document.body.removeChild(c0.domElement);
  697. c1.onFinishChange(function() {
  698. c1_changed = true;
  699. });
  700. document.body.appendChild(c1.domElement);
  701. c1.__input.focus();
  702. c1.__input.value = '1';
  703. ok(!c1_changed, 'NumberControllerBox didn\'t jump the gun ...');
  704. c1.__input.value += '2';
  705. c1.__input.blur();
  706. document.body.removeChild(c1.domElement);
  707. ok(c1_changed, 'NumberControllerBox fires when needed.');
  708. c2.onFinishChange(function() {
  709. c2_changed = true;
  710. });
  711. document.body.appendChild(c2.domElement);
  712. c2.__input.focus();
  713. c2.__input.value = 'friendBudd';
  714. ok(!c2_changed, 'StringController didn\'t jump the gun ...');
  715. c2.__input.value += 'y';
  716. c2.__input.blur();
  717. document.body.removeChild(c2.domElement);
  718. ok(c2_changed, 'StringController fires when needed.');
  719. });
  720. function equalish(a, b, tolerance, label) {
  721. return ok(Math.abs(a - b) < tolerance, label);
  722. }
  723. module('GUI Appearance');
  724. test('Auto placement', function() {
  725. var gui = new GUI();
  726. gui.add({ x: 0 }, 'x');
  727. var gui2 = new GUI();
  728. gui2.add({ x: 0 }, 'x');
  729. equal($('.dg.ac').length, 1, 'A single auto-place container created');
  730. equal($('.dg.ac').children().length, 2, 'Containing two GUI\'s');
  731. equal(gui.parent, undefined);
  732. equal(gui2.parent, undefined);
  733. $('.dg.ac').children().each(function(key, value) {
  734. ok($(value).hasClass(GUI.CLASS_AUTO_PLACE), 'GUI has auto-place class');
  735. ok($(value).hasClass(GUI.CLASS_MAIN), 'GUI has main class');
  736. });
  737. gui.destroy();
  738. gui2.destroy();
  739. });
  740. test('Auto placement scroll', function() {
  741. var gui = new GUI();
  742. // Add a lot of controllers. This will fail if you have some freakishly tall monitor.
  743. for (var i = 0; i < 50; i++) {
  744. gui.add({ x: 0 }, 'x');
  745. }
  746. ok($(gui.domElement).hasClass(GUI.CLASS_TOO_TALL), 'GUI has too tall class');
  747. notEqual($(gui.domElement).children('ul')[0].style.height, 'auto');
  748. gui.destroy();
  749. });
  750. test('Folders', function() {
  751. var gui = new GUI();
  752. gui.add({x:0}, 'x');
  753. var name1 = 'name';
  754. var f1 = gui.addFolder(name1);
  755. f1.add({ x: 0 }, 'x');
  756. equal(f1.name, name1, "Accepts name");
  757. equal($(f1.domElement).find('li.title').html(), name1, "Displays name");
  758. equal(f1.closed, true, "Closed by default");
  759. ok($(f1.domElement).find('ul').hasClass(GUI.CLASS_CLOSED), "Has closed class");
  760. var title = $(f1.domElement).find('li.title')[0];
  761. dom.fakeEvent(title, 'click');
  762. equal(f1.closed, false, "Opens on click");
  763. ok(!$(f1.domElement).find('ul').hasClass(GUI.CLASS_CLOSED), "Opens on click");
  764. dom.fakeEvent(title, 'click');
  765. equal(f1.closed, true, "Closes back up");
  766. ok($(f1.domElement).find('ul').hasClass(GUI.CLASS_CLOSED), "Closes back up");
  767. gui.destroy();
  768. });
  769. module("GUI Controller Methods");
  770. test('options', function() {
  771. var gui = new GUI();
  772. var controller = gui.add({ x: 0 }, 'x').options(0, 1, 2, 3, 4);
  773. $(controller.__select).children().each(function(key, value) {
  774. equals(value.innerHTML, key, 'By array name okay');
  775. equals(value.value, key, 'By array value okay');
  776. });
  777. controller = gui.add({ x: 0 }, 'x').options(
  778. {
  779. 0: '0',
  780. 1: '1',
  781. 2: '2',
  782. 3: '3',
  783. 4: '4'
  784. }
  785. );
  786. $(controller.__select).children().each(function(key, value) {
  787. equals(value.innerHTML, key, 'By array name okay');
  788. equals(value.value, key, 'By array value okay');
  789. });
  790. gui.destroy();
  791. });
  792. test('name', function() {
  793. var gui = new GUI();
  794. var name = 'hey man';
  795. var name2 = 'yoyoo';
  796. var controller = gui.add({ x: 0 }, 'x').name(name);
  797. equals($(controller.__li).find('.property-name').html(), name);
  798. controller.name(name2);
  799. equals($(controller.__li).find('.property-name').html(), name2);
  800. gui.destroy();
  801. });
  802. test('listen', function() {
  803. var gui = new GUI();
  804. var obj = { x: 0 };
  805. var returned1 = gui.add(obj, 'x');
  806. var returned2 = returned1.listen();
  807. obj.x = 10;
  808. setTimeout(function() {
  809. ok(returned1 === returned2, 'Returns self');
  810. equal(returned1.__input.value, obj.x, 'Updates display');
  811. gui.destroy();
  812. }, 0);
  813. });
  814. test('remove', function() {
  815. var gui = new GUI();
  816. var c = gui.add({x:0}, 'x');
  817. ok($.contains(gui.domElement, c.domElement), "Now you see it");
  818. c.remove();
  819. ok(!$.contains(gui.domElement, c.domElement), "Now you don't.");
  820. gui.destroy();
  821. });
  822. test('min, max & step', function() {
  823. var gui = new GUI();
  824. var min = -10;
  825. var max = 200;
  826. var step = 5;
  827. var c = gui.add({ x: 0 }, 'x');
  828. c.min(min);
  829. equals(c.__min, min, 'min');
  830. c.step(step);
  831. equals(c.__step, step, 'step');
  832. var c2 = c.max(max);
  833. equals(c.__max, max, 'max');
  834. notEqual(c2, c, 'Controller has changed.');
  835. ok($(c2.__li).find('.slider').length > 0, 'Slider added');
  836. equals(c.__step, step, 'step intact');
  837. gui.destroy();
  838. });
  839. module("GUI Controller Augmentation");
  840. test('Adds NumberControllerBox to sliders', function() {
  841. var gui = new GUI();
  842. var c = gui.add({ x: 0 }, 'x', 0, 10);
  843. ok($(c.__li).find('input').length > 0, 'NumberControllerBox added');
  844. gui.destroy();
  845. });
  846. test('Clickable rows for BooleanControllers', function() {
  847. var gui = new GUI();
  848. var c = gui.add({ x: false }, 'x');
  849. equal(c.__checkbox.checked, false, 'Acknowledges original');
  850. dom.fakeEvent(c.__li, 'click');
  851. equal($(c.__checkbox).attr('checked'), 'checked', 'Changes when I click the row');
  852. dom.fakeEvent(c.__li, 'click');
  853. equal(c.__checkbox.checked, false, 'Changes back');
  854. gui.destroy();
  855. });
  856. test('Clickable rows for FunctionControllers', function() {
  857. expect(3);
  858. var gui = new GUI();
  859. var c = gui.add({ x: function() {
  860. ok(true)
  861. } }, 'x');
  862. dom.fakeEvent(c.__li, 'click');
  863. c.fire();
  864. dom.fakeEvent(c.__li, 'click');
  865. gui.destroy();
  866. });
  867. module('GUI Saving');
  868. test('Remembering values', function() {
  869. var object = {
  870. number: 0,
  871. boolean: false,
  872. string: 'hey'
  873. };
  874. var controllers = {};
  875. var changed = {
  876. number: -20,
  877. boolean: true,
  878. string: 'hang'
  879. };
  880. var gui = new GUI();
  881. gui.remember(object);
  882. for (var i in object) {
  883. controllers[i] = gui.add(object, i);
  884. }
  885. for (i in controllers) {
  886. controllers[i].setValue(changed[i]);
  887. }
  888. var saveObject = gui.getSaveObject();
  889. gui.destroy();
  890. gui = new GUI({
  891. load: saveObject
  892. });
  893. gui.remember(object);
  894. for (i in object) {
  895. controllers[i] = gui.add(object, i);
  896. equal(object[i], changed[i]);
  897. }
  898. ensurePresetSelectDisplay(gui);
  899. gui.destroy();
  900. });
  901. test('Presets', function() {
  902. var presetName = 'New Preset';
  903. var object = {
  904. number: 0,
  905. boolean: false,
  906. string: 'hey'
  907. };
  908. var original = {};
  909. for (var i in object) {
  910. original[i] = object[i];
  911. }
  912. var controllers = {};
  913. var changed = {
  914. number: -20,
  915. boolean: true,
  916. string: 'hang'
  917. };
  918. var gui = new GUI();
  919. gui.remember(object);
  920. for (i in object) {
  921. controllers[i] = gui.add(object, i);
  922. }
  923. for (i in controllers) {
  924. controllers[i].setValue(changed[i]);
  925. }
  926. ensurePresetSelectDisplay(gui);
  927. gui.saveAs(presetName);
  928. ensurePresetSelectDisplay(gui);
  929. var saveObject = gui.getSaveObject();
  930. console.log(saveObject);
  931. gui.destroy();
  932. gui = new GUI({
  933. load: saveObject
  934. });
  935. gui.remember(object);
  936. for (i in object) {
  937. controllers[i] = gui.add(object, i);
  938. equal(object[i], changed[i], "Uses last defined preset");
  939. }
  940. equal(gui.preset, presetName, "Preset value correct");
  941. ensurePresetSelectDisplay(gui);
  942. gui.destroy();
  943. gui = new GUI({
  944. preset: "Default",
  945. load: saveObject
  946. });
  947. gui.remember(object);
  948. for (i in object) {
  949. controllers[i] = gui.add(object, i);
  950. equal(object[i], original[i], "Loads with explicitly set preset");
  951. }
  952. equal(gui.preset, "Default", "Preset value correct");
  953. ensurePresetSelectDisplay(gui);
  954. gui.preset = presetName;
  955. for (i in object) {
  956. equal(object[i], changed[i], "Changes via gui.preset property");
  957. }
  958. $(gui.__preset_select).val('Default');
  959. dom.fakeEvent(gui.__preset_select, 'change');
  960. for (i in object) {
  961. equal(object[i], original[i], "Changes via dropdown");
  962. }
  963. gui.destroy();
  964. });
  965. function ensurePresetSelectDisplay(gui) {
  966. equal($(gui.__preset_select).children('option:selected')[0].value, gui.preset, "Dropdown display matches preset value");
  967. }
  968. });
  969. </script>
  970. </head>
  971. <body>
  972. <h1 id="qunit-header"></h1>
  973. <h2 id="qunit-banner"></h2>
  974. <div id="qunit-testrunner-toolbar"></div>
  975. <h2 id="qunit-userAgent"></h2>
  976. <ol id="qunit-tests"></ol>
  977. <div id="qunit-fixture">test markup, will be hidden</div>
  978. </body>
  979. </html>