1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.control; 27 28 import javafx.event.ActionEvent; 29 import javafx.event.EventHandler; 30 import javafx.scene.Scene; 31 import javafx.scene.input.KeyCode; 32 import javafx.scene.input.MouseEvent; 33 import javafx.scene.layout.GridPane; 34 import javafx.scene.layout.VBox; 35 import javafx.scene.paint.Color; 36 import javafx.stage.Stage; 37 import com.sun.javafx.pgstub.StubToolkit; 38 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer; 39 import com.sun.javafx.scene.control.infrastructure.MouseEventFirer; 40 import com.sun.javafx.scene.control.infrastructure.MouseEventGenerator; 41 import javafx.scene.control.skin.ColorPickerPaletteRetriever; 42 import javafx.scene.control.skin.ColorPickerSkin; 43 import com.sun.javafx.tk.Toolkit; 44 import org.junit.Before; 45 import org.junit.Test; 46 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains; 47 import static org.junit.Assert.assertEquals; 48 import static org.junit.Assert.assertFalse; 49 import static org.junit.Assert.assertNotNull; 50 import static org.junit.Assert.assertNull; 51 import static org.junit.Assert.assertTrue; 52 53 public class ColorPickerTest { 54 private ColorPicker colorPicker; 55 private Toolkit tk; 56 private Scene scene; 57 private Stage stage; 58 private VBox root; 59 60 @Before public void setup() { 61 tk = (StubToolkit)Toolkit.getToolkit(); 62 colorPicker = new ColorPicker(); 63 Scene scene = new Scene(new VBox(20), 800, 600); 64 VBox box = (VBox)scene.getRoot(); 65 box.getChildren().add(colorPicker); 66 stage = new Stage(); 67 stage.setScene(scene); 68 stage.show(); 69 tk.firePulse(); 70 } 71 72 /********************************************************************* 73 * Tests for default values * 74 ********************************************************************/ 75 76 @Test public void noArgConstructorSetsTheStyleClass() { 77 assertStyleClassContains(colorPicker, "color-picker"); 78 } 79 80 @Test public void noArgConstructor_valueIsNonNull() { 81 assertNotNull(colorPicker.getValue()); 82 } 83 84 @Test public void noArgConstructor_showingIsFalse() { 85 assertFalse(colorPicker.isShowing()); 86 } 87 88 @Test public void singleArgConstructorSetsTheStyleClass() { 89 final ColorPicker cp = new ColorPicker(Color.WHITE); 90 assertStyleClassContains(cp, "color-picker"); 91 } 92 93 @Test public void singleArgConstructor_showingIsFalse() { 94 final ColorPicker cp = new ColorPicker(Color.WHITE); 95 assertFalse(cp.isShowing()); 96 } 97 98 @Test public void singleArgConstructor_valueIsNonNull() { 99 final ColorPicker cp = new ColorPicker(Color.WHITE); 100 assertNotNull(cp.getValue()); 101 } 102 103 @Test public void defaultActionHandlerIsNotDefined() { 104 assertNull(colorPicker.getOnAction()); 105 } 106 107 @Test public void testGetSetValue() { 108 final ColorPicker cp = new ColorPicker(Color.WHITE); 109 cp.setValue(Color.PINK); 110 assertEquals(cp.getValue(), Color.PINK); 111 } 112 113 @Test public void testCustomColors() { 114 final ColorPicker cp = new ColorPicker(Color.WHITE); 115 cp.getCustomColors().addAll(new Color(0.83, .55, .214, 1), new Color(.811, .222, .621, 1)); 116 assertEquals(cp.getCustomColors().get(0), new Color(0.83, .55, .214, 1)); 117 assertEquals(cp.getCustomColors().get(1), new Color(.811, .222, .621, 1)); 118 } 119 120 @Test public void ensureCanSetValueToNonNullColorAndBackAgain() { 121 colorPicker.setValue(Color.PINK); 122 assertEquals(Color.PINK, colorPicker.getValue()); 123 colorPicker.setValue(null); 124 assertNull(colorPicker.getValue()); 125 } 126 127 @Test public void ensureCanToggleShowing() { 128 colorPicker.show(); 129 assertTrue(colorPicker.isShowing()); 130 colorPicker.hide(); 131 assertFalse(colorPicker.isShowing()); 132 } 133 134 @Test public void ensureCanNotToggleShowingWhenDisabled() { 135 colorPicker.setDisable(true); 136 colorPicker.show(); 137 assertFalse(colorPicker.isShowing()); 138 colorPicker.setDisable(false); 139 colorPicker.show(); 140 assertTrue(colorPicker.isShowing()); 141 } 142 143 @Test public void ensureCanSetOnAction() { 144 EventHandler<ActionEvent> onAction = t -> { }; 145 colorPicker.setOnAction(onAction); 146 assertEquals(onAction, colorPicker.getOnAction()); 147 } 148 149 @Test public void ensureOnActionPropertyReferencesBean() { 150 assertEquals(colorPicker, colorPicker.onActionProperty().getBean()); 151 } 152 }