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