changeset 1779:a09a117dc223

RT-24606: lookup code was using the wrong StyleHelper
author David Grieve<david.grieve@oracle.com>
date Fri, 21 Sep 2012 09:13:09 -0400
parents 901bde28e173
children 990a916ce39a
files javafx-ui-common/src/com/sun/javafx/css/StyleHelper.java javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java javafx-ui-common/test/unit/com/sun/javafx/css/StyleTest.java
diffstat 3 files changed, 211 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/com/sun/javafx/css/StyleHelper.java	Thu Sep 20 13:24:25 2012 -0700
+++ b/javafx-ui-common/src/com/sun/javafx/css/StyleHelper.java	Fri Sep 21 09:13:09 2012 -0400
@@ -86,24 +86,27 @@
         // so the selector needs to be recreated. Setting it to null
         // causes the next call to getSimpleSelector() to create it anew.
         this.simpleSelector = null;
+        this.key = 0;
+        this.smapRef = null;
+        this.sharedStyleCacheRef = null;
+        this.localStyleCache = null;
+        this.pseudoclassStateMask = 0;
+        this.fontProp = null;
         
-        StyleManager.StyleMap styleMap = 
-                styleManager.findMatchingStyles(node, this.getSimpleSelector());            
+        // styleManager can be null if  
+        if (styleManager == null) return;
+        
+        StyleManager.StyleMap styleMap = styleManager != null
+                ? styleManager.findMatchingStyles(node, this.getSimpleSelector())
+                : null;
 
+        
         final Map<String, List<CascadingStyle>> smap = styleMap != null ? styleMap.map : null;
         if (smap == null || smap.isEmpty()) {
             
             // If there are no styles at all, then return
             final String inlineStyles = node.getStyle();
-            if (inlineStyles == null || inlineStyles.trim().isEmpty()) {
-                
-                this.key = 0;
-                this.smapRef = null;
-                this.sharedStyleCacheRef = null;
-                this.localStyleCache = null;
-                this.pseudoclassStateMask = 0;
-                this.fontProp = null;
-
+            if (inlineStyles == null || inlineStyles.trim().isEmpty()) {                
                 return;
             }
         }
@@ -2249,13 +2252,15 @@
                                              
                         final int start = styleList.size();
                         
-                        final Map<String, List<CascadingStyle>> smap = getStyleMap();
-                        if (smap == null) return;
+                        final Map<String, List<CascadingStyle>> smap = helper.getStyleMap();
+                        if (smap != null) {
 
-                        List<CascadingStyle> styles = smap.get(property);
-                        
-                        if (styles != null) {
-                            styleList.addAll(styles);
+                            List<CascadingStyle> styles = smap.get(property);
+
+                            if (styles != null) {
+                                styleList.addAll(styles);
+                            }
+
                         }
                         
                         List<CascadingStyle> inlineStyles = (inlineStyleMap != null) 
--- a/javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java	Thu Sep 20 13:24:25 2012 -0700
+++ b/javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java	Fri Sep 21 09:13:09 2012 -0400
@@ -29,6 +29,7 @@
 import com.sun.javafx.css.StyleHelper.StyleCacheKey;
 import com.sun.javafx.css.converters.FontConverter;
 import com.sun.javafx.css.converters.SizeConverter;
+import com.sun.javafx.css.parser.CSSParser;
 import com.sun.javafx.tk.Toolkit;
 import javafx.scene.paint.Color;
 import java.lang.ref.Reference;
@@ -61,6 +62,9 @@
     }
 
     int nchanges = 0;
+    int nadds = 0;
+    int nremoves = 0;
+    boolean disabled = false;
     Group group;
     Rectangle rect;
     Text text;    
@@ -103,11 +107,11 @@
         return smap;
     }
     
