/* * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package test.javafx.css; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; import com.sun.javafx.css.CascadingStyle; import com.sun.javafx.css.ParsedValueImpl; import com.sun.javafx.css.PseudoClassState; import com.sun.javafx.css.StyleManager; import test.com.sun.javafx.css.TestNode; import test.com.sun.javafx.css.TestNodeBase; import com.sun.javafx.sg.prism.NGNode; import javafx.beans.value.WritableValue; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.css.converter.BooleanConverter; import com.sun.javafx.geom.BaseBounds; import com.sun.javafx.geom.transform.BaseTransform; import com.sun.javafx.jmx.MXNodeAlgorithm; import com.sun.javafx.jmx.MXNodeAlgorithmContext; import com.sun.javafx.scene.NodeHelper; import javafx.css.CompoundSelector; import javafx.css.CssMetaData; import javafx.css.CssParser; import javafx.css.CssParserShim; import javafx.css.Declaration; import javafx.css.DeclarationShim; import javafx.css.FontCssMetaData; import javafx.css.ParsedValue; import javafx.css.PseudoClass; import javafx.css.Rule; import javafx.css.RuleShim; import javafx.css.Selector; import javafx.css.SelectorShim; import javafx.css.SimpleSelector; import javafx.css.SimpleSelectorShim; import javafx.css.Style; import javafx.css.StyleConverter; import javafx.css.StyleOrigin; import javafx.css.Styleable; import javafx.css.StyleableProperty; import javafx.css.Stylesheet; import javafx.css.StylesheetShim; import org.junit.Test; import static org.junit.Assert.*; public class CssMetaDataTest { public CssMetaDataTest() { } private static CssMetaData get(List> list, String prop) { for (CssMetaData styleable : list) { if (prop.equals(styleable.getProperty())) return styleable; } return null; } /** * Test of getCssMetaData method of class Styleable. */ @Test public void testGetCssMetaData_Styleable() { Styleable styleable = new TestNode(); List> expResult = TestNode.getClassCssMetaData(); List result = styleable.getCssMetaData(); assertEquals(expResult, result); } @Test public void testGetJavafxBeansProperty() { TestNode testNode = new TestNode(); WritableValue prop = TestNodeBase.StyleableProperties.TEST.getStyleableProperty(testNode); assert(prop != null); CssMetaData result = ((StyleableProperty)prop).getCssMetaData(); assert(result == TestNodeBase.StyleableProperties.TEST); } /** * Test of getProperty method, of class CssMetaData. */ @Test public void testGetProperty() { String expResult = "-fx-test"; String result = TestNodeBase.StyleableProperties.TEST.getProperty(); assertEquals(expResult, result); } /** * Test of getConverter method, of class CssMetaData. */ @Test public void testGetConverter() { StyleConverter expResult = BooleanConverter.getInstance(); StyleConverter result = TestNodeBase.StyleableProperties.TEST.getConverter(); assertEquals(expResult, result); } /** * Test of getInitialValue method, of class CssMetaData. */ @Test public void testGetInitialValue() { TestNode testNode = new TestNode(); Double expResult = testNode.getXyzzy(); Double result = (Double)TestNode.StyleableProperties.XYZZY.getInitialValue(testNode); assertEquals(expResult, result); } /** * Test of getSubProperties method, of class CssMetaData. */ @Test public void testGetSubProperties() { CssMetaData fontProp = new FontCssMetaData("-fx-font", Font.getDefault()) { @Override public boolean isSettable(TestNode n) { return true; } @Override public StyleableProperty getStyleableProperty(TestNode n) { return null; } }; List> list = fontProp.getSubProperties(); assertNotNull(list); } /** * Test of isInherits method, of class CssMetaData. */ @Test public void testIsInherits() { boolean expResult = false; boolean result = TestNode.StyleableProperties.XYZZY.isInherits(); assertEquals(expResult, result); } /** * Test of toString method, of class CssMetaData. */ @Test public void testToString() { CssMetaData fontProp = new FontCssMetaData("-fx-font", Font.getDefault()) { @Override public boolean isSettable(TestNode n) { return true; } @Override public StyleableProperty getStyleableProperty(TestNode n) { return null; } }; String string = fontProp.toString(); assertNotNull(string); } /** * Test of equals method, of class CssMetaData. */ @Test public void testEquals() { TestNode testNode = new TestNode(); Node node = new Node() { }; CssMetaData testNodeOpacity = get(TestNode.getClassCssMetaData(), "-fx-opacity"); CssMetaData nodeOpacity = get(Node.getClassCssMetaData(), "-fx-opacity"); assertTrue(testNodeOpacity.equals(nodeOpacity)); } static int ord = 0; static CascadingStyle createCascadingStyle(Selector selector, Declaration declaration) { Set pseudoClasses = null; if (selector instanceof SimpleSelector) { pseudoClasses = SimpleSelectorShim.getPseudoClassStates((SimpleSelector)selector); } else { pseudoClasses = new PseudoClassState(); for (SimpleSelector sel : ((CompoundSelector)selector).getSelectors()) { Set selectorPseudoClasses = SimpleSelectorShim.getPseudoClassStates(sel); pseudoClasses.addAll(selectorPseudoClasses); } } return new CascadingStyle( new Style(selector, declaration), pseudoClasses, 0, ord++ ); } @Test @org.junit.Ignore public void testGetMatchingStyles() { final Stylesheet stylesheet = StylesheetShim.getStylesheet(); stylesheet.setOrigin(StyleOrigin.USER_AGENT); StyleManager.getInstance().getInstance().setDefaultUserAgentStylesheet(stylesheet); final List rules = stylesheet.getRules(); // // .root { -fx-base: red; -fx-color: -fx-base; } // List rootStyleClass = new ArrayList(); rootStyleClass.add("root"); Selector root = SimpleSelectorShim.getSimpleSelector("*", rootStyleClass, null, null); ParsedValue fxBaseValue = new CssParserShim().parseExpr("-fx-base", "red"); Declaration fxBase = DeclarationShim.getDeclaration("-fx-base", fxBaseValue, false); ParsedValueImpl fxColorValue = new ParsedValueImpl(fxBase.getProperty(), null, true); Declaration fxColor = DeclarationShim.getDeclaration("-fx-color", fxColorValue, false); List selectors = new ArrayList(); Collections.addAll(selectors, root); List declarations = new ArrayList(); Collections.addAll(declarations, fxBase, fxColor); Rule baseRule = RuleShim.getRule(selectors, declarations); rules.add(baseRule); // // .rect { -fx-fill: -fx-color; } // List rectStyleClass = new ArrayList(); rectStyleClass.add("rect"); Selector rect = SimpleSelectorShim.getSimpleSelector("*", rectStyleClass, null, null); ParsedValue fxFillValue = new CssParserShim().parseExpr("-fx-fill", "-fx-color"); Declaration fxFill = DeclarationShim.getDeclaration("-fx-fill", fxFillValue, false); selectors = new ArrayList(); Collections.addAll(selectors, rect); declarations = new ArrayList(); Collections.addAll(declarations, fxFill); Rule rectRule = RuleShim.getRule(selectors, declarations); rules.add(rectRule); // .rect:hover { -fx-fill: yellow; } List pseudoclasses = new ArrayList(); pseudoclasses.add("hover"); Selector rectHover = SimpleSelectorShim.getSimpleSelector("*", rectStyleClass, pseudoclasses, null); ParsedValueImpl fxFillHoverValue = new ParsedValueImpl(Color.YELLOW, null); Declaration fxFillHover = DeclarationShim.getDeclaration("-fx-fill", fxFillHoverValue, false); selectors = new ArrayList(); Collections.addAll(selectors, rectHover); declarations = new ArrayList(); Collections.addAll(declarations, fxFillHover); Rule rectHoverRule = RuleShim.getRule(selectors, declarations); rules.add(rectHoverRule); List