< prev index next >

modules/javafx.graphics/src/test/java/test/javafx/css/CssParserTest.java

Print this page
rev 10372 : 8176404: Remove public test-only convenience method from CssParser
Reviewed-by:


  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 test.javafx.css;
  27 
  28 import com.sun.javafx.css.*;
  29 
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.DataInputStream;
  33 import java.io.DataOutputStream;
  34 import java.io.IOException;
  35 import java.util.Arrays;
  36 import java.util.List;
  37 import java.util.Map;
  38 import javafx.css.CssParser;

  39 import javafx.css.Declaration;
  40 import javafx.css.FontFace;
  41 
  42 import javafx.css.ParsedValue;
  43 import javafx.css.ParsedValue;
  44 import javafx.css.Rule;
  45 import javafx.css.RuleShim;
  46 import javafx.css.Stylesheet;
  47 import javafx.scene.paint.Color;
  48 import javafx.scene.paint.LinearGradient;
  49 import javafx.scene.paint.Paint;
  50 import javafx.scene.paint.Stop;
  51 import javafx.scene.text.Font;
  52 import javafx.util.Duration;
  53 import org.junit.Test;
  54 import static org.junit.Assert.*;
  55 
  56 
  57 public class CssParserTest {
  58 
  59     @Test
  60     public void testRT_16959() {
  61 
  62         CssParser instance = new CssParser();
  63 
  64         // RT-16959 is an infinite loop on incomplete linear gradient
  65         ParsedValue result = instance.parseExpr("-fx-background-color", "linear-gradient(from 0% 0% to 0% 100%, )");

  66         assertNull("parseExpr", result);
  67 
  68         // The bad syntax should be skipped. The stylesheet should have one
  69         // linear gradient with colors red, white, blue.
  70         Stylesheet ss = instance.parse(
  71             "* { "
  72             +   "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, ); "
  73             +   "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, red, white, blue); "
  74             + "}"
  75         );
  76 
  77         assertNotNull(ss);
  78         List<Rule> rules = ss.getRules();
  79         assertEquals(1,rules.size(),0);
  80         List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
  81         assertTrue(decls.size()==1);
  82         Declaration decl = decls.get(0);
  83         ParsedValue value = decl.getParsedValue();
  84         assertTrue(value != null);
  85 


 129         Paint[] layers = (Paint[])value.convert(null);
 130         assertTrue(layers.length == 1);
 131 
 132         LinearGradient lg = (LinearGradient)layers[0];
 133         List<Stop> stops = lg.getStops();
 134         assertTrue(stops.size()==5);
 135         assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(0).getColor());
 136         assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(1).getColor());
 137         assertEquals(Color.rgb(248, 248, 246, 0.8), stops.get(2).getColor());
 138         assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(3).getColor());
 139         assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(4).getColor());
 140 
 141     }
 142 
 143     @Test
 144     public void testParseSizeWithInvalidDigits() {
 145 
 146         CssParser instance = new CssParser();
 147 
 148         // RT-16959 is an infinite loop on incomplete linear gradient
 149         ParsedValue result = instance.parseExpr("-fx-font-size", "10ptx");
 150         assertNull("parseExpr", result);
 151 
 152         // The bad syntax should be skipped.
 153         Stylesheet ss = instance.parse(
 154             "* {"
 155             +  "-fx-font-size: 10ptx; "
 156             +  "-fx-font-size: 12px; "
 157             + "}"
 158         );
 159 
 160         assertNotNull(ss);
 161         List<Rule> rules = ss.getRules();
 162         assertEquals(1,rules.size(),0);
 163         List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
 164         assertTrue(decls.size()==1);
 165         Declaration decl = decls.get(0);
 166         ParsedValue value = decl.getParsedValue();
 167         assertTrue(value != null);
 168 
 169         Double size = (Double)value.convert(Font.font("Amble", 12));


 273                     assertNull(fontFaceSrc.getFormat());
 274                     break;
 275                 }
 276                 case URL: {
 277                     String src = fontFaceSrc.getSrc();
 278                     assertEquals(src, "http://themes.googleusercontent.com/static/fonts/breeserif/v2/LQ7WLTaITDg4OSRuOZCps73hpw3pgy2gAi-Ip7WPMi0.woff");
 279                     assertEquals(fontFaceSrc.getFormat(), "woff");
 280                     break;
 281                 }
 282                 case REFERENCE:
 283                 default:
 284                         fail();
 285             }
 286         }
 287 
 288         return nFontFaceSrcs;
 289     }
 290 
 291     @Test public void testRT_32522() {
 292 
 293         ParsedValue value = new CssParser().parseExpr("foo", "1 2em 3 4;");
 294         Object obj = value.convert(Font.font(13));
 295         assert obj instanceof Number[];
 296         assertArrayEquals(new Number[] {1d, 26d, 3d, 4d}, (Number[])obj);
 297 
 298         value = new CssParser().parseExpr("foo", "1;");
 299         obj = value.convert(null);
 300         assert obj instanceof Number;
 301         assertEquals(1d, (Number)obj);
 302 
 303     }
 304 
 305     @Test public void testRT_38483() {
 306 
 307         Duration expected = Duration.millis(42);
 308         ParsedValue value = new CssParser().parseExpr("foo", "42ms;");
 309         Object observed = value.convert(null);
 310         assertEquals(expected, observed);
 311 
 312         value = new CssParser().parseExpr("foo", "indefinite;");
 313         observed = value.convert(null);
 314         assertEquals(Duration.INDEFINITE, observed);
 315     }
 316 }


  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 test.javafx.css;
  27 
  28 import com.sun.javafx.css.*;
  29 
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.DataInputStream;
  33 import java.io.DataOutputStream;
  34 import java.io.IOException;
  35 import java.util.Arrays;
  36 import java.util.List;
  37 import java.util.Map;
  38 import javafx.css.CssParser;
  39 import javafx.css.CssParserShim;
  40 import javafx.css.Declaration;
  41 import javafx.css.FontFace;
  42 
  43 import javafx.css.ParsedValue;
  44 import javafx.css.ParsedValue;
  45 import javafx.css.Rule;
  46 import javafx.css.RuleShim;
  47 import javafx.css.Stylesheet;
  48 import javafx.scene.paint.Color;
  49 import javafx.scene.paint.LinearGradient;
  50 import javafx.scene.paint.Paint;
  51 import javafx.scene.paint.Stop;
  52 import javafx.scene.text.Font;
  53 import javafx.util.Duration;
  54 import org.junit.Test;
  55 import static org.junit.Assert.*;
  56 
  57 
  58 public class CssParserTest {
  59 
  60     @Test
  61     public void testRT_16959() {
  62 
  63         CssParser instance = new CssParser();
  64 
  65         // RT-16959 is an infinite loop on incomplete linear gradient
  66         ParsedValue result = new CssParserShim(instance)
  67                 .parseExpr("-fx-background-color", "linear-gradient(from 0% 0% to 0% 100%, )");
  68         assertNull("parseExpr", result);
  69 
  70         // The bad syntax should be skipped. The stylesheet should have one
  71         // linear gradient with colors red, white, blue.
  72         Stylesheet ss = instance.parse(
  73             "* { "
  74             +   "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, ); "
  75             +   "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, red, white, blue); "
  76             + "}"
  77         );
  78 
  79         assertNotNull(ss);
  80         List<Rule> rules = ss.getRules();
  81         assertEquals(1,rules.size(),0);
  82         List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
  83         assertTrue(decls.size()==1);
  84         Declaration decl = decls.get(0);
  85         ParsedValue value = decl.getParsedValue();
  86         assertTrue(value != null);
  87 


 131         Paint[] layers = (Paint[])value.convert(null);
 132         assertTrue(layers.length == 1);
 133 
 134         LinearGradient lg = (LinearGradient)layers[0];
 135         List<Stop> stops = lg.getStops();
 136         assertTrue(stops.size()==5);
 137         assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(0).getColor());
 138         assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(1).getColor());
 139         assertEquals(Color.rgb(248, 248, 246, 0.8), stops.get(2).getColor());
 140         assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(3).getColor());
 141         assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(4).getColor());
 142 
 143     }
 144 
 145     @Test
 146     public void testParseSizeWithInvalidDigits() {
 147 
 148         CssParser instance = new CssParser();
 149 
 150         // RT-16959 is an infinite loop on incomplete linear gradient
 151         ParsedValue result = new CssParserShim(instance).parseExpr("-fx-font-size", "10ptx");
 152         assertNull("parseExpr", result);
 153 
 154         // The bad syntax should be skipped.
 155         Stylesheet ss = instance.parse(
 156             "* {"
 157             +  "-fx-font-size: 10ptx; "
 158             +  "-fx-font-size: 12px; "
 159             + "}"
 160         );
 161 
 162         assertNotNull(ss);
 163         List<Rule> rules = ss.getRules();
 164         assertEquals(1,rules.size(),0);
 165         List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
 166         assertTrue(decls.size()==1);
 167         Declaration decl = decls.get(0);
 168         ParsedValue value = decl.getParsedValue();
 169         assertTrue(value != null);
 170 
 171         Double size = (Double)value.convert(Font.font("Amble", 12));


 275                     assertNull(fontFaceSrc.getFormat());
 276                     break;
 277                 }
 278                 case URL: {
 279                     String src = fontFaceSrc.getSrc();
 280                     assertEquals(src, "http://themes.googleusercontent.com/static/fonts/breeserif/v2/LQ7WLTaITDg4OSRuOZCps73hpw3pgy2gAi-Ip7WPMi0.woff");
 281                     assertEquals(fontFaceSrc.getFormat(), "woff");
 282                     break;
 283                 }
 284                 case REFERENCE:
 285                 default:
 286                         fail();
 287             }
 288         }
 289 
 290         return nFontFaceSrcs;
 291     }
 292 
 293     @Test public void testRT_32522() {
 294 
 295         ParsedValue value = new CssParserShim().parseExpr("foo", "1 2em 3 4;");
 296         Object obj = value.convert(Font.font(13));
 297         assert obj instanceof Number[];
 298         assertArrayEquals(new Number[] {1d, 26d, 3d, 4d}, (Number[])obj);
 299 
 300         value = new CssParserShim().parseExpr("foo", "1;");
 301         obj = value.convert(null);
 302         assert obj instanceof Number;
 303         assertEquals(1d, (Number)obj);
 304 
 305     }
 306 
 307     @Test public void testRT_38483() {
 308 
 309         Duration expected = Duration.millis(42);
 310         ParsedValue value = new CssParserShim().parseExpr("foo", "42ms;");
 311         Object observed = value.convert(null);
 312         assertEquals(expected, observed);
 313 
 314         value = new CssParserShim().parseExpr("foo", "indefinite;");
 315         observed = value.convert(null);
 316         assertEquals(Duration.INDEFINITE, observed);
 317     }
 318 }
< prev index next >