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