1 /* 2 * Copyright (c) 2012, 2019, 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.css; 27 28 29 import com.sun.javafx.css.StyleManager; 30 31 import java.io.IOException; 32 import javafx.css.CssMetaData; 33 import javafx.css.CssParser; 34 import javafx.css.PseudoClass; 35 import javafx.css.StyleableProperty; 36 import javafx.css.Stylesheet; 37 import javafx.scene.Group; 38 import javafx.scene.Scene; 39 import javafx.scene.paint.Color; 40 import javafx.scene.paint.Paint; 41 import javafx.scene.shape.Rectangle; 42 import static org.junit.Assert.*; 43 44 import org.junit.AfterClass; 45 import org.junit.Before; 46 import org.junit.Test; 47 48 public class Node_cssStateTransition_Test { 49 50 public Node_cssStateTransition_Test() { 51 } 52 53 private static void resetStyleManager() { 54 StyleManager sm = StyleManager.getInstance(); 55 sm.userAgentStylesheetContainers.clear(); 56 sm.platformUserAgentStylesheetContainers.clear(); 57 sm.stylesheetContainerMap.clear(); 58 sm.cacheContainerMap.clear(); 59 sm.hasDefaultUserAgentStylesheet = false; 60 } 61 62 @Before 63 public void setUp() { 64 resetStyleManager(); 65 } 66 67 @AfterClass 68 public static void cleanupOnce() { 69 resetStyleManager(); 70 } 71 72 @Test 73 public void testPropertiesResetOnStyleclassChange() { 74 75 Rectangle rect = new Rectangle(50,50); 76 Paint defaultFill = rect.getFill(); 77 Paint defaultStroke = rect.getStroke(); 78 Double defaultStrokeWidth = Double.valueOf(rect.getStrokeWidth()); 79 80 CssMetaData metaData = ((StyleableProperty)rect.fillProperty()).getCssMetaData(); 81 assertEquals(defaultFill, metaData.getInitialValue(rect)); 82 metaData = ((StyleableProperty)rect.strokeProperty()).getCssMetaData(); 83 assertEquals(defaultStroke, metaData.getInitialValue(rect)); 84 metaData = ((StyleableProperty)rect.strokeWidthProperty()).getCssMetaData(); 85 assertEquals(defaultStrokeWidth, metaData.getInitialValue(rect)); 86 87 Stylesheet stylesheet = null; 88 try { 89 // Note: setDefaultUserAgentStylesheet in StyleManager won't replace the UA stylesheet unless it has a name, 90 // and that name needs to be different from the current one, if any. This matters when running 91 // these tests from the same VM since StyleManager is a singleton. 92 stylesheet = new CssParser().parse( 93 "testPropertiesResetOnStyleclassChange", 94 ".rect { -fx-fill: red; -fx-stroke: yellow; -fx-stroke-width: 3px; }" + 95 ".rect.green { -fx-fill: green; }" + 96 ".green { -fx-stroke: green; }" 97 98 ); 99 } catch(IOException ioe) { 100 fail(); 101 } 102 103 rect.getStyleClass().add("rect"); 104 105 Group root = new Group(); 106 root.getChildren().add(rect); 107 StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet); 108 109 Scene scene = new Scene(root); 110 111 root.applyCss(); 112 113 assertEquals(Color.RED, rect.getFill()); 114 assertEquals(Color.YELLOW, rect.getStroke()); 115 assertEquals(3d, rect.getStrokeWidth(), 1e-6); 116 117 rect.getStyleClass().add("green"); 118 root.applyCss(); 119 120 assertEquals(Color.GREEN, rect.getFill()); 121 assertEquals(Color.GREEN, rect.getStroke()); 122 assertEquals(3d, rect.getStrokeWidth(), 1e-6); 123 124 rect.getStyleClass().remove("rect"); 125 root.applyCss(); 126 127 assertEquals(defaultFill, rect.getFill()); 128 assertEquals(Color.GREEN, rect.getStroke()); 129 assertEquals(defaultStrokeWidth.doubleValue(), rect.getStrokeWidth(), 1e-6); 130 131 rect.getStyleClass().remove("green"); 132 root.applyCss(); 133 134 assertEquals(defaultFill, rect.getFill()); 135 assertEquals(defaultStroke, rect.getStroke()); 136 assertEquals(defaultStrokeWidth.doubleValue(), rect.getStrokeWidth(), 1e-6); 137 } 138 139 @Test 140 public void testPropertiesResetOnPsedudoClassStateChange() { 141 142 Rectangle rect = new Rectangle(50,50); 143 Paint defaultFill = rect.getFill(); 144 Paint defaultStroke = rect.getStroke(); 145 Double defaultStrokeWidth = Double.valueOf(rect.getStrokeWidth()); 146 147 CssMetaData metaData = ((StyleableProperty)rect.fillProperty()).getCssMetaData(); 148 assertEquals(defaultFill, metaData.getInitialValue(rect)); 149 metaData = ((StyleableProperty)rect.strokeProperty()).getCssMetaData(); 150 assertEquals(defaultStroke, metaData.getInitialValue(rect)); 151 metaData = ((StyleableProperty)rect.strokeWidthProperty()).getCssMetaData(); 152 assertEquals(defaultStrokeWidth, metaData.getInitialValue(rect)); 153 154 Stylesheet stylesheet = null; 155 try { 156 // Note: setDefaultUserAgentStylesheet in StyleManager won't replace the UA stylesheet unless it has a name, 157 // and that name needs to be different from the current one, if any. This matters when running 158 // these tests from the same VM since StyleManager is a singleton. 159 stylesheet = new CssParser().parse( 160 "testPropertiesResetOnPsedudoClassStateChange", 161 ".rect:hover { -fx-fill: red; -fx-stroke: yellow; -fx-stroke-width: 3px; }" + 162 ".rect:hover:focused { -fx-fill: green; }" + 163 ".rect:focused { -fx-stroke: green; }" 164 165 ); 166 } catch(IOException ioe) { 167 fail(); 168 } 169 170 rect.getStyleClass().add("rect"); 171 172 Group root = new Group(); 173 root.getChildren().add(rect); 174 StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet); 175 176 Scene scene = new Scene(root); 177 178 root.applyCss(); 179 180 assertEquals(defaultFill, rect.getFill()); 181 assertEquals(defaultStroke, rect.getStroke()); 182 assertEquals(defaultStrokeWidth, rect.getStrokeWidth(), 1e-6); 183 184 rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("hover"), true); 185 root.applyCss(); 186 187 assertEquals(Color.RED, rect.getFill()); 188 assertEquals(Color.YELLOW, rect.getStroke()); 189 assertEquals(3d, rect.getStrokeWidth(), 1e-6); 190 191 rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), true); 192 root.applyCss(); 193 194 assertEquals(Color.GREEN, rect.getFill()); 195 assertEquals(Color.GREEN, rect.getStroke()); 196 assertEquals(3d, rect.getStrokeWidth(), 1e-6); 197 198 rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("hover"), false); 199 root.applyCss(); 200 201 assertEquals(defaultFill, rect.getFill()); 202 assertEquals(Color.GREEN, rect.getStroke()); 203 assertEquals(defaultStrokeWidth.doubleValue(), rect.getStrokeWidth(), 1e-6); 204 205 rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), false); 206 root.applyCss(); 207 208 assertEquals(defaultFill, rect.getFill()); 209 assertEquals(defaultStroke, rect.getStroke()); 210 assertEquals(defaultStrokeWidth.doubleValue(), rect.getStrokeWidth(), 1e-6); 211 212 } 213 214 }