1 /* 2 * Copyright (c) 2010, 2015, 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 javafx.css.CssMetaData; 29 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*; 30 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer; 31 import test.com.sun.javafx.pgstub.StubToolkit; 32 import 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.geometry.Orientation; 40 import javafx.scene.Node; 41 import javafx.scene.Scene; 42 import javafx.scene.control.Button; 43 import javafx.scene.control.ToolBar; 44 import javafx.scene.input.KeyCode; 45 import javafx.scene.layout.Pane; 46 import javafx.scene.shape.Rectangle; 47 import javafx.stage.Stage; 48 import static org.junit.Assert.*; 49 50 51 import org.junit.Before; 52 import org.junit.Test; 53 54 /** 55 * 56 * @author srikalyc 57 */ 58 public class ToolbarTest { 59 private ToolBar toolBar;//Empty 60 private ToolBar toolBarWithItems;//Items 61 private Toolkit tk; 62 private Node node1; 63 private Node node2; 64 65 @Before public void setup() { 66 tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM) 67 toolBar = new ToolBar(); 68 node1 = new Rectangle(); 69 node2 = new Rectangle(2.0,4.0); 70 toolBarWithItems = new ToolBar(node1,node2); 71 } 72 73 74 75 /********************************************************************* 76 * Tests for default values * 77 ********************************************************************/ 78 79 @Test public void defaultConstructorShouldSetStyleClassTo_toolbar() { 80 assertStyleClassContains(toolBar, "tool-bar"); 81 } 82 83 @Test public void defaultFocusTraversibleIsFalse() { 84 assertFalse(toolBar.isFocusTraversable()); 85 } 86 @Test public void defaultVarArgConstructorShouldSetStyleClassTo_toolbar() { 87 assertStyleClassContains(toolBarWithItems, "tool-bar"); 88 } 89 90 @Test public void defaultVarArgConstructorCheckItems() { 91 assertNotNull(toolBarWithItems.getItems()); 92 assertEquals(toolBarWithItems.getItems().size(), 2.0, 0.0); 93 assertSame(toolBarWithItems.getItems().get(0), node1); 94 assertSame(toolBarWithItems.getItems().get(1), node2); 95 } 96 97 @Test public void defaultOrientation() { 98 assertSame(toolBar.getOrientation(), Orientation.HORIZONTAL); 99 } 100 101 102 /********************************************************************* 103 * Tests for property binding * 104 ********************************************************************/ 105 106 @Test public void orientationPropertyHasBeanReference() { 107 assertSame(toolBar, toolBar.orientationProperty().getBean()); 108 } 109 110 @Test public void orientationPropertyHasName() { 111 assertEquals("orientation", toolBar.orientationProperty().getName()); 112 } 113 114 115 /********************************************************************* 116 * CSS related Tests * 117 ********************************************************************/ 118 @Test public void whenOrientationIsBound_impl_cssSettable_ReturnsFalse() { 119 CssMetaData styleable = ((StyleableProperty)toolBar.orientationProperty()).getCssMetaData(); 120 assertTrue(styleable.isSettable(toolBar)); 121 ObjectProperty<Orientation> other = new SimpleObjectProperty<Orientation>(Orientation.VERTICAL); 122 toolBar.orientationProperty().bind(other); 123 assertFalse(styleable.isSettable(toolBar)); 124 } 125 126 @Test public void whenOrientationIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 127 CssMetaData styleable = ((StyleableProperty)toolBar.orientationProperty()).getCssMetaData(); 128 assertTrue(styleable.isSettable(toolBar)); 129 } 130 131 @Test public void canSpecifyOrientationViaCSS() { 132 ((StyleableProperty)toolBar.orientationProperty()).applyStyle(null, Orientation.VERTICAL); 133 assertSame(Orientation.VERTICAL, toolBar.getOrientation()); 134 } 135 136 /********************************************************************* 137 * Miscellaneous Tests * 138 ********************************************************************/ 139 @Test public void setOrientationAndSeeValueIsReflectedInModel() { 140 toolBar.setOrientation(Orientation.HORIZONTAL); 141 assertSame(toolBar.orientationProperty().getValue(), Orientation.HORIZONTAL); 142 } 143 144 @Test public void setOrientationAndSeeValue() { 145 toolBar.setOrientation(Orientation.VERTICAL); 146 assertSame(toolBar.getOrientation(), Orientation.VERTICAL); 147 } 148 149 @Test public void rt18501_duplicate_items_are_not_allowed() { 150 ToolBarSkin toolbarSkin = new ToolBarSkin(toolBar); 151 toolBar.setSkin(toolbarSkin); 152 toolBar.getItems().clear(); 153 node1 = new Rectangle(); 154 node2 = new Rectangle(2.0,4.0); 155 final List<Node> list1 = new ArrayList<Node>(); 156 list1.add(node1); 157 158 toolBar.getItems().add(node1); 159 160 list1.add(node2); 161 162 Button b3 = new Button("button"); 163 b3.setOnAction(e -> { 164 try { 165 toolBar.getItems().setAll(list1); 166 } catch (Exception iae) { 167 fail("Duplicate items are not allowed " + iae.toString()); 168 } 169 }); 170 171 b3.fire(); 172 } 173 174 @Test public void toolBarFocusTraversalTest() { 175 toolBar.getItems().clear(); 176 177 toolBar.setFocusTraversable(true); 178 assertTrue(toolBar.isFocusTraversable()); 179 180 // Create 5 buttons - put 2 of them in a Pane 181 Button btn1 = new Button("Btn1"); 182 Button btn2 = new Button("Btn2"); 183 184 Button btn3 = new Button("Btn3"); 185 Button btn4 = new Button("Btn4"); 186 Pane p1 = new Pane(btn3, btn4); 187 188 Button btn5 = new Button("Btn5"); 189 190 toolBar.getItems().addAll(btn1, btn2, p1, btn5); 191 192 ToolBarSkin toolbarSkin = new ToolBarSkin(toolBar); 193 toolBar.setSkin(toolbarSkin); 194 195 Scene scene = new Scene(toolBar); 196 Stage stage = new Stage(); 197 stage.setScene(scene); 198 stage.show(); 199 tk.firePulse(); 200 201 toolBar.getScene().getWindow().requestFocus(); 202 assertTrue(btn1.isFocused()); 203 204 KeyEventFirer keyboard1 = new KeyEventFirer(btn1); 205 keyboard1.doKeyPress(KeyCode.TAB); 206 tk.firePulse(); 207 assertTrue(btn2.isFocused()); 208 209 KeyEventFirer keyboard2 = new KeyEventFirer(btn2); 210 keyboard2.doKeyPress(KeyCode.TAB); 211 tk.firePulse(); 212 assertTrue(btn3.isFocused()); 213 214 KeyEventFirer keyboard3 = new KeyEventFirer(btn3); 215 keyboard3.doKeyPress(KeyCode.TAB); 216 tk.firePulse(); 217 assertTrue(btn4.isFocused()); 218 219 KeyEventFirer keyboard4 = new KeyEventFirer(btn4); 220 keyboard4.doKeyPress(KeyCode.TAB); 221 tk.firePulse(); 222 assertTrue(btn5.isFocused()); 223 } 224 }