1 /* 2 * Copyright (c) 2010, 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 javafx.scene.control; 27 28 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassDoesNotExist; 29 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassExists; 30 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains; 31 import static org.junit.Assert.assertEquals; 32 import static org.junit.Assert.assertFalse; 33 import static org.junit.Assert.assertNotNull; 34 import static org.junit.Assert.assertNull; 35 import static org.junit.Assert.assertSame; 36 import static org.junit.Assert.assertTrue; 37 38 import com.sun.javafx.scene.control.infrastructure.StageLoader; 39 import javafx.application.Platform; 40 import javafx.beans.property.BooleanProperty; 41 import javafx.beans.property.DoubleProperty; 42 import javafx.beans.property.ObjectProperty; 43 import javafx.beans.property.SimpleBooleanProperty; 44 import javafx.beans.property.SimpleDoubleProperty; 45 import javafx.beans.property.SimpleObjectProperty; 46 import javafx.beans.value.ChangeListener; 47 import javafx.beans.value.ObservableValue; 48 import javafx.css.CssMetaData; 49 import javafx.css.StyleableProperty; 50 import javafx.event.Event; 51 import javafx.event.EventHandler; 52 import javafx.geometry.Bounds; 53 import javafx.geometry.Side; 54 import javafx.scene.Group; 55 import javafx.scene.Scene; 56 import javafx.scene.input.KeyEvent; 57 import javafx.scene.input.MouseEvent; 58 import javafx.scene.layout.StackPane; 59 import javafx.scene.layout.VBox; 60 import javafx.stage.Stage; 61 62 import org.junit.Before; 63 import org.junit.Ignore; 64 import org.junit.Test; 65 66 import com.sun.javafx.pgstub.StubToolkit; 67 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer; 68 import com.sun.javafx.scene.control.infrastructure.MouseEventGenerator; 69 import com.sun.javafx.scene.control.skin.TabPaneSkin; 70 import com.sun.javafx.scene.input.KeyCodeMap; 71 import com.sun.javafx.tk.Toolkit; 72 73 import java.util.Arrays; 74 import java.util.HashMap; 75 import java.util.LinkedList; 76 import java.util.List; 77 import java.util.Map; 78 79 public class TabPaneTest { 80 private TabPane tabPane;//Empty string 81 private Toolkit tk; 82 private TabPane.TabPaneSelectionModel sm; 83 private Tab tab1; 84 private Tab tab2; 85 private Tab tab3; 86 private Scene scene; 87 private Stage stage; 88 private StackPane root; 89 90 @Before public void setup() { 91 tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM) 92 tabPane = new TabPane(); 93 tab1 = new Tab("one"); 94 tab2 = new Tab("two"); 95 tab3 = new Tab("three"); 96 sm = new TabPane.TabPaneSelectionModel(tabPane); 97 root = new StackPane(); 98 scene = new Scene(root); 99 stage = new Stage(); 100 stage.setScene(scene); 101 } 102 103 /********************************************************************* 104 * Helper methods * 105 ********************************************************************/ 106 107 private void show() { 108 stage.show(); 109 stage.requestFocus(); 110 } 111 112 /********************************************************************* 113 * Tests for default values * 114 ********************************************************************/ 115 116 @Test public void defaultConstructorShouldSetStyleClassTo_tabpane() { 117 assertStyleClassContains(tabPane, "tab-pane"); 118 } 119 120 @Test public void defaultConstructorSelectionModelNotNull() { 121 assertNotNull(tabPane.getSelectionModel()); 122 } 123 124 @Test public void defaultConstructorInitialTabsEmpty() { 125 assertNotNull(tabPane.getTabs()); 126 assertEquals(tabPane.getTabs().size(), 0.0, 0.0); 127 } 128 129 @Test public void defaultSideIsTop() { 130 assertSame(tabPane.getSide(), Side.TOP); 131 } 132 133 @Test public void defaultConstructorTabClosingPolicy() { 134 assertNotNull(tabPane.getTabClosingPolicy()); 135 assertSame(tabPane.getTabClosingPolicy(), TabPane.TabClosingPolicy.SELECTED_TAB); 136 } 137 138 @Test public void defaultConstructorRotateGraphic() { 139 assertFalse(tabPane.isRotateGraphic()); 140 } 141 142 @Test public void defaultTabMinWidth() { 143 assertEquals(tabPane.getTabMinWidth(), 0.0, 0.0); 144 } 145 146 @Test public void defaultTabMaxWidth() { 147 assertEquals(tabPane.getTabMaxWidth(), Double.MAX_VALUE, 0.0); 148 } 149 150 @Test public void defaultTabMinHeight() { 151 assertEquals(tabPane.getTabMinHeight(), 0.0, 0.0); 152 } 153 154 @Test public void defaultTabMaxHeight() { 155 assertEquals(tabPane.getTabMaxHeight(), Double.MAX_VALUE, 0.0); 156 } 157 158 @Test public void defaultSelectionModelEmpty() { 159 assertTrue(tabPane.getSelectionModel().isEmpty()); 160 } 161 162 @Test public void initialBoundsInParentMatchesWidthAndHeight() { 163 TabPane testPane = new TabPane(); 164 testPane.resize(400, 400); 165 testPane.setSkin(new TabPaneSkin(testPane)); 166 Bounds boundsInParent = testPane.getBoundsInParent(); 167 assertEquals(testPane.getWidth(), boundsInParent.getWidth(), 0.0); 168 assertEquals(testPane.getHeight(), boundsInParent.getHeight(), 0.0); 169 } 170 171 /********************************************************************* 172 * Tests for property binding * 173 ********************************************************************/ 174 175 @Test public void checkSelectionModelPropertyBind() { 176 ObjectProperty objPr = new SimpleObjectProperty<SingleSelectionModel<Tab>>(null); 177 tabPane.selectionModelProperty().bind(objPr); 178 assertNull("selectionModel cannot be bound", tabPane.selectionModelProperty().getValue()); 179 objPr.setValue(sm); 180 assertSame("selectionModel cannot be bound", tabPane.selectionModelProperty().getValue(), sm); 181 } 182 183 @Test public void checkSidePropertyBind() { 184 ObjectProperty objPr = new SimpleObjectProperty<Side>(Side.BOTTOM); 185 tabPane.sideProperty().bind(objPr); 186 assertSame("side cannot be bound", tabPane.sideProperty().getValue(), Side.BOTTOM); 187 objPr.setValue(Side.RIGHT); 188 assertSame("side cannot be bound", tabPane.sideProperty().getValue(), Side.RIGHT); 189 } 190 191 @Test public void checkTabClosingPropertyBind() { 192 ObjectProperty objPr = new SimpleObjectProperty<TabPane.TabClosingPolicy>(TabPane.TabClosingPolicy.UNAVAILABLE); 193 tabPane.tabClosingPolicyProperty().bind(objPr); 194 assertSame("side cannot be bound", tabPane.tabClosingPolicyProperty().getValue(), TabPane.TabClosingPolicy.UNAVAILABLE); 195 objPr.setValue(TabPane.TabClosingPolicy.ALL_TABS); 196 assertSame("side cannot be bound", tabPane.tabClosingPolicyProperty().getValue(), TabPane.TabClosingPolicy.ALL_TABS); 197 } 198 199 @Test public void checkRotateGraphicsPropertyBind() { 200 BooleanProperty objPr = new SimpleBooleanProperty(false); 201 tabPane.rotateGraphicProperty().bind(objPr); 202 assertFalse("rotateGraphic cannot be bound", tabPane.rotateGraphicProperty().getValue()); 203 objPr.setValue(true); 204 assertTrue("rotateGraphic cannot be bound", tabPane.rotateGraphicProperty().getValue()); 205 } 206 207 @Test public void checkTabMinWidthPropertyBind() { 208 DoubleProperty objPr = new SimpleDoubleProperty(2.0); 209 tabPane.tabMinWidthProperty().bind(objPr); 210 assertEquals("tabMinWidthProperty cannot be bound", tabPane.tabMinWidthProperty().getValue(), 2.0, 0.0); 211 objPr.setValue(5.0); 212 assertEquals("tabMinWidthProperty cannot be bound", tabPane.tabMinWidthProperty().getValue(), 5.0, 0.0); 213 } 214 215 @Test public void checkTabMaxWidthPropertyBind() { 216 DoubleProperty objPr = new SimpleDoubleProperty(2.0); 217 tabPane.tabMaxWidthProperty().bind(objPr); 218 assertEquals("tabMaxWidthProperty cannot be bound", tabPane.tabMaxWidthProperty().getValue(), 2.0, 0.0); 219 objPr.setValue(5.0); 220 assertEquals("tabMaxWidthProperty cannot be bound", tabPane.tabMaxWidthProperty().getValue(), 5.0, 0.0); 221 } 222 223 224 @Test public void checkTabMinHeightPropertyBind() { 225 DoubleProperty objPr = new SimpleDoubleProperty(2.0); 226 tabPane.tabMinHeightProperty().bind(objPr); 227 assertEquals("tabMinHeightProperty cannot be bound", tabPane.tabMinHeightProperty().getValue(), 2.0, 0.0); 228 objPr.setValue(5.0); 229 assertEquals("tabMinHeightProperty cannot be bound", tabPane.tabMinHeightProperty().getValue(), 5.0, 0.0); 230 } 231 232 @Test public void checkTabMaxHeightPropertyBind() { 233 DoubleProperty objPr = new SimpleDoubleProperty(2.0); 234 tabPane.tabMaxHeightProperty().bind(objPr); 235 assertEquals("tabMaxHeightProperty cannot be bound", tabPane.tabMaxHeightProperty().getValue(), 2.0, 0.0); 236 objPr.setValue(5.0); 237 assertEquals("tabMaxHeightProperty cannot be bound", tabPane.tabMaxHeightProperty().getValue(), 5.0, 0.0); 238 } 239 240 @Test public void selectionModelPropertyHasBeanReference() { 241 assertSame(tabPane, tabPane.selectionModelProperty().getBean()); 242 } 243 244 @Test public void selectionModelPropertyHasName() { 245 assertEquals("selectionModel", tabPane.selectionModelProperty().getName()); 246 } 247 248 @Test public void sidePropertyHasBeanReference() { 249 assertSame(tabPane, tabPane.sideProperty().getBean()); 250 } 251 252 @Test public void sidePropertyHasName() { 253 assertEquals("side", tabPane.sideProperty().getName()); 254 } 255 256 @Test public void tabClosingPolicyPropertyHasBeanReference() { 257 assertSame(tabPane, tabPane.tabClosingPolicyProperty().getBean()); 258 } 259 260 @Test public void tabClosingPolicyPropertyHasName() { 261 assertEquals("tabClosingPolicy", tabPane.tabClosingPolicyProperty().getName()); 262 } 263 264 @Test public void rotateGraphicPropertyHasBeanReference() { 265 assertSame(tabPane, tabPane.rotateGraphicProperty().getBean()); 266 } 267 268 @Test public void rotateGraphicPropertyHasName() { 269 assertEquals("rotateGraphic", tabPane.rotateGraphicProperty().getName()); 270 } 271 272 @Test public void tabMinWidthPropertyHasBeanReference() { 273 assertSame(tabPane, tabPane.tabMinWidthProperty().getBean()); 274 } 275 276 @Test public void tabMinWidthPropertyHasName() { 277 assertEquals("tabMinWidth", tabPane.tabMinWidthProperty().getName()); 278 } 279 280 @Test public void tabMaxWidthPropertyHasBeanReference() { 281 assertSame(tabPane, tabPane.tabMaxWidthProperty().getBean()); 282 } 283 284 @Test public void tabMaxWidthPropertyHasName() { 285 assertEquals("tabMaxWidth", tabPane.tabMaxWidthProperty().getName()); 286 } 287 288 @Test public void tabMinHeightPropertyHasBeanReference() { 289 assertSame(tabPane, tabPane.tabMinHeightProperty().getBean()); 290 } 291 292 @Test public void tabMinHeightPropertyHasName() { 293 assertEquals("tabMinHeight", tabPane.tabMinHeightProperty().getName()); 294 } 295 296 @Test public void tabMaxHeightPropertyHasBeanReference() { 297 assertSame(tabPane, tabPane.tabMaxHeightProperty().getBean()); 298 } 299 300 @Test public void tabMaxHeightPropertyHasName() { 301 assertEquals("tabMaxHeight", tabPane.tabMaxHeightProperty().getName()); 302 } 303 304 305 /********************************************************************* 306 * Check for Pseudo classes * 307 ********************************************************************/ 308 309 @Test public void settingSideSetsPseudoClass() { 310 tabPane.setSide(Side.BOTTOM); 311 assertPseudoClassExists(tabPane, "bottom"); 312 assertPseudoClassDoesNotExist(tabPane, "top"); 313 assertPseudoClassDoesNotExist(tabPane, "left"); 314 assertPseudoClassDoesNotExist(tabPane, "right"); 315 } 316 317 @Test public void clearingSideClearsPseudoClass() { 318 tabPane.setSide(Side.BOTTOM); 319 tabPane.setSide(Side.LEFT); 320 assertPseudoClassExists(tabPane, "left"); 321 assertPseudoClassDoesNotExist(tabPane, "bottom"); 322 assertPseudoClassDoesNotExist(tabPane, "top"); 323 assertPseudoClassDoesNotExist(tabPane, "right"); 324 } 325 326 327 /********************************************************************* 328 * CSS related Tests * 329 ********************************************************************/ 330 331 @Test public void whenTabMinWidthIsBound_impl_cssSettable_ReturnsFalse() { 332 CssMetaData styleable = ((StyleableProperty)tabPane.tabMinWidthProperty()).getCssMetaData(); 333 assertTrue(styleable.isSettable(tabPane)); 334 DoubleProperty other = new SimpleDoubleProperty(30.0); 335 tabPane.tabMinWidthProperty().bind(other); 336 assertFalse(styleable.isSettable(tabPane)); 337 } 338 339 @Test public void whenTabMinWidthIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 340 CssMetaData styleable = ((StyleableProperty)tabPane.tabMinWidthProperty()).getCssMetaData(); 341 assertTrue(styleable.isSettable(tabPane)); 342 } 343 344 @Test public void canSpecifyTabMinWidthViaCSS() { 345 ((StyleableProperty)tabPane.tabMinWidthProperty()).applyStyle(null, 34.0); 346 assertEquals(34.0, tabPane.getTabMinWidth(), 0.0); 347 } 348 349 @Test public void whenTabMaxWidthIsBound_impl_cssSettable_ReturnsFalse() { 350 CssMetaData styleable = ((StyleableProperty)tabPane.tabMaxWidthProperty()).getCssMetaData(); 351 assertTrue(styleable.isSettable(tabPane)); 352 DoubleProperty other = new SimpleDoubleProperty(30.0); 353 tabPane.tabMaxWidthProperty().bind(other); 354 assertFalse(styleable.isSettable(tabPane)); 355 } 356 357 @Test public void whenTabMaxWidthIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 358 CssMetaData styleable = ((StyleableProperty)tabPane.tabMaxWidthProperty()).getCssMetaData(); 359 assertTrue(styleable.isSettable(tabPane)); 360 } 361 362 @Test public void canSpecifyTabMaxWidthViaCSS() { 363 ((StyleableProperty)tabPane.tabMaxWidthProperty()).applyStyle(null, 34.0); 364 assertEquals(34.0, tabPane.getTabMaxWidth(), 0.0); 365 } 366 367 @Test public void whenTabMinHeightIsBound_impl_cssSettable_ReturnsFalse() { 368 CssMetaData styleable = ((StyleableProperty)tabPane.tabMinHeightProperty()).getCssMetaData(); 369 assertTrue(styleable.isSettable(tabPane)); 370 DoubleProperty other = new SimpleDoubleProperty(30.0); 371 tabPane.tabMinHeightProperty().bind(other); 372 assertFalse(styleable.isSettable(tabPane)); 373 } 374 375 @Test public void whenTabMinHeightIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 376 CssMetaData styleable = ((StyleableProperty)tabPane.tabMinHeightProperty()).getCssMetaData(); 377 assertTrue(styleable.isSettable(tabPane)); 378 } 379 380 @Test public void canSpecifyTabMinHeightViaCSS() { 381 ((StyleableProperty)tabPane.tabMinHeightProperty()).applyStyle(null, 34.0); 382 assertEquals(34.0, tabPane.getTabMinHeight(), 0.0); 383 } 384 385 @Test public void whenTabMaxHeightIsBound_impl_cssSettable_ReturnsFalse() { 386 CssMetaData styleable = ((StyleableProperty)tabPane.tabMaxHeightProperty()).getCssMetaData(); 387 assertTrue(styleable.isSettable(tabPane)); 388 DoubleProperty other = new SimpleDoubleProperty(30.0); 389 tabPane.tabMaxHeightProperty().bind(other); 390 assertFalse(styleable.isSettable(tabPane)); 391 } 392 393 @Test public void whenTabMaxHeightIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 394 CssMetaData styleable = ((StyleableProperty)tabPane.tabMaxHeightProperty()).getCssMetaData(); 395 assertTrue(styleable.isSettable(tabPane)); 396 } 397 398 @Test public void canSpecifyTabMaxHeightViaCSS() { 399 ((StyleableProperty)tabPane.tabMaxHeightProperty()).applyStyle(null, 34.0); 400 assertEquals(34.0, tabPane.getTabMaxHeight(), 0.0); 401 } 402 403 404 /********************************************************************* 405 * Miscellaneous Tests * 406 ********************************************************************/ 407 408 @Test public void setSelectionModelAndSeeValueIsReflectedInModel() { 409 tabPane.setSelectionModel(sm); 410 assertSame(tabPane.selectionModelProperty().getValue(), sm); 411 } 412 413 @Test public void setselectionModelAndSeeValue() { 414 tabPane.setSelectionModel(sm); 415 assertSame(tabPane.getSelectionModel(), sm); 416 } 417 418 @Test public void setSideAndSeeValueIsReflectedInModel() { 419 tabPane.setSide(Side.BOTTOM); 420 assertSame(tabPane.sideProperty().getValue(), Side.BOTTOM); 421 } 422 423 @Test public void setSideAndSeeValue() { 424 tabPane.setSide(Side.LEFT); 425 assertSame(tabPane.getSide(), Side.LEFT); 426 } 427 428 @Test public void setTabClosingPolicyAndSeeValueIsReflectedInModel() { 429 tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS); 430 assertSame(tabPane.tabClosingPolicyProperty().getValue(), TabPane.TabClosingPolicy.ALL_TABS); 431 } 432 433 @Test public void setTabClosingPolicyAndSeeValue() { 434 tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); 435 assertSame(tabPane.getTabClosingPolicy(), TabPane.TabClosingPolicy.UNAVAILABLE); 436 } 437 438 @Test public void setRotateGraphicAndSeeValueIsReflectedInModel() { 439 tabPane.setRotateGraphic(true); 440 assertTrue(tabPane.rotateGraphicProperty().getValue()); 441 } 442 443 @Test public void setRotateGraphicAndSeeValue() { 444 tabPane.setRotateGraphic(true); 445 assertTrue(tabPane.isRotateGraphic()); 446 } 447 448 @Test public void setTabMinWidthAndSeeValueIsReflectedInModel() { 449 tabPane.setTabMinWidth(30.0); 450 assertEquals(tabPane.tabMinWidthProperty().getValue(), 30.0, 0.0); 451 } 452 453 @Test public void setTabMinWidthAndSeeValue() { 454 tabPane.setTabMinWidth(30.0); 455 assertEquals(tabPane.getTabMinWidth(), 30.0, 0.0); 456 } 457 458 @Test public void setTabMaxWidthAndSeeValueIsReflectedInModel() { 459 tabPane.setTabMaxWidth(30.0); 460 assertEquals(tabPane.tabMaxWidthProperty().getValue(), 30.0, 0.0); 461 } 462 463 @Test public void setTabMaxWidthAndSeeValue() { 464 tabPane.setTabMaxWidth(30.0); 465 assertEquals(tabPane.getTabMaxWidth(), 30.0, 0.0); 466 } 467 468 @Test public void setTabMinHeightAndSeeValueIsReflectedInModel() { 469 tabPane.setTabMinHeight(30.0); 470 assertEquals(tabPane.tabMinHeightProperty().getValue(), 30.0, 0.0); 471 } 472 473 @Test public void setTabMinHeightAndSeeValue() { 474 tabPane.setTabMinHeight(30.0); 475 assertEquals(tabPane.getTabMinHeight(), 30.0, 0.0); 476 } 477 478 @Test public void setTabMaxHeightAndSeeValueIsReflectedInModel() { 479 tabPane.setTabMaxHeight(30.0); 480 assertEquals(tabPane.tabMaxHeightProperty().getValue(), 30.0, 0.0); 481 } 482 483 @Test public void setTabMaxHeightAndSeeValue() { 484 tabPane.setTabMaxHeight(30.0); 485 assertEquals(tabPane.getTabMaxHeight(), 30.0, 0.0); 486 } 487 488 @Test public void addTabsCheckItemCountInSelectionModel() { 489 tabPane.getTabs().add(tab1); 490 tabPane.getTabs().add(tab2); 491 assertEquals(tabPane.getSelectionModel().getItemCount(), 2.0, 0.0); 492 } 493 494 @Test public void addTabsCheckItemInSelectionModel() { 495 tabPane.getTabs().add(tab1); 496 tabPane.getTabs().add(tab2); 497 assertSame(tabPane.getSelectionModel().getModelItem(0), tab1); 498 assertSame(tabPane.getSelectionModel().getModelItem(1), tab2); 499 } 500 501 @Test public void addTabsCheckSelectUsingObjInSelectionModel() { 502 tabPane.getTabs().add(tab1); 503 tabPane.getTabs().add(tab2); 504 tabPane.getSelectionModel().select(tab1); 505 assertTrue(tabPane.getSelectionModel().isSelected(0)); 506 } 507 508 @Test public void addTabsCheckSelectUsingIndexInSelectionModel() { 509 tabPane.getTabs().add(tab1); 510 tabPane.getTabs().add(tab2); 511 tabPane.getSelectionModel().select(1); 512 assertTrue(tabPane.getSelectionModel().isSelected(1)); 513 } 514 515 @Test public void addTabsCheckClearAndSelectUsingIndexInSelectionModel() { 516 tabPane.getTabs().add(tab1); 517 tabPane.getTabs().add(tab2); 518 tabPane.getSelectionModel().clearAndSelect(1); 519 assertTrue(tabPane.getSelectionModel().isSelected(1)); 520 } 521 522 @Test public void addTabsCheckSelectMultipleItemsAndClearOneItemInSelectionModel() { 523 tabPane.getTabs().add(tab1); 524 tabPane.getTabs().add(tab2); 525 tabPane.getSelectionModel().select(0); 526 tabPane.getSelectionModel().select(1); 527 tabPane.getSelectionModel().clearSelection(0); 528 assertFalse(tabPane.getSelectionModel().isSelected(0)); 529 assertTrue(tabPane.getSelectionModel().isSelected(1)); 530 } 531 532 @Test public void addTabsCheckSelectMultipleItemsAndClearAllSelectionInSelectionModel() { 533 tabPane.getTabs().add(tab1); 534 tabPane.getTabs().add(tab2); 535 tabPane.getSelectionModel().select(0); 536 tabPane.getSelectionModel().select(1); 537 tabPane.getSelectionModel().clearSelection(); 538 assertFalse(tabPane.getSelectionModel().isSelected(0)); 539 assertFalse(tabPane.getSelectionModel().isSelected(1)); 540 assertTrue(tabPane.getSelectionModel().isEmpty()); 541 } 542 543 @Test public void addTabsCheckSelectUsingIteratorKindOfMethodsInSelectionModel() { 544 tabPane.getTabs().add(tab1); 545 tabPane.getTabs().add(tab2); 546 tabPane.getSelectionModel().selectFirst(); 547 assertTrue(tabPane.getSelectionModel().isSelected(0)); 548 assertFalse(tabPane.getSelectionModel().isSelected(1)); 549 tabPane.getSelectionModel().selectNext(); 550 assertFalse(tabPane.getSelectionModel().isSelected(0)); 551 assertTrue(tabPane.getSelectionModel().isSelected(1)); 552 tabPane.getSelectionModel().clearSelection(); 553 tabPane.getSelectionModel().selectLast(); 554 assertFalse(tabPane.getSelectionModel().isSelected(0)); 555 assertTrue(tabPane.getSelectionModel().isSelected(1)); 556 tabPane.getSelectionModel().selectPrevious(); 557 assertTrue(tabPane.getSelectionModel().isSelected(0)); 558 assertFalse(tabPane.getSelectionModel().isSelected(1)); 559 } 560 561 @Test public void flipTabOrder_RT20156() { 562 tabPane.setSkin(new TabPaneSkin(tabPane)); 563 tabPane.getTabs().add(tab1); 564 tabPane.getTabs().add(tab2); 565 tabPane.getTabs().add(tab3); 566 567 assertEquals("one", tabPane.getTabs().get(0).getText()); 568 assertEquals("two", tabPane.getTabs().get(1).getText()); 569 assertEquals("three", tabPane.getTabs().get(2).getText()); 570 571 final int lastTabIndex = tabPane.getTabs().size()-1; 572 final Tab lastTab = tabPane.getTabs().get(lastTabIndex); 573 tabPane.getTabs().remove(lastTabIndex); 574 tabPane.getTabs().add(0, lastTab); 575 576 assertEquals("three", tabPane.getTabs().get(0).getText()); 577 assertEquals("one", tabPane.getTabs().get(1).getText()); 578 assertEquals("two", tabPane.getTabs().get(2).getText()); 579 assertTrue(tabPane.getSelectionModel().isSelected(1)); 580 } 581 582 @Test public void disableAllTabs() { 583 tabPane.setSkin(new TabPaneSkin(tabPane)); 584 tabPane.getTabs().add(tab1); 585 tabPane.getTabs().add(tab2); 586 tabPane.getTabs().add(tab3); 587 588 tab1.setDisable(true); 589 tab2.setDisable(true); 590 tab3.setDisable(true); 591 592 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 593 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 594 } 595 596 @Test public void disableFirstTabs() { 597 tabPane.setSkin(new TabPaneSkin(tabPane)); 598 tabPane.getTabs().add(tab1); 599 tabPane.getTabs().add(tab2); 600 tabPane.getTabs().add(tab3); 601 602 tab1.setDisable(true); 603 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 604 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 605 } 606 607 @Test public void disableATabAndSelectItByIndex() { 608 tabPane.setSkin(new TabPaneSkin(tabPane)); 609 tabPane.getTabs().add(tab1); 610 tabPane.getTabs().add(tab2); 611 tabPane.getTabs().add(tab3); 612 613 tab2.setDisable(true); 614 tabPane.getSelectionModel().select(1); 615 assertEquals(1, tabPane.getSelectionModel().getSelectedIndex()); 616 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 617 } 618 619 @Test public void selectByIndexADisableTab() { 620 tabPane.setSkin(new TabPaneSkin(tabPane)); 621 tabPane.getTabs().add(tab1); 622 tabPane.getTabs().add(tab2); 623 tabPane.getTabs().add(tab3); 624 625 tabPane.getSelectionModel().select(1); 626 tab2.setDisable(true); 627 assertEquals(1, tabPane.getSelectionModel().getSelectedIndex()); 628 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 629 assertTrue(tab2.isDisable()); 630 } 631 632 @Test public void disableATabAndSelectItByTab() { 633 tabPane.setSkin(new TabPaneSkin(tabPane)); 634 tabPane.getTabs().add(tab1); 635 tabPane.getTabs().add(tab2); 636 tabPane.getTabs().add(tab3); 637 638 tab2.setDisable(true); 639 tabPane.getSelectionModel().select(tab2); 640 assertEquals(1, tabPane.getSelectionModel().getSelectedIndex()); 641 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 642 } 643 644 @Test public void selectByTabADisableTab() { 645 tabPane.setSkin(new TabPaneSkin(tabPane)); 646 tabPane.getTabs().add(tab1); 647 tabPane.getTabs().add(tab2); 648 tabPane.getTabs().add(tab3); 649 650 tabPane.getSelectionModel().select(tab2); 651 tab2.setDisable(true); 652 assertEquals(1, tabPane.getSelectionModel().getSelectedIndex()); 653 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 654 assertTrue(tab2.isDisable()); 655 } 656 657 @Test public void navigateOverDisableTab() { 658 tabPane.getTabs().add(tab1); 659 tabPane.getTabs().add(tab2); 660 tabPane.getTabs().add(tab3); 661 662 root.getChildren().add(tabPane); 663 show(); 664 tabPane.requestFocus(); 665 666 assertTrue(tabPane.isFocused()); 667 668 tab2.setDisable(true); 669 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 670 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 671 assertTrue(tab2.isDisable()); 672 673 KeyEventFirer keyboard = new KeyEventFirer(tabPane); 674 keyboard.doRightArrowPress(); 675 676 assertEquals(2, tabPane.getSelectionModel().getSelectedIndex()); 677 assertEquals(tab3, tabPane.getSelectionModel().getSelectedItem()); 678 679 keyboard.doLeftArrowPress(); 680 681 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 682 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 683 } 684 685 @Ignore 686 @Test public void mousePressSelectsATab_RT20476() { 687 tabPane.getTabs().add(tab1); 688 tabPane.getTabs().add(tab2); 689 tabPane.getTabs().add(tab3); 690 691 tab1.setContent(new Button("TAB1")); 692 tab2.setContent(new Button("TAB2")); 693 tab3.setContent(new Button("TAB3")); 694 695 root.getChildren().add(tabPane); 696 show(); 697 698 root.applyCss(); 699 root.resize(300, 300); 700 root.layout(); 701 702 tk.firePulse(); 703 assertTrue(tabPane.isFocused()); 704 705 double xval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinX(); 706 double yval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinY(); 707 708 scene.impl_processMouseEvent( 709 MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+75, yval+20)); 710 tk.firePulse(); 711 712 assertEquals(1, tabPane.getSelectionModel().getSelectedIndex()); 713 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 714 } 715 716 private int counter = 0; 717 @Ignore 718 @Test public void setOnSelectionChangedFiresTwice_RT21089() { 719 tabPane.getTabs().add(tab1); 720 tabPane.getTabs().add(tab2); 721 722 tab1.setContent(new Button("TAB1")); 723 tab2.setContent(new Button("TAB2")); 724 725 root.getChildren().add(tabPane); 726 show(); 727 728 root.applyCss(); 729 root.resize(300, 300); 730 root.layout(); 731 732 tk.firePulse(); 733 assertTrue(tabPane.isFocused()); 734 735 tab2.setOnSelectionChanged(event -> { 736 assertEquals(0, counter++); 737 }); 738 739 double xval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinX(); 740 double yval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinY(); 741 742 scene.impl_processMouseEvent( 743 MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+75, yval+20)); 744 tk.firePulse(); 745 746 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 747 748 scene.impl_processMouseEvent( 749 MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+75, yval+20)); 750 tk.firePulse(); 751 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 752 } 753 754 @Test public void unableToSelectNextTabWhenFirstTabIsClosed_RT22326() { 755 tabPane.getTabs().add(tab1); 756 tabPane.getTabs().add(tab2); 757 tabPane.getTabs().add(tab3); 758 759 assertEquals("one", tabPane.getTabs().get(0).getText()); 760 assertEquals("two", tabPane.getTabs().get(1).getText()); 761 assertEquals("three", tabPane.getTabs().get(2).getText()); 762 763 tabPane.getTabs().remove(tab1); 764 765 assertEquals(2, tabPane.getTabs().size()); 766 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 767 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 768 tabPane.getSelectionModel().selectNext(); 769 assertEquals(tab3, tabPane.getSelectionModel().getSelectedItem()); 770 } 771 772 @Test public void removeFirstTabNextTabShouldBeSelected() { 773 tabPane.getTabs().add(tab1); 774 tabPane.getTabs().add(tab2); 775 tabPane.getTabs().add(tab3); 776 777 assertEquals("one", tabPane.getTabs().get(0).getText()); 778 assertEquals("two", tabPane.getTabs().get(1).getText()); 779 assertEquals("three", tabPane.getTabs().get(2).getText()); 780 781 tabPane.getTabs().remove(tab1); 782 783 assertEquals(2, tabPane.getTabs().size()); 784 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 785 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 786 } 787 788 @Test public void selectionModelShouldNotBeNullWhenClosingFirstTab_RT22925() { 789 tabPane.getTabs().add(tab1); 790 tabPane.getTabs().add(tab2); 791 tabPane.getTabs().add(tab3); 792 793 tabPane.getSelectionModel().selectedItemProperty().addListener((ov, t, t1) -> { 794 assertEquals(t.getText(), "one"); 795 assertEquals(t1.getText(), "two"); 796 }); 797 798 assertEquals("one", tabPane.getTabs().get(0).getText()); 799 assertEquals("two", tabPane.getTabs().get(1).getText()); 800 assertEquals("three", tabPane.getTabs().get(2).getText()); 801 802 tabPane.getTabs().remove(tab1); 803 804 assertEquals(2, tabPane.getTabs().size()); 805 assertEquals(0, tabPane.getSelectionModel().getSelectedIndex()); 806 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 807 } 808 809 810 boolean button1Focused = false; 811 @Test public void focusTraversalShouldLookInsideEmbeddedEngines() { 812 813 Button b1 = new Button("Button1"); 814 final ChangeListener<Boolean> focusListener = (observable, oldVal, newVal) -> { 815 button1Focused = true; 816 }; 817 b1.focusedProperty().addListener(focusListener); 818 819 final ScrollPane sp = new ScrollPane(); 820 final VBox vbox1 = new VBox(); 821 vbox1.setSpacing(10); 822 vbox1.setTranslateX(10); 823 vbox1.setTranslateY(10); 824 vbox1.getChildren().addAll(b1); 825 tab1.setContent(vbox1); 826 sp.setContent(vbox1); 827 tab1.setContent(sp); 828 tabPane.getTabs().add(tab1); 829 830 tabPane.getTabs().add(tab2); 831 832 final Scene scene1 = new Scene(new Group(), 400, 400); 833 ((Group)scene1.getRoot()).getChildren().add(tabPane); 834 835 stage.setScene(scene1); 836 stage.show(); 837 stage.requestFocus(); 838 839 final KeyEvent tabEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCodeMap.valueOf(0x09), 840 false, false, false, false); 841 Platform.runLater(() -> { 842 tabPane.requestFocus(); 843 Event.fireEvent(tabPane, tabEvent); 844 845 }); 846 847 assertTrue(button1Focused); 848 849 } 850 851 @Test public void test_rt_35013() { 852 SplitPane splitPane = new SplitPane(); 853 splitPane.getItems().addAll(new Button("Button1"), new Button("Button2")); 854 855 TabPane tabPane = new TabPane(); 856 Tab emptyTab; 857 Tab splitTab = new Tab("SplitPane Tab"); 858 splitTab.setContent(splitPane); 859 tabPane.getTabs().addAll(emptyTab = new Tab("Empty Tab"), splitTab); 860 861 StageLoader sl = new StageLoader(tabPane); 862 863 tabPane.getSelectionModel().select(emptyTab); 864 Toolkit.getToolkit().firePulse(); 865 assertFalse(splitPane.getParent().isVisible()); 866 867 tabPane.getSelectionModel().select(splitTab); 868 Toolkit.getToolkit().firePulse(); 869 assertTrue(splitPane.getParent().isVisible()); 870 871 sl.dispose(); 872 } 873 874 @Test public void test_rt_36456_default_selectionMovesBackwardOne() { 875 Tab tab0 = new Tab("Tab 0"); 876 Tab tab1 = new Tab("Tab 1"); 877 Tab tab2 = new Tab("Tab 2"); 878 879 TabPane tabPane = new TabPane(); 880 tabPane.getTabs().addAll(tab0, tab1, tab2); 881 tabPane.getSelectionModel().select(tab1); 882 883 StageLoader sl = new StageLoader(tabPane); 884 885 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 886 tabPane.getTabs().remove(tab1); 887 assertEquals(tab0, tabPane.getSelectionModel().getSelectedItem()); 888 889 sl.dispose(); 890 } 891 892 @Test public void test_rt_36456_selectionMovesBackwardTwoSkippingDisabledTab() { 893 Tab tab0 = new Tab("Tab 0"); 894 Tab tab1 = new Tab("Tab 1"); 895 tab1.setDisable(true); 896 Tab tab2 = new Tab("Tab 2"); 897 898 TabPane tabPane = new TabPane(); 899 tabPane.getTabs().addAll(tab0, tab1, tab2); 900 tabPane.getSelectionModel().select(tab2); 901 902 StageLoader sl = new StageLoader(tabPane); 903 904 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 905 tabPane.getTabs().remove(tab2); 906 907 // selection should jump from tab2 to tab0, as tab1 is disabled 908 assertEquals(tab0, tabPane.getSelectionModel().getSelectedItem()); 909 910 sl.dispose(); 911 } 912 913 @Test public void test_rt_36456_selectionMovesForwardOne() { 914 Tab tab0 = new Tab("Tab 0"); 915 tab0.setDisable(true); 916 Tab tab1 = new Tab("Tab 1"); 917 Tab tab2 = new Tab("Tab 2"); 918 919 TabPane tabPane = new TabPane(); 920 tabPane.getTabs().addAll(tab0, tab1, tab2); 921 tabPane.getSelectionModel().select(tab1); 922 923 StageLoader sl = new StageLoader(tabPane); 924 925 assertEquals(tab1, tabPane.getSelectionModel().getSelectedItem()); 926 tabPane.getTabs().remove(tab1); 927 928 // selection should move to the next non-disabled tab - in this case tab2 929 assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem()); 930 931 sl.dispose(); 932 } 933 934 @Test public void test_rt_36456_selectionMovesForwardTwoSkippingDisabledTab() { 935 Tab tab0 = new Tab("Tab 0"); 936 Tab tab1 = new Tab("Tab 1"); 937 tab1.setDisable(true); 938 Tab tab2 = new Tab("Tab 2"); 939 940 TabPane tabPane = new TabPane(); 941 tabPane.getTabs().addAll(tab0, tab1, tab2); 942 tabPane.getSelectionModel().select(tab0); 943 944 StageLoader sl = new StageLoader(tabPane); 945 946 Tab selectedTab = tabPane.getSelectionModel().getSelectedItem(); 947 assertEquals(tab0, selectedTab); 948 tabPane.getTabs().remove(tab0); 949 950 // selection should move to the next non-disabled tab - in this case tab2 951 selectedTab = tabPane.getSelectionModel().getSelectedItem(); 952 assertEquals(tab2.getText() + " != " + tab2.getText(), tab2, selectedTab); 953 954 sl.dispose(); 955 } 956 957 @Test public void test_rt_36908() { 958 TabPane pane = new TabPane(); 959 final Tab disabled = new Tab("Disabled"); 960 disabled.setDisable(true); 961 962 Tab tab1 = new Tab("Tab 1"); 963 Tab tab2 = new Tab("Tab 2"); 964 pane.getTabs().addAll(disabled, tab1, tab2); 965 966 assertEquals(1, pane.getSelectionModel().getSelectedIndex()); 967 assertEquals(tab1, pane.getSelectionModel().getSelectedItem()); 968 } 969 970 @Test public void test_rt_24658() { 971 Button btn = new Button("Button"); 972 final Tab disabled = new Tab("Disabled"); 973 disabled.setContent(btn); 974 975 TabPane pane = new TabPane(); 976 pane.getTabs().addAll(disabled); 977 978 assertEquals(0, pane.getSelectionModel().getSelectedIndex()); 979 assertEquals(disabled, pane.getSelectionModel().getSelectedItem()); 980 assertFalse(btn.isDisabled()); 981 982 disabled.setDisable(true); 983 assertTrue(btn.isDisabled()); 984 985 disabled.setDisable(false); 986 assertFalse(btn.isDisabled()); 987 } 988 989 @Test public void test_rt_38382_noAddToTabPane() { 990 test_rt_38382(false); 991 } 992 993 @Test public void test_rt_38382_addToTabPane() { 994 test_rt_38382(true); 995 } 996 997 public void test_rt_38382(boolean addToTabPane) { 998 final List<String> names = Arrays.asList( 999 "Biomass", 1000 "Exploitable Population Biomass", 1001 "MSY", 1002 "Yield", 1003 "Recruitment", 1004 "Catch", 1005 "Effort"); 1006 final Map<Tab, List<String>> fooMap = new HashMap<>(); 1007 final List<Tab> tabList = new LinkedList<>(); 1008 for (String name : names) { 1009 final Tab tab = new Tab(); 1010 tab.setText(name); 1011 fooMap.put(tab, new LinkedList<>()); 1012 tabList.add(tab); 1013 } 1014 TabPane tabPane = new TabPane(); 1015 1016 if (addToTabPane) { 1017 tabPane.getTabs().setAll(tabList); 1018 } 1019 1020 fooMap.entrySet().forEach(entry -> { 1021 final Tab tab = entry.getKey(); 1022 assertTrue(tabList.contains(tab)); 1023 assertTrue(fooMap.containsKey(tab)); 1024 }); 1025 } 1026 }