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 com.sun.javafx.scene.control.skin.ColorPalette;
  42 import com.sun.javafx.scene.control.skin.ColorPickerPaletteRetriever;
  43 import com.sun.javafx.scene.control.skin.ColorPickerSkin;
  44 import com.sun.javafx.tk.Toolkit;
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
  48 import static org.junit.Assert.assertEquals;
  49 import static org.junit.Assert.assertFalse;
  50 import static org.junit.Assert.assertNotNull;
  51 import static org.junit.Assert.assertNull;
  52 import static org.junit.Assert.assertTrue;
  53 
  54 public class ColorPickerTest {
  55     private ColorPicker colorPicker;
  56     private Toolkit tk;
  57     private Scene scene;
  58     private Stage stage;
  59     private VBox root;
  60 
  61     @Before public void setup() {
  62         tk = (StubToolkit)Toolkit.getToolkit();
  63         colorPicker = new ColorPicker();
  64         Scene scene = new Scene(new VBox(20), 800, 600);
  65         VBox box = (VBox)scene.getRoot();
  66         box.getChildren().add(colorPicker);
  67         stage = new Stage();
  68         stage.setScene(scene);
  69         stage.show();
  70         tk.firePulse();
  71     }
  72 
  73     /*********************************************************************
  74      * Tests for default values                                         *
  75      ********************************************************************/
  76 
  77     @Test public void noArgConstructorSetsTheStyleClass() {
  78         assertStyleClassContains(colorPicker, "color-picker");
  79     }
  80     
  81     @Test public void noArgConstructor_valueIsNonNull() {
  82         assertNotNull(colorPicker.getValue());
  83     }
  84     
  85     @Test public void noArgConstructor_showingIsFalse() {
  86         assertFalse(colorPicker.isShowing());
  87     }
  88 
  89     @Test public void singleArgConstructorSetsTheStyleClass() {
  90         final ColorPicker cp = new ColorPicker(Color.WHITE);
  91         assertStyleClassContains(cp, "color-picker");
  92     }
  93     
  94     @Test public void singleArgConstructor_showingIsFalse() {
  95         final ColorPicker cp = new ColorPicker(Color.WHITE);
  96         assertFalse(cp.isShowing());
  97     }
  98 
  99     @Test public void singleArgConstructor_valueIsNonNull() {
 100         final ColorPicker cp = new ColorPicker(Color.WHITE);
 101         assertNotNull(cp.getValue());
 102     }
 103     
 104     @Test public void defaultActionHandlerIsNotDefined() {
 105         assertNull(colorPicker.getOnAction());
 106     }
 107     
 108     @Test public void testGetSetValue() {
 109         final ColorPicker cp = new ColorPicker(Color.WHITE);
 110         cp.setValue(Color.PINK);
 111         assertEquals(cp.getValue(), Color.PINK);
 112     }
 113     
 114     @Test public void testCustomColors() {
 115         final ColorPicker cp = new ColorPicker(Color.WHITE);
 116         cp.getCustomColors().addAll(new Color(0.83, .55, .214, 1), new Color(.811, .222, .621, 1));
 117         assertEquals(cp.getCustomColors().get(0),  new Color(0.83, .55, .214, 1));
 118         assertEquals(cp.getCustomColors().get(1),  new Color(.811, .222, .621, 1));
 119     }
 120     
 121     @Test public void ensureCanSetValueToNonNullColorAndBackAgain() {
 122         colorPicker.setValue(Color.PINK);
 123         assertEquals(Color.PINK, colorPicker.getValue());
 124         colorPicker.setValue(null);
 125         assertNull(colorPicker.getValue());
 126     }
 127     
 128     @Test public void ensureCanToggleShowing() {
 129         colorPicker.show();
 130         assertTrue(colorPicker.isShowing());
 131         colorPicker.hide();
 132         assertFalse(colorPicker.isShowing());
 133     }
 134     
 135     @Test public void ensureCanNotToggleShowingWhenDisabled() {
 136         colorPicker.setDisable(true);
 137         colorPicker.show();
 138         assertFalse(colorPicker.isShowing());
 139         colorPicker.setDisable(false);
 140         colorPicker.show();
 141         assertTrue(colorPicker.isShowing());
 142     }
 143      
 144     @Test public void ensureCanSetOnAction() {
 145         EventHandler<ActionEvent> onAction = t -> { };
 146         colorPicker.setOnAction(onAction);
 147         assertEquals(onAction, colorPicker.getOnAction());
 148     }
 149     
 150     @Test public void ensureOnActionPropertyReferencesBean() {
 151         assertEquals(colorPicker, colorPicker.onActionProperty().getBean());
 152     }
 153     
 154     @Test public void ensureCanSelectColorFromPalette() {
 155          final MouseEventGenerator generator = new MouseEventGenerator();
 156          ColorPickerSkin skin = (ColorPickerSkin)colorPicker.getSkin();
 157          assertTrue(skin != null);
 158          ColorPalette colorPalette = ColorPickerPaletteRetriever.getColorPalette(colorPicker);
 159          colorPicker.show();
 160          tk.firePulse();
 161          assertTrue(colorPicker.isShowing());
 162          GridPane grid = colorPalette.getColorGrid();
 163          double xval = grid.getBoundsInLocal().getMinX();
 164          double yval = grid.getBoundsInLocal().getMinY();
 165         
 166         Scene paletteScene = ColorPickerPaletteRetriever.getPopup(colorPicker).getScene();
 167         paletteScene.getWindow().requestFocus();
 168         
 169         paletteScene.impl_processMouseEvent(
 170                 generator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+85, yval+40));
 171         
 172         paletteScene.impl_processMouseEvent(
 173                 generator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+85, yval+40));
 174         tk.firePulse();
 175         
 176         assertEquals(colorPicker.getValue().toString(), "0x330033ff");
 177     }
 178     
 179     @Test public void testEscapeClosesCustomColorDialog() {
 180 //        final MouseEventGenerator generator = new MouseEventGenerator();
 181         ColorPickerSkin skin = (ColorPickerSkin)colorPicker.getSkin();
 182         assertTrue(skin != null);
 183         ColorPalette colorPalette = ColorPickerPaletteRetriever.getColorPalette(colorPicker);
 184         colorPicker.show();
 185         tk.firePulse();
 186         assertTrue(colorPicker.isShowing());
 187         Hyperlink link = ColorPickerPaletteRetriever.getCustomColorLink(colorPalette);
 188          
 189         Scene paletteScene = ColorPickerPaletteRetriever.getPopup(colorPicker).getScene();
 190         paletteScene.getWindow().requestFocus();
 191         
 192         //Click on CustomColor hyperlink to show the custom color dialog.
 193         Hyperlink hyperlink = ColorPickerPaletteRetriever.getCustomColorLink(colorPalette);
 194         MouseEventFirer mouse = new MouseEventFirer(hyperlink);
 195         mouse.fireMousePressAndRelease();
 196         mouse.dispose();
 197 
 198         Stage dialog = ColorPickerPaletteRetriever.getCustomColorDialog(colorPalette);
 199         assertNotNull(dialog);
 200         assertTrue(dialog.isShowing());
 201         
 202         dialog.requestFocus();
 203         tk.firePulse();
 204         
 205         // fire KeyEvent (Escape) on custom color dialog to close it
 206         KeyEventFirer keyboard = new KeyEventFirer(dialog);
 207         keyboard.doKeyPress(KeyCode.ESCAPE);
 208         tk.firePulse();   
 209         assertTrue(!dialog.isShowing());
 210     }
 211 }