hsvpalette_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.HsvPaletteTest');
  15. goog.setTestOnly('goog.ui.HsvPaletteTest');
  16. goog.require('goog.color');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.classlist');
  19. goog.require('goog.events');
  20. goog.require('goog.events.Event');
  21. goog.require('goog.math.Coordinate');
  22. goog.require('goog.style');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.ui.Component');
  26. goog.require('goog.ui.HsvPalette');
  27. goog.require('goog.userAgent');
  28. var samplePalette;
  29. var eventWasFired = false;
  30. var stubs = new goog.testing.PropertyReplacer();
  31. function setUp() {
  32. samplePalette = new goog.ui.HsvPalette();
  33. }
  34. function tearDown() {
  35. samplePalette.dispose();
  36. stubs.reset();
  37. }
  38. function testRtl() {
  39. samplePalette.render(document.getElementById('sandboxRtl'));
  40. var color = '#ffffff';
  41. samplePalette.inputElement.value = color;
  42. samplePalette.handleInput(null);
  43. var expectedRight = samplePalette.hsImageEl_.offsetWidth -
  44. Math.ceil(samplePalette.hsHandleEl_.offsetWidth / 2);
  45. assertEquals(expectedRight + 'px', samplePalette.hsHandleEl_.style['right']);
  46. assertEquals('', samplePalette.hsHandleEl_.style['left']);
  47. }
  48. function testOptionalInitialColor() {
  49. var initialColor = '#0000ff';
  50. var customInitialPalette = new goog.ui.HsvPalette(null, initialColor);
  51. assertEquals(
  52. initialColor, goog.color.parse(customInitialPalette.getColor()).hex);
  53. }
  54. function testCustomClassName() {
  55. var customClassName = 'custom-plouf';
  56. var customClassPalette = new goog.ui.HsvPalette(null, null, customClassName);
  57. customClassPalette.createDom();
  58. assertTrue(
  59. goog.dom.classlist.contains(
  60. customClassPalette.getElement(), customClassName));
  61. }
  62. function testCannotDecorate() {
  63. assertFalse(samplePalette.canDecorate());
  64. }
  65. function testSetColor() {
  66. var color = '#abcdef';
  67. samplePalette.setColor(color);
  68. assertEquals(color, goog.color.parse(samplePalette.getColor()).hex);
  69. color = 'abcdef';
  70. samplePalette.setColor(color);
  71. assertEquals('#' + color, goog.color.parse(samplePalette.getColor()).hex);
  72. }
  73. function testChangeEvent() {
  74. // TODO(user): Add functionality to goog.testing.events to assert
  75. // an event was fired.
  76. goog.events.listen(
  77. samplePalette, goog.ui.Component.EventType.ACTION,
  78. function() { eventWasFired = true; });
  79. samplePalette.setColor('#123456');
  80. assertTrue(eventWasFired);
  81. }
  82. function testSetHsv() {
  83. // Start from red.
  84. samplePalette.setColor('#ff0000');
  85. // Setting hue to 0.5 should yield cyan.
  86. samplePalette.setHsv(0.5, null, null);
  87. assertEquals('#00ffff', goog.color.parse(samplePalette.getColor()).hex);
  88. // Setting saturation to 0 should yield white.
  89. samplePalette.setHsv(null, 0, null);
  90. assertEquals('#ffffff', goog.color.parse(samplePalette.getColor()).hex);
  91. // Setting value/brightness to 0 should yield black.
  92. samplePalette.setHsv(null, null, 0);
  93. assertEquals('#000000', goog.color.parse(samplePalette.getColor()).hex);
  94. }
  95. function testRender() {
  96. samplePalette.render(document.getElementById('sandbox'));
  97. assertTrue(samplePalette.isInDocument());
  98. var elem = samplePalette.getElement();
  99. assertNotNull(elem);
  100. assertEquals(String(goog.dom.TagName.DIV), elem.tagName);
  101. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) {
  102. assertSameElements(
  103. 'On IE6, the noalpha class must be present',
  104. ['goog-hsv-palette', 'goog-hsv-palette-noalpha'],
  105. goog.dom.classlist.get(elem));
  106. } else {
  107. assertEquals(
  108. 'The noalpha class must not be present', 'goog-hsv-palette',
  109. elem.className);
  110. }
  111. }
  112. function testSwatchTextIsReadable() {
  113. samplePalette.render(document.getElementById('sandbox'));
  114. var swatchElement = samplePalette.swatchElement;
  115. // Text should be black when background is light.
  116. samplePalette.setColor('#ccffff');
  117. assertEquals(
  118. '#000000',
  119. goog.color.parse(goog.style.getStyle(swatchElement, 'color')).hex);
  120. // Text should be white when background is dark.
  121. samplePalette.setColor('#410800');
  122. assertEquals(
  123. '#ffffff',
  124. goog.color.parse(goog.style.getStyle(swatchElement, 'color')).hex);
  125. }
  126. function testInputColor() {
  127. samplePalette.render(document.getElementById('sandbox'));
  128. var color = '#001122';
  129. samplePalette.inputElement.value = color;
  130. samplePalette.handleInput(null);
  131. assertEquals(color, goog.color.parse(samplePalette.getColor()).hex);
  132. }
  133. function testHandleMouseMoveValue() {
  134. samplePalette.render(document.getElementById('sandbox'));
  135. stubs.set(goog.dom, 'getPageScroll', function() {
  136. return new goog.math.Coordinate(0, 0);
  137. });
  138. // Raising the value/brightness of a dark red should yield a lighter red.
  139. samplePalette.setColor('#630c00');
  140. goog.style.setPageOffset(samplePalette.valueBackgroundImageElement, 0, 0);
  141. goog.style.setSize(samplePalette.valueBackgroundImageElement, 10, 100);
  142. var boundaries =
  143. goog.style.getBounds(samplePalette.valueBackgroundImageElement, 0, 0);
  144. var event = new goog.events.Event();
  145. event.clientY = -50;
  146. // TODO(user): Use
  147. // goog.testing.events.fireMouseDownEvent(
  148. // samplePalette.valueBackgroundImageElement);
  149. // when google.testing.events support specifying properties of the event
  150. // or find out how tod o it if it already supports it.
  151. samplePalette.handleMouseMoveV_(boundaries, event);
  152. assertEquals('#ff1e00', goog.color.parse(samplePalette.getColor()).hex);
  153. }
  154. function testHandleMouseMoveHueSaturation() {
  155. samplePalette.render(document.getElementById('sandbox'));
  156. stubs.set(goog.dom, 'getPageScroll', function() {
  157. return new goog.math.Coordinate(0, 0);
  158. });
  159. // The following hue/saturation selection should yield a light yellow.
  160. goog.style.setPageOffset(samplePalette.hsImageEl_, 0, 0);
  161. goog.style.setSize(samplePalette.hsImageEl_, 100, 100);
  162. var boundaries = goog.style.getBounds(samplePalette.hsImageEl_);
  163. var event = new goog.events.Event();
  164. event.clientX = 20;
  165. event.clientY = 85;
  166. // TODO(user): Use goog.testing.events when appropriate (see above).
  167. samplePalette.handleMouseMoveHs_(boundaries, event);
  168. assertEquals('#ffeec4', goog.color.parse(samplePalette.getColor()).hex);
  169. }