1 /*
   2  * Copyright (c) 2007, 2015 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 6566434 8039467
  27   @library ../../regtesthelpers
  28   @build Util Sysout AbstractTest
  29   @summary Choice in unfocusable window responds to keyboard
  30   @author Andrei Dmitriev: area=awt-choice
  31   @run main UnfocusableToplevel
  32 */
  33 
  34 /**
  35  * UnfocusableToplevel.java
  36  *
  37  * summary:
  38  */
  39 
  40 import java.awt.*;
  41 import java.awt.event.*;
  42 import test.java.awt.regtesthelpers.AbstractTest;
  43 import test.java.awt.regtesthelpers.Sysout;
  44 import test.java.awt.regtesthelpers.Util;
  45 
  46 public class UnfocusableToplevel {
  47 
  48     final static Robot robot = Util.createRobot();
  49     final static int REASONABLE_PATH_TIME = 5000;
  50 
  51     public static void main(String []s)
  52     {
  53         Frame f = new Frame();
  54         Window w = new Window(f);
  55         final Choice ch = new Choice();
  56 
  57         ch.add("item 1");
  58         ch.add("item 2");
  59         ch.add("item 3");
  60         ch.add("item 4");
  61         ch.add("item 5");
  62         w.add(ch);
  63         w.setLayout(new FlowLayout());
  64         w.setSize(200, 200);
  65 
  66         // Note that Window w is non focusable. Key press events will not be
  67         // consumed by w, but by any previously focused window & this can
  68         // disturb the environment. So creating tempFrameToHoldFocus frame,
  69         // to consume key press events.
  70         Frame tempFrameToHoldFocus = new Frame();
  71         tempFrameToHoldFocus.setVisible(true);
  72         Util.waitForIdle(robot);
  73 
  74         tempFrameToHoldFocus.requestFocus();
  75         Util.clickOnComp(tempFrameToHoldFocus, robot);
  76         Util.waitForIdle(robot);
  77 
  78         ch.addKeyListener(new KeyAdapter(){
  79                 public void keyTyped(KeyEvent e){
  80                     traceEvent("keytyped", e);
  81                 }
  82                 public void keyPressed(KeyEvent e){
  83                     traceEvent("keypress", e);
  84                 }
  85                 public void keyReleased(KeyEvent e){
  86                     traceEvent("keyrelease", e);
  87                 }
  88         });
  89 
  90         ch.addItemListener(new ItemListener(){
  91                 public void itemStateChanged(ItemEvent ie){
  92                     traceEvent("stateChanged", ie);
  93                 }
  94             });
  95 
  96         w.setVisible(true);
  97 
  98         Util.waitForIdle(robot);
  99 
 100         Util.clickOnComp(ch, robot);
 101         Util.waitForIdle(robot);
 102 
 103         // will not test if the dropdown become opened as there is no reliable
 104         // technique to accomplish that rather then checking color of dropdown
 105         // Will suppose that the dropdown appears
 106 
 107         testKeys();
 108         Util.waitForIdle(robot);
 109 
 110         tempFrameToHoldFocus.dispose();
 111         w.dispose();
 112         f.dispose();
 113     }
 114 
 115     private static void testKeys(){
 116         typeKey(KeyEvent.VK_UP);
 117         typeKey(KeyEvent.VK_DOWN);
 118         typeKey(KeyEvent.VK_K);
 119         typeKey(KeyEvent.VK_PAGE_UP);
 120         typeKey(KeyEvent.VK_PAGE_DOWN);
 121     }
 122 
 123     private static void typeKey(int keyChar){
 124         try {
 125             robot.keyPress(keyChar);
 126             robot.delay(5);
 127         } finally {
 128             robot.keyRelease(keyChar);
 129         }
 130         robot.delay(100);
 131     }
 132 
 133     private static void traceEvent(String message, AWTEvent e){
 134         AbstractTest.fail(message + " " + e.toString());
 135     }
 136 }
--- EOF ---