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 javafx.css.CssMetaData;
  29 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*;
  30 
  31 import com.sun.javafx.pgstub.StubToolkit;
  32 import com.sun.javafx.scene.control.skin.ToolBarSkin;
  33 import com.sun.javafx.tk.Toolkit;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 import javafx.beans.property.ObjectProperty;
  37 import javafx.beans.property.SimpleObjectProperty;
  38 import javafx.css.StyleableProperty;
  39 import javafx.event.ActionEvent;
  40 import javafx.event.EventHandler;
  41 import javafx.geometry.Orientation;
  42 import javafx.scene.Node;
  43 import javafx.scene.shape.Rectangle;
  44 import static org.junit.Assert.*;
  45 
  46 
  47 import org.junit.Before;
  48 import org.junit.Test;
  49 
  50 /**
  51  *
  52  * @author srikalyc
  53  */
  54 public class ToolbarTest {
  55     private ToolBar toolBar;//Empty 
  56     private ToolBar toolBarWithItems;//Items
  57     private Toolkit tk;
  58     private Node node1;
  59     private Node node2;
  60     
  61     @Before public void setup() {
  62         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  63         toolBar = new ToolBar();
  64         node1 = new Rectangle();
  65         node2 = new Rectangle(2.0,4.0);
  66         toolBarWithItems = new ToolBar(node1,node2);
  67     }
  68     
  69    
  70    
  71     /*********************************************************************
  72      * Tests for default values                                         *
  73      ********************************************************************/
  74     
  75     @Test public void defaultConstructorShouldSetStyleClassTo_toolbar() {
  76         assertStyleClassContains(toolBar, "tool-bar");
  77     }
  78     
  79     @Test public void defaultFocusTraversibleIsFalse() {
  80         assertFalse(toolBar.isFocusTraversable());
  81     }
  82     @Test public void defaultVarArgConstructorShouldSetStyleClassTo_toolbar() {
  83         assertStyleClassContains(toolBarWithItems, "tool-bar");
  84     }
  85     
  86     @Test public void defaultVarArgConstructorCheckItems() {
  87         assertNotNull(toolBarWithItems.getItems());
  88         assertEquals(toolBarWithItems.getItems().size(), 2.0, 0.0);
  89         assertSame(toolBarWithItems.getItems().get(0), node1);
  90         assertSame(toolBarWithItems.getItems().get(1), node2);
  91     }
  92 
  93     @Test public void defaultOrientation() {
  94         assertSame(toolBar.getOrientation(), Orientation.HORIZONTAL);
  95     }
  96 
  97     
  98     /*********************************************************************
  99      * Tests for property binding                                        *
 100      ********************************************************************/
 101 
 102     @Test public void orientationPropertyHasBeanReference() {
 103         assertSame(toolBar, toolBar.orientationProperty().getBean());
 104     }
 105 
 106     @Test public void orientationPropertyHasName() {
 107         assertEquals("orientation", toolBar.orientationProperty().getName());
 108     }
 109 
 110 
 111     /*********************************************************************
 112      * CSS related Tests                                                 *
 113      ********************************************************************/
 114     @Test public void whenOrientationIsBound_impl_cssSettable_ReturnsFalse() {
 115         CssMetaData styleable = ((StyleableProperty)toolBar.orientationProperty()).getCssMetaData();
 116         assertTrue(styleable.isSettable(toolBar));
 117         ObjectProperty<Orientation> other = new SimpleObjectProperty<Orientation>(Orientation.VERTICAL);
 118         toolBar.orientationProperty().bind(other);
 119         assertFalse(styleable.isSettable(toolBar));
 120     }
 121 
 122     @Test public void whenOrientationIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() {
 123         CssMetaData styleable = ((StyleableProperty)toolBar.orientationProperty()).getCssMetaData();
 124         assertTrue(styleable.isSettable(toolBar));
 125     }
 126 
 127     @Test public void canSpecifyOrientationViaCSS() {
 128         ((StyleableProperty)toolBar.orientationProperty()).applyStyle(null, Orientation.VERTICAL);
 129         assertSame(Orientation.VERTICAL, toolBar.getOrientation());
 130     }
 131 
 132     /*********************************************************************
 133      * Miscellaneous Tests                                         *
 134      ********************************************************************/
 135     @Test public void setOrientationAndSeeValueIsReflectedInModel() {
 136         toolBar.setOrientation(Orientation.HORIZONTAL);
 137         assertSame(toolBar.orientationProperty().getValue(), Orientation.HORIZONTAL);
 138     }
 139     
 140     @Test public void setOrientationAndSeeValue() {
 141         toolBar.setOrientation(Orientation.VERTICAL);
 142         assertSame(toolBar.getOrientation(), Orientation.VERTICAL);
 143     }
 144 
 145     @Test public void rt18501_duplicate_items_are_not_allowed() {
 146         ToolBarSkin toolbarSkin = new ToolBarSkin(toolBar);
 147         toolBar.setSkin(toolbarSkin);
 148         toolBar.getItems().clear();
 149         node1 = new Rectangle();
 150         node2 = new Rectangle(2.0,4.0);
 151         final List<Node> list1 = new ArrayList<Node>();
 152         list1.add(node1);
 153 
 154         toolBar.getItems().add(node1);
 155 
 156         list1.add(node2);
 157 
 158         Button b3 = new Button("button");
 159         b3.setOnAction(e -> {
 160             try {
 161                 toolBar.getItems().setAll(list1);
 162             } catch (Exception iae) {
 163                 fail("Duplicate items are not allowed " + iae.toString());
 164             }
 165         });
 166 
 167         b3.fire();
 168     }
 169     
 170 }