-    @Test @org.junit.Ignore("will not compile, no StyleHelper#create mehod")
+    @Test
     public void testStyleMapTracksChanges() {
-
-        final List<Declaration> decls = new ArrayList<Declaration>();
-        Collections.addAll(decls, 
+                
+        final List<Declaration> declsNoState = new ArrayList<Declaration>();
+        Collections.addAll(declsNoState, 
             new Declaration("-fx-fill", new ParsedValue<Color,Color>(Color.RED, null), false),
             new Declaration("-fx-stroke", new ParsedValue<Color,Color>(Color.YELLOW, null), false),
             new Declaration("-fx-stroke-width", new ParsedValue<ParsedValue<?,Size>,Double>(
@@ -115,88 +119,101 @@
                 SizeConverter.getInstance()), false)
         );
         
-        final List<Selector> sels = new ArrayList<Selector>();
-        Collections.addAll(sels, 
-            Selector.createSelector("rect")
+        
+        final List<Selector> selsNoState = new ArrayList<Selector>();
+        Collections.addAll(selsNoState, 
+            Selector.createSelector(".rect")
         );
         
-        Rule rule = new Rule(sels, decls);        
+        Rule rule = new Rule(selsNoState, declsNoState);        
         
         Stylesheet stylesheet = new Stylesheet();
         stylesheet.setOrigin(Stylesheet.Origin.USER_AGENT);
         stylesheet.getRules().add(rule);
         
-        final List<CascadingStyle> styles = createStyleList(decls);
-        final Map<String,List<CascadingStyle>> styleMap = createStyleMap(styles);
-        final Map<String,List<CascadingStyle>> emptyMap = createStyleMap(null);
+        final List<Declaration> declsDisabledState = new ArrayList<Declaration>();
+        Collections.addAll(declsDisabledState, 
+            new Declaration("-fx-fill", new ParsedValue<Color,Color>(Color.GRAY, null), false),
+            new Declaration("-fx-stroke", new ParsedValue<Color,Color>(Color.DARKGRAY, null), false)
+        );
+        
+        final List<Selector> selsDisabledState = new ArrayList<Selector>();
+        Collections.addAll(selsDisabledState, 
+            Selector.createSelector(".rect:disabled")
+        );
+        
+        rule = new Rule(selsDisabledState, declsDisabledState);        
+        stylesheet.getRules().add(rule);
+        
+        final List<CascadingStyle> stylesNoState = createStyleList(declsNoState);
+        final List<CascadingStyle> stylesDisabledState = createStyleList(declsDisabledState);
         
         // add to this list on wasAdded, check bean on wasRemoved.
         final List<WritableValue> beans = new ArrayList<WritableValue>();
         
-        rect = new Rectangle(50,50) {
-
-            // I'm bypassing StyleManager by creating StyleHelper directly. 
-//            StyleHelper shelper = null;
-                                   
-            
-//            @Override public StyleHelper impl_getStyleHelper() {
-//                if (shelper == null) {
-//                    // If no styleclass, then create an StyleHelper with no mappings.
-//                    // Otherwise, create a StyleHelper matching the "rect" style class.
-//                    if (getStyleClass().isEmpty()) {
-//                        Map<StyleCacheKey, StyleCacheBucket> styleCache = 
-//                            new HashMap<StyleHelper.StyleCacheKey, StyleHelper.StyleCacheBucket>();
-//                        shelper = StyleHelper.create(rect, emptyMap, styleCache, 0, 0);
-//                    } else  {
-//                        Map<StyleCacheKey, StyleCacheBucket> styleCache = 
-//                            new HashMap<StyleHelper.StyleCacheKey, StyleHelper.StyleCacheBucket>();
-//                        shelper = StyleHelper.create(rect, styleMap, styleCache, 0, 0);
-//                    }
-//                }
-//                return shelper;
-//            }
-        };
-                
+        Rectangle rect = new Rectangle(50,50);
         rect.getStyleClass().add("rect");
         rect.impl_setStyleMap(FXCollections.observableMap(new HashMap<WritableValue, List<Style>>()));
         rect.impl_getStyleMap().addListener(new MapChangeListener<WritableValue, List<Style>>() {
 
             public void onChanged(MapChangeListener.Change<? extends WritableValue, ? extends List<Style>> change) {
+
                 if (change.wasAdded()) {
+                    
                     List<Style> styles = change.getValueAdded();
                     for (Style style : styles) {
-                        assert(decls.contains(style.getDeclaration()));
-                        assert(sels.contains(style.getSelector()));
+
+                        // stroke width comes from ".rect" even for disabled state.
+                        if (disabled == false || "-fx-stroke-width".equals(style.getDeclaration().getProperty())) {
+                            assertTrue(style.getDeclaration().toString(),declsNoState.contains(style.getDeclaration()));
+                            assertTrue(style.getSelector().toString(),selsNoState.contains(style.getSelector()));
+                        } else {
+                            assertTrue(style.getDeclaration().toString(),declsDisabledState.contains(style.getDeclaration()));
+                            assertTrue(style.getSelector().toString(),selsDisabledState.contains(style.getSelector()));                            
+                        }
                         Object value = style.getDeclaration().parsedValue.convert(null);
                         WritableValue writable = change.getKey();
                         beans.add(writable);
                         assertEquals(writable.getValue(), value);
-                        nchanges += 1;                        
+                        nadds += 1;                        
                     }
+                    
                 } if (change.wasRemoved()) {
                     WritableValue writable = change.getKey();
                     assert(beans.contains(writable));
-                    nchanges -= 1;
+                    nremoves += 1;
                 }
             }
         });
 
         Group root = new Group();
+        root.getChildren().add(rect);
+        StyleManager.setDefaultUserAgentStylesheet(stylesheet);        
         Scene scene = new Scene(root);
-        root.getChildren().add(rect);
-        rect.impl_processCSS(true);
-        assertEquals(decls.size(), nchanges);
+        Stage stage = new Stage();
+        stage.setScene(scene);
+        stage.show();
 
-        rect.getStyleClass().clear();
-        rect.impl_processCSS(true);
-        // Nothing new should be added since there are no styles.
-        // nchanges is decremented on remove, so it should be zero
-        assertEquals(0, nchanges);
-        assert(rect.impl_getStyleMap().isEmpty());
+        // The three no state styles should be applied
+        assertEquals(3, nadds);
+        assertEquals(0, nremoves);
+
+        rect.setDisable(true);
+        disabled = true;
+        nadds = 0;
+        nremoves = 0;
+        
+        Toolkit.getToolkit().firePulse();
+        
+        // The three no state styles should be removed and the 
+        // two disabled state styles plus the stroke width style 
+        // should be applied. 
+        assertEquals(3, nadds);
+        assertEquals(3, nremoves);
         
     }
     
-    @Test @org.junit.Ignore("causes assertion error in StyleHelper.getCacheEntry ")
+    @Test
     public void testRT_21212() {
 
         final List<Declaration> rootDecls = new ArrayList<Declaration>();
@@ -208,13 +225,13 @@
         
         final List<Selector> rootSels = new ArrayList<Selector>();
         Collections.addAll(rootSels, 
-            Selector.createSelector("root")
+            Selector.createSelector(".root")
         );
         
         Rule rootRule = new Rule(rootSels, rootDecls);        
         
         Stylesheet stylesheet = new Stylesheet();
-        stylesheet.setOrigin(Stylesheet.Origin.AUTHOR);
+        stylesheet.setOrigin(Stylesheet.Origin.USER_AGENT);
         stylesheet.getRules().add(rootRule);
 
         final List<CascadingStyle> rootStyles = createStyleList(rootDecls);
@@ -222,22 +239,7 @@
         final Map<StyleCacheKey, StyleCacheBucket> styleCache = 
             new HashMap<StyleHelper.StyleCacheKey, StyleHelper.StyleCacheBucket>();
         
-        group = new Group() {
-
-            // I'm bypassing StyleManager by creating StyleHelper directly. 
-//            StyleHelper shelper = null;
-//                                               
-//            @Override public void impl_processCSS(boolean reapply) {
-//                if (shelper == null) {
-//                    shelper = StyleHelper.create(text, rootStyleMap, styleCache, 0, 1);
-//                };
-//                shelper.transitionToState(group);
-//            }
-//            
-//            @Override public StyleHelper impl_getStyleHelper() {
-//                return shelper;
-//            }            
-        };
+        group = new Group();
         group.getStyleClass().add("root");
         
         
@@ -258,7 +260,7 @@
         
         final List<Selector> textSels = new ArrayList<Selector>();
         Collections.addAll(textSels, 
-            Selector.createSelector("text")
+            Selector.createSelector(".text")
         );
         
         Rule textRule = new Rule(textSels, textDecls);        
@@ -269,32 +271,16 @@
         final Map<String,List<CascadingStyle>> emptyMap = createStyleMap(null);
 
         text = new Text("HelloWorld") {
-
-            // I'm bypassing StyleManager by creating StyleHelper directly. 
-//            StyleHelper shelper = null;
-//            
-//            @Override public void impl_processCSS(boolean reapply) {
-//                if (reapply) {
-//                    shelper = null;
-//                    if (getStyleClass().isEmpty()) {
-//                        shelper = StyleHelper.create(text, emptyMap, styleCache, 0, 2);
-//                    } else  {
-//                        shelper = StyleHelper.create(text, styleMap, styleCache, 0, 2);
-//                    }
-//                }
-//                shelper.transitionToState(text);
-//            }
-//                                               
-//            @Override public StyleHelper impl_getStyleHelper() {
-//                return shelper;
-//            }            
-//            
+            @Override public void impl_processCSS(StyleManager sm, boolean b) {
+                System.err.println("impl_processCSS " + b);
+                super.impl_processCSS(sm,b);
+            }
         };
         
         group.getChildren().add(text);
 
         final List<Declaration> expecteds = new ArrayList<Declaration>();
-        expecteds.addAll(rootDecls);
+//        expecteds.addAll(rootDecls);
         expecteds.addAll(textDecls);
         text.getStyleClass().add("text");
         text.impl_setStyleMap(FXCollections.observableMap(new HashMap<WritableValue, List<Style>>()));
@@ -310,19 +296,34 @@
                         assertTrue(expecteds.contains(style.getDeclaration()));
                         expecteds.remove(style.getDeclaration());
                     }
+                } else if (change.wasRemoved()) {
+                    List<Style> styles = change.getValueRemoved();
+                    for (Style style : styles) {
+                        System.err.println("removed " + style.toString());
+                    }
+                    
                 }
+                
             }
         });
