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