1 /*
   2  * Copyright (c) 2015, 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.skin;
  27 
  28 import com.sun.javafx.pgstub.StubToolkit;
  29 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  30 import com.sun.javafx.scene.control.infrastructure.MouseEventFirer;
  31 import com.sun.javafx.scene.control.infrastructure.MouseEventGenerator;
  32 import com.sun.javafx.tk.Toolkit;
  33 import javafx.scene.Scene;
  34 import javafx.scene.control.ColorPicker;
  35 import javafx.scene.control.Hyperlink;
  36 import javafx.scene.input.KeyCode;
  37 import javafx.scene.input.MouseEvent;
  38 import javafx.scene.layout.GridPane;
  39 import javafx.scene.layout.VBox;
  40 import javafx.stage.Stage;
  41 import org.junit.Before;
  42 import org.junit.Test;
  43 
  44 import static org.junit.Assert.assertEquals;
  45 import static org.junit.Assert.assertNotNull;
  46 import static org.junit.Assert.assertTrue;
  47 
  48 public class ColorPickerSkinTest {
  49     private ColorPicker colorPicker;
  50     private Toolkit tk;
  51     private Stage stage;
  52 
  53     @Before public void setup() {
  54         tk = Toolkit.getToolkit();
  55         colorPicker = new ColorPicker();
  56         Scene scene = new Scene(new VBox(20), 800, 600);
  57         VBox box = (VBox)scene.getRoot();
  58         box.getChildren().add(colorPicker);
  59         stage = new Stage();
  60         stage.setScene(scene);
  61         stage.show();
  62         tk.firePulse();
  63     }
  64 
  65 
  66     @Test public void ensureCanSelectColorFromPalette() {
  67          final MouseEventGenerator generator = new MouseEventGenerator();
  68          ColorPickerSkin skin = (ColorPickerSkin)colorPicker.getSkin();
  69          assertTrue(skin != null);
  70          ColorPalette colorPalette = ColorPickerPaletteRetriever.getColorPalette(colorPicker);
  71          colorPicker.show();
  72          tk.firePulse();
  73          assertTrue(colorPicker.isShowing());
  74          GridPane grid = colorPalette.getColorGrid();
  75          double xval = grid.getBoundsInLocal().getMinX();
  76          double yval = grid.getBoundsInLocal().getMinY();
  77         
  78         Scene paletteScene = ColorPickerPaletteRetriever.getPopup(colorPicker).getScene();
  79         paletteScene.getWindow().requestFocus();
  80         
  81         paletteScene.impl_processMouseEvent(
  82                 generator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+85, yval+40));
  83         
  84         paletteScene.impl_processMouseEvent(
  85                 generator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+85, yval+40));
  86         tk.firePulse();
  87         
  88         assertEquals(colorPicker.getValue().toString(), "0x330033ff");
  89     }
  90     
  91     @Test public void testEscapeClosesCustomColorDialog() {
  92 //        final MouseEventGenerator generator = new MouseEventGenerator();
  93         ColorPickerSkin skin = (ColorPickerSkin)colorPicker.getSkin();
  94         assertTrue(skin != null);
  95         ColorPalette colorPalette = ColorPickerPaletteRetriever.getColorPalette(colorPicker);
  96         colorPicker.show();
  97         tk.firePulse();
  98         assertTrue(colorPicker.isShowing());
  99         Hyperlink link = ColorPickerPaletteRetriever.getCustomColorLink(colorPalette);
 100          
 101         Scene paletteScene = ColorPickerPaletteRetriever.getPopup(colorPicker).getScene();
 102         paletteScene.getWindow().requestFocus();
 103         
 104         //Click on CustomColor hyperlink to show the custom color dialog.
 105         Hyperlink hyperlink = ColorPickerPaletteRetriever.getCustomColorLink(colorPalette);
 106         MouseEventFirer mouse = new MouseEventFirer(hyperlink);
 107         mouse.fireMousePressAndRelease();
 108         mouse.dispose();
 109 
 110         Stage dialog = ColorPickerPaletteRetriever.getCustomColorDialog(colorPalette);
 111         assertNotNull(dialog);
 112         assertTrue(dialog.isShowing());
 113         
 114         dialog.requestFocus();
 115         tk.firePulse();
 116         
 117         // fire KeyEvent (Escape) on custom color dialog to close it
 118         KeyEventFirer keyboard = new KeyEventFirer(dialog);
 119         keyboard.doKeyPress(KeyCode.ESCAPE);
 120         tk.firePulse();   
 121         assertTrue(!dialog.isShowing());
 122     }
 123 }