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