1 /*
   2  * Copyright (c) 2006, 2007, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   test
  26   @bug       6396785
  27   @summary   MenuItem activated with space should swallow this space.
  28   @author    anton.tarasov@...: area=awt.focus
  29   @run       applet MenuItemActivatedTest.html
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import javax.swing.*;
  35 import java.applet.Applet;
  36 import java.util.concurrent.atomic.AtomicBoolean;
  37 import java.lang.reflect.InvocationTargetException;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class MenuItemActivatedTest extends Applet {
  41     Robot robot;
  42     JFrame frame = new JFrame("Test Frame");
  43     JDialog dialog = new JDialog((Window)null, "Test Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
  44     JTextField text = new JTextField();
  45     JMenuBar bar = new JMenuBar();
  46     JMenu menu = new JMenu("Menu");
  47     JMenuItem item = new JMenuItem("item");
  48     AtomicBoolean gotEvent = new AtomicBoolean(false);
  49 
  50     public static void main(String[] args) {
  51         MenuItemActivatedTest app = new MenuItemActivatedTest();
  52         app.init();
  53         app.start();
  54     }
  55 
  56     public void init() {
  57         robot = Util.createRobot();
  58 
  59         // Create instructions for the user here, as well as set up
  60         // the environment -- set the layout manager, add buttons,
  61         // etc.
  62         this.setLayout (new BorderLayout ());
  63         Sysout.createDialogWithInstructions(new String[]
  64             {"This is an automatic test. Simply wait until it is done."
  65             });
  66     }
  67 
  68     public void start() {
  69         menu.setMnemonic('f');
  70         menu.add(item);
  71         bar.add(menu);
  72         frame.setJMenuBar(bar);
  73         frame.pack();
  74 
  75         item.addActionListener(new ActionListener() {
  76                 public void actionPerformed(ActionEvent ae) {
  77                     dialog.add(text);
  78                     dialog.pack();
  79                 dialog.setVisible(true);
  80                 }
  81             });
  82 
  83         text.addKeyListener(new KeyAdapter() {
  84                 public void keyTyped(KeyEvent e) {
  85                     if (e.getKeyChar() == ' ') {
  86                         Sysout.println(e.toString());
  87                         synchronized (gotEvent) {
  88                             gotEvent.set(true);
  89                             gotEvent.notifyAll();
  90                         }
  91                     }
  92                 }
  93             });
  94 
  95         frame.setVisible(true);
  96         Util.waitForIdle(robot);
  97 
  98         robot.keyPress(KeyEvent.VK_ALT);
  99         robot.delay(20);
 100         robot.keyPress(KeyEvent.VK_F);
 101         robot.delay(20);
 102         robot.keyRelease(KeyEvent.VK_F);
 103         robot.delay(20);
 104         robot.keyRelease(KeyEvent.VK_ALT);
 105         Util.waitForIdle(robot);
 106 
 107         item.setSelected(true);
 108         Util.waitForIdle(robot);
 109 
 110         robot.keyPress(KeyEvent.VK_SPACE);
 111         robot.delay(20);
 112         robot.keyRelease(KeyEvent.VK_SPACE);
 113 
 114         if (Util.waitForCondition(gotEvent, 2000)) {
 115             throw new TestFailedException("a space went into the dialog's text field!");
 116         }
 117 
 118         Sysout.println("Test passed.");
 119     }
 120 }
 121 
 122 class TestFailedException extends RuntimeException {
 123     TestFailedException(String msg) {
 124         super("Test failed: " + msg);
 125     }
 126 }
 127 
 128 /****************************************************
 129  Standard Test Machinery
 130  DO NOT modify anything below -- it's a standard
 131   chunk of code whose purpose is to make user
 132   interaction uniform, and thereby make it simpler
 133   to read and understand someone else's test.
 134  ****************************************************/
 135 
 136 /**
 137  This is part of the standard test machinery.
 138  It creates a dialog (with the instructions), and is the interface
 139   for sending text messages to the user.
 140  To print the instructions, send an array of strings to Sysout.createDialog
 141   WithInstructions method.  Put one line of instructions per array entry.
 142  To display a message for the tester to see, simply call Sysout.println
 143   with the string to be displayed.
 144  This mimics System.out.println but works within the test harness as well
 145   as standalone.
 146  */
 147 
 148 class Sysout
 149 {
 150     static TestDialog dialog;
 151 
 152     public static void createDialogWithInstructions( String[] instructions )
 153     {
 154         dialog = new TestDialog( new Frame(), "Instructions" );
 155         dialog.printInstructions( instructions );
 156 //        dialog.setVisible(true);
 157         println( "Any messages for the tester will display here." );
 158     }
 159 
 160     public static void createDialog( )
 161     {
 162         dialog = new TestDialog( new Frame(), "Instructions" );
 163         String[] defInstr = { "Instructions will appear here. ", "" } ;
 164         dialog.printInstructions( defInstr );
 165 //        dialog.setVisible(true);
 166         println( "Any messages for the tester will display here." );
 167     }
 168 
 169 
 170     public static void printInstructions( String[] instructions )
 171     {
 172         dialog.printInstructions( instructions );
 173     }
 174 
 175 
 176     public static void println( String messageIn )
 177     {
 178         dialog.displayMessage( messageIn );
 179     }
 180 
 181 }// Sysout  class
 182 
 183 /**
 184   This is part of the standard test machinery.  It provides a place for the
 185    test instructions to be displayed, and a place for interactive messages
 186    to the user to be displayed.
 187   To have the test instructions displayed, see Sysout.
 188   To have a message to the user be displayed, see Sysout.
 189   Do not call anything in this dialog directly.
 190   */
 191 class TestDialog extends Dialog
 192 {
 193 
 194     TextArea instructionsText;
 195     TextArea messageText;
 196     int maxStringLength = 80;
 197 
 198     //DO NOT call this directly, go through Sysout
 199     public TestDialog( Frame frame, String name )
 200     {
 201         super( frame, name );
 202         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 203         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 204         add( "North", instructionsText );
 205 
 206         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 207         add("Center", messageText);
 208 
 209         pack();
 210 
 211 //        setVisible(true);
 212     }// TestDialog()
 213 
 214     //DO NOT call this directly, go through Sysout
 215     public void printInstructions( String[] instructions )
 216     {
 217         //Clear out any current instructions
 218         instructionsText.setText( "" );
 219 
 220         //Go down array of instruction strings
 221 
 222         String printStr, remainingStr;
 223         for( int i=0; i < instructions.length; i++ )
 224         {
 225             //chop up each into pieces maxSringLength long
 226             remainingStr = instructions[ i ];
 227             while( remainingStr.length() > 0 )
 228             {
 229                 //if longer than max then chop off first max chars to print
 230                 if( remainingStr.length() >= maxStringLength )
 231                 {
 232                     //Try to chop on a word boundary
 233                     int posOfSpace = remainingStr.
 234                         lastIndexOf( ' ', maxStringLength - 1 );
 235 
 236                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 237 
 238                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 239                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 240                 }
 241                 //else just print
 242                 else
 243                 {
 244                     printStr = remainingStr;
 245                     remainingStr = "";
 246                 }
 247 
 248                 instructionsText.append( printStr + "\n" );
 249 
 250             }// while
 251 
 252         }// for
 253 
 254     }//printInstructions()
 255 
 256     //DO NOT call this directly, go through Sysout
 257     public void displayMessage( String messageIn )
 258     {
 259         messageText.append( messageIn + "\n" );
 260         System.out.println(messageIn);
 261     }
 262 
 263 }// TestDialog  class