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