|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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.skin; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | + |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import javafx.geometry.BoundingBox; |
| 34 | +import javafx.geometry.Insets; |
| 35 | +import javafx.scene.control.Spinner; |
| 36 | +import javafx.scene.control.skin.SpinnerSkin; |
| 37 | +import javafx.scene.layout.Region; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for SpinnerSkin |
| 41 | + */ |
| 42 | +public class SpinnerSkinTest { |
| 43 | + private static final double CONTROL_WIDTH = 300; |
| 44 | + private static final double CONTROL_HEIGHT = 300; |
| 45 | + private static final double PADDING_TOP = 5; |
| 46 | + private static final double PADDING_RIGHT = 7; |
| 47 | + private static final double PADDING_BOTTOM = 11; |
| 48 | + private static final double PADDING_LEFT = 13; |
| 49 | + private static final double WIDTH = CONTROL_WIDTH - PADDING_LEFT - PADDING_RIGHT; |
| 50 | + private static final double HEIGHT = CONTROL_HEIGHT - PADDING_TOP - PADDING_BOTTOM; |
| 51 | + private static final double PADDING_DECREMENT_ARROW_TOP = 1; |
| 52 | + private static final double PADDING_DECREMENT_ARROW_RIGHT = 2; |
| 53 | + private static final double PADDING_DECREMENT_ARROW_BOTTOM = 3; |
| 54 | + private static final double PADDING_DECREMENT_ARROW_LEFT = 4; |
| 55 | + private static final double PADDING_INCREMENT_ARROW_TOP = 2; |
| 56 | + private static final double PADDING_INCREMENT_ARROW_RIGHT = 1; |
| 57 | + private static final double PADDING_INCREMENT_ARROW_BOTTOM = 4; |
| 58 | + private static final double PADDING_INCREMENT_ARROW_LEFT = 3; |
| 59 | + private static final double DECREMENT_ARROW_WIDTH = PADDING_DECREMENT_ARROW_LEFT + PADDING_DECREMENT_ARROW_RIGHT; |
| 60 | + private static final double DECREMENT_ARROW_HEIGHT = PADDING_DECREMENT_ARROW_TOP + PADDING_DECREMENT_ARROW_BOTTOM; |
| 61 | + private static final double INCREMENT_ARROW_WIDTH = PADDING_INCREMENT_ARROW_LEFT + PADDING_INCREMENT_ARROW_RIGHT; |
| 62 | + private static final double INCREMENT_ARROW_HEIGHT = PADDING_INCREMENT_ARROW_TOP + PADDING_INCREMENT_ARROW_BOTTOM; |
| 63 | + |
| 64 | + private Spinner<?> spinner; |
| 65 | + |
| 66 | + private Region decrementArrowButton; |
| 67 | + private Region incrementArrowButton; |
| 68 | + |
| 69 | + @Before |
| 70 | + public void before() { |
| 71 | + spinner = new Spinner<>(); |
| 72 | + spinner.resize(CONTROL_WIDTH, CONTROL_HEIGHT); |
| 73 | + spinner.setSkin(new SpinnerSkin<>(spinner)); |
| 74 | + |
| 75 | + decrementArrowButton = (Region)spinner.lookup(".decrement-arrow-button"); |
| 76 | + incrementArrowButton = (Region)spinner.lookup(".increment-arrow-button"); |
| 77 | + |
| 78 | + // Give everything some weird paddings so anomalies should not go undetected: |
| 79 | + spinner.setPadding(new Insets(PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM, PADDING_LEFT)); |
| 80 | + decrementArrowButton.setPadding(new Insets(PADDING_DECREMENT_ARROW_TOP, PADDING_DECREMENT_ARROW_RIGHT, PADDING_DECREMENT_ARROW_BOTTOM, PADDING_DECREMENT_ARROW_LEFT)); |
| 81 | + incrementArrowButton.setPadding(new Insets(PADDING_INCREMENT_ARROW_TOP, PADDING_INCREMENT_ARROW_RIGHT, PADDING_INCREMENT_ARROW_BOTTOM, PADDING_INCREMENT_ARROW_LEFT)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void shouldPositionArrowsRightAndAboveEachOther() { // This is the default style |
| 86 | + spinner.layout(); |
| 87 | + |
| 88 | + double widest = Math.max(DECREMENT_ARROW_WIDTH, INCREMENT_ARROW_WIDTH); |
| 89 | + |
| 90 | + assertEquals(new BoundingBox(PADDING_LEFT + WIDTH - widest, PADDING_TOP + HEIGHT / 2, widest, HEIGHT / 2), decrementArrowButton.getBoundsInParent()); |
| 91 | + assertEquals(new BoundingBox(PADDING_LEFT + WIDTH - widest, PADDING_TOP, widest, HEIGHT / 2), incrementArrowButton.getBoundsInParent()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void shouldPositionArrowsLeftAndAboveEachOther() { |
| 96 | + spinner.getStyleClass().setAll(Spinner.STYLE_CLASS_ARROWS_ON_LEFT_VERTICAL); |
| 97 | + spinner.layout(); |
| 98 | + |
| 99 | + double widest = Math.max(DECREMENT_ARROW_WIDTH, INCREMENT_ARROW_WIDTH); |
| 100 | + |
| 101 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP + HEIGHT / 2, widest, HEIGHT / 2), decrementArrowButton.getBoundsInParent()); |
| 102 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP, widest, HEIGHT / 2), incrementArrowButton.getBoundsInParent()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldPositionArrowsLeftAndNextToEachOther() { |
| 107 | + spinner.getStyleClass().setAll(Spinner.STYLE_CLASS_ARROWS_ON_LEFT_HORIZONTAL); |
| 108 | + spinner.layout(); |
| 109 | + |
| 110 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP, DECREMENT_ARROW_WIDTH, HEIGHT), decrementArrowButton.getBoundsInParent()); |
| 111 | + assertEquals(new BoundingBox(PADDING_LEFT + DECREMENT_ARROW_WIDTH, PADDING_TOP, INCREMENT_ARROW_WIDTH, HEIGHT), incrementArrowButton.getBoundsInParent()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void shouldPositionArrowsRightAndNextToEachOther() { |
| 116 | + spinner.getStyleClass().setAll(Spinner.STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL); |
| 117 | + spinner.layout(); |
| 118 | + |
| 119 | + assertEquals(new BoundingBox(PADDING_LEFT + WIDTH - DECREMENT_ARROW_WIDTH - INCREMENT_ARROW_WIDTH, PADDING_TOP, DECREMENT_ARROW_WIDTH, HEIGHT), decrementArrowButton.getBoundsInParent()); |
| 120 | + assertEquals(new BoundingBox(PADDING_LEFT + WIDTH - INCREMENT_ARROW_WIDTH, PADDING_TOP, INCREMENT_ARROW_WIDTH, HEIGHT), incrementArrowButton.getBoundsInParent()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void shouldPositionArrowsSplitLeftAndRight() { |
| 125 | + spinner.getStyleClass().setAll(Spinner.STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL); |
| 126 | + spinner.layout(); |
| 127 | + |
| 128 | + double widest = Math.max(DECREMENT_ARROW_WIDTH, INCREMENT_ARROW_WIDTH); |
| 129 | + |
| 130 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP, widest, HEIGHT), decrementArrowButton.getBoundsInParent()); |
| 131 | + assertEquals(new BoundingBox(PADDING_LEFT + WIDTH - widest, PADDING_TOP, widest, HEIGHT), incrementArrowButton.getBoundsInParent()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void shouldPositionArrowsSplitTopAndBottom() { |
| 136 | + spinner.getStyleClass().setAll(Spinner.STYLE_CLASS_SPLIT_ARROWS_VERTICAL); |
| 137 | + spinner.layout(); |
| 138 | + |
| 139 | + double tallest = Math.max(DECREMENT_ARROW_HEIGHT, INCREMENT_ARROW_HEIGHT); |
| 140 | + |
| 141 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP + HEIGHT - tallest, WIDTH, tallest), decrementArrowButton.getBoundsInParent()); |
| 142 | + assertEquals(new BoundingBox(PADDING_LEFT, PADDING_TOP, WIDTH, tallest), incrementArrowButton.getBoundsInParent()); |
| 143 | + } |
| 144 | +} |
0 commit comments