test/jdk/java/awt/Choice/ChoiceKeyEventReaction/ChoiceKeyEventReaction.java
changeset 57516 fe5395d16475
parent 52748 9d2c9970c950
equal deleted inserted replaced
57515:2db64810f4fc 57516:fe5395d16475
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    28  * @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
    28  * @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
    29  * @author andrei.dmitriev : area=awt.choice
    29  * @author andrei.dmitriev : area=awt.choice
    30  * @run main ChoiceKeyEventReaction
    30  * @run main ChoiceKeyEventReaction
    31  */
    31  */
    32 
    32 
       
    33 import java.awt.Choice;
       
    34 import java.awt.FlowLayout;
       
    35 import java.awt.Frame;
       
    36 import java.awt.Point;
    33 import java.awt.Robot;
    37 import java.awt.Robot;
    34 import java.awt.Choice;
    38 import java.awt.TextField;
    35 import java.awt.Point;
       
    36 import java.awt.Toolkit;
    39 import java.awt.Toolkit;
    37 import java.awt.TextField;
       
    38 import java.awt.FlowLayout;
       
    39 import java.awt.event.InputEvent;
    40 import java.awt.event.InputEvent;
       
    41 import java.awt.event.ItemEvent;
       
    42 import java.awt.event.ItemListener;
       
    43 import java.awt.event.KeyAdapter;
    40 import java.awt.event.KeyEvent;
    44 import java.awt.event.KeyEvent;
    41 import java.awt.event.ItemEvent;
       
    42 import java.awt.event.KeyAdapter;
       
    43 import java.awt.event.ItemListener;
       
    44 import java.awt.Frame;
       
    45 
    45 
    46 public class ChoiceKeyEventReaction
    46 public class ChoiceKeyEventReaction
    47 {
    47 {
    48     private static Robot robot;
    48     private static Robot robot;
    49     private static Choice choice1 = new Choice();
    49     private static Choice choice1 = new Choice();
    58         createAndShowGUI();
    58         createAndShowGUI();
    59 
    59 
    60         try {
    60         try {
    61             robot = new Robot();
    61             robot = new Robot();
    62             robot.setAutoDelay(100);
    62             robot.setAutoDelay(100);
       
    63             robot.waitForIdle();
    63 
    64 
    64             moveFocusToTextField();
    65             moveFocusToTextField();
    65             testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);
    66             testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);
    66         } catch (Exception e) {
    67         } catch (Exception e) {
    67             throw new RuntimeException("Test failed. Exception thrown: "+e);
    68             throw new RuntimeException("Test failed. Exception thrown: "+e);
    97 
    98 
    98         frame.add(tf);
    99         frame.add(tf);
    99         frame.add(choice1);
   100         frame.add(choice1);
   100         frame.setLayout (new FlowLayout());
   101         frame.setLayout (new FlowLayout());
   101         frame.setSize (200,200);
   102         frame.setSize (200,200);
       
   103         frame.setLocationRelativeTo(null);
   102         frame.setVisible(true);
   104         frame.setVisible(true);
   103     }
   105     }
   104 
   106 
   105     private static void testKeyOnChoice(int button, int key) {
   107     private static void testKeyOnChoice(int button, int key) {
   106         pt = choice1.getLocationOnScreen();
   108         pt = choice1.getLocationOnScreen();
   107         robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);
   109         robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);
   108 
   110 
   109         robot.mousePress(button);
   111         robot.mousePress(button);
   110         robot.mouseRelease(button);
   112         robot.mouseRelease(button);
       
   113         robot.waitForIdle();
   111 
   114 
   112         robot.keyPress(key);
   115         robot.keyPress(key);
   113         robot.keyRelease(key);
   116         robot.keyRelease(key);
       
   117         robot.waitForIdle();
   114 
   118 
   115         System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);
   119         System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);
   116         if (itemChanged) {
   120         if (itemChanged) {
   117             throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");
   121             throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");
   118         }
   122         }
   119 
   123 
   120        // We may just write
   124         // We may just write
   121        // if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}
   125         // if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}
   122        // but  must report differently in these cases so put two separate if statements for simplicity.
   126         // but  must report differently in these cases so put two separate if statements for simplicity.
   123        if (toolkit.equals("sun.awt.windows.WToolkit") &&
   127         if (!toolkit.equals("sun.awt.X11.XToolkit") &&
   124            !keyTypedOnTextField) {
   128                !keyTypedOnTextField) {
   125            throw new RuntimeException("Test failed. (Win32) KeyEvent wasn't addressed to TextField. ");
   129            throw new RuntimeException("Test failed. (Win32/MacOS) KeyEvent wasn't addressed to TextField. ");
   126        }
   130         }
   127 
   131 
   128        if (!toolkit.equals("sun.awt.windows.WToolkit") &&
   132         if (toolkit.equals("sun.awt.X11.XToolkit") &&
   129           keyTypedOnTextField) {
   133                 keyTypedOnTextField) {
   130            throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");
   134             throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");
   131         }
   135         }
   132 
   136 
   133         System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");
   137         System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");
   134 
       
   135         //close opened choice
       
   136         robot.keyPress(KeyEvent.VK_ESCAPE);
       
   137         robot.keyRelease(KeyEvent.VK_ESCAPE);
       
   138     }
   138     }
   139 
   139 
   140     public static void moveFocusToTextField() {
   140     public static void moveFocusToTextField() {
   141         pt = tf.getLocationOnScreen();
   141         pt = tf.getLocationOnScreen();
   142         robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);
   142         robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);