| 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 |
|
| 15 |
goog.provide('goog.ui.HsvPaletteTest'); |
| 16 |
goog.setTestOnly('goog.ui.HsvPaletteTest'); |
| 17 |
|
| 18 |
goog.require('goog.color'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.dom.classlist'); |
| 21 |
goog.require('goog.events'); |
| 22 |
goog.require('goog.events.Event'); |
| 23 |
goog.require('goog.math.Coordinate'); |
| 24 |
goog.require('goog.style'); |
| 25 |
goog.require('goog.testing.PropertyReplacer'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.ui.Component'); |
| 28 |
goog.require('goog.ui.HsvPalette'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
var samplePalette; |
| 32 |
var eventWasFired = false; |
| 33 |
var stubs = new goog.testing.PropertyReplacer(); |
| 34 |
|
| 35 |
function setUp() { |
| 36 |
samplePalette = new goog.ui.HsvPalette(); |
| 37 |
} |
| 38 |
|
| 39 |
function tearDown() { |
| 40 |
samplePalette.dispose(); |
| 41 |
stubs.reset(); |
| 42 |
} |
| 43 |
|
| 44 |
function testRtl() { |
| 45 |
samplePalette.render(document.getElementById('sandboxRtl')); |
| 46 |
var color = '#ffffff'; |
| 47 |
samplePalette.inputElement.value = color; |
| 48 |
samplePalette.handleInput(null); |
| 49 |
var expectedRight = samplePalette.hsImageEl_.offsetWidth - |
| 50 |
Math.ceil(samplePalette.hsHandleEl_.offsetWidth / 2); |
| 51 |
assertEquals(expectedRight + 'px', samplePalette.hsHandleEl_.style['right']); |
| 52 |
assertEquals('', samplePalette.hsHandleEl_.style['left']); |
| 53 |
} |
| 54 |
|
| 55 |
function testOptionalInitialColor() { |
| 56 |
var initialColor = '#0000ff'; |
| 57 |
var customInitialPalette = new goog.ui.HsvPalette(null, initialColor); |
| 58 |
assertEquals( |
| 59 |
initialColor, goog.color.parse(customInitialPalette.getColor()).hex); |
| 60 |
} |
| 61 |
|
| 62 |
function testCustomClassName() { |
| 63 |
var customClassName = 'custom-plouf'; |
| 64 |
var customClassPalette = new goog.ui.HsvPalette(null, null, customClassName); |
| 65 |
customClassPalette.createDom(); |
| 66 |
assertTrue( |
| 67 |
goog.dom.classlist.contains( |
| 68 |
customClassPalette.getElement(), customClassName)); |
| 69 |
} |
| 70 |
|
| 71 |
function testCannotDecorate() { |
| 72 |
assertFalse(samplePalette.canDecorate()); |
| 73 |
} |
| 74 |
|
| 75 |
function testSetColor() { |
| 76 |
var color = '#abcdef'; |
| 77 |
samplePalette.setColor(color); |
| 78 |
assertEquals(color, goog.color.parse(samplePalette.getColor()).hex); |
| 79 |
color = 'abcdef'; |
| 80 |
samplePalette.setColor(color); |
| 81 |
assertEquals('#' + color, goog.color.parse(samplePalette.getColor()).hex); |
| 82 |
} |
| 83 |
|
| 84 |
function testChangeEvent() { |
| 85 |
// TODO(user): Add functionality to goog.testing.events to assert |
| 86 |
// an event was fired. |
| 87 |
goog.events.listen( |
| 88 |
samplePalette, goog.ui.Component.EventType.ACTION, |
| 89 |
function() { eventWasFired = true; }); |
| 90 |
samplePalette.setColor('#123456'); |
| 91 |
assertTrue(eventWasFired); |
| 92 |
} |
| 93 |
|
| 94 |
function testSetHsv() { |
| 95 |
// Start from red. |
| 96 |
samplePalette.setColor('#ff0000'); |
| 97 |
|
| 98 |
// Setting hue to 0.5 should yield cyan. |
| 99 |
samplePalette.setHsv(0.5, null, null); |
| 100 |
assertEquals('#00ffff', goog.color.parse(samplePalette.getColor()).hex); |
| 101 |
|
| 102 |
// Setting saturation to 0 should yield white. |
| 103 |
samplePalette.setHsv(null, 0, null); |
| 104 |
assertEquals('#ffffff', goog.color.parse(samplePalette.getColor()).hex); |
| 105 |
|
| 106 |
// Setting value/brightness to 0 should yield black. |
| 107 |
samplePalette.setHsv(null, null, 0); |
| 108 |
assertEquals('#000000', goog.color.parse(samplePalette.getColor()).hex); |
| 109 |
} |
| 110 |
|
| 111 |
function testRender() { |
| 112 |
samplePalette.render(document.getElementById('sandbox')); |
| 113 |
|
| 114 |
assertTrue(samplePalette.isInDocument()); |
| 115 |
|
| 116 |
var elem = samplePalette.getElement(); |
| 117 |
assertNotNull(elem); |
| 118 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 119 |
|
| 120 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) { |
| 121 |
assertSameElements( |
| 122 |
'On IE6, the noalpha class must be present', |
| 123 |
['goog-hsv-palette', 'goog-hsv-palette-noalpha'], |
| 124 |
goog.dom.classlist.get(elem)); |
| 125 |
} else { |
| 126 |
assertEquals( |
| 127 |
'The noalpha class must not be present', 'goog-hsv-palette', |
| 128 |
elem.className); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function testSwatchTextIsReadable() { |
| 133 |
samplePalette.render(document.getElementById('sandbox')); |
| 134 |
|
| 135 |
var swatchElement = samplePalette.swatchElement; |
| 136 |
|
| 137 |
// Text should be black when background is light. |
| 138 |
samplePalette.setColor('#ccffff'); |
| 139 |
assertEquals( |
| 140 |
'#000000', |
| 141 |
goog.color.parse(goog.style.getStyle(swatchElement, 'color')).hex); |
| 142 |
|
| 143 |
// Text should be white when background is dark. |
| 144 |
samplePalette.setColor('#410800'); |
| 145 |
assertEquals( |
| 146 |
'#ffffff', |
| 147 |
goog.color.parse(goog.style.getStyle(swatchElement, 'color')).hex); |
| 148 |
} |
| 149 |
|
| 150 |
function testInputColor() { |
| 151 |
samplePalette.render(document.getElementById('sandbox')); |
| 152 |
var color = '#001122'; |
| 153 |
samplePalette.inputElement.value = color; |
| 154 |
samplePalette.handleInput(null); |
| 155 |
assertEquals(color, goog.color.parse(samplePalette.getColor()).hex); |
| 156 |
} |
| 157 |
|
| 158 |
function testHandleMouseMoveValue() { |
| 159 |
samplePalette.render(document.getElementById('sandbox')); |
| 160 |
stubs.set(goog.dom, 'getPageScroll', function() { |
| 161 |
return new goog.math.Coordinate(0, 0); |
| 162 |
}); |
| 163 |
|
| 164 |
// Raising the value/brightness of a dark red should yield a lighter red. |
| 165 |
samplePalette.setColor('#630c00'); |
| 166 |
goog.style.setPageOffset(samplePalette.valueBackgroundImageElement, 0, 0); |
| 167 |
goog.style.setSize(samplePalette.valueBackgroundImageElement, 10, 100); |
| 168 |
var boundaries = |
| 169 |
goog.style.getBounds(samplePalette.valueBackgroundImageElement, 0, 0); |
| 170 |
|
| 171 |
var event = new goog.events.Event(); |
| 172 |
event.clientY = -50; |
| 173 |
// TODO(user): Use |
| 174 |
// goog.testing.events.fireMouseDownEvent( |
| 175 |
// samplePalette.valueBackgroundImageElement); |
| 176 |
// when google.testing.events support specifying properties of the event |
| 177 |
// or find out how tod o it if it already supports it. |
| 178 |
samplePalette.handleMouseMoveV_(boundaries, event); |
| 179 |
assertEquals('#ff1e00', goog.color.parse(samplePalette.getColor()).hex); |
| 180 |
} |
| 181 |
|
| 182 |
function testHandleMouseMoveHueSaturation() { |
| 183 |
samplePalette.render(document.getElementById('sandbox')); |
| 184 |
stubs.set(goog.dom, 'getPageScroll', function() { |
| 185 |
return new goog.math.Coordinate(0, 0); |
| 186 |
}); |
| 187 |
|
| 188 |
// The following hue/saturation selection should yield a light yellow. |
| 189 |
goog.style.setPageOffset(samplePalette.hsImageEl_, 0, 0); |
| 190 |
goog.style.setSize(samplePalette.hsImageEl_, 100, 100); |
| 191 |
var boundaries = goog.style.getBounds(samplePalette.hsImageEl_); |
| 192 |
|
| 193 |
var event = new goog.events.Event(); |
| 194 |
event.clientX = 20; |
| 195 |
event.clientY = 85; |
| 196 |
// TODO(user): Use goog.testing.events when appropriate (see above). |
| 197 |
samplePalette.handleMouseMoveHs_(boundaries, event); |
| 198 |
assertEquals('#ffeec4', goog.color.parse(samplePalette.getColor()).hex); |
| 199 |
} |