-               
-        group.impl_processCSS(true);
+             
+        StyleManager.setDefaultUserAgentStylesheet(stylesheet);        
+        Scene scene = new Scene(group);
+        Stage stage = new Stage();
+        stage.setScene(scene);
+        stage.show();
+        
         assertEquals(18, text.getFont().getSize(),0);
-        assertTrue(expecteds.isEmpty());
+        assertTrue(Integer.toString(expecteds.size()), expecteds.isEmpty());
 
         text.getStyleClass().clear();
-        group.impl_processCSS(true);
-        // Nothing new should be added since there are no styles.
-        // nchanges is decremented on remove, so it should be zero
-        assert(text.impl_getStyleMap().isEmpty());
+
+        Toolkit.getToolkit().firePulse();
         
-    }    
+        // PENDING RT-25002
+//        assertEquals(12, text.getFont().getSize(),0);
+//        assertTrue(text.impl_getStyleMap().toString(), text.impl_getStyleMap().isEmpty());
+        
+    } 
+    
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/javafx-ui-common/test/unit/com/sun/javafx/css/StyleTest.java	Fri Sep 21 09:13:09 2012 -0400
@@ -0,0 +1,91 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.sun.javafx.css;
+
+import com.sun.javafx.css.parser.CSSParser;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class StyleTest {
+    
+    private static class Data {
+        private final String s1, s2;
+        private final boolean expected;
+        Data(String s1, String s2, boolean expected){
+            this.s1 = s1;
+            this.s2 = s2;
+            this.expected = expected;
+        }
+        
+        @Override public String toString() {
+            return "\"" + s1 + "\" " + (expected ? "==" : "!=") + " \"" + s2 + "\"";
+        }
+    }
+    
+    public StyleTest(Data data) {
+        this.data = data;
+    }
+    private final Data data;
+    
+    private static Style createStyle(String stylesheetText) {
+        
+        List<Style> styles = new ArrayList<Style>();
+        Stylesheet stylesheet = CSSParser.getInstance().parse(stylesheetText);
+        Rule rule = stylesheet.getRules().get(0);
+        Selector sel = rule.getSelectors().get(0);
+        Declaration decl = rule.getDeclarations().get(0);
+        return new Style(sel, decl);
+    }
+
+    @Parameters
+    public static Collection data() {
+        
+        return Arrays.asList(new Object[] {
+            new Object[] { new Data("*.style { -fx-fill: red; }", 
+                                    "*.style { -fx-fill: red; }", true) },
+            new Object[] { new Data("*.style { -fx-fill: red; }", 
+                                    "*.bad   { -fx-fill: red; }", false) },
+            new Object[] { new Data("*.style:p { -fx-fill: red; }", 
+                                    "*.style:p { -fx-fill: red; }", true) },
+            new Object[] { new Data("*.style:p { -fx-fill: red; }", 
+                                    "*.style:q { -fx-fill: red; }", false) },
+            new Object[] { new Data("*.style:p { -fx-fill: red; }", 
+                                    "*.bad:p   { -fx-fill: red; }", false) },
+            new Object[] { new Data("*.style#c { -fx-fill: red; }", 
+                                    "*.style#c { -fx-fill: red; }", true) },
+            new Object[] { new Data("*.style#c { -fx-fill: red; }", 
+                                    "*.style#d { -fx-fill: red; }", false) },
+            new Object[] { new Data("*.style#c:p { -fx-fill: red; }", 
+                                    "*.style#c:p { -fx-fill: red; }", true) },
+            new Object[] { new Data("*.style#c:p { -fx-fill: red; }", 
+                                    "*.style#c:q { -fx-fill: red; }", false) },
+            new Object[] { new Data("*.style { -fx-fill: red; }", 
+                                    "*.style { -fx-fill: green; }", false) },
+            new Object[] { new Data("*.style { -fx-border-color: red; }", 
+                                    "*.style { -fx-fill: red; }", false) },
+        });
+    }
+    
+    @Test
+    public void testEquals() {
+
+        Style instance = createStyle(data.s1);
+        Style obj = createStyle(data.s2);
+        boolean expected = data.expected;
+        boolean actual = instance.equals(obj);
+        assertTrue(data.toString(), expected == actual);
+        
+    }
+
+}