< prev index next >

test/jdk/java/awt/KeyboardFocusmanager/TypeAhead/TestDialogTypeAhead.java

Print this page




   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 4799136
  27 @summary Tests that type-ahead for dialog works and doesn't block program
  28 @author  area=awt.focus
  29 @run applet TestDialogTypeAhead.html



  30 */
  31 
  32 // Note there is no @ in front of test above.  This is so that the
  33 //  harness will not mistake this file as a test file.  It should
  34 //  only see the html file as a test file. (the harness runs all
  35 //  valid test files, so it would run this test twice if this file
  36 //  were valid as well as the html file.)
  37 // Also, note the area= after Your Name in the author tag.  Here, you
  38 //  should put which functional area the test falls in.  See the
  39 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  40 //  areas.
  41 // Note also the 'TestDialogTypeAhead.html' in the run tag.  This should
  42 //  be changed to the name of the test.
  43 
  44 
  45 /**
  46  * TestDialogTypeAhead.java
  47  *
  48  * summary:
  49  */
  50 
  51 import java.applet.Applet;
  52 import java.awt.*;
  53 import java.awt.event.*;
  54 import java.lang.reflect.InvocationTargetException;
  55 import test.java.awt.regtesthelpers.Util;
  56 
  57 //Automated tests should run as applet tests if possible because they
  58 // get their environments cleaned up, including AWT threads, any
  59 // test created threads, and any system resources used by the test
  60 // such as file descriptors.  (This is normally not a problem as
  61 // main tests usually run in a separate VM, however on some platforms
  62 // such as the Mac, separate VMs are not possible and non-applet
  63 // tests will cause problems).  Also, you don't have to worry about
  64 // synchronisation stuff in Applet tests they way you do in main
  65 // tests...
  66 
  67 
  68 public class TestDialogTypeAhead extends Applet
  69 {
  70     //Declare things used in the test, like buttons and labels here
  71     static Frame f;
  72     static Button b;
  73     static Dialog d;
  74     static Button ok;
  75     static Semaphore pressSema = new Semaphore();
  76     static Semaphore robotSema = new Semaphore();
  77     static volatile boolean gotFocus = false;
  78     static Robot robot;







  79     public void init()
  80     {
  81         //Create instructions for the user here, as well as set up
  82         // the environment -- set the layout manager, add buttons,
  83         // etc.
  84 
  85         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  86                 public void eventDispatched(AWTEvent e) {
  87                     System.err.println(e.toString());
  88                 }
  89             }, AWTEvent.KEY_EVENT_MASK);
  90 
  91         KeyboardFocusManager.setCurrentKeyboardFocusManager(new TestKFM());
  92 
  93         this.setLayout (new BorderLayout ());
  94 
  95         f = new Frame("frame");
  96         b = new Button("press");
  97         d = new Dialog(f, "dialog", true);
  98         ok = new Button("ok");
  99         d.add(ok);
 100         d.pack();
 101 
 102         ok.addKeyListener(new KeyAdapter() {
 103                 public void keyPressed(KeyEvent e) {
 104                     System.err.println("OK pressed");
 105                     d.dispose();
 106                     f.dispose();
 107                     // Typed-ahead key events should only be accepted if
 108                     // they arrive after FOCUS_GAINED
 109                     if (gotFocus) {
 110                         pressSema.raise();
 111                     }
 112                 }
 113             });
 114         ok.addFocusListener(new FocusAdapter() {


 122         b.addActionListener(new ActionListener() {
 123                 public void actionPerformed(ActionEvent e) {
 124                     System.err.println("B pressed");
 125 
 126                     EventQueue.invokeLater(new Runnable() {
 127                             public void run() {
 128                                 waitTillShown(d);
 129                                 TestDialogTypeAhead.this.d.toFront();
 130                                 TestDialogTypeAhead.this.moveMouseOver(d);
 131                             }
 132                         });
 133 
 134                     d.setVisible(true);
 135                 }
 136             });
 137 
 138     }//End  init()
 139 
 140     public void start ()
 141     {
 142         //Get things going.  Request focus, set size, et cetera
 143         setSize (200,200);
 144         setVisible(true);
 145         validate();
 146         try {
 147             robot = new Robot();
 148         } catch (Exception e) {
 149             throw new RuntimeException("Can't create robot:" + e);
 150         }
 151 
 152         f.setVisible(true);
 153         waitTillShown(b);
 154         System.err.println("b is shown");
 155         f.toFront();
 156         moveMouseOver(f);
 157         waitForIdle();
 158         makeFocused(b);
 159         waitForIdle();
 160         System.err.println("b is focused");
 161 
 162         robot.keyPress(KeyEvent.VK_SPACE);
 163         robot.keyRelease(KeyEvent.VK_SPACE);
 164         try {
 165             robotSema.doWait(1000);
 166         } catch (InterruptedException ie) {
 167             throw new RuntimeException("Interrupted!");
 168         }
 169         if (!robotSema.getState()) {
 170             throw new RuntimeException("robotSema hasn't been triggered");
 171         }




   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 4799136
  28   @summary Tests that type-ahead for dialog works and doesn't block program
  29   @library    ../../regtesthelpers
  30   @modules java.desktop/sun.awt
  31   @build      Util
  32   @run main TestDialogTypeAhead
  33 */
  34 




















  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.lang.reflect.InvocationTargetException;












  38 
  39 public class TestDialogTypeAhead {

  40     //Declare things used in the test, like buttons and labels here
  41     static Frame f;
  42     static Button b;
  43     static Dialog d;
  44     static Button ok;
  45     static Semaphore pressSema = new Semaphore();
  46     static Semaphore robotSema = new Semaphore();
  47     static volatile boolean gotFocus = false;
  48     static Robot robot;
  49 
  50     public static void main(final String[] args) {
  51         TestDialogTypeAhead app = new TestDialogTypeAhead();
  52         app.init();
  53         app.start();
  54     }
  55 
  56     public void init()
  57     {




  58         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  59                 public void eventDispatched(AWTEvent e) {
  60                     System.err.println(e.toString());
  61                 }
  62             }, AWTEvent.KEY_EVENT_MASK);
  63 
  64         KeyboardFocusManager.setCurrentKeyboardFocusManager(new TestKFM());
  65 


  66         f = new Frame("frame");
  67         b = new Button("press");
  68         d = new Dialog(f, "dialog", true);
  69         ok = new Button("ok");
  70         d.add(ok);
  71         d.pack();
  72 
  73         ok.addKeyListener(new KeyAdapter() {
  74                 public void keyPressed(KeyEvent e) {
  75                     System.err.println("OK pressed");
  76                     d.dispose();
  77                     f.dispose();
  78                     // Typed-ahead key events should only be accepted if
  79                     // they arrive after FOCUS_GAINED
  80                     if (gotFocus) {
  81                         pressSema.raise();
  82                     }
  83                 }
  84             });
  85         ok.addFocusListener(new FocusAdapter() {


  93         b.addActionListener(new ActionListener() {
  94                 public void actionPerformed(ActionEvent e) {
  95                     System.err.println("B pressed");
  96 
  97                     EventQueue.invokeLater(new Runnable() {
  98                             public void run() {
  99                                 waitTillShown(d);
 100                                 TestDialogTypeAhead.this.d.toFront();
 101                                 TestDialogTypeAhead.this.moveMouseOver(d);
 102                             }
 103                         });
 104 
 105                     d.setVisible(true);
 106                 }
 107             });
 108 
 109     }//End  init()
 110 
 111     public void start ()
 112     {




 113         try {
 114             robot = new Robot();
 115         } catch (Exception e) {
 116             throw new RuntimeException("Can't create robot:" + e);
 117         }
 118         f.setLocationRelativeTo(null);
 119         f.setVisible(true);
 120         waitTillShown(b);
 121         System.err.println("b is shown");
 122         f.toFront();
 123         moveMouseOver(f);
 124         waitForIdle();
 125         makeFocused(b);
 126         waitForIdle();
 127         System.err.println("b is focused");
 128 
 129         robot.keyPress(KeyEvent.VK_SPACE);
 130         robot.keyRelease(KeyEvent.VK_SPACE);
 131         try {
 132             robotSema.doWait(1000);
 133         } catch (InterruptedException ie) {
 134             throw new RuntimeException("Interrupted!");
 135         }
 136         if (!robotSema.getState()) {
 137             throw new RuntimeException("robotSema hasn't been triggered");
 138         }


< prev index next >