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