< prev index next >

modules/javafx.controls/src/test/java/test/javafx/scene/control/MenuBarTest.java

Print this page


   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


  28 import com.sun.javafx.scene.SceneHelper;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertTrue;
  32 
  33 import javafx.scene.Node;
  34 import javafx.scene.Parent;
  35 import javafx.scene.Scene;
  36 import javafx.scene.control.skin.MenuBarSkinShim;
  37 import javafx.scene.input.KeyCode;
  38 import javafx.scene.input.MouseEvent;
  39 import javafx.scene.layout.AnchorPane;
  40 import javafx.scene.layout.VBox;
  41 import javafx.stage.Stage;
  42 
  43 import org.junit.Before;
  44 import org.junit.Test;
  45 
  46 import test.com.sun.javafx.pgstub.StubToolkit;
  47 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;

  48 import test.com.sun.javafx.scene.control.infrastructure.MouseEventGenerator;
  49 import com.sun.javafx.scene.control.ContextMenuContent;
  50 import com.sun.javafx.scene.control.MenuBarMenuButtonShim;
  51 import javafx.scene.control.skin.MenuBarSkin;
  52 import com.sun.javafx.tk.Toolkit;
  53 import javafx.scene.control.CheckBox;
  54 import javafx.scene.control.Menu;
  55 import javafx.scene.control.MenuBar;
  56 import javafx.scene.control.MenuButton;
  57 import javafx.scene.control.MenuItem;
  58 
  59 
  60 /**
  61  *
  62  * @author lubermud
  63  */
  64 
  65 public class MenuBarTest {
  66     private MenuBar menuBar;
  67     private Toolkit tk;


 321         MenuItem menuItem2 = new MenuItem("MenuItem2");
 322         MenuItem menuItem3 = new MenuItem("MenuItem3");
 323 
 324         menu1.getItems().add(menuItem1);
 325         menu2.getItems().add(menuItem2);
 326         menu3.getItems().add(menuItem3);
 327 
 328         menuBar.getMenus().addAll(menu1, menu2, menu3);
 329         menu2.setDisable(true);
 330 
 331         root.getChildren().addAll(menuBar);
 332         startApp(root);
 333         tk.firePulse();
 334 
 335         MenuBarSkin skin = (MenuBarSkin)menuBar.getSkin();
 336         assertTrue(skin != null);
 337 
 338         double xval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinX();
 339         double yval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinY();
 340 

 341         MenuButton mb = MenuBarSkinShim.getNodeForMenu(skin, 0);
 342         mb.getScene().getWindow().requestFocus();
 343         SceneHelper.processMouseEvent(scene,
 344             MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+20, yval+20));
 345         SceneHelper.processMouseEvent(scene,
 346             MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+20, yval+20));
 347         assertTrue(menu1.isShowing());
 348 


 349         KeyEventFirer keyboard = new KeyEventFirer(mb.getScene());
 350         keyboard.doKeyPress(KeyCode.RIGHT);
 351         tk.firePulse();
 352         assertTrue(menu3.isShowing());































 353     }
 354 
 355 
 356      @Test public void testMenuOnShowingEventFiringWithMenuHideOperation() {
 357         VBox root = new VBox();
 358         Menu menu = new Menu("Menu");
 359 
 360         MenuItem menuItem1 = new MenuItem("MenuItem1");
 361         menu.getItems().addAll(menuItem1);
 362 
 363         menuBar.getMenus().add(menu);
 364         menuBar.setLayoutX(100);
 365         menuBar.setLayoutY(100);
 366 
 367         root.getChildren().addAll(menuBar);
 368         startApp(root);
 369         tk.firePulse();
 370 
 371         MenuBarSkin skin = (MenuBarSkin)menuBar.getSkin();
 372         assertTrue(skin != null);


   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


  28 import com.sun.javafx.scene.SceneHelper;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertTrue;
  32 
  33 import javafx.scene.Node;
  34 import javafx.scene.Parent;
  35 import javafx.scene.Scene;
  36 import javafx.scene.control.skin.MenuBarSkinShim;
  37 import javafx.scene.input.KeyCode;
  38 import javafx.scene.input.MouseEvent;
  39 import javafx.scene.layout.AnchorPane;
  40 import javafx.scene.layout.VBox;
  41 import javafx.stage.Stage;
  42 
  43 import org.junit.Before;
  44 import org.junit.Test;
  45 
  46 import test.com.sun.javafx.pgstub.StubToolkit;
  47 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  48 import test.com.sun.javafx.scene.control.infrastructure.KeyModifier;
  49 import test.com.sun.javafx.scene.control.infrastructure.MouseEventGenerator;
  50 import com.sun.javafx.scene.control.ContextMenuContent;
  51 import com.sun.javafx.scene.control.MenuBarMenuButtonShim;
  52 import javafx.scene.control.skin.MenuBarSkin;
  53 import com.sun.javafx.tk.Toolkit;
  54 import javafx.scene.control.CheckBox;
  55 import javafx.scene.control.Menu;
  56 import javafx.scene.control.MenuBar;
  57 import javafx.scene.control.MenuButton;
  58 import javafx.scene.control.MenuItem;
  59 
  60 
  61 /**
  62  *
  63  * @author lubermud
  64  */
  65 
  66 public class MenuBarTest {
  67     private MenuBar menuBar;
  68     private Toolkit tk;


 322         MenuItem menuItem2 = new MenuItem("MenuItem2");
 323         MenuItem menuItem3 = new MenuItem("MenuItem3");
 324 
 325         menu1.getItems().add(menuItem1);
 326         menu2.getItems().add(menuItem2);
 327         menu3.getItems().add(menuItem3);
 328 
 329         menuBar.getMenus().addAll(menu1, menu2, menu3);
 330         menu2.setDisable(true);
 331 
 332         root.getChildren().addAll(menuBar);
 333         startApp(root);
 334         tk.firePulse();
 335 
 336         MenuBarSkin skin = (MenuBarSkin)menuBar.getSkin();
 337         assertTrue(skin != null);
 338 
 339         double xval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinX();
 340         double yval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinY();
 341 
 342         // Click on menu1 to open it
 343         MenuButton mb = MenuBarSkinShim.getNodeForMenu(skin, 0);
 344         mb.getScene().getWindow().requestFocus();
 345         SceneHelper.processMouseEvent(scene,
 346             MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+20, yval+20));
 347         SceneHelper.processMouseEvent(scene,
 348             MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+20, yval+20));
 349         assertTrue(menu1.isShowing());
 350 
 351         // Right key press should skip menu2 that is disabled and 
 352         // should open up menu3
 353         KeyEventFirer keyboard = new KeyEventFirer(mb.getScene());
 354         keyboard.doKeyPress(KeyCode.RIGHT);
 355         tk.firePulse();
 356         assertTrue(menu3.isShowing());
 357 
 358         // Another right key press should cycle back and open menu1
 359         keyboard.doKeyPress(KeyCode.RIGHT);
 360         tk.firePulse();
 361         assertTrue(menu1.isShowing());
 362 
 363         // Left key press should cycle to end and open menu3
 364         keyboard.doKeyPress(KeyCode.LEFT);
 365         tk.firePulse();
 366         assertTrue(menu3.isShowing());
 367 
 368         // Another Left key press should skip menu2 that is disabled
 369         // and should open up menu1
 370         keyboard.doKeyPress(KeyCode.LEFT);
 371         tk.firePulse();
 372         assertTrue(menu1.isShowing());
 373 
 374 
 375         // Now, close the exapanded menu1, 
 376         keyboard.doKeyPress(KeyCode.ESCAPE);
 377         tk.firePulse();
 378 
 379         // Disable all menus and test menu selection key press
 380         menu1.setDisable(true);
 381         menu3.setDisable(true);
 382 
 383         keyboard.doKeyPress(KeyCode.RIGHT, KeyModifier.ALT);
 384         tk.firePulse();
 385         assertFalse(menu1.isShowing());
 386         assertFalse(menu2.isShowing());
 387         assertFalse(menu3.isShowing());
 388     }
 389 
 390 
 391      @Test public void testMenuOnShowingEventFiringWithMenuHideOperation() {
 392         VBox root = new VBox();
 393         Menu menu = new Menu("Menu");
 394 
 395         MenuItem menuItem1 = new MenuItem("MenuItem1");
 396         menu.getItems().addAll(menuItem1);
 397 
 398         menuBar.getMenus().add(menu);
 399         menuBar.setLayoutX(100);
 400         menuBar.setLayoutY(100);
 401 
 402         root.getChildren().addAll(menuBar);
 403         startApp(root);
 404         tk.firePulse();
 405 
 406         MenuBarSkin skin = (MenuBarSkin)menuBar.getSkin();
 407         assertTrue(skin != null);


< prev index next >