1 /*
   2  * Copyright (c) 2010, 2013, 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 
  29 import javafx.css.ParsedValue;
  30 import javafx.css.CssMetaData;
  31 import javafx.css.CssParser;
  32 import com.sun.javafx.pgstub.StubToolkit;
  33 import com.sun.javafx.tk.Toolkit;
  34 import javafx.css.StyleableProperty;
  35 import javafx.scene.Parent;
  36 import javafx.scene.Scene;
  37 import javafx.scene.layout.StackPane;
  38 import javafx.stage.Stage;
  39 import javafx.util.StringConverter;
  40 import static org.junit.Assert.assertEquals;
  41 import org.junit.Assert;
  42 import org.junit.Before;
  43 import org.junit.Test;
  44 
  45 /**
  46  * @author smarks
  47  */
  48 public class SliderTest {
  49     
  50     
  51     private Slider slider;
  52     private Toolkit tk;
  53     private Scene scene;
  54     private Stage stage;
  55     
  56     @Before public void setup() {
  57         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  58         slider = new Slider();
  59     }
  60     
  61     protected void startApp(Parent root) {
  62         scene = new Scene(root,800,600);
  63         stage = new Stage();
  64         stage.setScene(scene);
  65         stage.show();
  66         tk.firePulse();
  67     }
  68     @Test public void testSettingMinorTickCountViaCSS() {
  69         StackPane pane = new StackPane();
  70         pane.getChildren().add(slider);
  71         startApp(pane);
  72         
  73         ParsedValue pv = new CssParser().parseExpr("-fx-minor-tick-count","2");
  74         Object val = pv.convert(null);        
  75         try {
  76             ((StyleableProperty)slider.minorTickCountProperty()).applyStyle(null, val);
  77             assertEquals(2, slider.getMinorTickCount(), 0.);
  78         } catch (Exception e) {
  79             Assert.fail(e.toString());
  80         }
  81     }
  82     
  83     @Test public void testSettingTickLabelFormatter() {
  84         StackPane pane = new StackPane();
  85         pane.getChildren().add(slider);
  86         slider.setShowTickLabels(true);
  87         slider.setShowTickMarks(true);
  88         slider.setLabelFormatter(new StringConverter<Double>() {
  89             @Override public String toString(Double t) {
  90                 return "Ok.";
  91             }
  92             @Override public Double fromString(String string) {
  93                 return 10.0;
  94             }
  95         });
  96         startApp(pane);
  97         assertEquals("Ok.", slider.getLabelFormatter().toString(10.0));
  98     }
  99     
 100 //    Slider slider;
 101 //
 102 //    /**
 103 //     * Creates a slider.
 104 //     *
 105 //     * Assumes min == 0, value == 0, max == 100, horizontal
 106 //     * (i.e., vertical == false).
 107 //     */
 108 //    @Override protected Node createNodeToTest() {
 109 //        slider = new Slider();
 110 //        return slider;
 111 //    }
 112 //
 113 //    // TESTS
 114 //
 115 //    @Test
 116 //    public void ensureSkinExists() {
 117 //        assertNotNull(slider.getSkin());
 118 //    }
 119 //
 120 //    @Test
 121 //    public void ensureSkinNodesExist() {
 122 //        assertNotNull(findNodeByStyleClass("thumb"));
 123 //        assertNotNull(findNodeByStyleClass("track"));
 124 //    }
 125 //
 126 //    @Test
 127 //    public void thumbIsPositionedAtLeftWhenValueIsMinimum() {
 128 //        Node thumb = findNodeByStyleClass("thumb");
 129 //        Node track = findNodeByStyleClass("track");
 130 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 131 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 132 //        double trackLeftX = trackBounds.getMinX();
 133 //        assertTrue(thumbBounds.getMinX() < trackLeftX);
 134 //        assertTrue(thumbBounds.getMaxX() > trackLeftX);
 135 //    }
 136 //
 137 //    @Test
 138 //    public void thumbIsPositionedAtHcenterWhenValueIsMiddle() {
 139 //        slider.setValue(50);
 140 //        awaitQuiescent();
 141 //        Node thumb = findNodeByStyleClass("thumb");
 142 //        Node track = findNodeByStyleClass("track");
 143 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 144 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 145 //        double trackCenterX = trackBounds.getMinX() + trackBounds.getWidth() / 2.0;
 146 //        assertTrue(thumbBounds.getMinX() < trackCenterX);
 147 //        assertTrue(thumbBounds.getMaxX() > trackCenterX);
 148 //    }
 149 //
 150 //    @Test
 151 //    public void thumbIsPositionedAtRightWhenValueIsMaximum() {
 152 //        slider.setValue(100);
 153 //        awaitQuiescent();
 154 //        Node thumb = findNodeByStyleClass("thumb");
 155 //        Node track = findNodeByStyleClass("track");
 156 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 157 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 158 //        double trackRightX = trackBounds.getMaxX();
 159 //        assertTrue(thumbBounds.getMinX() < trackRightX);
 160 //        assertTrue(thumbBounds.getMaxX() > trackRightX);
 161 //    }
 162 //
 163 //    @Test
 164 //    public void thumbIsPositionedAtTopWhenValueIsMinimum() {
 165 //        slider.setVertical(true);
 166 //        awaitQuiescent();
 167 //        Node track = findNodeByStyleClass("track");
 168 //        Node thumb = findNodeByStyleClass("thumb");
 169 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 170 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 171 //        double trackTopY = trackBounds.getMinY();
 172 //        assertTrue(thumbBounds.getMinY() < trackTopY);
 173 //        assertTrue(thumbBounds.getMaxY() > trackTopY);
 174 //    }
 175 //
 176 //    @Test
 177 //    public void thumbIsPositionedAtVcenterWhenValueIsMiddle() {
 178 //        slider.setVertical(true);
 179 //        slider.setValue(50);
 180 //        awaitQuiescent();
 181 //        Node track = findNodeByStyleClass("track");
 182 //        Node thumb = findNodeByStyleClass("thumb");
 183 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 184 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 185 //        double trackCenterY = trackBounds.getMinY() + trackBounds.getHeight() / 2.0;
 186 //        assertTrue(thumbBounds.getMinY() < trackCenterY);
 187 //        assertTrue(thumbBounds.getMaxY() > trackCenterY);
 188 //    }
 189 //
 190 //    @Test
 191 //    public void thumbIsPositionedAtBottomWhenValueIsMaximum() {
 192 //        slider.setVertical(true);
 193 //        slider.setValue(100);
 194 //        awaitQuiescent();
 195 //        Node track = findNodeByStyleClass("track");
 196 //        Node thumb = findNodeByStyleClass("thumb");
 197 //        Bounds thumbBounds = thumb.localToScene(thumb.getBoundsInLocal());
 198 //        Bounds trackBounds = track.localToScene(track.getBoundsInLocal());
 199 //        double trackBottomY = trackBounds.getMaxY();
 200 //        assertTrue(thumbBounds.getMinY() < trackBottomY);
 201 //        assertTrue(thumbBounds.getMaxY() > trackBottomY);
 202 //    }
 203 //
 204 //    @Test
 205 //    public void movingThumbShouldChangeValue() {
 206 //        double originalValue = slider.getValue();
 207 //        Node thumb = findNodeByStyleClass("thumb");
 208 //        mouse().positionAtCenterOf(thumb);
 209 //        Point2D center = centerOf(thumb);
 210 //        mouse().leftPressDragRelease(50, center.getY());
 211 //        assertTrue(slider.getValue() > originalValue);
 212 //    }
 213 
 214 }