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 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
39 import javafx.css.ParsedValue;
40 import javafx.scene.paint.Color;
41 import javafx.scene.paint.LinearGradient;
42 import javafx.scene.paint.Paint;
43 import javafx.scene.paint.Stop;
44 import javafx.scene.text.Font;
45 import javafx.util.Duration;
46 import org.junit.Test;
47 import static org.junit.Assert.*;
48
49
50 public class CssParserTest {
51
52 @Test
53 public void testRT_16959() {
54
55 CssParser instance = new CssParser();
56
57 // RT-16959 is an infinite loop on incomplete linear gradient
58 ParsedValue result = instance.parseExpr("-fx-background-color", "linear-gradient(from 0% 0% to 0% 100%, )");
59 assertNull("parseExpr", result);
60
61 // The bad syntax should be skipped. The stylesheet should have one
62 // linear gradient with colors red, white, blue.
63 Stylesheet ss = instance.parse(
64 "* { "
65 + "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, ); "
66 + "-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, red, white, blue); "
67 + "}"
68 );
69
70 assertNotNull(ss);
71 List<Rule> rules = ss.getRules();
72 assertEquals(1,rules.size(),0);
73 List<Declaration> decls = ss.getRules().get(0).getUnobservedDeclarationList();
74 assertTrue(decls.size()==1);
75 Declaration decl = decls.get(0);
76 ParsedValue value = decl.getParsedValue();
77 assertTrue(value != null);
78
79 Paint[] layers = (Paint[])value.convert(null);
80 assertTrue(layers.length == 1);
81
82 LinearGradient lg = (LinearGradient)layers[0];
83 List<Stop> stops = lg.getStops();
84 assertTrue(stops.size()==3);
85 assertEquals(Color.RED, stops.get(0).getColor());
86 assertEquals(Color.WHITE, stops.get(1).getColor());
87 assertEquals(Color.BLUE, stops.get(2).getColor());
88
89 }
90
91
92 @Test
93 public void testRT_17770() {
96 // Missing term should be ignored
97 String stylesheetText =
98 "* {"
99 + "-fx-background-color: linear-gradient( "
100 + "to right, "
101 + "rgba(141, 138, 125, 0.0), "
102 + "rgba(248, 248, 246, 0.3) 45%, "
103 + "rgba(248, 248, 246, 0.8) 50%, "
104 + "rgba(248, 248, 246, 0.3) 55%, "
105 + "rgba(141, 138, 125, 0.0), "
106 + "); "
107 + "}";
108
109 CssParser instance = new CssParser();
110
111 Stylesheet ss = instance.parse(stylesheetText);
112
113 assertNotNull(ss);
114 List<Rule> rules = ss.getRules();
115 assertEquals(1,rules.size(),0);
116 List<Declaration> decls = ss.getRules().get(0).getUnobservedDeclarationList();
117 assertTrue(decls.size()==1);
118 Declaration decl = decls.get(0);
119 ParsedValue value = decl.getParsedValue();
120 assertTrue(value != null);
121
122 Paint[] layers = (Paint[])value.convert(null);
123 assertTrue(layers.length == 1);
124
125 LinearGradient lg = (LinearGradient)layers[0];
126 List<Stop> stops = lg.getStops();
127 assertTrue(stops.size()==5);
128 assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(0).getColor());
129 assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(1).getColor());
130 assertEquals(Color.rgb(248, 248, 246, 0.8), stops.get(2).getColor());
131 assertEquals(Color.rgb(248, 248, 246, 0.3), stops.get(3).getColor());
132 assertEquals(Color.rgb(141, 138, 125, 0.0), stops.get(4).getColor());
133
134 }
135
136 @Test
137 public void testParseSizeWithInvalidDigits() {
138
139 CssParser instance = new CssParser();
140
141 // RT-16959 is an infinite loop on incomplete linear gradient
142 ParsedValue result = instance.parseExpr("-fx-font-size", "10ptx");
143 assertNull("parseExpr", result);
144
145 // The bad syntax should be skipped.
146 Stylesheet ss = instance.parse(
147 "* {"
148 + "-fx-font-size: 10ptx; "
149 + "-fx-font-size: 12px; "
150 + "}"
151 );
152
153 assertNotNull(ss);
154 List<Rule> rules = ss.getRules();
155 assertEquals(1,rules.size(),0);
156 List<Declaration> decls = ss.getRules().get(0).getUnobservedDeclarationList();
157 assertTrue(decls.size()==1);
158 Declaration decl = decls.get(0);
159 ParsedValue value = decl.getParsedValue();
160 assertTrue(value != null);
161
162 Double size = (Double)value.convert(Font.font("Amble", 12));
163 assertTrue(Double.compare(size, 12) == 0);
164 }
165
166
167 @Test
168 public void testRT_17830() {
169
170 CssParser instance = new CssParser();
171
172 // The empty declaration should be skipped. The stylesheet should have
173 // two declarations.
174 Stylesheet ss = instance.parse(".rt17830 {-fx-fill: red;; -fx-stroke: yellow; }");
175
176 assertNotNull(ss);
177 List<Rule> rules = ss.getRules();
178 assertEquals(1,rules.size(),0);
179 List<Declaration> decls = ss.getRules().get(0).getUnobservedDeclarationList();
180 assertEquals(2,decls.size(),0);
181
182 Declaration decl = decls.get(0);
183 ParsedValue value = decl.getParsedValue();
184 assertTrue(value != null);
185 Paint paint = (Paint)value.convert(null);
186 assertEquals(Color.RED, paint);
187
188 decl = decls.get(1);
189 value = decl.getParsedValue();
190 assertTrue(value != null);
191 paint = (Paint)value.convert(null);
192 assertEquals(Color.YELLOW, paint);
193 }
194
195 @Test
196 public void testRT_20311() {
197
198 CssParser instance = new CssParser();
199
|
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 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
86 Paint[] layers = (Paint[])value.convert(null);
87 assertTrue(layers.length == 1);
88
89 LinearGradient lg = (LinearGradient)layers[0];
90 List<Stop> stops = lg.getStops();
91 assertTrue(stops.size()==3);
92 assertEquals(Color.RED, stops.get(0).getColor());
93 assertEquals(Color.WHITE, stops.get(1).getColor());
94 assertEquals(Color.BLUE, stops.get(2).getColor());
95
96 }
97
98
99 @Test
100 public void testRT_17770() {
103 // Missing term should be ignored
104 String stylesheetText =
105 "* {"
106 + "-fx-background-color: linear-gradient( "
107 + "to right, "
108 + "rgba(141, 138, 125, 0.0), "
109 + "rgba(248, 248, 246, 0.3) 45%, "
110 + "rgba(248, 248, 246, 0.8) 50%, "
111 + "rgba(248, 248, 246, 0.3) 55%, "
112 + "rgba(141, 138, 125, 0.0), "
113 + "); "
114 + "}";
115
116 CssParser instance = new CssParser();
117
118 Stylesheet ss = instance.parse(stylesheetText);
119
120 assertNotNull(ss);
121 List<Rule> rules = ss.getRules();
122 assertEquals(1,rules.size(),0);
123 List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
124 assertTrue(decls.size()==1);
125 Declaration decl = decls.get(0);
126 ParsedValue value = decl.getParsedValue();
127 assertTrue(value != null);
128
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));
170 assertTrue(Double.compare(size, 12) == 0);
171 }
172
173
174 @Test
175 public void testRT_17830() {
176
177 CssParser instance = new CssParser();
178
179 // The empty declaration should be skipped. The stylesheet should have
180 // two declarations.
181 Stylesheet ss = instance.parse(".rt17830 {-fx-fill: red;; -fx-stroke: yellow; }");
182
183 assertNotNull(ss);
184 List<Rule> rules = ss.getRules();
185 assertEquals(1,rules.size(),0);
186 List<Declaration> decls = RuleShim.getUnobservedDeclarationList(ss.getRules().get(0));
187 assertEquals(2,decls.size(),0);
188
189 Declaration decl = decls.get(0);
190 ParsedValue value = decl.getParsedValue();
191 assertTrue(value != null);
192 Paint paint = (Paint)value.convert(null);
193 assertEquals(Color.RED, paint);
194
195 decl = decls.get(1);
196 value = decl.getParsedValue();
197 assertTrue(value != null);
198 paint = (Paint)value.convert(null);
199 assertEquals(Color.YELLOW, paint);
200 }
201
202 @Test
203 public void testRT_20311() {
204
205 CssParser instance = new CssParser();
206
|