< prev index next >

test/jdk/java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeForModalDialogTest/ConsumeForModalDialogTest.java

Print this page




   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   test

  25   @bug       6391688
  26   @summary   Tests that next mnemonic KeyTyped is consumed for a modal dialog.
  27   @author    anton.tarasov@sun.com: area=awt.focus
  28   @run       applet ConsumeForModalDialogTest.html
  29 */
  30 
  31 import javax.swing.*;
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.applet.Applet;
  35 import java.util.concurrent.atomic.AtomicBoolean;
  36 import java.lang.reflect.InvocationTargetException;
  37 
  38 public class ConsumeForModalDialogTest extends Applet {
  39     Robot robot;
  40     JFrame frame = new JFrame("Test Frame");
  41     JDialog dialog = new JDialog((Window)null, "Test Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
  42     JTextField text = new JTextField();
  43     static boolean passed = true;
  44 
  45     public static void main(String[] args) {
  46         ConsumeForModalDialogTest app = new ConsumeForModalDialogTest();
  47         app.init();
  48         app.start();
  49     }
  50 
  51     public void init() {
  52         try {
  53             robot = new Robot();
  54             robot.setAutoDelay(50);
  55         } catch (AWTException e) {
  56             throw new RuntimeException("Error: unable to create robot", e);
  57         }
  58         // Create instructions for the user here, as well as set up
  59         // the environment -- set the layout manager, add buttons,
  60         // etc.
  61         this.setLayout (new BorderLayout ());
  62     }
  63 
  64     public void start() {
  65 
  66         text.addKeyListener(new KeyAdapter() {
  67                 public void keyTyped(KeyEvent e) {
  68                     System.out.println(e.toString());
  69                     passed = false;
  70                 }
  71             });
  72 
  73         JMenuItem testItem = new JMenuItem();
  74         testItem.setMnemonic('s');
  75         testItem.setText("Test");
  76 
  77         testItem.addActionListener(new ActionListener() {
  78                 public void actionPerformed(ActionEvent ae) {
  79                     dialog.setVisible(true);
  80             }
  81         });
  82 
  83         JMenu menu = new JMenu();
  84         menu.setMnemonic('f');
  85         menu.setText("File");
  86         menu.add(testItem);
  87 
  88         JMenuBar menuBar = new JMenuBar();
  89         menuBar.add(menu);
  90 
  91         dialog.setSize(100, 100);
  92         dialog.add(text);
  93 
  94         frame.setJMenuBar(menuBar);
  95         frame.setSize(100, 100);

  96         frame.setVisible(true);
  97 
  98         robot.waitForIdle();
  99 
 100         if (!frame.isFocusOwner()) {
 101             Point loc = frame.getLocationOnScreen();
 102             Dimension size = frame.getSize();
 103             robot.mouseMove(loc.x + size.width/2, loc.y + size.height/2);
 104             robot.delay(10);
 105             robot.mousePress(MouseEvent.BUTTON1_MASK);
 106             robot.delay(10);
 107             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
 108 
 109             robot.waitForIdle();
 110 
 111             int iter = 10;
 112             while (!frame.isFocusOwner() && iter-- > 0) {
 113                 robot.delay(200);
 114             }
 115             if (iter <= 0) {




   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   @key headful
  27   @bug        6391688
  28   @summary    Tests that next mnemonic KeyTyped is consumed for a modal dialog.
  29   @run        main ConsumeForModalDialogTest

  30 */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.*;



  35 
  36 public class ConsumeForModalDialogTest {
  37     Robot robot;
  38     JFrame frame = new JFrame("Test Frame");
  39     JDialog dialog = new JDialog((Window)null, "Test Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
  40     JTextField text = new JTextField();
  41     static boolean passed = true;
  42 
  43     public static void main(String[] args) {
  44         ConsumeForModalDialogTest app = new ConsumeForModalDialogTest();
  45         app.init();
  46         app.start();
  47     }
  48 
  49     public void init() {
  50         try {
  51             robot = new Robot();
  52             robot.setAutoDelay(50);
  53         } catch (AWTException e) {
  54             throw new RuntimeException("Error: unable to create robot", e);
  55         }




  56     }
  57 
  58     public void start() {
  59 
  60         text.addKeyListener(new KeyAdapter() {
  61                 public void keyTyped(KeyEvent e) {
  62                     System.out.println(e.toString());
  63                     passed = false;
  64                 }
  65             });
  66 
  67         JMenuItem testItem = new JMenuItem();
  68         testItem.setMnemonic('s');
  69         testItem.setText("Test");
  70 
  71         testItem.addActionListener(new ActionListener() {
  72                 public void actionPerformed(ActionEvent ae) {
  73                     dialog.setVisible(true);
  74             }
  75         });
  76 
  77         JMenu menu = new JMenu();
  78         menu.setMnemonic('f');
  79         menu.setText("File");
  80         menu.add(testItem);
  81 
  82         JMenuBar menuBar = new JMenuBar();
  83         menuBar.add(menu);
  84 
  85         dialog.setSize(100, 100);
  86         dialog.add(text);
  87 
  88         frame.setJMenuBar(menuBar);
  89         frame.setSize(100, 100);
  90         frame.setLocationRelativeTo(null);
  91         frame.setVisible(true);
  92 
  93         robot.waitForIdle();
  94 
  95         if (!frame.isFocusOwner()) {
  96             Point loc = frame.getLocationOnScreen();
  97             Dimension size = frame.getSize();
  98             robot.mouseMove(loc.x + size.width/2, loc.y + size.height/2);
  99             robot.delay(10);
 100             robot.mousePress(MouseEvent.BUTTON1_MASK);
 101             robot.delay(10);
 102             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
 103 
 104             robot.waitForIdle();
 105 
 106             int iter = 10;
 107             while (!frame.isFocusOwner() && iter-- > 0) {
 108                 robot.delay(200);
 109             }
 110             if (iter <= 0) {


< prev index next >