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 com.sun.javafx.css;
27
28 import javafx.css.CssParser;
29 import javafx.css.StyleOrigin;
30 import javafx.css.StyleableProperty;
31 import javafx.css.Stylesheet;
32 import javafx.scene.Group;
33 import javafx.scene.Parent;
34 import javafx.scene.Scene;
35 import javafx.scene.SubScene;
36 import javafx.scene.layout.Pane;
37 import javafx.scene.paint.Color;
38 import javafx.scene.paint.Paint;
39 import javafx.scene.shape.Rectangle;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 import java.net.URL;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.concurrent.atomic.AtomicBoolean;
49
50 import static org.junit.Assert.*;
51
52 /**
53 *
54 * @author dgrieve
55 */
56 public class StyleManagerTest {
57
58 public StyleManagerTest() {
59 }
60
61 @Before
62 public void setUp() {
63 StyleManager sm = StyleManager.getInstance();
64 sm.userAgentStylesheetContainers.clear();
65 sm.platformUserAgentStylesheetContainers.clear();
66 sm.stylesheetContainerMap.clear();
67 sm.cacheContainerMap.clear();
68 sm.hasDefaultUserAgentStylesheet = false;
69 }
70
71 @Test
72 public void testMethod_getInstance() {
73 Scene scene = new Scene(new Group());
74 StyleManager sm = StyleManager.getInstance();
75 assertNotNull(sm);
76 }
77
78 private static int indexOf(final List<StyleManager.StylesheetContainer> list, final String fname) {
79
80 for (int n=0, nMax=list.size(); n<nMax; n++) {
81 StyleManager.StylesheetContainer container = list.get(n);
82 if (fname.equals(container.fname)) {
83 return n;
84 }
85 }
86
87 return -1;
88 }
89
90 @Test
91 public void testAddUserAgentStyleshseet_String() {
92 StyleManager sm = StyleManager.getInstance();
93 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
94 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css");
95 assertEquals(0,index);
96
97 }
98
99 @Test
100 public void testAddUserAgentStyleshseet_String_Multiple() {
101 StyleManager sm = StyleManager.getInstance();
102 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
103 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
104
105 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css");
106 assertEquals(0,index);
107
108 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css");
109 assertEquals(1, index);
110 }
111
112 @Test
113 public void testAddUserAgentStyleshseet_String_Duplicate() {
114 StyleManager sm = StyleManager.getInstance();
115 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
116 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
117 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
118
119 assertTrue(sm.platformUserAgentStylesheetContainers.size() == 2);
120
121 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
122 assertEquals(0,index);
123
124 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
125 assertEquals(1, index);
126
127 }
128
129 @Test
130 public void testSetDefaultUserAgentStyleshseet_String() {
131 StyleManager sm = StyleManager.getInstance();
132 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
133
134 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css");
135 assertEquals(0, index);
136 }
137
138 @Test
139 public void testSetUserAgentStyleshseet_String_Multiple() {
140 StyleManager sm = StyleManager.getInstance();
141 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
142 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
143
144 assertTrue(sm.platformUserAgentStylesheetContainers.size() == 2);
145
146 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css");
147 assertEquals(0,index);
148
149 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css");
150 assertEquals(1, index);
151 }
152
153 @Test
154 public void testSetUserAgentStyleshseet_String_Multiple2() {
155 StyleManager sm = StyleManager.getInstance();
156 // same as before but set default after adding another.
157 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
158 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
159
160 assertEquals(2, sm.platformUserAgentStylesheetContainers.size());
161
162 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
163 assertEquals(0,index);
164
165 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
166 assertEquals(1, index);
167 }
168
169 @Test
170 public void testSetUserAgentStyleshseet_String_Duplicate() {
171 StyleManager sm = StyleManager.getInstance();
172 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
173 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
174 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
175
176 assertEquals(2, sm.platformUserAgentStylesheetContainers.size());
177
178 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css");
179 assertEquals(0,index);
180
181 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css");
182 assertEquals(1, index);
183 }
184
185 @Test
186 public void testAddUserAgentStyleshseet_Stylesheet() {
187
188 try {
189 StyleManager sm = StyleManager.getInstance();
190 URL ua0_url = StyleManagerTest.class.getResource("ua0.css");
191 Stylesheet stylesheet = new CssParser().parse(ua0_url);
192 sm.addUserAgentStylesheet(null,stylesheet);
193
194 assertEquals(1, sm.platformUserAgentStylesheetContainers.size());
195
196 int index = indexOf(sm.platformUserAgentStylesheetContainers,ua0_url.toExternalForm());
197 assertEquals(0, index);
198
199 } catch (Exception ioe) {
200 fail(ioe.getMessage());
201 }
202
203 }
204
205 @Test
206 public void testSetDefaultUserAgentStyleshseet_Stylesheet() {
207
208 try {
209 StyleManager sm = StyleManager.getInstance();
210
211 URL ua1_url = StyleManagerTest.class.getResource("ua1.css");
212 Stylesheet stylesheet = new CssParser().parse(ua1_url);
213 sm.addUserAgentStylesheet(null,stylesheet);
214
215 URL ua0_url = StyleManagerTest.class.getResource("ua0.css");
216 stylesheet = new CssParser().parse(ua0_url);
217 sm.setDefaultUserAgentStylesheet(stylesheet);
218
219 assertEquals(2, sm.platformUserAgentStylesheetContainers.size());
220
221 int index = indexOf(sm.platformUserAgentStylesheetContainers,ua0_url.toExternalForm());
222 assertEquals(0, index);
223
224 index = indexOf(sm.platformUserAgentStylesheetContainers,ua1_url.toExternalForm());
225 assertEquals(1, index);
226
227 } catch (Exception ioe) {
228 fail(ioe.getMessage());
229 }
230
231 }
232
233 @Test
234 public void testSceneUAStylesheetAdded() {
235 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
236 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
237
238 StyleManager sm = StyleManager.getInstance();
239 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
240
241 scene.getRoot().applyCss();
242
243 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
244 assertEquals(0,index);
245
246 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
247 assertEquals(-1, index);
248
249 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
250 assertEquals(0,index);
251
252 // the Scene user-agent stylesheet is not a platform user-agent stylesheet
253 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css");
254 assertEquals(-1, index);
255
256 }
257
258 @Test
259 public void testSubSceneUAStylesheetAdded() {
260 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
261 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
262
263 StyleManager sm = StyleManager.getInstance();
264 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
265
266 scene.getRoot().applyCss();
267
268 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
269 assertEquals(0,index);
270
271 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
272 assertEquals(-1, index);
273
274 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
275 assertEquals(0,index);
276
277 // the Scene user-agent stylesheet is not a platform user-agent stylesheet
278 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css");
279 assertEquals(-1, index);
280
281 }
282
283 @Test
284 public void testForgetParent() {
285
286 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
287
288 StyleManager sm = StyleManager.getInstance();
289 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
290
291 scene.getRoot().applyCss();
292
293 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
294 assertEquals(0,index);
295
296 sm.forget(scene.getRoot());
297
298 // forgetting the scene should not affect the platform user-agent stylesheets
299 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
300 assertEquals(0,index);
301
302 }
303
304 @Test
305 public void testForgetParent_withSceneUAStylesheet() {
306
307 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
308 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
309
310 StyleManager sm = StyleManager.getInstance();
311 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
312
313 scene.getRoot().applyCss();
314
315 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
316 assertEquals(0, index);
317
318 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
319 assertEquals(-1, index);
320
321 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
322 assertEquals(0,index);
323
324 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
325 assertEquals(-1, index);
326
327 sm.forget(scene.getRoot());
328
329 // forgetting the parent should not affect the platform user-agent stylesheets
330 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
331 assertEquals(0,index);
332
333 // only forgetting the scene should affect the platform user-agent stylesheets
334 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
335 assertEquals(0, index);
336
337 }
338
339 @Test
340 public void testForgetParent_withTwoScenes() {
341 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
342 scene0.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
343
344 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
345 scene1.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
346
347 StyleManager sm = StyleManager.getInstance();
348 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
349
350 scene0.getRoot().applyCss();
351 scene1.getRoot().applyCss();
352
353 // even though there are two scenes using this stylesheet, there should only be one container.
354 assertEquals(1, sm.userAgentStylesheetContainers.size());
355
356 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
357 assertEquals(0,index);
358
359 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
360 assertEquals(-1, index);
361
362 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
363 assertEquals(0, index);
364
365 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
366 assertEquals(-1, index);
367
368 sm.forget(scene0.getRoot());
369
370 // forgetting the scene should not affect the platform user-agent stylesheets
371 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
372 assertEquals(0,index);
373
374 // we should still have ua1.css since scene1 is still using it
375 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
376 assertEquals(0,index);
377
378 sm.forget(scene1.getRoot());
379
380 // only forgetting the scene should affect the platform user-agent stylesheets
381 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
382 assertEquals(0, index);
383 }
384
385 @Test
386 public void testForgetParent_withParentStylesheet() {
387
388 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
389 scene.getRoot().getStylesheets().add("/com/sun/javafx/css/ua1.css");
390
391 StyleManager sm = StyleManager.getInstance();
392 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
393
394 scene.getRoot().applyCss();
395
396 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
397 assertEquals(0, index);
398
399 assertTrue(sm.userAgentStylesheetContainers.isEmpty());
400 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
401
402 sm.forget(scene.getRoot());
403
404 // forgetting the scene should not affect the platform user-agent stylesheets
405 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
406 assertEquals(0, index);
407
408 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
409
410 }
411
412 @Test
413 public void testForgetParent_withMultipleParentStylesheets() {
414
415 final Parent parent0 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
416 parent0.getStylesheets().add("/com/sun/javafx/css/ua1.css");
417
418 final Parent parent1 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
419 parent1.getStylesheets().add("/com/sun/javafx/css/ua1.css");
420
421 Scene scene = new Scene(new Group(parent0, parent1));
422
423 StyleManager sm = StyleManager.getInstance();
424 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
425
426 scene.getRoot().applyCss();
427
428 assertTrue(sm.userAgentStylesheetContainers.isEmpty());
429
430 StyleManager.StylesheetContainer container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua1.css");
431 assertNotNull(container);
432 assertTrue(container.parentUsers.contains(parent0));
433 assertTrue(container.parentUsers.contains(parent1));
434
435 sm.forget(parent0);
436
437 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
438 assertFalse(container.parentUsers.contains(parent0));
439 assertTrue(container.parentUsers.contains(parent1));
440
441 sm.forget(parent1);
442
443 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
444 assertFalse(container.parentUsers.contains(parent0));
445 assertFalse(container.parentUsers.contains(parent1));
446 }
447
448 @Test
449 public void testForgetScene() {
450
451 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
452
453 StyleManager sm = StyleManager.getInstance();
454 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
455
456 scene.getRoot().applyCss();
457
458 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
459 assertEquals(0,index);
460
461 sm.forget(scene);
462
463 // forgetting the scene should not affect the platform user-agent stylesheets
464 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
465 assertEquals(0,index);
466 }
467
468 @Test
469 public void testForgetScene_withUAStylesheet() {
470
471 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
472 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
473
474 StyleManager sm = StyleManager.getInstance();
475 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
476
477 scene.getRoot().applyCss();
478
479 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
480 assertEquals(0, index);
481
482 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
483 assertEquals(-1, index);
484
485 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
486 assertEquals(0,index);
487
488 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
489 assertEquals(-1, index);
490
491 sm.forget(scene);
492
493 // forgetting the scene should not affect the platform user-agent stylesheets
494 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
495 assertEquals(0,index);
496
497 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
498 assertEquals(-1, index);
499
500 }
501
502 @Test
503 public void testForgetScene_withTwoScenes() {
504 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
505 scene0.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
506
507 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
508 scene1.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
509
510 StyleManager sm = StyleManager.getInstance();
511 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
512
513 scene0.getRoot().applyCss();
514 scene1.getRoot().applyCss();
515
516 // even though there are two scenes using this stylesheet, there should only be one container.
517 assertEquals(1, sm.userAgentStylesheetContainers.size());
518
519 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
520 assertEquals(0,index);
521
522 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
523 assertEquals(-1, index);
524
525 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
526 assertEquals(0,index);
527
528 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
529 assertEquals(-1, index);
530
531 sm.forget(scene0);
532
533 // forgetting the scene should not affect the platform user-agent stylesheets
534 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
535 assertEquals(0,index);
536
537 // we should still have ua1.css since scene1 is still using it
538 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
539 assertEquals(0,index);
540
541 sm.forget(scene1);
542
543 // having forgotten scene1, userAgentStylesheetContainers should be empty.
544 assertTrue(sm.userAgentStylesheetContainers.isEmpty());
545 }
546
547 @Test
548 public void testForgetSubScene() {
549
550 Pane subSceneRoot = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
551 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
552 Scene scene = new Scene(new Group(subScene));
553
554 StyleManager sm = StyleManager.getInstance();
555 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
556
557 scene.getRoot().applyCss();
558
559 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
560 assertEquals(0,index);
561
562 sm.forget(subScene);
563
564 // forgetting the scene should not affect the platform user-agent stylesheets
565 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
566 assertEquals(0,index);
567 }
568
569 @Test
570 public void testForgetSubScene_withUAStylesheet() {
571
572 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
573 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
574 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
575 Scene scene = new Scene(new Group(subScene));
576
577 StyleManager sm = StyleManager.getInstance();
578 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
579
580 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
581 assertEquals(0, index);
582
583 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
584 assertEquals(-1, index);
585
586 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
587 assertEquals(0,index);
588
589 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
590 assertEquals(-1, index);
591
592 sm.forget(subScene);
593
594 // forgetting the scene should not affect the platform user-agent stylesheets
595 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
596 assertEquals(0,index);
597
598 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
599 assertEquals(-1, index);
600
601 }
602
603 @Test
604 public void testForgetSubScene_with_UAStylesheet_and_ParentStylesheet() {
605
606 // make sure forget(SubScene) get's children with stylesheets
607 Group group = new Group(new Rectangle(){{ getStyleClass().add("rect"); }});
608 group.getStylesheets().add("/com/sun/javafx/css/ua2.css");
609 Pane subSceneRoot = new Pane(group);
610 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
611 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
612 Scene scene = new Scene(new Group(subScene));
613
614 StyleManager sm = StyleManager.getInstance();
615 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
616
617 scene.getRoot().applyCss();
618
619 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
620 assertEquals(0,index);
621
622 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
623 assertEquals(-1, index);
624
625 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
626 assertEquals(0,index);
627
628 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
629 assertEquals(-1, index);
630
631 sm.forget(subScene);
632
633 // forgetting the scene should not affect the platform user-agent stylesheets
634 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
635 assertEquals(0,index);
636
637 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
638 assertEquals(-1, index);
639
640 }
641
642 @Test
643 public void testChangeSubSceneStylesheet() {
644
645 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
646 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
647 Scene scene = new Scene(new Group(subScene));
648
649 StyleManager sm = StyleManager.getInstance();
650 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
651
652 scene.getRoot().applyCss();
653
654 scene.getRoot().applyCss();
655
656 scene.getRoot().applyCss();
657
658 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
659 assertEquals(0, index);
660
661 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css");
662 assertEquals(-1, index);
663
664 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
665
666 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
667 assertEquals(0,index);
668
669 sm.forget(subScene);
670
671 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua2.css");
672 scene.getRoot().applyCss();
673
674 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua2.css");
675 assertEquals(0, index);
676
677 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css");
678 assertEquals(-1,index);
679
680 }
681
682 @Test
683 public void testFindMatchingStyles_defaultStyleSheet() {
684
685 StyleManager sm = StyleManager.getInstance();
686 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
687
688 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
689 Scene scene = new Scene(new Group(rect));
690
691 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
692 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
693
694 assertTrue(styleMap.containsKey("-fx-fill"));
695
696 List<CascadingStyle> styles = styleMap.get("-fx-fill");
697 assertEquals(1, styles.size());
698
699 Object obj = styles.get(0).getParsedValue().convert(null);
700 assertEquals(Color.RED, obj);
701 }
702
703 @Test
704 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet() {
705
706 StyleManager sm = StyleManager.getInstance();
707 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
708
709 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
710 Scene scene = new Scene(new Group(rect));
711 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
712
713 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
714 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
715
716 scene.getRoot().applyCss();
717
718 // scene stylesheet should totally replace default
719 assertFalse(styleMap.containsKey("-fx-fill"));
720 assertTrue(styleMap.containsKey("-fx-stroke"));
721
722 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
723 assertEquals(1, styles.size());
724
725 Object obj = styles.get(0).getParsedValue().convert(null);
726 assertEquals(Color.YELLOW, obj);
727 }
728
729 @Test
730 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet_sceneAuthorStylesheet() {
731
732 StyleManager sm = StyleManager.getInstance();
733 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
734
735 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
736 Scene scene = new Scene(new Group(rect));
737 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
738 scene.getStylesheets().add("/com/sun/javafx/css/ua2.css");
739
740 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
741 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
742
743 scene.getRoot().applyCss();
744
745 // ua2.css has fill
746 assertTrue(styleMap.containsKey("-fx-fill"));
747 assertTrue(styleMap.containsKey("-fx-stroke"));
748
749 // ua1.css and ua2.css have stroke
750 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
751 assertEquals(2, styles.size());
752
753 Object obj = styles.get(0).getParsedValue().convert(null);
754 assertEquals(Color.GREEN, obj);
755
756 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0
757 // since we have a scene user-agent stylesheet
758 styles = styleMap.get("-fx-fill");
759 assertEquals(1, styles.size());
760
761 obj = styles.get(0).getParsedValue().convert(null);
762 assertEquals(Color.BLUE, obj);
763 }
764
765 @Test
766 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet() {
767
768 StyleManager sm = StyleManager.getInstance();
769 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
770
771 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
772 Pane subSceneRoot = new Pane(rect);
773 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
774 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
775 Scene scene = new Scene(new Group(subScene));
776
777 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
778 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
779
780 scene.getRoot().applyCss();
781
782 // SubScene stylesheet should totally replace default
783 assertFalse(styleMap.containsKey("-fx-fill"));
784 assertTrue(styleMap.containsKey("-fx-stroke"));
785
786 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
787 assertEquals(1, styles.size());
788
789 Object obj = styles.get(0).getParsedValue().convert(null);
790 assertEquals(Color.YELLOW, obj);
791 }
792
793 @Test
794 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet_parentStylesheet() {
795
796 StyleManager sm = StyleManager.getInstance();
797 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
798
799 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
800 Group group = new Group(rect);
801 group.getStylesheets().add("/com/sun/javafx/css/ua2.css");
802 Pane subSceneRoot = new Pane(group);
803 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
804 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
805 Scene scene = new Scene(new Group(subScene));
806
807 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
808 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
809
810 scene.getRoot().applyCss();
811
812 // ua2.css has fill
813 assertTrue(styleMap.containsKey("-fx-fill"));
814 assertTrue(styleMap.containsKey("-fx-stroke"));
815
816 // ua1.css and ua2.css have stroke
817 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
818 assertEquals(2, styles.size());
819
820 Object obj = styles.get(0).getParsedValue().convert(null);
821 assertEquals(Color.GREEN, obj);
822
823 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0
824 // since we have a scene user-agent stylesheet
825 styles = styleMap.get("-fx-fill");
826 assertEquals(1, styles.size());
827
828 obj = styles.get(0).getParsedValue().convert(null);
829 assertEquals(Color.BLUE, obj);
830 }
831
832 @Test
833 public void testSwitchDefaultUserAgentStylesheets() {
834
835 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
836 Group group = new Group(rect);
837 Pane subSceneRoot = new Pane(group);
838 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
839 Scene scene = new Scene(new Group(subScene));
840
841 StyleManager sm = StyleManager.getInstance();
842 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css");
843
844 scene.getRoot().applyCss();
845
846 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
847 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
848
849 // ua0.css has fill
850 assertTrue(styleMap.containsKey("-fx-fill"));
851 assertFalse(styleMap.containsKey("-fx-stroke"));
852
853 List<CascadingStyle> styles = styleMap.get("-fx-fill");
854 assertEquals(1, styles.size());
855
856 Object obj = styles.get(0).getParsedValue().convert(null);
857 assertEquals(Color.RED, obj);
858
859 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
860
861 matchingStyles = sm.findMatchingStyles(rect, subScene, null);
862 styleMap = matchingStyles.getCascadingStyles();
863
864 // ua1.css has stroke
865 assertTrue(styleMap.containsKey("-fx-stroke"));
866 assertFalse(styleMap.containsKey("-fx-fill"));
867
868 styles = styleMap.get("-fx-stroke");
869 assertEquals(1, styles.size());
870
871 obj = styles.get(0).getParsedValue().convert(null);
872 assertEquals(Color.YELLOW, obj);
873 }
874
875 @Test
876 public void testGetCacheContainer() {
877
878 Rectangle rectangle = new Rectangle();
879 SubScene subScene = new SubScene(null, 0, 0);
880
881 StyleManager sm = StyleManager.getInstance();
882
883 // no scene, should return null
884 StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, subScene);
885
886 assertNull(container);
887
888 // has scene, should return non-null
889 subScene.setRoot(new Group());
890 Scene scene = new Scene(new Group(rectangle,subScene));
891 container = sm.getCacheContainer(rectangle, subScene);
892
893 assertNotNull(container);
894
895 }
896
897 @Test
898 public void testGetCacheContainer_styleable() {
899 Rectangle rectangle = new Rectangle();
900
901 StyleManager sm = StyleManager.getInstance();
902
903 // no scene, should return null
904 StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, null);
905
906 assertNull(container);
907
908 // has scene, should return non-null
909 Scene scene = new Scene(new Group(rectangle));
910 container = sm.getCacheContainer(rectangle, null);
911
912 assertNotNull(container);
913
914 }
915
916 @Test
917 public void testGetCacheContainer_subScene() {
918
919 SubScene subScene = new SubScene(null, 0, 0);
920
921 StyleManager sm = StyleManager.getInstance();
922
923 // no scene, should return null
924 StyleManager.CacheContainer container = sm.getCacheContainer(null, subScene);
925
926 assertNull(container);
927
928 // has scene, should return non-null
929 subScene.setRoot(new Group());
930 Scene scene = new Scene(new Group(subScene));
931 container = sm.getCacheContainer(null, subScene);
932
933 assertNotNull(container);
934
935 }
936
937 @Test
938 public void testRT_37025() {
939
940 //
941 // The issue in RT-37025 was that the stylesheet container wasn't getting removed even
942 // though the parent had been forgotten. The StyleManager#forget(Parent) method didn't
943 // look to see if _any_ stylesheet container had the parent as a reference.
944 //
945 final StyleManager sm = StyleManager.getInstance();
946
947 // This test needs a bit more complexity to the scene-graph
948 Group group = null;
949 Pane pane = new Pane(
950 new Group(
951 new Pane(
952 // I want these to be a Parent, not a Node
953 new Group(new Pane(){{ getStyleClass().add("rect"); }}),
954 group = new Group(new Pane(){{ getStyleClass().add("rect"); }})
955 )
956 )
957 );
958 pane.getStylesheets().add("/com/sun/javafx/css/ua0.css");
959 group.getStylesheets().add("/com/sun/javafx/css/ua1.css");
960
961 Group root = new Group(pane);
962 Scene scene = new Scene(root);
963
964 root.applyCss();
965
966 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css"));
967 StyleManager.StylesheetContainer container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua0.css");
968 assertEquals(7, container.parentUsers.list.size());
969
970 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
971 container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua1.css");
972 assertEquals(2, container.parentUsers.list.size());
973
974 ((Pane)group.getParent()).getChildren().remove(group);
975
976 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css"));
977 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css"));
978 container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua0.css");
979 assertEquals(5, container.parentUsers.list.size());
980
981 scene.setRoot(new Group());
982 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua0.css"));
983 assertFalse(StyleManager.cacheContainerMap.containsKey(root));
984 assertTrue(StyleManager.cacheContainerMap.containsKey(scene.getRoot()));
985
986 }
987
988 @Test
989 public void test_setUserAgentStylesheets() {
990
991 List<String> uaStylesheets = new ArrayList<>();
992 Collections.addAll(uaStylesheets, "/com/sun/javafx/css/ua0.css", "/com/sun/javafx/css/ua1.css");
993
994 final StyleManager sm = StyleManager.getInstance();
995 sm.setUserAgentStylesheets(uaStylesheets);
996
997 assertEquals(2, sm.platformUserAgentStylesheetContainers.size());
998 assertEquals("/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers.get(0).fname);
999 assertEquals("/com/sun/javafx/css/ua1.css", sm.platformUserAgentStylesheetContainers.get(1).fname);
1000 }
1001
1002 @Test
1003 public void test_setUserAgentStylesheets_overwrites_existing() {
1004
1005 List<String> uaStylesheets = new ArrayList<>();
1006 Collections.addAll(uaStylesheets, "/com/sun/javafx/css/ua0.css");
1007
1008 final StyleManager sm = StyleManager.getInstance();
1009
1010 // 1 - overwrite default user agent stylesheet
1011 sm.platformUserAgentStylesheetContainers.clear();;
1012 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
1013 assertEquals(1, sm.platformUserAgentStylesheetContainers.size());
1014 assertEquals("/com/sun/javafx/css/ua1.css", sm.platformUserAgentStylesheetContainers.get(0).fname);
1015
1016 sm.setUserAgentStylesheets(uaStylesheets);
1017 assertEquals(1, sm.platformUserAgentStylesheetContainers.size());
1018 assertEquals("/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers.get(0).fname);
1019
1020 // 2 - overwrite other user-agent stylesheets
1021 sm.platformUserAgentStylesheetContainers.clear();;
1022 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css");
1023 assertEquals(1, sm.platformUserAgentStylesheetContainers.size());
1024
1025 sm.setUserAgentStylesheets(uaStylesheets);
1026 assertEquals(1, sm.platformUserAgentStylesheetContainers.size());
1027 assertEquals("/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers.get(0).fname);
1028 }
1029
1030 @Test
1031 public void testRT_38687_with_Scene() {
1032
1033 Rectangle rect = new Rectangle(50,50) {{ getStyleClass().add("rect"); }};
1034 Scene scene = new Scene(new Group(rect));
1035 scene.setUserAgentStylesheet("com/sun/javafx/css/ua0.css");
1036 scene.getRoot().applyCss();
1037
1038 StyleableProperty<Paint> fillProperty = (StyleableProperty<Paint>)rect.fillProperty();
1039 assertEquals(StyleOrigin.USER_AGENT, fillProperty.getStyleOrigin());
1040
1041 scene.setUserAgentStylesheet("com/sun/javafx/css/ua1.css");
1042 scene.getRoot().applyCss();
1043
1044 assertEquals(null, fillProperty.getStyleOrigin());
1045
1046 rect.setFill(Color.GREEN);
1047
1048 scene.setUserAgentStylesheet("com/sun/javafx/css/rt38637.css");
1049 scene.getRoot().applyCss();
1050 assertEquals(StyleOrigin.USER, fillProperty.getStyleOrigin());
1051
1052 }
1053
1054 @Test
1055 public void testRT_38687_with_SubScene() {
1056
1057 Rectangle rect = new Rectangle(50,50) {{ getStyleClass().add("rect"); }};
1058 Group group = new Group(rect);
1059 SubScene subScene = new SubScene(group, 100, 100);
1060 subScene.setUserAgentStylesheet("com/sun/javafx/css/ua0.css");
1061
1062 Scene scene = new Scene(new Group(subScene));
1063 scene.getRoot().applyCss();
1064
1065 StyleableProperty<Paint> fillProperty = (StyleableProperty<Paint>)rect.fillProperty();
1066 assertEquals(StyleOrigin.USER_AGENT, fillProperty.getStyleOrigin());
1067
1068 subScene.setUserAgentStylesheet("com/sun/javafx/css/ua1.css");
1069 scene.getRoot().applyCss();
1070
1071 assertEquals(null, fillProperty.getStyleOrigin());
1072
1073 rect.setFill(Color.GREEN);
1074
1075 subScene.setUserAgentStylesheet("com/sun/javafx/css/rt38637.css");
1076 scene.getRoot().applyCss();
1077 assertEquals(StyleOrigin.USER, fillProperty.getStyleOrigin());
1078
1079 }
1080
1081 @Test
1082 public void testConcurrentAccess() {
1083 final int NUM_THREADS = 10;
1084 final Thread[] bgThreads = new Thread[NUM_THREADS];
1085 final AtomicBoolean err = new AtomicBoolean(false);
1086 for (int i = 0; i < NUM_THREADS; i++) {
1087 Thread thr = new Thread(() -> {
1088 try {
1089 for (int j = 0; j < 1000; j++) {
1090 Scene scene = new Scene(new Group());
1091 scene.setUserAgentStylesheet("com/sun/javafx/css/ua0.css");
1092 scene.getRoot().applyCss();
1093 }
1094 } catch (RuntimeException ex) {
1095 err.set(true);
1096 throw ex;
1097 }
1098 });
1099 thr.setName("MyThread-" + i);
1100 thr.setDaemon(true);
1101 bgThreads[i] = thr;
1102 }
1103
1104 for (Thread thr : bgThreads) {
1105 thr.start();
1106 }
1107
1108 try {
1109 for (Thread thr : bgThreads) {
1110 thr.join();
1111 }
|
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.com.sun.javafx.css;
27
28 import com.sun.javafx.css.CascadingStyle;
29 import com.sun.javafx.css.StyleManager;
30 import com.sun.javafx.css.StyleManagerShim;
31 import com.sun.javafx.css.StyleMap;
32 import javafx.css.CssParser;
33 import javafx.css.StyleOrigin;
34 import javafx.css.StyleableProperty;
35 import javafx.css.Stylesheet;
36 import javafx.scene.Group;
37 import javafx.scene.Parent;
38 import javafx.scene.Scene;
39 import javafx.scene.SubScene;
40 import javafx.scene.layout.Pane;
41 import javafx.scene.paint.Color;
42 import javafx.scene.paint.Paint;
43 import javafx.scene.shape.Rectangle;
44 import org.junit.Before;
45 import org.junit.Test;
46
47 import java.net.URL;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.concurrent.atomic.AtomicBoolean;
53
54 import static org.junit.Assert.*;
55
56 /**
57 *
58 * @author dgrieve
59 */
60 public class StyleManagerTest {
61
62 public StyleManagerTest() {
63 }
64
65 @Before
66 public void setUp() {
67 StyleManagerShim sm = StyleManagerShim.getInstance();
68 sm.userAgentStylesheetContainers_clear();
69 sm.platformUserAgentStylesheetContainers_clear();
70 sm.stylesheetContainerMap_clear();
71 sm.cacheContainerMap_clear();
72 sm.set_hasDefaultUserAgentStylesheet(false);
73 }
74
75 @Test
76 public void testMethod_getInstance() {
77 Scene scene = new Scene(new Group());
78 StyleManagerShim sm = StyleManagerShim.getInstance();
79 assertNotNull(sm);
80 }
81
82 @Test
83 public void testAddUserAgentStyleshseet_String() {
84 StyleManagerShim sm = StyleManagerShim.getInstance();
85 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
86 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
87 assertEquals(0,index);
88
89 }
90
91 @Test
92 public void testAddUserAgentStyleshseet_String_Multiple() {
93 StyleManagerShim sm = StyleManagerShim.getInstance();
94 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
95 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
96
97 int index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua0.css");
98 assertEquals(0,index);
99
100 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
101 assertEquals(1, index);
102 }
103
104 @Test
105 public void testAddUserAgentStyleshseet_String_Duplicate() {
106 StyleManagerShim sm = StyleManagerShim.getInstance();
107 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
108 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
109 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
110
111 assertTrue(sm.platformUserAgentStylesheetContainers_size() == 2);
112
113 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
114 assertEquals(0,index);
115
116 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
117 assertEquals(1, index);
118
119 }
120
121 @Test
122 public void testSetDefaultUserAgentStyleshseet_String() {
123 StyleManagerShim sm = StyleManagerShim.getInstance();
124 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
125
126 int index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua0.css");
127 assertEquals(0, index);
128 }
129
130 @Test
131 public void testSetUserAgentStyleshseet_String_Multiple() {
132 StyleManagerShim sm = StyleManagerShim.getInstance();
133 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
134 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
135
136 assertTrue(sm.platformUserAgentStylesheetContainers_size() == 2);
137
138 int index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua0.css");
139 assertEquals(0,index);
140
141 index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua1.css");
142 assertEquals(1, index);
143 }
144
145 @Test
146 public void testSetUserAgentStyleshseet_String_Multiple2() {
147 StyleManagerShim sm = StyleManagerShim.getInstance();
148 // same as before but set default after adding another.
149 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
150 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
151
152 assertEquals(2, sm.platformUserAgentStylesheetContainers_size());
153
154 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
155 assertEquals(0,index);
156
157 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
158 assertEquals(1, index);
159 }
160
161 @Test
162 public void testSetUserAgentStyleshseet_String_Duplicate() {
163 StyleManagerShim sm = StyleManagerShim.getInstance();
164 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
165 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
166 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
167
168 assertEquals(2, sm.platformUserAgentStylesheetContainers_size());
169
170 int index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua0.css");
171 assertEquals(0,index);
172
173 index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua1.css");
174 assertEquals(1, index);
175 }
176
177 @Test
178 public void testAddUserAgentStyleshseet_Stylesheet() {
179
180 try {
181 StyleManagerShim sm = StyleManagerShim.getInstance();
182 URL ua0_url = StyleManagerTest.class.getResource("ua0.css");
183 Stylesheet stylesheet = new CssParser().parse(ua0_url);
184 sm.addUserAgentStylesheet(null,stylesheet);
185
186 assertEquals(1, sm.platformUserAgentStylesheetContainers_size());
187
188 int index = sm.platformUserAgentStylesheetContainers_indexOf(ua0_url.toExternalForm());
189 assertEquals(0, index);
190
191 } catch (Exception ioe) {
192 fail(ioe.getMessage());
193 }
194
195 }
196
197 @Test
198 public void testSetDefaultUserAgentStyleshseet_Stylesheet() {
199
200 try {
201 StyleManagerShim sm = StyleManagerShim.getInstance();
202
203 URL ua1_url = StyleManagerTest.class.getResource("ua1.css");
204 Stylesheet stylesheet = new CssParser().parse(ua1_url);
205 sm.addUserAgentStylesheet(null,stylesheet);
206
207 URL ua0_url = StyleManagerTest.class.getResource("ua0.css");
208 stylesheet = new CssParser().parse(ua0_url);
209 sm.setDefaultUserAgentStylesheet(stylesheet);
210
211 assertEquals(2, sm.platformUserAgentStylesheetContainers_size());
212
213 int index = sm.platformUserAgentStylesheetContainers_indexOf(ua0_url.toExternalForm());
214 assertEquals(0, index);
215
216 index = sm.platformUserAgentStylesheetContainers_indexOf(ua1_url.toExternalForm());
217 assertEquals(1, index);
218
219 } catch (Exception ioe) {
220 fail(ioe.getMessage());
221 }
222
223 }
224
225 @Test
226 public void testSceneUAStylesheetAdded() {
227 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
228 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
229
230 StyleManagerShim sm = StyleManagerShim.getInstance();
231 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
232
233 scene.getRoot().applyCss();
234
235 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
236 assertEquals(0,index);
237
238 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
239 assertEquals(-1, index);
240
241 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
242 assertEquals(0,index);
243
244 // the Scene user-agent stylesheet is not a platform user-agent stylesheet
245 index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua1.css");
246 assertEquals(-1, index);
247
248 }
249
250 @Test
251 public void testSubSceneUAStylesheetAdded() {
252 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
253 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
254
255 StyleManagerShim sm = StyleManagerShim.getInstance();
256 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
257
258 scene.getRoot().applyCss();
259
260 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
261 assertEquals(0,index);
262
263 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
264 assertEquals(-1, index);
265
266 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
267 assertEquals(0,index);
268
269 // the Scene user-agent stylesheet is not a platform user-agent stylesheet
270 index = sm.platformUserAgentStylesheetContainers_indexOf( "/test/com/sun/javafx/css/ua1.css");
271 assertEquals(-1, index);
272
273 }
274
275 @Test
276 public void testForgetParent() {
277
278 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
279
280 StyleManagerShim sm = StyleManagerShim.getInstance();
281 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
282
283 scene.getRoot().applyCss();
284
285 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
286 assertEquals(0,index);
287
288 sm.forget(scene.getRoot());
289
290 // forgetting the scene should not affect the platform user-agent stylesheets
291 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
292 assertEquals(0,index);
293
294 }
295
296 @Test
297 public void testForgetParent_withSceneUAStylesheet() {
298
299 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
300 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
301
302 StyleManagerShim sm = StyleManagerShim.getInstance();
303 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
304
305 scene.getRoot().applyCss();
306
307 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
308 assertEquals(0, index);
309
310 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
311 assertEquals(-1, index);
312
313 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
314 assertEquals(0,index);
315
316 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
317 assertEquals(-1, index);
318
319 sm.forget(scene.getRoot());
320
321 // forgetting the parent should not affect the platform user-agent stylesheets
322 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
323 assertEquals(0,index);
324
325 // only forgetting the scene should affect the platform user-agent stylesheets
326 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
327 assertEquals(0, index);
328
329 }
330
331 @Test
332 public void testForgetParent_withTwoScenes() {
333 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
334 scene0.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
335
336 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
337 scene1.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
338
339 StyleManagerShim sm = StyleManagerShim.getInstance();
340 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
341
342 scene0.getRoot().applyCss();
343 scene1.getRoot().applyCss();
344
345 // even though there are two scenes using this stylesheet, there should only be one container.
346 assertEquals(1, sm.userAgentStylesheetContainers_size());
347
348 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
349 assertEquals(0,index);
350
351 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
352 assertEquals(-1, index);
353
354 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
355 assertEquals(0, index);
356
357 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
358 assertEquals(-1, index);
359
360 sm.forget(scene0.getRoot());
361
362 // forgetting the scene should not affect the platform user-agent stylesheets
363 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
364 assertEquals(0,index);
365
366 // we should still have ua1.css since scene1 is still using it
367 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
368 assertEquals(0,index);
369
370 sm.forget(scene1.getRoot());
371
372 // only forgetting the scene should affect the platform user-agent stylesheets
373 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
374 assertEquals(0, index);
375 }
376
377 @Test
378 public void testForgetParent_withParentStylesheet() {
379
380 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
381 scene.getRoot().getStylesheets().add("/test/com/sun/javafx/css/ua1.css");
382
383 StyleManagerShim sm = StyleManagerShim.getInstance();
384 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
385
386 scene.getRoot().applyCss();
387
388 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
389 assertEquals(0, index);
390
391 assertTrue(sm.userAgentStylesheetContainers_isEmpty());
392 assertTrue(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
393
394 sm.forget(scene.getRoot());
395
396 // forgetting the scene should not affect the platform user-agent stylesheets
397 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
398 assertEquals(0, index);
399
400 assertFalse(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
401
402 }
403
404 @Test
405 public void testForgetParent_withMultipleParentStylesheets() {
406
407 final Parent parent0 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
408 parent0.getStylesheets().add("/test/com/sun/javafx/css/ua1.css");
409
410 final Parent parent1 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
411 parent1.getStylesheets().add("/test/com/sun/javafx/css/ua1.css");
412
413 Scene scene = new Scene(new Group(parent0, parent1));
414
415 StyleManagerShim sm = StyleManagerShim.getInstance();
416 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
417
418 scene.getRoot().applyCss();
419
420 assertTrue(sm.userAgentStylesheetContainers_isEmpty());
421
422 StyleManagerShim.StylesheetContainer container = sm.stylesheetContainerMap_get("/test/com/sun/javafx/css/ua1.css");
423 assertNotNull(container);
424 assertTrue(container.parentUsers_contains(parent0));
425 assertTrue(container.parentUsers_contains(parent1));
426
427 sm.forget(parent0);
428
429 assertTrue(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
430 assertFalse(container.parentUsers_contains(parent0));
431 assertTrue(container.parentUsers_contains(parent1));
432
433 sm.forget(parent1);
434
435 assertFalse(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
436 assertFalse(container.parentUsers_contains(parent0));
437 assertFalse(container.parentUsers_contains(parent1));
438 }
439
440 @Test
441 public void testForgetScene() {
442
443 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
444
445 StyleManagerShim sm = StyleManagerShim.getInstance();
446 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
447
448 scene.getRoot().applyCss();
449
450 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
451 assertEquals(0,index);
452
453 sm.forget(scene);
454
455 // forgetting the scene should not affect the platform user-agent stylesheets
456 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
457 assertEquals(0,index);
458 }
459
460 @Test
461 public void testForgetScene_withUAStylesheet() {
462
463 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
464 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
465
466 StyleManagerShim sm = StyleManagerShim.getInstance();
467 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
468
469 scene.getRoot().applyCss();
470
471 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
472 assertEquals(0, index);
473
474 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
475 assertEquals(-1, index);
476
477 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
478 assertEquals(0,index);
479
480 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
481 assertEquals(-1, index);
482
483 sm.forget(scene);
484
485 // forgetting the scene should not affect the platform user-agent stylesheets
486 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
487 assertEquals(0,index);
488
489 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
490 assertEquals(-1, index);
491
492 }
493
494 @Test
495 public void testForgetScene_withTwoScenes() {
496 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
497 scene0.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
498
499 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
500 scene1.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
501
502 StyleManagerShim sm = StyleManagerShim.getInstance();
503 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
504
505 scene0.getRoot().applyCss();
506 scene1.getRoot().applyCss();
507
508 // even though there are two scenes using this stylesheet, there should only be one container.
509 assertEquals(1, sm.userAgentStylesheetContainers_size());
510
511 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
512 assertEquals(0,index);
513
514 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
515 assertEquals(-1, index);
516
517 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
518 assertEquals(0,index);
519
520 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
521 assertEquals(-1, index);
522
523 sm.forget(scene0);
524
525 // forgetting the scene should not affect the platform user-agent stylesheets
526 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
527 assertEquals(0,index);
528
529 // we should still have ua1.css since scene1 is still using it
530 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
531 assertEquals(0,index);
532
533 sm.forget(scene1);
534
535 // having forgotten scene1, userAgentStylesheetContainers should be empty.
536 assertTrue(sm.userAgentStylesheetContainers_isEmpty());
537 }
538
539 @Test
540 public void testForgetSubScene() {
541
542 Pane subSceneRoot = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }});
543 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
544 Scene scene = new Scene(new Group(subScene));
545
546 StyleManagerShim sm = StyleManagerShim.getInstance();
547 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
548
549 scene.getRoot().applyCss();
550
551 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
552 assertEquals(0,index);
553
554 sm.forget(subScene);
555
556 // forgetting the scene should not affect the platform user-agent stylesheets
557 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
558 assertEquals(0,index);
559 }
560
561 @Test
562 public void testForgetSubScene_withUAStylesheet() {
563
564 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
565 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
566 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
567 Scene scene = new Scene(new Group(subScene));
568
569 StyleManagerShim sm = StyleManagerShim.getInstance();
570 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
571
572 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
573 assertEquals(0, index);
574
575 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
576 assertEquals(-1, index);
577
578 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
579 assertEquals(0,index);
580
581 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
582 assertEquals(-1, index);
583
584 sm.forget(subScene);
585
586 // forgetting the scene should not affect the platform user-agent stylesheets
587 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
588 assertEquals(0,index);
589
590 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
591 assertEquals(-1, index);
592
593 }
594
595 @Test
596 public void testForgetSubScene_with_UAStylesheet_and_ParentStylesheet() {
597
598 // make sure forget(SubScene) get's children with stylesheets
599 Group group = new Group(new Rectangle(){{ getStyleClass().add("rect"); }});
600 group.getStylesheets().add("/test/com/sun/javafx/css/ua2.css");
601 Pane subSceneRoot = new Pane(group);
602 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
603 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
604 Scene scene = new Scene(new Group(subScene));
605
606 StyleManagerShim sm = StyleManagerShim.getInstance();
607 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
608
609 scene.getRoot().applyCss();
610
611 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
612 assertEquals(0,index);
613
614 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
615 assertEquals(-1, index);
616
617 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
618 assertEquals(0,index);
619
620 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
621 assertEquals(-1, index);
622
623 sm.forget(subScene);
624
625 // forgetting the scene should not affect the platform user-agent stylesheets
626 index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
627 assertEquals(0,index);
628
629 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
630 assertEquals(-1, index);
631
632 }
633
634 @Test
635 public void testChangeSubSceneStylesheet() {
636
637 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }}));
638 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
639 Scene scene = new Scene(new Group(subScene));
640
641 StyleManagerShim sm = StyleManagerShim.getInstance();
642 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
643
644 scene.getRoot().applyCss();
645
646 scene.getRoot().applyCss();
647
648 scene.getRoot().applyCss();
649
650 int index = sm.platformUserAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
651 assertEquals(0, index);
652
653 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua0.css");
654 assertEquals(-1, index);
655
656 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
657
658 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
659 assertEquals(0,index);
660
661 sm.forget(subScene);
662
663 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua2.css");
664 scene.getRoot().applyCss();
665
666 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua2.css");
667 assertEquals(0, index);
668
669 index = sm.userAgentStylesheetContainers_indexOf("/test/com/sun/javafx/css/ua1.css");
670 assertEquals(-1,index);
671
672 }
673
674 @Test
675 public void testFindMatchingStyles_defaultStyleSheet() {
676
677 StyleManagerShim sm = StyleManagerShim.getInstance();
678 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
679
680 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
681 Scene scene = new Scene(new Group(rect));
682
683 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
684 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
685
686 assertTrue(styleMap.containsKey("-fx-fill"));
687
688 List<CascadingStyle> styles = styleMap.get("-fx-fill");
689 assertEquals(1, styles.size());
690
691 Object obj = styles.get(0).getParsedValue().convert(null);
692 assertEquals(Color.RED, obj);
693 }
694
695 @Test
696 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet() {
697
698 StyleManagerShim sm = StyleManagerShim.getInstance();
699 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
700
701 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
702 Scene scene = new Scene(new Group(rect));
703 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
704
705 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
706 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
707
708 scene.getRoot().applyCss();
709
710 // scene stylesheet should totally replace default
711 assertFalse(styleMap.containsKey("-fx-fill"));
712 assertTrue(styleMap.containsKey("-fx-stroke"));
713
714 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
715 assertEquals(1, styles.size());
716
717 Object obj = styles.get(0).getParsedValue().convert(null);
718 assertEquals(Color.YELLOW, obj);
719 }
720
721 @Test
722 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet_sceneAuthorStylesheet() {
723
724 StyleManagerShim sm = StyleManagerShim.getInstance();
725 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
726
727 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
728 Scene scene = new Scene(new Group(rect));
729 scene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
730 scene.getStylesheets().add("/test/com/sun/javafx/css/ua2.css");
731
732 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null);
733 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
734
735 scene.getRoot().applyCss();
736
737 // ua2.css has fill
738 assertTrue(styleMap.containsKey("-fx-fill"));
739 assertTrue(styleMap.containsKey("-fx-stroke"));
740
741 // ua1.css and ua2.css have stroke
742 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
743 assertEquals(2, styles.size());
744
745 Object obj = styles.get(0).getParsedValue().convert(null);
746 assertEquals(Color.GREEN, obj);
747
748 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0
749 // since we have a scene user-agent stylesheet
750 styles = styleMap.get("-fx-fill");
751 assertEquals(1, styles.size());
752
753 obj = styles.get(0).getParsedValue().convert(null);
754 assertEquals(Color.BLUE, obj);
755 }
756
757 @Test
758 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet() {
759
760 StyleManagerShim sm = StyleManagerShim.getInstance();
761 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
762
763 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
764 Pane subSceneRoot = new Pane(rect);
765 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
766 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
767 Scene scene = new Scene(new Group(subScene));
768
769 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
770 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
771
772 scene.getRoot().applyCss();
773
774 // SubScene stylesheet should totally replace default
775 assertFalse(styleMap.containsKey("-fx-fill"));
776 assertTrue(styleMap.containsKey("-fx-stroke"));
777
778 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
779 assertEquals(1, styles.size());
780
781 Object obj = styles.get(0).getParsedValue().convert(null);
782 assertEquals(Color.YELLOW, obj);
783 }
784
785 @Test
786 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet_parentStylesheet() {
787
788 StyleManagerShim sm = StyleManagerShim.getInstance();
789 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
790
791 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
792 Group group = new Group(rect);
793 group.getStylesheets().add("/test/com/sun/javafx/css/ua2.css");
794 Pane subSceneRoot = new Pane(group);
795 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
796 subScene.setUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
797 Scene scene = new Scene(new Group(subScene));
798
799 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
800 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
801
802 scene.getRoot().applyCss();
803
804 // ua2.css has fill
805 assertTrue(styleMap.containsKey("-fx-fill"));
806 assertTrue(styleMap.containsKey("-fx-stroke"));
807
808 // ua1.css and ua2.css have stroke
809 List<CascadingStyle> styles = styleMap.get("-fx-stroke");
810 assertEquals(2, styles.size());
811
812 Object obj = styles.get(0).getParsedValue().convert(null);
813 assertEquals(Color.GREEN, obj);
814
815 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0
816 // since we have a scene user-agent stylesheet
817 styles = styleMap.get("-fx-fill");
818 assertEquals(1, styles.size());
819
820 obj = styles.get(0).getParsedValue().convert(null);
821 assertEquals(Color.BLUE, obj);
822 }
823
824 @Test
825 public void testSwitchDefaultUserAgentStylesheets() {
826
827 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }};
828 Group group = new Group(rect);
829 Pane subSceneRoot = new Pane(group);
830 SubScene subScene = new SubScene(subSceneRoot, 100, 100);
831 Scene scene = new Scene(new Group(subScene));
832
833 StyleManagerShim sm = StyleManagerShim.getInstance();
834 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua0.css");
835
836 scene.getRoot().applyCss();
837
838 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null);
839 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles();
840
841 // ua0.css has fill
842 assertTrue(styleMap.containsKey("-fx-fill"));
843 assertFalse(styleMap.containsKey("-fx-stroke"));
844
845 List<CascadingStyle> styles = styleMap.get("-fx-fill");
846 assertEquals(1, styles.size());
847
848 Object obj = styles.get(0).getParsedValue().convert(null);
849 assertEquals(Color.RED, obj);
850
851 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
852
853 matchingStyles = sm.findMatchingStyles(rect, subScene, null);
854 styleMap = matchingStyles.getCascadingStyles();
855
856 // ua1.css has stroke
857 assertTrue(styleMap.containsKey("-fx-stroke"));
858 assertFalse(styleMap.containsKey("-fx-fill"));
859
860 styles = styleMap.get("-fx-stroke");
861 assertEquals(1, styles.size());
862
863 obj = styles.get(0).getParsedValue().convert(null);
864 assertEquals(Color.YELLOW, obj);
865 }
866
867 @Test
868 public void testGetCacheContainer() {
869
870 Rectangle rectangle = new Rectangle();
871 SubScene subScene = new SubScene(null, 0, 0);
872
873 StyleManagerShim sm = StyleManagerShim.getInstance();
874
875 // no scene, should return null
876 assertTrue(sm.isCacheContainerNull(rectangle, subScene));
877
878 // has scene, should return non-null
879 subScene.setRoot(new Group());
880 Scene scene = new Scene(new Group(rectangle,subScene));
881
882 assertFalse(sm.isCacheContainerNull(rectangle, subScene));
883
884 }
885
886 @Test
887 public void testGetCacheContainer_styleable() {
888 Rectangle rectangle = new Rectangle();
889
890 StyleManagerShim sm = StyleManagerShim.getInstance();
891
892 // no scene, should return null
893 assertTrue(sm.isCacheContainerNull(rectangle, null));
894
895 // has scene, should return non-null
896 Scene scene = new Scene(new Group(rectangle));
897
898 assertFalse(sm.isCacheContainerNull(rectangle, null));
899
900 }
901
902 @Test
903 public void testGetCacheContainer_subScene() {
904
905 SubScene subScene = new SubScene(null, 0, 0);
906
907 StyleManagerShim sm = StyleManagerShim.getInstance();
908
909 // no scene, should return null
910 assertTrue(sm.isCacheContainerNull(null, subScene));
911
912 // has scene, should return non-null
913 subScene.setRoot(new Group());
914 Scene scene = new Scene(new Group(subScene));
915
916 assertFalse(sm.isCacheContainerNull(null, subScene));
917
918 }
919
920 @Test
921 public void testRT_37025() {
922
923 //
924 // The issue in RT-37025 was that the stylesheet container wasn't getting removed even
925 // though the parent had been forgotten. The StyleManager#forget(Parent) method didn't
926 // look to see if _any_ stylesheet container had the parent as a reference.
927 //
928 final StyleManagerShim sm = StyleManagerShim.getInstance();
929
930 // This test needs a bit more complexity to the scene-graph
931 Group group = null;
932 Pane pane = new Pane(
933 new Group(
934 new Pane(
935 // I want these to be a Parent, not a Node
936 new Group(new Pane(){{ getStyleClass().add("rect"); }}),
937 group = new Group(new Pane(){{ getStyleClass().add("rect"); }})
938 )
939 )
940 );
941 pane.getStylesheets().add("/test/com/sun/javafx/css/ua0.css");
942 group.getStylesheets().add("/test/com/sun/javafx/css/ua1.css");
943
944 Group root = new Group(pane);
945 Scene scene = new Scene(root);
946
947 root.applyCss();
948
949 assertTrue(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua0.css"));
950 StyleManagerShim.StylesheetContainer container = sm.stylesheetContainerMap_get("/test/com/sun/javafx/css/ua0.css");
951 assertEquals(7, container.parentUsers_list_size());
952
953 assertTrue(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
954 container = sm.stylesheetContainerMap_get("/test/com/sun/javafx/css/ua1.css");
955 assertEquals(2, container.parentUsers_list_size());
956
957 ((Pane)group.getParent()).getChildren().remove(group);
958
959 assertFalse(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua1.css"));
960 assertTrue(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua0.css"));
961 container = sm.stylesheetContainerMap_get("/test/com/sun/javafx/css/ua0.css");
962 assertEquals(5, container.parentUsers_list_size());
963
964 scene.setRoot(new Group());
965 assertFalse(sm.stylesheetContainerMap_containsKey("/test/com/sun/javafx/css/ua0.css"));
966 assertFalse(StyleManager.cacheContainerMap.containsKey(root));
967 assertTrue(StyleManager.cacheContainerMap.containsKey(scene.getRoot()));
968
969 }
970
971 @Test
972 public void test_setUserAgentStylesheets() {
973
974 List<String> uaStylesheets = new ArrayList<>();
975 Collections.addAll(uaStylesheets, "/test/com/sun/javafx/css/ua0.css", "/test/com/sun/javafx/css/ua1.css");
976
977 final StyleManagerShim sm = StyleManagerShim.getInstance();
978 sm.setUserAgentStylesheets(uaStylesheets);
979
980 assertEquals(2, sm.platformUserAgentStylesheetContainers_size());
981 assertEquals("/test/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers_getfname(0));
982 assertEquals("/test/com/sun/javafx/css/ua1.css", sm.platformUserAgentStylesheetContainers_getfname(1));
983 }
984
985 @Test
986 public void test_setUserAgentStylesheets_overwrites_existing() {
987
988 List<String> uaStylesheets = new ArrayList<>();
989 Collections.addAll(uaStylesheets, "/test/com/sun/javafx/css/ua0.css");
990
991 final StyleManagerShim sm = StyleManagerShim.getInstance();
992
993 // 1 - overwrite default user agent stylesheet
994 sm.platformUserAgentStylesheetContainers_clear();;
995 sm.setDefaultUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
996 assertEquals(1, sm.platformUserAgentStylesheetContainers_size());
997 assertEquals("/test/com/sun/javafx/css/ua1.css", sm.platformUserAgentStylesheetContainers_getfname(0));
998
999 sm.setUserAgentStylesheets(uaStylesheets);
1000 assertEquals(1, sm.platformUserAgentStylesheetContainers_size());
1001 assertEquals("/test/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers_getfname(0));
1002
1003 // 2 - overwrite other user-agent stylesheets
1004 sm.platformUserAgentStylesheetContainers_clear();;
1005 sm.addUserAgentStylesheet("/test/com/sun/javafx/css/ua1.css");
1006 assertEquals(1, sm.platformUserAgentStylesheetContainers_size());
1007
1008 sm.setUserAgentStylesheets(uaStylesheets);
1009 assertEquals(1, sm.platformUserAgentStylesheetContainers_size());
1010 assertEquals("/test/com/sun/javafx/css/ua0.css", sm.platformUserAgentStylesheetContainers_getfname(0));
1011 }
1012
1013 @Test
1014 public void testRT_38687_with_Scene() {
1015
1016 Rectangle rect = new Rectangle(50,50) {{ getStyleClass().add("rect"); }};
1017 Scene scene = new Scene(new Group(rect));
1018 scene.setUserAgentStylesheet("test/com/sun/javafx/css/ua0.css");
1019 scene.getRoot().applyCss();
1020
1021 StyleableProperty<Paint> fillProperty = (StyleableProperty<Paint>)rect.fillProperty();
1022 assertEquals(StyleOrigin.USER_AGENT, fillProperty.getStyleOrigin());
1023
1024 scene.setUserAgentStylesheet("test/com/sun/javafx/css/ua1.css");
1025 scene.getRoot().applyCss();
1026
1027 assertEquals(null, fillProperty.getStyleOrigin());
1028
1029 rect.setFill(Color.GREEN);
1030
1031 scene.setUserAgentStylesheet("test/com/sun/javafx/css/rt38637.css");
1032 scene.getRoot().applyCss();
1033 assertEquals(StyleOrigin.USER, fillProperty.getStyleOrigin());
1034
1035 }
1036
1037 @Test
1038 public void testRT_38687_with_SubScene() {
1039
1040 Rectangle rect = new Rectangle(50,50) {{ getStyleClass().add("rect"); }};
1041 Group group = new Group(rect);
1042 SubScene subScene = new SubScene(group, 100, 100);
1043 subScene.setUserAgentStylesheet("test/com/sun/javafx/css/ua0.css");
1044
1045 Scene scene = new Scene(new Group(subScene));
1046 scene.getRoot().applyCss();
1047
1048 StyleableProperty<Paint> fillProperty = (StyleableProperty<Paint>)rect.fillProperty();
1049 assertEquals(StyleOrigin.USER_AGENT, fillProperty.getStyleOrigin());
1050
1051 subScene.setUserAgentStylesheet("test/com/sun/javafx/css/ua1.css");
1052 scene.getRoot().applyCss();
1053
1054 assertEquals(null, fillProperty.getStyleOrigin());
1055
1056 rect.setFill(Color.GREEN);
1057
1058 subScene.setUserAgentStylesheet("test/com/sun/javafx/css/rt38637.css");
1059 scene.getRoot().applyCss();
1060 assertEquals(StyleOrigin.USER, fillProperty.getStyleOrigin());
1061
1062 }
1063
1064 @Test
1065 public void testConcurrentAccess() {
1066 final int NUM_THREADS = 10;
1067 final Thread[] bgThreads = new Thread[NUM_THREADS];
1068 final AtomicBoolean err = new AtomicBoolean(false);
1069 for (int i = 0; i < NUM_THREADS; i++) {
1070 Thread thr = new Thread(() -> {
1071 try {
1072 for (int j = 0; j < 1000; j++) {
1073 Scene scene = new Scene(new Group());
1074 scene.setUserAgentStylesheet("test/com/sun/javafx/css/ua0.css");
1075 scene.getRoot().applyCss();
1076 }
1077 } catch (RuntimeException ex) {
1078 err.set(true);
1079 throw ex;
1080 }
1081 });
1082 thr.setName("MyThread-" + i);
1083 thr.setDaemon(true);
1084 bgThreads[i] = thr;
1085 }
1086
1087 for (Thread thr : bgThreads) {
1088 thr.start();
1089 }
1090
1091 try {
1092 for (Thread thr : bgThreads) {
1093 thr.join();
1094 }
|