1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 com.sun.javafx.css.parser.CSSParser; 29 import javafx.scene.Group; 30 import javafx.scene.Parent; 31 import javafx.scene.Scene; 32 import javafx.scene.SubScene; 33 import javafx.scene.layout.Pane; 34 import javafx.scene.paint.Color; 35 import javafx.scene.shape.Rectangle; 36 import javafx.stage.PopupWindow; 37 import javafx.stage.Window; 38 import org.junit.Before; 39 import org.junit.Test; 40 41 import java.net.URL; 42 import java.util.List; 43 import java.util.Map; 44 45 import static org.junit.Assert.*; 46 47 /** 48 * 49 * @author dgrieve 50 */ 51 public class StyleManagerTest { 52 53 public StyleManagerTest() { 54 } 55 56 @Before 57 public void setUp() { 58 StyleManager sm = StyleManager.getInstance(); 59 sm.userAgentStylesheetContainers.clear(); 60 sm.platformUserAgentStylesheetContainers.clear(); 61 sm.stylesheetContainerMap.clear(); 62 sm.cacheContainerMap.clear(); 63 sm.hasDefaultUserAgentStylesheet = false; 64 } 65 66 @Test 67 public void testMethod_getInstance() { 68 Scene scene = new Scene(new Group()); 69 StyleManager sm = StyleManager.getInstance(); 70 assertNotNull(sm); 71 } 72 73 private static int indexOf(final List<StyleManager.StylesheetContainer> list, final String fname) { 74 75 for (int n=0, nMax=list.size(); n<nMax; n++) { 76 StyleManager.StylesheetContainer container = list.get(n); 77 if (fname.equals(container.fname)) { 78 return n; 79 } 80 } 81 82 return -1; 83 } 84 85 @Test 86 public void testAddUserAgentStyleshseet_String() { 87 StyleManager sm = StyleManager.getInstance(); 88 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 89 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css"); 90 assertEquals(0,index); 91 92 } 93 94 @Test 95 public void testAddUserAgentStyleshseet_String_Multiple() { 96 StyleManager sm = StyleManager.getInstance(); 97 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 98 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 99 100 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css"); 101 assertEquals(0,index); 102 103 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css"); 104 assertEquals(1, index); 105 } 106 107 @Test 108 public void testAddUserAgentStyleshseet_String_Duplicate() { 109 StyleManager sm = StyleManager.getInstance(); 110 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 111 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 112 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 113 114 assertTrue(sm.platformUserAgentStylesheetContainers.size() == 2); 115 116 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 117 assertEquals(0,index); 118 119 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 120 assertEquals(1, index); 121 122 } 123 124 @Test 125 public void testSetDefaultUserAgentStyleshseet_String() { 126 StyleManager sm = StyleManager.getInstance(); 127 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 128 129 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css"); 130 assertEquals(0,index); 131 } 132 133 @Test 134 public void testSetUserAgentStyleshseet_String_Multiple() { 135 StyleManager sm = StyleManager.getInstance(); 136 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 137 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 138 139 assertTrue(sm.platformUserAgentStylesheetContainers.size() == 2); 140 141 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css"); 142 assertEquals(0,index); 143 144 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css"); 145 assertEquals(1, index); 146 } 147 148 @Test 149 public void testSetUserAgentStyleshseet_String_Multiple2() { 150 StyleManager sm = StyleManager.getInstance(); 151 // same as before but set default after adding another. 152 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 153 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 154 155 assertEquals(2, sm.platformUserAgentStylesheetContainers.size()); 156 157 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 158 assertEquals(0,index); 159 160 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 161 assertEquals(1, index); 162 } 163 164 @Test 165 public void testSetUserAgentStyleshseet_String_Duplicate() { 166 StyleManager sm = StyleManager.getInstance(); 167 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 168 sm.addUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 169 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 170 171 assertEquals(2, sm.platformUserAgentStylesheetContainers.size()); 172 173 int index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua0.css"); 174 assertEquals(0,index); 175 176 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css"); 177 assertEquals(1, index); 178 } 179 180 @Test 181 public void testAddUserAgentStyleshseet_Stylesheet() { 182 183 try { 184 StyleManager sm = StyleManager.getInstance(); 185 URL ua0_url = StyleManagerTest.class.getResource("ua0.css"); 186 Stylesheet stylesheet = CSSParser.getInstance().parse(ua0_url); 187 sm.addUserAgentStylesheet(null,stylesheet); 188 189 assertEquals(1, sm.platformUserAgentStylesheetContainers.size()); 190 191 int index = indexOf(sm.platformUserAgentStylesheetContainers,ua0_url.toExternalForm()); 192 assertEquals(0, index); 193 194 } catch (Exception ioe) { 195 fail(ioe.getMessage()); 196 } 197 198 } 199 200 @Test 201 public void testSetDefaultUserAgentStyleshseet_Stylesheet() { 202 203 try { 204 StyleManager sm = StyleManager.getInstance(); 205 206 URL ua1_url = StyleManagerTest.class.getResource("ua1.css"); 207 Stylesheet stylesheet = CSSParser.getInstance().parse(ua1_url); 208 sm.addUserAgentStylesheet(null,stylesheet); 209 210 URL ua0_url = StyleManagerTest.class.getResource("ua0.css"); 211 stylesheet = CSSParser.getInstance().parse(ua0_url); 212 sm.setDefaultUserAgentStylesheet(stylesheet); 213 214 assertEquals(2, sm.platformUserAgentStylesheetContainers.size()); 215 216 int index = indexOf(sm.platformUserAgentStylesheetContainers,ua0_url.toExternalForm()); 217 assertEquals(0, index); 218 219 index = indexOf(sm.platformUserAgentStylesheetContainers,ua1_url.toExternalForm()); 220 assertEquals(1, index); 221 222 } catch (Exception ioe) { 223 fail(ioe.getMessage()); 224 } 225 226 } 227 228 @Test 229 public void testSceneUAStylesheetAdded() { 230 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 231 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 232 233 StyleManager sm = StyleManager.getInstance(); 234 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 235 236 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 237 assertEquals(0,index); 238 239 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 240 assertEquals(-1, index); 241 242 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 243 assertEquals(0,index); 244 245 // the Scene user-agent stylesheet is not a platform user-agent stylesheet 246 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css"); 247 assertEquals(-1, index); 248 249 } 250 251 @Test 252 public void testSubSceneUAStylesheetAdded() { 253 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 254 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 255 256 StyleManager sm = StyleManager.getInstance(); 257 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 258 259 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 260 assertEquals(0,index); 261 262 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 263 assertEquals(-1, index); 264 265 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 266 assertEquals(0,index); 267 268 // the Scene user-agent stylesheet is not a platform user-agent stylesheet 269 index = indexOf(sm.platformUserAgentStylesheetContainers, "/com/sun/javafx/css/ua1.css"); 270 assertEquals(-1, index); 271 272 } 273 274 @Test 275 public void testForgetParent() { 276 277 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 278 279 StyleManager sm = StyleManager.getInstance(); 280 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 281 282 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 283 assertEquals(0,index); 284 285 sm.forget(scene.getRoot()); 286 287 // forgetting the scene should not affect the platform user-agent stylesheets 288 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 289 assertEquals(0,index); 290 291 } 292 293 @Test 294 public void testForgetParent_withSceneUAStylesheet() { 295 296 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 297 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 298 299 StyleManager sm = StyleManager.getInstance(); 300 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 301 302 // sm.findMatchingStyles(scene.getRoot(), null, null); 303 304 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 305 assertEquals(0,index); 306 307 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 308 assertEquals(-1, index); 309 310 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 311 assertEquals(0,index); 312 313 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 314 assertEquals(-1, index); 315 316 sm.forget(scene.getRoot()); 317 318 // forgetting the parent should not affect the platform user-agent stylesheets 319 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 320 assertEquals(0,index); 321 322 // only forgetting the scene should affect the platform user-agent stylesheets 323 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 324 assertEquals(0, index); 325 326 } 327 328 @Test 329 public void testForgetParent_withTwoScenes() { 330 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 331 scene0.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 332 333 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 334 scene1.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 335 336 StyleManager sm = StyleManager.getInstance(); 337 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 338 339 // even though there are two scenes using this stylesheet, there should only be one container. 340 assertEquals(1, sm.userAgentStylesheetContainers.size()); 341 342 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 343 assertEquals(0,index); 344 345 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 346 assertEquals(-1, index); 347 348 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 349 assertEquals(0,index); 350 351 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 352 assertEquals(-1, index); 353 354 sm.forget(scene0.getRoot()); 355 356 // forgetting the scene should not affect the platform user-agent stylesheets 357 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 358 assertEquals(0,index); 359 360 // we should still have ua1.css since scene1 is still using it 361 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 362 assertEquals(0,index); 363 364 sm.forget(scene1.getRoot()); 365 366 // only forgetting the scene should affect the platform user-agent stylesheets 367 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 368 assertEquals(0, index); 369 } 370 371 @Test 372 public void testForgetParent_withParentStylesheet() { 373 374 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 375 scene.getRoot().getStylesheets().add("/com/sun/javafx/css/ua1.css"); 376 377 StyleManager sm = StyleManager.getInstance(); 378 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 379 380 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 381 assertEquals(0, index); 382 383 assertTrue(sm.userAgentStylesheetContainers.isEmpty()); 384 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); 385 386 sm.forget(scene.getRoot()); 387 388 // forgetting the scene should not affect the platform user-agent stylesheets 389 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 390 assertEquals(0, index); 391 392 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); 393 394 } 395 396 @Test 397 public void testForgetParent_withMultipleParentStylesheets() { 398 399 final Parent parent0 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }}); 400 parent0.getStylesheets().add("/com/sun/javafx/css/ua1.css"); 401 402 final Parent parent1 = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }}); 403 parent1.getStylesheets().add("/com/sun/javafx/css/ua1.css"); 404 405 Scene scene = new Scene(new Group(parent0, parent1)); 406 407 StyleManager sm = StyleManager.getInstance(); 408 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 409 410 assertTrue(sm.userAgentStylesheetContainers.isEmpty()); 411 412 StyleManager.StylesheetContainer container = sm.stylesheetContainerMap.get("/com/sun/javafx/css/ua1.css"); 413 assertNotNull(container); 414 assertTrue(container.parentUsers.contains(parent0)); 415 assertTrue(container.parentUsers.contains(parent1)); 416 417 sm.forget(parent0); 418 419 assertTrue(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); 420 assertFalse(container.parentUsers.contains(parent0)); 421 assertTrue(container.parentUsers.contains(parent1)); 422 423 sm.forget(parent1); 424 425 assertFalse(sm.stylesheetContainerMap.containsKey("/com/sun/javafx/css/ua1.css")); 426 assertFalse(container.parentUsers.contains(parent0)); 427 assertFalse(container.parentUsers.contains(parent1)); 428 } 429 430 @Test 431 public void testForgetScene() { 432 433 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 434 435 StyleManager sm = StyleManager.getInstance(); 436 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 437 438 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 439 assertEquals(0,index); 440 441 sm.forget(scene); 442 443 // forgetting the scene should not affect the platform user-agent stylesheets 444 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 445 assertEquals(0,index); 446 } 447 448 @Test 449 public void testForgetScene_withUAStylesheet() { 450 451 Scene scene = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 452 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 453 454 StyleManager sm = StyleManager.getInstance(); 455 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 456 457 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 458 assertEquals(0,index); 459 460 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 461 assertEquals(-1, index); 462 463 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 464 assertEquals(0,index); 465 466 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 467 assertEquals(-1, index); 468 469 sm.forget(scene); 470 471 // forgetting the scene should not affect the platform user-agent stylesheets 472 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 473 assertEquals(0,index); 474 475 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 476 assertEquals(-1, index); 477 478 } 479 480 @Test 481 public void testForgetScene_withTwoScenes() { 482 Scene scene0 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 483 scene0.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 484 485 Scene scene1 = new Scene(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 486 scene1.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 487 488 StyleManager sm = StyleManager.getInstance(); 489 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 490 491 // even though there are two scenes using this stylesheet, there should only be one container. 492 assertEquals(1, sm.userAgentStylesheetContainers.size()); 493 494 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 495 assertEquals(0,index); 496 497 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 498 assertEquals(-1, index); 499 500 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 501 assertEquals(0,index); 502 503 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 504 assertEquals(-1, index); 505 506 sm.forget(scene0); 507 508 // forgetting the scene should not affect the platform user-agent stylesheets 509 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 510 assertEquals(0,index); 511 512 // we should still have ua1.css since scene1 is still using it 513 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 514 assertEquals(0,index); 515 516 sm.forget(scene1); 517 518 // having forgotten scene1, userAgentStylesheetContainers should be empty. 519 assertTrue(sm.userAgentStylesheetContainers.isEmpty()); 520 } 521 522 @Test 523 public void testForgetSubScene() { 524 525 Pane subSceneRoot = new Pane(new Rectangle(){{ getStyleClass().add("rect"); }}); 526 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 527 Scene scene = new Scene(new Group(subScene)); 528 529 StyleManager sm = StyleManager.getInstance(); 530 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 531 532 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 533 assertEquals(0,index); 534 535 sm.forget(subScene); 536 537 // forgetting the scene should not affect the platform user-agent stylesheets 538 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 539 assertEquals(0,index); 540 } 541 542 @Test 543 public void testForgetSubScene_withUAStylesheet() { 544 545 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 546 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 547 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 548 Scene scene = new Scene(new Group(subScene)); 549 550 StyleManager sm = StyleManager.getInstance(); 551 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 552 553 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 554 assertEquals(0,index); 555 556 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 557 assertEquals(-1, index); 558 559 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 560 assertEquals(0,index); 561 562 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 563 assertEquals(-1, index); 564 565 sm.forget(subScene); 566 567 // forgetting the scene should not affect the platform user-agent stylesheets 568 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 569 assertEquals(0,index); 570 571 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 572 assertEquals(-1, index); 573 574 } 575 576 @Test 577 public void testForgetSubScene_with_UAStylesheet_and_ParentStylesheet() { 578 579 // make sure forget(SubScene) get's children with stylesheets 580 Group group = new Group(new Rectangle(){{ getStyleClass().add("rect"); }}); 581 group.getStylesheets().add("/com/sun/javafx/css/ua2.css"); 582 Pane subSceneRoot = new Pane(group); 583 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 584 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 585 Scene scene = new Scene(new Group(subScene)); 586 587 StyleManager sm = StyleManager.getInstance(); 588 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 589 590 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 591 assertEquals(0,index); 592 593 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 594 assertEquals(-1, index); 595 596 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 597 assertEquals(0,index); 598 599 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 600 assertEquals(-1, index); 601 602 sm.forget(subScene); 603 604 // forgetting the scene should not affect the platform user-agent stylesheets 605 index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 606 assertEquals(0,index); 607 608 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 609 assertEquals(-1, index); 610 611 } 612 613 @Test 614 public void testChangeSubSceneStylesheet() { 615 616 Pane subSceneRoot = new Pane(new Group(new Rectangle(){{ getStyleClass().add("rect"); }})); 617 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 618 Scene scene = new Scene(new Group(subScene)); 619 620 StyleManager sm = StyleManager.getInstance(); 621 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 622 623 int index = indexOf(sm.platformUserAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 624 assertEquals(0,index); 625 626 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua0.css"); 627 assertEquals(-1, index); 628 629 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 630 631 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 632 assertEquals(0,index); 633 634 sm.forget(subScene); 635 636 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua2.css"); 637 638 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua2.css"); 639 assertEquals(0, index); 640 641 index = indexOf(sm.userAgentStylesheetContainers,"/com/sun/javafx/css/ua1.css"); 642 assertEquals(-1,index); 643 644 } 645 646 @Test 647 public void testFindMatchingStyles_defaultStyleSheet() { 648 649 StyleManager sm = StyleManager.getInstance(); 650 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 651 652 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 653 Scene scene = new Scene(new Group(rect)); 654 655 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null); 656 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 657 658 assertTrue(styleMap.containsKey("-fx-fill")); 659 660 List<CascadingStyle> styles = styleMap.get("-fx-fill"); 661 assertEquals(1, styles.size()); 662 663 Object obj = styles.get(0).getParsedValueImpl().convert(null); 664 assertEquals(Color.RED, obj); 665 } 666 667 @Test 668 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet() { 669 670 StyleManager sm = StyleManager.getInstance(); 671 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 672 673 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 674 Scene scene = new Scene(new Group(rect)); 675 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 676 677 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null); 678 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 679 680 // scene stylesheet should totally replace default 681 assertFalse(styleMap.containsKey("-fx-fill")); 682 assertTrue(styleMap.containsKey("-fx-stroke")); 683 684 List<CascadingStyle> styles = styleMap.get("-fx-stroke"); 685 assertEquals(1, styles.size()); 686 687 Object obj = styles.get(0).getParsedValueImpl().convert(null); 688 assertEquals(Color.YELLOW, obj); 689 } 690 691 @Test 692 public void testFindMatchingStyles_defaultStyleSheet_sceneUserAgentStylesheet_sceneAuthorStylesheet() { 693 694 StyleManager sm = StyleManager.getInstance(); 695 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 696 697 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 698 Scene scene = new Scene(new Group(rect)); 699 scene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 700 scene.getStylesheets().add("/com/sun/javafx/css/ua2.css"); 701 702 StyleMap matchingStyles = sm.findMatchingStyles(rect, null, null); 703 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 704 705 // ua2.css has fill 706 assertTrue(styleMap.containsKey("-fx-fill")); 707 assertTrue(styleMap.containsKey("-fx-stroke")); 708 709 // ua1.css and ua2.css have stroke 710 List<CascadingStyle> styles = styleMap.get("-fx-stroke"); 711 assertEquals(2, styles.size()); 712 713 Object obj = styles.get(0).getParsedValueImpl().convert(null); 714 assertEquals(Color.GREEN, obj); 715 716 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0 717 // since we have a scene user-agent stylesheet 718 styles = styleMap.get("-fx-fill"); 719 assertEquals(1, styles.size()); 720 721 obj = styles.get(0).getParsedValueImpl().convert(null); 722 assertEquals(Color.BLUE, obj); 723 } 724 725 @Test 726 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet() { 727 728 StyleManager sm = StyleManager.getInstance(); 729 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 730 731 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 732 Pane subSceneRoot = new Pane(rect); 733 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 734 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 735 Scene scene = new Scene(new Group(subScene)); 736 737 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); 738 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 739 740 // SubScene stylesheet should totally replace default 741 assertFalse(styleMap.containsKey("-fx-fill")); 742 assertTrue(styleMap.containsKey("-fx-stroke")); 743 744 List<CascadingStyle> styles = styleMap.get("-fx-stroke"); 745 assertEquals(1, styles.size()); 746 747 Object obj = styles.get(0).getParsedValueImpl().convert(null); 748 assertEquals(Color.YELLOW, obj); 749 } 750 751 @Test 752 public void testFindMatchingStyles_defaultStyleSheet_subSceneUserAgentStylesheet_parentStylesheet() { 753 754 StyleManager sm = StyleManager.getInstance(); 755 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 756 757 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 758 Group group = new Group(rect); 759 group.getStylesheets().add("/com/sun/javafx/css/ua2.css"); 760 Pane subSceneRoot = new Pane(group); 761 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 762 subScene.setUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 763 Scene scene = new Scene(new Group(subScene)); 764 765 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); 766 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 767 768 // ua2.css has fill 769 assertTrue(styleMap.containsKey("-fx-fill")); 770 assertTrue(styleMap.containsKey("-fx-stroke")); 771 772 // ua1.css and ua2.css have stroke 773 List<CascadingStyle> styles = styleMap.get("-fx-stroke"); 774 assertEquals(2, styles.size()); 775 776 Object obj = styles.get(0).getParsedValueImpl().convert(null); 777 assertEquals(Color.GREEN, obj); 778 779 // ua0.css and ua2.css have fill, but we shouldn't get anything from ua0 780 // since we have a scene user-agent stylesheet 781 styles = styleMap.get("-fx-fill"); 782 assertEquals(1, styles.size()); 783 784 obj = styles.get(0).getParsedValueImpl().convert(null); 785 assertEquals(Color.BLUE, obj); 786 } 787 788 @Test 789 public void testSwitchDefaultUserAgentStylesheets() { 790 791 Rectangle rect = new Rectangle(){{ getStyleClass().add("rect"); }}; 792 Group group = new Group(rect); 793 Pane subSceneRoot = new Pane(group); 794 SubScene subScene = new SubScene(subSceneRoot, 100, 100); 795 Scene scene = new Scene(new Group(subScene)); 796 797 StyleManager sm = StyleManager.getInstance(); 798 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua0.css"); 799 800 StyleMap matchingStyles = sm.findMatchingStyles(rect, subScene, null); 801 Map<String,List<CascadingStyle>> styleMap = matchingStyles.getCascadingStyles(); 802 803 // ua0.css has fill 804 assertTrue(styleMap.containsKey("-fx-fill")); 805 assertFalse(styleMap.containsKey("-fx-stroke")); 806 807 List<CascadingStyle> styles = styleMap.get("-fx-fill"); 808 assertEquals(1, styles.size()); 809 810 Object obj = styles.get(0).getParsedValueImpl().convert(null); 811 assertEquals(Color.RED, obj); 812 813 sm.setDefaultUserAgentStylesheet("/com/sun/javafx/css/ua1.css"); 814 815 matchingStyles = sm.findMatchingStyles(rect, subScene, null); 816 styleMap = matchingStyles.getCascadingStyles(); 817 818 // ua1.css has stroke 819 assertTrue(styleMap.containsKey("-fx-stroke")); 820 assertFalse(styleMap.containsKey("-fx-fill")); 821 822 styles = styleMap.get("-fx-stroke"); 823 assertEquals(1, styles.size()); 824 825 obj = styles.get(0).getParsedValueImpl().convert(null); 826 assertEquals(Color.YELLOW, obj); 827 } 828 829 @Test 830 public void testGetCacheContainer() { 831 832 Rectangle rectangle = new Rectangle(); 833 SubScene subScene = new SubScene(null, 0, 0); 834 835 StyleManager sm = StyleManager.getInstance(); 836 837 // no scene, should return null 838 StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, subScene); 839 840 assertNull(container); 841 842 // has scene, should return non-null 843 subScene.setRoot(new Group()); 844 Scene scene = new Scene(new Group(rectangle,subScene)); 845 container = sm.getCacheContainer(rectangle, subScene); 846 847 assertNotNull(container); 848 849 } 850 851 @Test 852 public void testGetCacheContainer_styleable() { 853 Rectangle rectangle = new Rectangle(); 854 855 StyleManager sm = StyleManager.getInstance(); 856 857 // no scene, should return null 858 StyleManager.CacheContainer container = sm.getCacheContainer(rectangle, null); 859 860 assertNull(container); 861 862 // has scene, should return non-null 863 Scene scene = new Scene(new Group(rectangle)); 864 container = sm.getCacheContainer(rectangle, null); 865 866 assertNotNull(container); 867 868 } 869 870 @Test 871 public void testGetCacheContainer_subScene() { 872 873 SubScene subScene = new SubScene(null, 0, 0); 874 875 StyleManager sm = StyleManager.getInstance(); 876 877 // no scene, should return null 878 StyleManager.CacheContainer container = sm.getCacheContainer(null, subScene); 879 880 assertNull(container); 881 882 // has scene, should return non-null 883 subScene.setRoot(new Group()); 884 Scene scene = new Scene(new Group(subScene)); 885 container = sm.getCacheContainer(null, subScene); 886 887 assertNotNull(container); 888 889 } 890 891 }