1 /*
   2  * Copyright (c) 2010, 2017, 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.event.EventDispatchChainImpl;
  29 
  30 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*;
  31 
  32 import test.com.sun.javafx.pgstub.StubToolkit;
  33 import com.sun.javafx.tk.Toolkit;
  34 import javafx.beans.property.BooleanProperty;
  35 import javafx.beans.property.ObjectProperty;
  36 import javafx.beans.property.ReadOnlyBooleanProperty;
  37 import javafx.beans.property.ReadOnlyObjectProperty;
  38 import javafx.beans.property.SimpleBooleanProperty;
  39 import javafx.beans.property.SimpleObjectProperty;
  40 import javafx.beans.property.SimpleStringProperty;
  41 import javafx.beans.property.StringProperty;
  42 import javafx.event.Event;
  43 import javafx.event.EventDispatchChain;
  44 import javafx.event.EventHandler;
  45 import javafx.scene.Node;
  46 import javafx.scene.control.ContextMenu;
  47 import javafx.scene.control.TabPane;
  48 import javafx.scene.control.TabShim;
  49 import javafx.scene.control.Tooltip;
  50 import javafx.scene.shape.Rectangle;
  51 import javafx.scene.layout.VBox;
  52 import static org.junit.Assert.*;
  53 
  54 
  55 import org.junit.Before;
  56 import org.junit.Ignore;
  57 import org.junit.Test;
  58 
  59 /**
  60  *
  61  * @author srikalyc
  62  */
  63 public class TabTest {
  64     private TabShim tab;//Empty string
  65     private TabShim tabWithStr;//
  66     private TabPane dummyTabPane;
  67     private Toolkit tk;
  68         EventHandler eh;
  69 
  70     @Before public void setup() {
  71         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  72         tab = new TabShim();
  73         tabWithStr = new TabShim("text");
  74         dummyTabPane = new TabPane();
  75         eh = event -> { };
  76     }
  77 
  78 
  79 
  80     /*********************************************************************
  81      * Tests for default values                                         *
  82      ********************************************************************/
  83 
  84     @Test public void defaultConstructorShouldSetStyleClassTo_tab() {
  85         assertStyleClassContains(tab, "tab");
  86     }
  87 
  88     @Test public void oneArgConstructorShouldSetStyleClassTo_tab() {
  89         assertStyleClassContains(tabWithStr, "tab");
  90     }
  91 
  92     @Test public void defaultConstructorText() {
  93         assertNull(tab.getText());
  94     }
  95 
  96     @Test public void oneConstructorText() {
  97         assertEquals(tabWithStr.getText(), "text");
  98     }
  99 
 100     @Test public void defaultId() {
 101         assertNull(tab.getId());
 102     }
 103 
 104     @Test public void defaultStyle() {
 105         assertNull(tab.getStyle());
 106     }
 107 
 108     @Test public void defaultSelected() {
 109         assertFalse(tab.isSelected());
 110     }
 111 
 112     @Test public void defaultTab() {
 113         assertNull(tab.getTabPane());
 114     }
 115 
 116     @Test public void checkDefaultGraphic() {
 117         assertNull(tab.getGraphic());
 118         assertNull(tabWithStr.getGraphic());
 119     }
 120 
 121     @Test public void checkDefaultContent() {
 122         assertNull(tab.getContent());
 123         assertNull(tabWithStr.getContent());
 124     }
 125 
 126     @Test public void checkDefaultContextMenu() {
 127         assertNull(tab.getContextMenu());
 128         assertNull(tabWithStr.getContextMenu());
 129     }
 130 
 131     @Test public void defaultClosable() {
 132         assertTrue(tab.isClosable());
 133     }
 134 
 135     @Test public void checkDefaultOnSelectetionChanged() {
 136         assertNull(tab.getOnSelectionChanged());
 137         assertNull(tabWithStr.getOnSelectionChanged());
 138     }
 139 
 140     @Test public void checkDefaultOnClosed() {
 141         assertNull(tab.getOnClosed());
 142         assertNull(tabWithStr.getOnClosed());
 143     }
 144 
 145     @Test public void checkDefaultTooltip() {
 146         assertNull(tab.getTooltip());
 147         assertNull(tabWithStr.getTooltip());
 148     }
 149 
 150 
 151 
 152     /*********************************************************************
 153      * Tests for property binding                                        *
 154      ********************************************************************/
 155 
 156 
 157     @Test public void checkIdPropertyBind() {
 158         StringProperty objPr = new SimpleStringProperty("one");
 159         tab.idProperty().bind(objPr);
 160         assertEquals("idProperty cannot be bound", tab.idProperty().getValue(), "one");
 161         objPr.setValue("another");
 162         assertEquals("idProperty cannot be bound", tab.idProperty().getValue(), "another");
 163     }
 164 
 165     @Test public void checkStylePropertyBind() {
 166         StringProperty objPr = new SimpleStringProperty("one");
 167         tab.styleProperty().bind(objPr);
 168         assertEquals("styleProperty cannot be bound", tab.styleProperty().getValue(), "one");
 169         objPr.setValue("another");
 170         assertEquals("styleProperty cannot be bound", tab.styleProperty().getValue(), "another");
 171     }
 172 
 173     @Test public void checkSelectedPropertyReadOnly() {
 174         assertTrue(tab.selectedProperty() instanceof ReadOnlyBooleanProperty);
 175     }
 176 
 177     @Test public void checkTabPanePropertyReadOnly() {
 178         assertTrue(tab.tabPaneProperty() instanceof ReadOnlyObjectProperty);
 179     }
 180 
 181     @Test public void checkTextPropertyBind() {
 182         StringProperty strPr = new SimpleStringProperty("value");
 183         tab.textProperty().bind(strPr);
 184         assertEquals("Text cannot be bound", tab.textProperty().getValue(), "value");
 185         strPr.setValue("newvalue");
 186         assertEquals("Text cannot be bound", tab.textProperty().getValue(), "newvalue");
 187     }
 188 
 189     @Test public void checkGraphicPropertyBind() {
 190         ObjectProperty objPr = new SimpleObjectProperty<Node>(null);
 191         Rectangle rect = new Rectangle(10, 20);
 192         tab.graphicProperty().bind(objPr);
 193         assertNull("Graphic cannot be bound", tab.graphicProperty().getValue());
 194         objPr.setValue(rect);
 195         assertSame("Graphic cannot be bound", tab.graphicProperty().getValue(), rect);
 196     }
 197 
 198     @Test public void checkContentPropertyBind() {
 199         ObjectProperty objPr = new SimpleObjectProperty<Node>(null);
 200         Rectangle rect = new Rectangle(10, 20);
 201         tab.contentProperty().bind(objPr);
 202         assertNull("content cannot be bound", tab.contentProperty().getValue());
 203         objPr.setValue(rect);
 204         assertSame("content cannot be bound", tab.contentProperty().getValue(), rect);
 205     }
 206 
 207     @Test public void checkContextMenuPropertyBind() {
 208         ObjectProperty objPr = new SimpleObjectProperty<ContextMenu>(null);
 209         ContextMenu mnu = new ContextMenu();
 210         tab.contextMenuProperty().bind(objPr);
 211         assertNull("contextMenu cannot be bound", tab.contextMenuProperty().getValue());
 212         objPr.setValue(mnu);
 213         assertSame("contextMenu cannot be bound", tab.contextMenuProperty().getValue(), mnu);
 214     }
 215 
 216     @Test public void checkClosablePropertyBind() {
 217         BooleanProperty pr = new SimpleBooleanProperty(true);
 218         tab.closableProperty().bind(pr);
 219         assertTrue("closable cannot be bound", tab.closableProperty().getValue());
 220         pr.setValue(false);
 221         assertFalse("closable cannot be bound", tab.closableProperty().getValue());
 222     }
 223 
 224     @Test public void checkOnSelectionChangedPropertyBind() {
 225         ObjectProperty objPr = new SimpleObjectProperty<EventHandler<Event>>(null);
 226         tab.onSelectionChangedProperty().bind(objPr);
 227         assertNull("onSelectionChanged cannot be bound", tab.onSelectionChangedProperty().getValue());
 228         objPr.setValue(eh);
 229         assertSame("onSelectionChanged cannot be bound", tab.onSelectionChangedProperty().getValue(), eh);
 230     }
 231 
 232     @Test public void checkOnClosedPropertyBind() {
 233         ObjectProperty objPr = new SimpleObjectProperty<EventHandler<Event>>(null);
 234         tab.onClosedProperty().bind(objPr);
 235         assertNull("onSelectionChanged cannot be bound", tab.onClosedProperty().getValue());
 236         objPr.setValue(eh);
 237         assertSame("onSelectionChanged cannot be bound", tab.onClosedProperty().getValue(), eh);
 238     }
 239 
 240     @Test public void checkTooltipPropertyBind() {
 241         ObjectProperty objPr = new SimpleObjectProperty<Tooltip>(null);
 242         tab.tooltipProperty().bind(objPr);
 243         assertNull("tooltip cannot be bound", tab.tooltipProperty().getValue());
 244         Tooltip tt = new Tooltip();
 245         objPr.setValue(tt);
 246         assertSame("tooltip cannot be bound", tab.tooltipProperty().getValue(), tt);
 247     }
 248 
 249     @Test public void textPropertyHasBeanReference() {
 250         assertSame(tab, tab.textProperty().getBean());
 251     }
 252 
 253     @Test public void textPropertyHasName() {
 254         assertEquals("text", tab.textProperty().getName());
 255     }
 256 
 257 
 258     @Test public void graphicPropertyHasBeanReference() {
 259         assertSame(tab, tab.graphicProperty().getBean());
 260     }
 261 
 262     @Test public void graphicPropertyHasName() {
 263         assertEquals("graphic", tab.graphicProperty().getName());
 264     }
 265 
 266     @Test public void contentPropertyHasBeanReference() {
 267         assertSame(tab, tab.contentProperty().getBean());
 268     }
 269 
 270     @Test public void contentPropertyHasName() {
 271         assertEquals("content", tab.contentProperty().getName());
 272     }
 273 
 274     @Test public void contextMenuPropertyHasBeanReference() {
 275         assertSame(tab, tab.contextMenuProperty().getBean());
 276     }
 277 
 278     @Test public void contextMenuPropertyHasName() {
 279         assertEquals("contextMenu", tab.contextMenuProperty().getName());
 280     }
 281 
 282     @Test public void closablePropertyHasBeanReference() {
 283         assertSame(tab, tab.closableProperty().getBean());
 284     }
 285 
 286     @Test public void closablePropertyHasName() {
 287         assertEquals("closable", tab.closableProperty().getName());
 288     }
 289 
 290     @Test public void onSelectionChangedPropertyHasBeanReference() {
 291         assertSame(tab, tab.onSelectionChangedProperty().getBean());
 292     }
 293 
 294     @Test public void onSelectionChangedPropertyHasName() {
 295         assertEquals("onSelectionChanged", tab.onSelectionChangedProperty().getName());
 296     }
 297 
 298     @Test public void onClosedPropertyHasBeanReference() {
 299         assertSame(tab, tab.onClosedProperty().getBean());
 300     }
 301 
 302     @Test public void onClosedPropertyHasName() {
 303         assertEquals("onClosed",  tab.onClosedProperty().getName());
 304     }
 305 
 306     @Test public void tooltipPropertyHasBeanReference() {
 307         assertSame(tab, tab.tooltipProperty().getBean());
 308     }
 309 
 310     @Test public void tooltipPropertyHasName() {
 311         assertEquals("tooltip",  tab.tooltipProperty().getName());
 312     }
 313 
 314 
 315 
 316     /*********************************************************************
 317      * Miscellaneous Tests                                               *
 318      ********************************************************************/
 319     @Test public void setIdAndSeeValueIsReflectedInModel() {
 320         tab.setId("one");
 321         assertEquals(tab.idProperty().getValue(), "one");
 322     }
 323 
 324     @Test public void setIdAndSeeValue() {
 325         tab.setId("one");
 326         assertEquals(tab.getId(), "one");
 327     }
 328 
 329     @Test public void setStyleAndSeeValueIsReflectedInModel() {
 330         tab.setStyle("one");
 331         assertEquals(tab.styleProperty().getValue(), "one");
 332     }
 333 
 334     @Test public void setStyleAndSeeValue() {
 335         tab.setStyle("one");
 336         assertEquals(tab.getStyle(), "one");
 337     }
 338 
 339     @Test public void setSelectedAndSeeValueIsReflectedInModel() {
 340         tab.shim_setSelected(true);
 341         assertTrue(tab.selectedProperty().getValue());
 342     }
 343 
 344     @Test public void setSelectedAndSeeValue() {
 345         tab.shim_setSelected(true);
 346         assertTrue(tab.isSelected());
 347     }
 348 
 349     @Test public void setTabpaneAndSeeValueIsReflectedInModel() {
 350         tab.shim_setTabPane(dummyTabPane);
 351         assertSame(tab.tabPaneProperty().getValue(), dummyTabPane);
 352     }
 353 
 354     @Test public void setTabpaneAndSeeValue() {
 355         tab.shim_setTabPane(dummyTabPane);
 356         assertSame(tab.getTabPane(), dummyTabPane);
 357     }
 358 
 359     @Test public void setTextAndSeeValueIsReflectedInModel() {
 360         tab.setText("tmp");
 361         assertEquals(tab.textProperty().getValue(), "tmp");
 362     }
 363 
 364     @Test public void setTextAndSeeValue() {
 365         tab.setText("tmp");
 366         assertEquals(tab.getText(), "tmp");
 367     }
 368 
 369     @Test public void setGraphicAndSeeValueIsReflectedInModel() {
 370         Rectangle rect = new Rectangle();
 371         tab.setGraphic(rect);
 372         assertEquals(tab.graphicProperty().getValue(), rect);
 373     }
 374 
 375     @Test public void setGraphicAndSeeValue() {
 376         Rectangle rect = new Rectangle();
 377         tab.setGraphic(rect);
 378         assertEquals(tab.getGraphic(), rect);
 379     }
 380 
 381     @Test public void setContentAndSeeValueIsReflectedInModel() {
 382         Rectangle rect = new Rectangle();
 383         tab.setContent(rect);
 384         assertEquals(tab.contentProperty().getValue(), rect);
 385     }
 386 
 387     @Test public void setContentAndSeeValue() {
 388         Rectangle rect = new Rectangle();
 389         tab.setContent(rect);
 390         assertEquals(tab.getContent(), rect);
 391     }
 392 
 393     @Test public void setContextMenuAndSeeValueIsReflectedInModel() {
 394         ContextMenu mnu = new ContextMenu();
 395         tab.setContextMenu(mnu);
 396         assertSame(tab.contextMenuProperty().getValue(), mnu);
 397     }
 398 
 399     @Test public void setContextMenuAndSeeValue() {
 400         ContextMenu mnu = new ContextMenu();
 401         tab.setContextMenu(mnu);
 402         assertSame(tab.getContextMenu(), mnu);
 403     }
 404 
 405     @Test public void setClosableAndSeeValueIsReflectedInModel() {
 406         tab.setClosable(true);
 407         assertTrue(tab.closableProperty().getValue());
 408     }
 409 
 410     @Test public void setClosableAndSeeValue() {
 411         tab.setClosable(true);
 412         assertTrue(tab.isClosable());
 413     }
 414 
 415     @Test public void setOnSelectionChangedAndSeeValueIsReflectedInModel() {
 416         tab.setOnSelectionChanged(eh);
 417         assertSame(tab.onSelectionChangedProperty().getValue(), eh);
 418     }
 419 
 420     @Test public void setOnSelectionChangedAndSeeValue() {
 421         tab.setOnSelectionChanged(eh);
 422         assertSame(tab.getOnSelectionChanged(), eh);
 423     }
 424 
 425     @Test public void setOnClosedAndSeeValueIsReflectedInModel() {
 426         tab.setOnClosed(eh);
 427         assertSame(tab.onClosedProperty().getValue(), eh);
 428     }
 429 
 430     @Test public void setOnClosedAndSeeValue() {
 431         tab.setOnClosed(eh);
 432         assertSame(tab.getOnClosed(), eh);
 433     }
 434 
 435     @Test public void setTooltipAndSeeValueIsReflectedInModel() {
 436         Tooltip tt = new Tooltip();
 437         tab.setTooltip(tt);
 438         assertSame(tab.tooltipProperty().getValue(), tt);
 439     }
 440 
 441     @Test public void setTooltipAndSeeValue() {
 442         Tooltip tt = new Tooltip();
 443         tab.setTooltip(tt);
 444         assertSame(tab.getTooltip(), tt);
 445     }
 446     @Ignore("The following test is incomplete with no proper sense.")
 447     @Test public void checkEventDispatcherChain() {
 448         EventDispatchChain chain = new EventDispatchChainImpl();
 449         tab.buildEventDispatchChain(chain);
 450     }
 451 
 452     @Test public void setDisableAndSeeValue() {
 453         tab.setDisable(true);
 454         assertTrue(tab.isDisable());
 455     }
 456 
 457     @Test public void setDisableAndSeeDisabledValue() {
 458         tab.setDisable(true);
 459         assertTrue(tab.isDisabled());
 460     }
 461 
 462     @Test public void setDisableOnTabPaneAndSeeValue() {
 463         dummyTabPane.getTabs().add(tab);
 464         assertFalse(tab.isDisable());
 465         assertFalse(tab.isDisabled());
 466 
 467         dummyTabPane.setDisable(true);
 468         assertFalse(tab.isDisable());
 469         assertTrue(tab.isDisabled());
 470 
 471         dummyTabPane.setDisable(false);
 472         assertFalse(tab.isDisable());
 473         assertFalse(tab.isDisabled());
 474     }
 475 
 476     @Test public void setDisableOnTabPaneContentAndSeeValue() {
 477         VBox vBox = new VBox();
 478         dummyTabPane.getTabs().add(tab);
 479         tab.setDisable(true);
 480         tab.setContent(vBox);
 481         assertTrue(vBox.isDisable());
 482         assertTrue(vBox.isDisabled());
 483         tab.setContent(null);
 484     }
 485 }