< prev index next >

test/jdk/java/awt/Focus/FocusSubRequestTest/FocusSubRequestTest.java

Print this page


   1 /*
   2  * Copyright (c) 2004, 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        5082319
  27   @summary    Tests that focus request for already focused component doesn't block key events.
  28   @author     anton.tarasov@sun.com
  29   @run applet FocusSubRequestTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 public class FocusSubRequestTest extends Applet {
  37     Frame frame = new Frame("Test Frame");
  38     Button button = new Button("button");
  39     boolean passed = false;
  40     Robot robot;
  41 






  42     public void init() {
  43         frame.add(button);
  44         button.addFocusListener(new FocusAdapter() {
  45                 public void focusGained(FocusEvent e) {
  46                     System.out.println("FocusSubRequestTest: focusGained for: " + e.getSource());
  47                     ((Component)e.getSource()).requestFocus();
  48                 }
  49             });
  50 
  51         button.addKeyListener(new KeyAdapter() {
  52                 public void keyPressed(KeyEvent e) {
  53                     System.out.println("FocusSubRequestTest: keyPressed for: " + e.getSource());
  54                     passed = true;
  55                 }
  56             });
  57 
  58         try {
  59             robot = new Robot();
  60         } catch(Exception e) {
  61             throw new RuntimeException("Error: unable to create robot", e);
  62         }
  63     }
  64 
  65     public void start() {
  66         frame.pack();
  67         frame.setLocation(getLocation().x + getSize().width + 20, 0);
  68         frame.setVisible(true);
  69 
  70         waitTillShown(button);
  71         frame.toFront();
  72 
  73         robot.delay(100);
  74         robot.keyPress(KeyEvent.VK_K);
  75         robot.delay(100);
  76         robot.keyRelease(KeyEvent.VK_K);
  77 
  78         robot.waitForIdle();
  79 
  80         if(passed) {
  81             System.out.println("Test passed.");
  82         } else {
  83             throw new RuntimeException("Test failed.");
  84         }
  85     }
  86 
  87     private void waitTillShown(Component component) {
   1 /*
   2  * Copyright (c) 2004, 2018, 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   @key headful
  27   @bug        5082319
  28   @summary    Tests that focus request for already focused component doesn't block key events.
  29   @run main FocusSubRequestTest

  30 */
  31 

  32 import java.awt.*;
  33 import java.awt.event.*;
  34 
  35 public class FocusSubRequestTest {
  36     Frame frame = new Frame("Test Frame");
  37     Button button = new Button("button");
  38     boolean passed = false;
  39     Robot robot;
  40 
  41     public static void main(final String[] args) {
  42         FocusSubRequestTest app = new FocusSubRequestTest();
  43         app.init();
  44         app.start();
  45     }
  46 
  47     public void init() {
  48         frame.add(button);
  49         button.addFocusListener(new FocusAdapter() {
  50                 public void focusGained(FocusEvent e) {
  51                     System.out.println("FocusSubRequestTest: focusGained for: " + e.getSource());
  52                     ((Component)e.getSource()).requestFocus();
  53                 }
  54             });
  55 
  56         button.addKeyListener(new KeyAdapter() {
  57                 public void keyPressed(KeyEvent e) {
  58                     System.out.println("FocusSubRequestTest: keyPressed for: " + e.getSource());
  59                     passed = true;
  60                 }
  61             });
  62 
  63         try {
  64             robot = new Robot();
  65         } catch(Exception e) {
  66             throw new RuntimeException("Error: unable to create robot", e);
  67         }
  68     }
  69 
  70     public void start() {
  71         frame.pack();
  72         frame.setLocationRelativeTo(null);
  73         frame.setVisible(true);
  74 
  75         waitTillShown(button);
  76         frame.toFront();
  77 
  78         robot.delay(100);
  79         robot.keyPress(KeyEvent.VK_K);
  80         robot.delay(100);
  81         robot.keyRelease(KeyEvent.VK_K);
  82 
  83         robot.waitForIdle();
  84 
  85         if(passed) {
  86             System.out.println("Test passed.");
  87         } else {
  88             throw new RuntimeException("Test failed.");
  89         }
  90     }
  91 
  92     private void waitTillShown(Component component) {
< prev index next >