modules/controls/src/test/java/javafx/scene/control/ColorPickerTest.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


  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();


 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 }


  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();


 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 }