/* * Copyright (c) 2012, 2014, 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 com.sun.javafx.css; import com.sun.javafx.css.parser.CSSParser; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.SubScene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.PopupWindow; import javafx.stage.Window; import org.junit.Before; import org.junit.Test; import java.net.URL; import java.util.List; import java.util.Map; import static org.junit.Assert.*; /** * * @author dgrieve */ public class StyleManagerTest { public StyleManagerTest() { } @Before public void setUp() { StyleManager sm = StyleManager.getInstance(); sm.userAgentStylesheetContainers.clear(); sm.platformUserAgentStylesheetContainers.clear(); sm.stylesheetContainerMap.clear(); sm.cacheContainerMap.clear(); sm.hasDefaultUserAgentStylesheet = false; } @Test public void testMethod_getInstance() { Scene scene = new Scene(new Group()); StyleManager sm = StyleManager.getInstance(); assertNotNull(sm); } private static int indexOf(final List list, final String fname) { for (int n=0, nMax=list.size(); n> styleMap = matchingStyles.getCascadingStyles(); assertTrue(styleMap.containsKey("-fx-fill")); List styles = styleMap.get("-fx-fill"); assertEquals(1, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.RED, obj); } @Test public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet() { StyleManager sm = StyleManager.getInstance(); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; Scene scene = new Scene(new Group(rect)); scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null); Map> styleMap = matchingStyles.getCascadingStyles(); // scene stylesheet should totally replace default assertFalse(styleMap.containsKey("-fx-fill")); assertTrue(styleMap.containsKey("-fx-stroke")); List styles = styleMap.get("-fx-stroke"); assertEquals(1, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.YELLOW, obj); } @Test public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet_sceneAuthorStylesheet() { StyleManager sm = StyleManager.getInstance(); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; Scene scene = new Scene(new Group(rect)); scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); scene.getStylesheets().add("/com/sun/javafx/css/ua2.css"); StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null); Map> styleMap = matchingStyles.getCascadingStyles(); // ua2.css has fill assertTrue(styleMap.containsKey("-fx-fill")); assertTrue(styleMap.containsKey("-fx-stroke")); // ua1.css and ua2.css have stroke List styles = styleMap.get("-fx-stroke"); assertEquals(2, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.GREEN, obj); // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0 // since we have a scene user-agent stylesheet styles = styleMap.get("-fx-fill"); assertEquals(1, styles.size()); obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.BLUE, obj); } @Test public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet() { StyleManager sm = StyleManager.getInstance(); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; Pane subSceneRoot = new Pane(rect); SubScene subScene = new SubScene(subSceneRoot, 100, 100); subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); Scene scene = new Scene(new Group(subScene)); StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); Map> styleMap = matchingStyles.getCascadingStyles(); // SubScene stylesheet should totally replace default assertFalse(styleMap.containsKey("-fx-fill")); assertTrue(styleMap.containsKey("-fx-stroke")); List styles = styleMap.get("-fx-stroke"); assertEquals(1, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.YELLOW, obj); } @Test public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet_parentStylesheet() { StyleManager sm = StyleManager.getInstance(); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; Group group = new Group(rect); group.getStylesheets().add("/com/sun/javafx/css/ua2.css"); Pane subSceneRoot = new Pane(group); SubScene subScene = new SubScene(subSceneRoot, 100, 100); subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); Scene scene = new Scene(new Group(subScene)); StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); Map> styleMap = matchingStyles.getCascadingStyles(); // ua2.css has fill assertTrue(styleMap.containsKey("-fx-fill")); assertTrue(styleMap.containsKey("-fx-stroke")); // ua1.css and ua2.css have stroke List styles = styleMap.get("-fx-stroke"); assertEquals(2, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.GREEN, obj); // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0 // since we have a scene user-agent stylesheet styles = styleMap.get("-fx-fill"); assertEquals(1, styles.size()); obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.BLUE, obj); } @Test public void testSwitchDefaultUserAgentStylesheets() { Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; Group group = new Group(rect); Pane subSceneRoot = new Pane(group); SubScene subScene = new SubScene(subSceneRoot, 100, 100); Scene scene = new Scene(new Group(subScene)); StyleManager sm = StyleManager.getInstance(); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); Map> styleMap = matchingStyles.getCascadingStyles(); // ua0.css has fill assertTrue(styleMap.containsKey("-fx-fill")); assertFalse(styleMap.containsKey("-fx-stroke")); List styles = styleMap.get("-fx-fill"); assertEquals(1, styles.size()); Object obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.RED, obj); sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); matchingStyles = sm.findMatchingStyles(rect, subScene, null); styleMap = matchingStyles.getCascadingStyles(); // ua1.css has stroke assertTrue(styleMap.containsKey("-fx-stroke")); assertFalse(styleMap.containsKey("-fx-fill")); styles = styleMap.get("-fx-stroke"); assertEquals(1, styles.size()); obj = styles.get(0).getParsedValueImpl().convert(null); assertEquals(Color.YELLOW, obj); } @Test public void testGetCacheContainer() { Rectangle rectangle = new Rectangle(); SubScene subScene = new SubScene(null, 0, 0); StyleManager sm = StyleManager.getInstance(); // no scene, should return null StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, subScene); assertNull(container); // has scene, should return non-null subScene.setRoot(new Group()); Scene scene = new Scene(new Group(rectangle,subScene)); container = sm.getCacheContainer(rectangle, subScene); assertNotNull(container); } @Test public void testGetCacheContainer_styleable() { Rectangle rectangle = new Rectangle(); StyleManager sm = StyleManager.getInstance(); // no scene, should return null StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, null); assertNull(container); // has scene, should return non-null Scene scene = new Scene(new Group(rectangle)); container = sm.getCacheContainer(rectangle, null); assertNotNull(container); } @Test public void testGetCacheContainer_subScene() { SubScene subScene = new SubScene(null, 0, 0); StyleManager sm = StyleManager.getInstance(); // no scene, should return null StyleManager.CacheContainer container = sm.getCacheContainer(null, subScene); assertNull(container); // has scene, should return non-null subScene.setRoot(new Group()); Scene scene = new Scene(new Group(subScene)); container = sm.getCacheContainer(null, subScene); assertNotNull(container); } @Test public void testRT_37025() { // // The issue in RT-37025 was that the stylesheet container wasn't getting removed even // though the parent had been forgotten. The StyleManager#forget(Parent) method didn't // look to see if _any_ stylesheet container had the parent as a reference. // final StyleManager sm = StyleManager.getInstance(); // This test needs a bit more complexity to the scene-graph Group group = null; Pane pane = new Pane( new Group( new Pane( // I want these to be a Parent, not a Node new Group(new Pane(){{ getStyleClass().add("rect"); }}), group = new Group(new Pane(){{ getStyleClass().add("rect"); }}) ) ) ); pane.getStylesheets().add("/com/sun/javafx/css/ua0.css"); group.getStylesheets().add("/com/sun/javafx/css/ua1.css"); Group root = new Group(pane); Scene scene = new Scene(root); root.applyCss(); assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css")); StyleManager.StylesheetContainer container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua0.css"); assertEquals(7, container.parentUsers.list.size()); assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua1.css"); assertEquals(2, container.parentUsers.list.size()); ((Pane)group.getParent()).getChildren().remove(group); assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css")); container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua0.css"); assertEquals(5, container.parentUsers.list.size()); scene.setRoot(new Group()); assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css")); assertFalse(StyleManager.cacheContainerMap.containsKey(root)); assertTrue(StyleManager.cacheContainerMap.containsKey(scene.getRoot())); } }