| 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.ColorPaletteTest'); |
| 16 |
goog.setTestOnly('goog.ui.ColorPaletteTest'); |
| 17 |
|
| 18 |
goog.require('goog.color'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.ui.ColorPalette'); |
| 22 |
|
| 23 |
var emptyPalette, samplePalette; |
| 24 |
|
| 25 |
function setUp() { |
| 26 |
emptyPalette = new goog.ui.ColorPalette(); |
| 27 |
samplePalette = |
| 28 |
new goog.ui.ColorPalette(['red', '#00FF00', 'rgb(0, 0, 255)']); |
| 29 |
samplePalette.setSelectedColor('blue'); |
| 30 |
} |
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
emptyPalette.dispose(); |
| 34 |
samplePalette.dispose(); |
| 35 |
document.getElementById('sandbox').innerHTML = ''; |
| 36 |
} |
| 37 |
|
| 38 |
function testEmptyColorPalette() { |
| 39 |
var colors = emptyPalette.getColors(); |
| 40 |
assertNotNull(colors); |
| 41 |
assertEquals(0, colors.length); |
| 42 |
|
| 43 |
var nodes = emptyPalette.getContent(); |
| 44 |
assertNotNull(nodes); |
| 45 |
assertEquals(0, nodes.length); |
| 46 |
} |
| 47 |
|
| 48 |
function testSampleColorPalette() { |
| 49 |
var colors = samplePalette.getColors(); |
| 50 |
assertNotNull(colors); |
| 51 |
assertEquals(3, colors.length); |
| 52 |
assertEquals('red', colors[0]); |
| 53 |
assertEquals('#00FF00', colors[1]); |
| 54 |
assertEquals('rgb(0, 0, 255)', colors[2]); |
| 55 |
|
| 56 |
var nodes = samplePalette.getContent(); |
| 57 |
assertNotNull(nodes); |
| 58 |
assertEquals(3, nodes.length); |
| 59 |
assertEquals('goog-palette-colorswatch', nodes[0].className); |
| 60 |
assertEquals('goog-palette-colorswatch', nodes[1].className); |
| 61 |
assertEquals('goog-palette-colorswatch', nodes[2].className); |
| 62 |
assertEquals('#ff0000', goog.color.parse(nodes[0].style.backgroundColor).hex); |
| 63 |
assertEquals('#00ff00', goog.color.parse(nodes[1].style.backgroundColor).hex); |
| 64 |
assertEquals('#0000ff', goog.color.parse(nodes[2].style.backgroundColor).hex); |
| 65 |
} |
| 66 |
|
| 67 |
function testGetColors() { |
| 68 |
var emptyColors = emptyPalette.getColors(); |
| 69 |
assertNotNull(emptyColors); |
| 70 |
assertEquals(0, emptyColors.length); |
| 71 |
|
| 72 |
var sampleColors = samplePalette.getColors(); |
| 73 |
assertNotNull(sampleColors); |
| 74 |
assertEquals(3, sampleColors.length); |
| 75 |
assertEquals('red', sampleColors[0]); |
| 76 |
assertEquals('#00FF00', sampleColors[1]); |
| 77 |
assertEquals('rgb(0, 0, 255)', sampleColors[2]); |
| 78 |
} |
| 79 |
|
| 80 |
function testSetColors() { |
| 81 |
emptyPalette.setColors(['black', '#FFFFFF']); |
| 82 |
|
| 83 |
var colors = emptyPalette.getColors(); |
| 84 |
assertNotNull(colors); |
| 85 |
assertEquals(2, colors.length); |
| 86 |
assertEquals('black', colors[0]); |
| 87 |
assertEquals('#FFFFFF', colors[1]); |
| 88 |
|
| 89 |
var nodes = emptyPalette.getContent(); |
| 90 |
assertNotNull(nodes); |
| 91 |
assertEquals(2, nodes.length); |
| 92 |
assertEquals('goog-palette-colorswatch', nodes[0].className); |
| 93 |
assertEquals('goog-palette-colorswatch', nodes[1].className); |
| 94 |
assertEquals('#000000', goog.color.parse(nodes[0].style.backgroundColor).hex); |
| 95 |
assertEquals('#ffffff', goog.color.parse(nodes[1].style.backgroundColor).hex); |
| 96 |
assertEquals('black', nodes[0].title); |
| 97 |
assertEquals('RGB (255, 255, 255)', nodes[1].title); |
| 98 |
|
| 99 |
samplePalette.setColors(['#336699', 'cyan']); |
| 100 |
|
| 101 |
var newColors = samplePalette.getColors(); |
| 102 |
assertNotNull(newColors); |
| 103 |
assertEquals(2, newColors.length); |
| 104 |
assertEquals('#336699', newColors[0]); |
| 105 |
assertEquals('cyan', newColors[1]); |
| 106 |
|
| 107 |
var newNodes = samplePalette.getContent(); |
| 108 |
assertNotNull(newNodes); |
| 109 |
assertEquals(2, newNodes.length); |
| 110 |
assertEquals('goog-palette-colorswatch', newNodes[0].className); |
| 111 |
assertEquals('goog-palette-colorswatch', newNodes[1].className); |
| 112 |
assertEquals( |
| 113 |
'#336699', goog.color.parse(newNodes[0].style.backgroundColor).hex); |
| 114 |
assertEquals( |
| 115 |
'#00ffff', goog.color.parse(newNodes[1].style.backgroundColor).hex); |
| 116 |
} |
| 117 |
|
| 118 |
function testSetColorsWithLabels() { |
| 119 |
emptyPalette.setColors(['#00f', '#FFFFFF', 'black'], ['blue', 'white']); |
| 120 |
var nodes = emptyPalette.getContent(); |
| 121 |
assertEquals('blue', nodes[0].title); |
| 122 |
assertEquals('white', nodes[1].title); |
| 123 |
assertEquals('black', nodes[2].title); |
| 124 |
} |
| 125 |
|
| 126 |
function testRender() { |
| 127 |
samplePalette.render(document.getElementById('sandbox')); |
| 128 |
|
| 129 |
assertTrue(samplePalette.isInDocument()); |
| 130 |
|
| 131 |
var elem = samplePalette.getElement(); |
| 132 |
assertNotNull(elem); |
| 133 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 134 |
assertEquals('goog-palette', elem.className); |
| 135 |
|
| 136 |
var table = elem.firstChild; |
| 137 |
assertEquals('TABLE', table.tagName); |
| 138 |
assertEquals('goog-palette-table', table.className); |
| 139 |
} |
| 140 |
|
| 141 |
function testGetSelectedColor() { |
| 142 |
assertNull(emptyPalette.getSelectedColor()); |
| 143 |
assertEquals('#0000ff', samplePalette.getSelectedColor()); |
| 144 |
} |
| 145 |
|
| 146 |
function testSetSelectedColor() { |
| 147 |
emptyPalette.setSelectedColor('red'); |
| 148 |
assertNull(emptyPalette.getSelectedColor()); |
| 149 |
|
| 150 |
samplePalette.setSelectedColor('red'); |
| 151 |
assertEquals('#ff0000', samplePalette.getSelectedColor()); |
| 152 |
samplePalette.setSelectedColor(17); // Invalid color spec. |
| 153 |
assertNull(samplePalette.getSelectedColor()); |
| 154 |
|
| 155 |
samplePalette.setSelectedColor('rgb(0, 255, 0)'); |
| 156 |
assertEquals('#00ff00', samplePalette.getSelectedColor()); |
| 157 |
samplePalette.setSelectedColor(false); // Invalid color spec. |
| 158 |
assertNull(samplePalette.getSelectedColor()); |
| 159 |
|
| 160 |
samplePalette.setSelectedColor('#0000FF'); |
| 161 |
assertEquals('#0000ff', samplePalette.getSelectedColor()); |
| 162 |
samplePalette.setSelectedColor(null); // Invalid color spec. |
| 163 |
assertNull(samplePalette.getSelectedColor()); |
| 164 |
} |