changeset 56:cffb592b0930

Menu sample from KAM
author Alexandre (Shura) Iline <alexandre.iline@sun.com>
date Tue, 10 Jul 2012 14:39:14 +0400
parents 23d44916e7f3
children 7844f43c4834
files tools/Jemmy/JemmyFX/samples/org/jemmy/samples/menu/MenuApp.java tools/Jemmy/JemmyFX/samples/org/jemmy/samples/menu/MenuSample.java
diffstat 2 files changed, 314 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/Jemmy/JemmyFX/samples/org/jemmy/samples/menu/MenuApp.java	Tue Jul 10 14:39:14 2012 +0400
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.jemmy.samples.menu;
+
+
+import javafx.application.Application;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.scene.Scene;
+import javafx.scene.control.*;
+import javafx.scene.layout.BorderPane;
+import javafx.stage.Stage;
+
+
+/**
+ * This small FX app is used in JemmyFX Menu sample. 
+ * It displays various Menu and ContextMenu controls.
+ * @author KAM
+ */
+public class MenuApp extends Application {
+    
+
+    public static void main(String[] args) {
+        launch(args);
+    }
+    
+    @Override
+    public void start(Stage stage) throws Exception {
+
+        CheckMenuItem enabled = new CheckMenuItem("_Enabled");
+        enabled.setSelected(true);
+        
+        RadioMenuItem option1 = new RadioMenuItem("Option _1");
+        RadioMenuItem option2 = new RadioMenuItem("Option _2");
+        option2.setId("option2");
+        option2.setOnAction(new PrintAction("Option 2 is pushed"));
+        RadioMenuItem option3 = new RadioMenuItem("Option _3");
+        
+        ToggleGroup options = new ToggleGroup();
+        option1.setToggleGroup(options);
+        option2.setToggleGroup(options);
+        option3.setToggleGroup(options);
+        options.selectToggle(option3);
+        
+        Menu optionsMenu = new Menu("O_ptions");
+        optionsMenu.setId("options");
+        optionsMenu.getItems().setAll(enabled, new SeparatorMenuItem(), option1, option2, option3);
+        
+        MenuItem newFile = new MenuItem("_New");
+        MenuItem openFile = new MenuItem("_Open");
+        MenuItem closeFile = new MenuItem("_Close");
+        closeFile.setOnAction(new PrintAction("Close action"));
+        
+        Menu fileMenu = new Menu("_File");
+        fileMenu.setId("file");
+        fileMenu.getItems().setAll(newFile, openFile, closeFile, new SeparatorMenuItem(), optionsMenu);
+        
+        MenuItem runItem = new MenuItem("_Run");
+        runItem.setOnAction(new PrintAction("Run action"));
+        
+        Menu action = new Menu("A_ction");
+        action.getItems().setAll(runItem);
+        
+        MenuBar menuBar = new MenuBar();
+        menuBar.getMenus().setAll(fileMenu, action);
+        
+        ContextMenu contextMenu = new ContextMenu(new MenuItem("_My menu item 1"), new MenuItem("My menu item _2"));
+        
+        ScrollPane scrollPane = new ScrollPane();
+        scrollPane.setContextMenu(contextMenu);
+        
+        MenuButton menuButton = new MenuButton("_Menu Button");
+        menuButton.getItems().setAll(new MenuItem("_One"), new MenuItem("_Two"), new MenuItem("T_hree"));
+        
+        BorderPane borderPane = new BorderPane();
+        borderPane.setTop(menuBar);
+        borderPane.setCenter(scrollPane);
+        borderPane.setBottom(menuButton);
+        
+        Scene scene = new Scene(borderPane);
+        
+        stage.setTitle("MenuApp");
+        stage.setScene(scene);
+        stage.show();
+    }
+    
+    class PrintAction implements EventHandler<ActionEvent> {
+        
+        private String text;
+
+        public PrintAction(String text) {
+            this.text = text;
+        }
+        
+        public void handle(ActionEvent t) {
+            System.out.println(text);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/Jemmy/JemmyFX/samples/org/jemmy/samples/menu/MenuSample.java	Tue Jul 10 14:39:14 2012 +0400
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.jemmy.samples.menu;
+
+
+import javafx.scene.Scene;
+import javafx.scene.control.MenuButton;
+import javafx.scene.control.MenuItem;
+import javafx.scene.control.ScrollPane;
+import javafx.stage.Popup;
+import org.jemmy.fx.ByText;
+import org.jemmy.fx.ByWindowType;
+import org.jemmy.fx.NodeDock;
+import org.jemmy.fx.SceneDock;
+import org.jemmy.fx.control.*;
+import org.jemmy.input.StringMenuOwner;
+import org.jemmy.interfaces.Keyboard;
+import org.jemmy.interfaces.Mouse.MouseButtons;
+import org.jemmy.lookup.Lookup;
+import org.jemmy.resources.StringComparePolicy;
+import org.jemmy.samples.SampleBase;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * 
+ * @author KAM
+ */
+public class MenuSample extends SampleBase {
+    private static SceneDock scene;
+    private static MenuBarDock menuBar;
+    
+    @BeforeClass
+    public static void launch() throws InterruptedException {
+        // Running the test application
+        startApp(MenuApp.class);
+        
+        // Obtaining a Dock for scene
+        scene = new SceneDock();
+
+        /**
+        * Looking up for MenuBar. There is just one MenuBar in the scene so
+        * no criteria is specified.
+        */
+        menuBar = new MenuBarDock(scene.asParent());
+    }
+
+    /**
+     * Pushing menu item in menuBar.
+     */ 
+    @Test
+    public void pushMenu() {
+        
+        // Pressing Action > Run menu item. Items are looked up by text which
+        // also includes mnemonics so it is better to use ids as in pushMenuOption
+        menuBar.menu().push(
+                new ByTextMenuItem("A_ction", StringComparePolicy.EXACT), 
+                new ByTextMenuItem("Run", StringComparePolicy.SUBSTRING));
+    }
+    
+    /**
+     * Pushing menu to change selected radio option.
+     */
+    @Test
+    public void pushMenuOption() {
+        
+        // Pressing File > Options > Option 2 menu item. Items are looked up by 
+        // ids which is the best approach.
+        menuBar.menu().push(
+                new ByIdMenuItem("file"), 
+                new ByIdMenuItem("options"),
+                new ByIdMenuItem("option2"));
+    }
+    
+    /**
+     * Checking whether menu item is checked.
+     * http://javafx-jira.kenai.com/browse/JMY-177
+     */
+    @Test
+    public void getCheckedMenuItem() {
+        
+        // Calling Menu.select() method to open parent menu so that 
+        // necessary menu item is visible
+        MenuItemDock options = new MenuItemDock(menuBar.menu().select(
+                new ByIdMenuItem("file"), new ByIdMenuItem("options")));
+        
+        // Looking up for necessary menu item by text
+        MenuItemDock menuItem = new MenuItemDock(options.asMenuParent(), 
+                new ByTextMenuItem("Enabled", StringComparePolicy.SUBSTRING));
+        
+        // Getting selected property of menu item
+        Boolean selected = menuItem.wrap().getProperty(Boolean.class, "isSelected");
+        System.out.println("Enabled menu item is selected = " + selected);
+        
+        // Closing a menu via several ESC key presses
+        // http://javafx-jira.kenai.com/browse/JMY-181
+        menuItem.keyboard().pushKey(Keyboard.KeyboardButtons.ESCAPE);
+        menuItem.keyboard().pushKey(Keyboard.KeyboardButtons.ESCAPE);
+    }
+    
+    /**
+     * Checking what menu option is selected.
+     * http://javafx-jira.kenai.com/browse/JMY-177
+     */
+    @Test
+    public void getSelectedRadioMenuItem() {
+        
+        // Calling Menu.select() method to open parent menu so that 
+        // necessary menu item is visible
+        MenuItemDock options = new MenuItemDock(menuBar.menu().select(
+                new ByIdMenuItem("file"), new ByIdMenuItem("options")));
+        
+        // Geting all the option menu items
+        Lookup<MenuItem> lookup = options.asMenuParent().lookup(new ByTextMenuItem<MenuItem>("Option", StringComparePolicy.SUBSTRING));
+        System.out.println("Found " + lookup.size() + " option menu items.");
+        MenuItemDock selectedMenuItem = null;
+        for (int i = 0; i < lookup.size(); i++) {
+            MenuItemDock menuItem = new MenuItemDock(lookup.wrap(i));
+            Boolean selected = menuItem.wrap().getProperty(Boolean.class, "isSelected");
+            if (selected) {
+                selectedMenuItem = menuItem;
+            }
+            System.out.println(menuItem.wrap().getProperty("getText") + " menu item is selected = " + selected);
+        }
+        
+        // Selecting checked item to close the menu
+        selectedMenuItem.mouse().click();
+    }
+    
+    /**
+     * Push ContextMenuItem
+     * 
+     * TODO: Doesn't seem to be possible in current JemmyFX version
+     * http://javafx-jira.kenai.com/browse/JMY-180
+     */
+    @Test @Ignore // TODO: Fix this sample one the issue is fixed
+    public void pushContextMenuItem() {
+        
+        // Obtaining a nodeDock for the ScrollPane which has a context menu
+        NodeDock scrollPane = new NodeDock(scene.asParent(), ScrollPane.class);
+        
+        // Clicking right mouse button once
+        // http://javafx-jira.kenai.com/browse/JMY-179
+        scrollPane.mouse().click(1, scrollPane.wrap().getClickPoint(), MouseButtons.BUTTON3);
+        
+        // Obtaining a popup scene
+        SceneDock popupScene = new SceneDock(new ByWindowType<Scene>(Popup.class));
+        
+//        scene2.asParent().lookup().dump(System.out);
+//        MenuDock menu = new MenuDock(scene2.asParent());
+//        menu.menu().push(new ByTextMenuItem("My Menu Item", StringComparePolicy.EXACT));
+    }
+    
+    /**
+     * Push MenuButton menu item.
+     */
+    @Test
+    public void pushMenuButtonMenuItem() {
+        
+        // Obtaining MenuButtonDock by its text.
+        MenuButtonDock menuButton = new MenuButtonDock(scene.asParent(), new ByText<MenuButton>("_Menu Button"));
+        
+        // JemmyDock API is not sufficient in this area so we're switching to plain old wrap stuff
+        // http://javafx-jira.kenai.com/browse/JMY-178
+        StringMenuOwner button_menu_owner = menuButton.wrap().as(StringMenuOwner.class, MenuItem.class);
+        
+        // Pushing menu item by text
+        button_menu_owner.push("_Two");
+    }
+}