jdk/test/java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeNextMnemonicKeyTypedTest.java
changeset 28087 622b2f420bc3
equal deleted inserted replaced
28086:8f695682f781 28087:622b2f420bc3
       
     1 /*
       
     2  * Copyright (c) 2006, 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.
       
     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       6346690
       
    27   @summary   Tests that key_typed is consumed after mnemonic key_pressed is handled for a menu item.
       
    28   @author    anton.tarasov@sun.com: area=awt-focus
       
    29   @library   ../../../../lib/testlibrary
       
    30   @build jdk.testlibrary.OSInfo
       
    31   @run       applet ConsumeNextMnemonicKeyTypedTest.html
       
    32 */
       
    33 
       
    34 import java.awt.*;
       
    35 import javax.swing.*;
       
    36 import java.awt.event.*;
       
    37 import java.applet.Applet;
       
    38 
       
    39 
       
    40 public class ConsumeNextMnemonicKeyTypedTest extends Applet {
       
    41     Robot robot;
       
    42     JFrame frame = new JFrame("Test Frame");
       
    43     JTextField text = new JTextField();
       
    44     JMenuBar bar = new JMenuBar();
       
    45     JMenu menu = new JMenu("Menu");
       
    46     JMenuItem item = new JMenuItem("item");
       
    47 
       
    48     public static void main(String[] args) {
       
    49         ConsumeNextMnemonicKeyTypedTest app = new ConsumeNextMnemonicKeyTypedTest();
       
    50         app.init();
       
    51         app.start();
       
    52     }
       
    53 
       
    54     public void init() {
       
    55         try {
       
    56             robot = new Robot();
       
    57             robot.setAutoDelay(50);
       
    58         } catch (AWTException e) {
       
    59             throw new RuntimeException("Error: unable to create robot", e);
       
    60         }
       
    61         // Create instructions for the user here, as well as set up
       
    62         // the environment -- set the layout manager, add buttons,
       
    63         // etc.
       
    64         this.setLayout (new BorderLayout ());
       
    65         Sysout.createDialogWithInstructions(new String[]
       
    66             {"Automatic test. Simply wait until it's done."});
       
    67     }
       
    68 
       
    69     public void start() {
       
    70         menu.setMnemonic('f');
       
    71         item.setMnemonic('i');
       
    72         menu.add(item);
       
    73         bar.add(menu);
       
    74 
       
    75         frame.add(text);
       
    76         frame.setJMenuBar(bar);
       
    77         frame.pack();
       
    78 
       
    79         frame.setLocation(800, 0);
       
    80         frame.setVisible(true);
       
    81 
       
    82         test();
       
    83     }
       
    84 
       
    85     void test() {
       
    86 
       
    87         robot.waitForIdle();
       
    88 
       
    89         if (!text.isFocusOwner()) {
       
    90             robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5);
       
    91             robot.delay(100);
       
    92             robot.mousePress(MouseEvent.BUTTON1_MASK);
       
    93             robot.delay(100);
       
    94             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
       
    95 
       
    96             int iter = 10;
       
    97             while (!text.isFocusOwner() && iter-- > 0) {
       
    98                 robot.delay(200);
       
    99             }
       
   100             if (iter <= 0) {
       
   101                 Sysout.println("Test: text field couldn't be focused!");
       
   102                 return;
       
   103             }
       
   104         }
       
   105 
       
   106         robot.keyPress(KeyEvent.VK_A);
       
   107         robot.delay(100);
       
   108         robot.keyRelease(KeyEvent.VK_A);
       
   109 
       
   110         robot.waitForIdle();
       
   111 
       
   112         String charA = text.getText();
       
   113         System.err.println("Test: character typed with VK_A: " + charA);
       
   114 
       
   115         robot.keyPress(KeyEvent.VK_BACK_SPACE);
       
   116         robot.delay(100);
       
   117         robot.keyRelease(KeyEvent.VK_BACK_SPACE);
       
   118 
       
   119         robot.waitForIdle();
       
   120 
       
   121         if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
       
   122             robot.keyPress(KeyEvent.VK_CONTROL);
       
   123         }
       
   124         robot.keyPress(KeyEvent.VK_ALT);
       
   125         robot.keyPress(KeyEvent.VK_F);
       
   126         robot.delay(100);
       
   127         robot.keyRelease(KeyEvent.VK_F);
       
   128         robot.keyRelease(KeyEvent.VK_ALT);
       
   129         if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
       
   130             robot.keyRelease(KeyEvent.VK_CONTROL);
       
   131         }
       
   132 
       
   133         robot.waitForIdle();
       
   134 
       
   135         String string = text.getText();
       
   136 
       
   137         robot.keyPress(KeyEvent.VK_I);
       
   138         robot.delay(100);
       
   139         robot.keyRelease(KeyEvent.VK_I);
       
   140 
       
   141         robot.waitForIdle();
       
   142 
       
   143         Sysout.println("Test: character typed after mnemonic key press: " + text.getText());
       
   144 
       
   145         if (!text.getText().equals(string)) {
       
   146             throw new RuntimeException("Test failed!");
       
   147         }
       
   148 
       
   149         robot.keyPress(KeyEvent.VK_A);
       
   150         robot.delay(100);
       
   151         robot.keyRelease(KeyEvent.VK_A);
       
   152 
       
   153         robot.waitForIdle();
       
   154 
       
   155         System.err.println("Test: chracter typed with VK_A: " + text.getText());
       
   156 
       
   157         if (!charA.equals(text.getText())) {
       
   158             throw new RuntimeException("Test failed!");
       
   159         }
       
   160 
       
   161         Sysout.println("Test passed.");
       
   162     }
       
   163 }
       
   164 
       
   165 /****************************************************
       
   166  Standard Test Machinery
       
   167  DO NOT modify anything below -- it's a standard
       
   168   chunk of code whose purpose is to make user
       
   169   interaction uniform, and thereby make it simpler
       
   170   to read and understand someone else's test.
       
   171  ****************************************************/
       
   172 
       
   173 /**
       
   174  This is part of the standard test machinery.
       
   175  It creates a dialog (with the instructions), and is the interface
       
   176   for sending text messages to the user.
       
   177  To print the instructions, send an array of strings to Sysout.createDialog
       
   178   WithInstructions method.  Put one line of instructions per array entry.
       
   179  To display a message for the tester to see, simply call Sysout.println
       
   180   with the string to be displayed.
       
   181  This mimics System.out.println but works within the test harness as well
       
   182   as standalone.
       
   183  */
       
   184 
       
   185 class Sysout
       
   186 {
       
   187     static TestDialog dialog;
       
   188 
       
   189     public static void createDialogWithInstructions( String[] instructions )
       
   190     {
       
   191         dialog = new TestDialog( new Frame(), "Instructions" );
       
   192         dialog.printInstructions( instructions );
       
   193         dialog.setVisible(true);
       
   194         println( "Any messages for the tester will display here." );
       
   195     }
       
   196 
       
   197     public static void createDialog( )
       
   198     {
       
   199         dialog = new TestDialog( new Frame(), "Instructions" );
       
   200         String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   201         dialog.printInstructions( defInstr );
       
   202         dialog.setVisible(true);
       
   203         println( "Any messages for the tester will display here." );
       
   204     }
       
   205 
       
   206 
       
   207     public static void printInstructions( String[] instructions )
       
   208     {
       
   209         dialog.printInstructions( instructions );
       
   210     }
       
   211 
       
   212 
       
   213     public static void println( String messageIn )
       
   214     {
       
   215         dialog.displayMessage( messageIn );
       
   216     }
       
   217 
       
   218 }// Sysout  class
       
   219 
       
   220 /**
       
   221   This is part of the standard test machinery.  It provides a place for the
       
   222    test instructions to be displayed, and a place for interactive messages
       
   223    to the user to be displayed.
       
   224   To have the test instructions displayed, see Sysout.
       
   225   To have a message to the user be displayed, see Sysout.
       
   226   Do not call anything in this dialog directly.
       
   227   */
       
   228 class TestDialog extends Dialog
       
   229 {
       
   230 
       
   231     TextArea instructionsText;
       
   232     TextArea messageText;
       
   233     int maxStringLength = 80;
       
   234 
       
   235     //DO NOT call this directly, go through Sysout
       
   236     public TestDialog( Frame frame, String name )
       
   237     {
       
   238         super( frame, name );
       
   239         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   240         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   241         add( "North", instructionsText );
       
   242 
       
   243         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   244         add("Center", messageText);
       
   245 
       
   246         pack();
       
   247 
       
   248         setVisible(true);
       
   249     }// TestDialog()
       
   250 
       
   251     //DO NOT call this directly, go through Sysout
       
   252     public void printInstructions( String[] instructions )
       
   253     {
       
   254         //Clear out any current instructions
       
   255         instructionsText.setText( "" );
       
   256 
       
   257         //Go down array of instruction strings
       
   258 
       
   259         String printStr, remainingStr;
       
   260         for( int i=0; i < instructions.length; i++ )
       
   261         {
       
   262             //chop up each into pieces maxSringLength long
       
   263             remainingStr = instructions[ i ];
       
   264             while( remainingStr.length() > 0 )
       
   265             {
       
   266                 //if longer than max then chop off first max chars to print
       
   267                 if( remainingStr.length() >= maxStringLength )
       
   268                 {
       
   269                     //Try to chop on a word boundary
       
   270                     int posOfSpace = remainingStr.
       
   271                         lastIndexOf( ' ', maxStringLength - 1 );
       
   272 
       
   273                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   274 
       
   275                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   276                     remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   277                 }
       
   278                 //else just print
       
   279                 else
       
   280                 {
       
   281                     printStr = remainingStr;
       
   282                     remainingStr = "";
       
   283                 }
       
   284 
       
   285                 instructionsText.append( printStr + "\n" );
       
   286 
       
   287             }// while
       
   288 
       
   289         }// for
       
   290 
       
   291     }//printInstructions()
       
   292 
       
   293     //DO NOT call this directly, go through Sysout
       
   294     public void displayMessage( String messageIn )
       
   295     {
       
   296         messageText.append( messageIn + "\n" );
       
   297         System.out.println(messageIn);
       
   298     }
       
   299 
       
   300 }// TestDialog  class