hsvapalette_test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.HsvaPaletteTest');
  15. goog.setTestOnly('goog.ui.HsvaPaletteTest');
  16. goog.require('goog.color.alpha');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.classlist');
  19. goog.require('goog.events.Event');
  20. goog.require('goog.math.Coordinate');
  21. goog.require('goog.style');
  22. goog.require('goog.testing.PropertyReplacer');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.ui.HsvaPalette');
  25. goog.require('goog.userAgent');
  26. var samplePalette;
  27. var eventWasFired = false;
  28. var stubs = new goog.testing.PropertyReplacer();
  29. function setUp() {
  30. samplePalette = new goog.ui.HsvaPalette();
  31. }
  32. function tearDown() {
  33. samplePalette.dispose();
  34. stubs.reset();
  35. }
  36. function testZeroAlpha() {
  37. var palette = new goog.ui.HsvaPalette(null, undefined, 0);
  38. assertEquals(0, palette.getAlpha());
  39. }
  40. function testOptionalInitialColor() {
  41. var alpha = 0.5;
  42. var color = '#0000ff';
  43. var palette = new goog.ui.HsvaPalette(null, color, alpha);
  44. assertEquals(color, palette.getColor());
  45. assertEquals(alpha, palette.getAlpha());
  46. }
  47. function testCustomClassName() {
  48. var customClassName = 'custom-plouf';
  49. var customClassPalette =
  50. new goog.ui.HsvaPalette(null, null, null, customClassName);
  51. customClassPalette.createDom();
  52. assertTrue(
  53. goog.dom.classlist.contains(
  54. customClassPalette.getElement(), customClassName));
  55. }
  56. function testSetColor() {
  57. var color = '#abcdef01';
  58. samplePalette.setColorRgbaHex(color);
  59. assertEquals(
  60. color, goog.color.alpha.parse(samplePalette.getColorRgbaHex()).hex);
  61. color = 'abcdef01';
  62. samplePalette.setColorRgbaHex(color);
  63. assertEquals(
  64. '#' + color, goog.color.alpha.parse(samplePalette.getColorRgbaHex()).hex);
  65. }
  66. function testRender() {
  67. samplePalette.render(document.getElementById('sandbox'));
  68. assertTrue(samplePalette.isInDocument());
  69. var elem = samplePalette.getElement();
  70. assertNotNull(elem);
  71. assertEquals(String(goog.dom.TagName.DIV), elem.tagName);
  72. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) {
  73. assertSameElements(
  74. 'On IE6, the noalpha class must be present',
  75. ['goog-hsva-palette', 'goog-hsva-palette-noalpha'],
  76. goog.dom.classlist.get(elem));
  77. } else {
  78. assertEquals(
  79. 'The noalpha class must not be present', 'goog-hsva-palette',
  80. elem.className);
  81. }
  82. }
  83. function testInputColor() {
  84. samplePalette.render(document.getElementById('sandbox'));
  85. var color = '#00112233';
  86. samplePalette.inputElement.value = color;
  87. samplePalette.handleInput(null);
  88. assertEquals(
  89. color, goog.color.alpha.parse(samplePalette.getColorRgbaHex()).hex);
  90. }
  91. function testHandleMouseMoveAlpha() {
  92. samplePalette.render(document.getElementById('sandbox'));
  93. stubs.set(goog.dom, 'getPageScroll', function() {
  94. return new goog.math.Coordinate(0, 0);
  95. });
  96. // Lowering the opacity of a dark, opaque red should yield a
  97. // more transparent red.
  98. samplePalette.setColorRgbaHex('#630c0000');
  99. goog.style.setPageOffset(samplePalette.aImageEl_, 0, 0);
  100. goog.style.setSize(samplePalette.aImageEl_, 10, 100);
  101. var boundaries = goog.style.getBounds(samplePalette.aImageEl_);
  102. var event = new goog.events.Event();
  103. event.clientY = boundaries.top;
  104. samplePalette.handleMouseMoveA_(boundaries, event);
  105. assertEquals('#630c00ff', samplePalette.getColorRgbaHex());
  106. }
  107. function testSwatchOpacity() {
  108. samplePalette.render(document.getElementById('sandbox'));
  109. samplePalette.setAlpha(1);
  110. assertEquals(1, goog.style.getOpacity(samplePalette.swatchElement));
  111. samplePalette.setAlpha(0x99 / 0xff);
  112. assertEquals(0.6, goog.style.getOpacity(samplePalette.swatchElement));
  113. samplePalette.setAlpha(0);
  114. assertEquals(0, goog.style.getOpacity(samplePalette.swatchElement));
  115. }
  116. function testNoTransparencyBehavior() {
  117. samplePalette.render(document.getElementById('sandbox'));
  118. samplePalette.inputElement.value = '#abcdef22';
  119. samplePalette.handleInput(null);
  120. samplePalette.inputElement.value = '#abcdef';
  121. samplePalette.handleInput(null);
  122. assertEquals(1, goog.style.getOpacity(samplePalette.swatchElement));
  123. }