jdk/test/java/awt/List/ActionEventTest/ActionEventTest.java
changeset 37572 7b0c62fdb5d8
child 39056 d99e63b6d962
equal deleted inserted replaced
37571:f26a25afa773 37572:7b0c62fdb5d8
       
     1 /*
       
     2  * Copyright (c) 2016, 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 6191390
       
    27  * @summary Verify that ActionEvent is received with correct modifiers set.
       
    28  * @run main ActionEventTest
       
    29  */
       
    30 
       
    31 import java.awt.AWTException;
       
    32 import java.awt.FlowLayout;
       
    33 import java.awt.Frame;
       
    34 import java.awt.List;
       
    35 import java.awt.Robot;
       
    36 import java.awt.event.ActionEvent;
       
    37 import java.awt.event.ActionListener;
       
    38 import java.awt.event.KeyEvent;
       
    39 
       
    40 public class ActionEventTest extends Frame {
       
    41     List list;
       
    42     Robot robot;
       
    43 
       
    44     public ActionEventTest() {
       
    45         try {
       
    46             robot = new Robot();
       
    47         } catch(AWTException e) {
       
    48             throw new RuntimeException(e.getMessage());
       
    49         }
       
    50 
       
    51         list = new List(1, false);
       
    52         list.add("0");
       
    53         add(list);
       
    54         setSize(400,400);
       
    55         setLayout(new FlowLayout());
       
    56         pack();
       
    57         setVisible(true);
       
    58         robot.waitForIdle();
       
    59     }
       
    60 
       
    61     void performTest() {
       
    62         list.addActionListener(new ActionListener() {
       
    63             @Override
       
    64             public void actionPerformed(ActionEvent ae) {
       
    65                 int md = ae.getModifiers();
       
    66                 int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
       
    67                         | ActionEvent.SHIFT_MASK;
       
    68 
       
    69                 if ((md & expectedMask) != expectedMask) {
       
    70 
       
    71                     robot.keyRelease(KeyEvent.VK_ALT);
       
    72                     robot.keyRelease(KeyEvent.VK_SHIFT);
       
    73                     robot.keyRelease(KeyEvent.VK_CONTROL);
       
    74                     dispose();
       
    75                     throw new RuntimeException("Action Event modifiers are not"
       
    76                         + " set correctly.");
       
    77                 }
       
    78             }
       
    79         });
       
    80 
       
    81         list.select(0);
       
    82         robot.keyPress(KeyEvent.VK_ALT);
       
    83         robot.keyPress(KeyEvent.VK_SHIFT);
       
    84         robot.keyPress(KeyEvent.VK_CONTROL);
       
    85         // Press Enter on list item, to generate action event.
       
    86         robot.keyPress(KeyEvent.VK_ENTER);
       
    87         robot.keyRelease(KeyEvent.VK_ENTER);
       
    88         robot.waitForIdle();
       
    89         robot.keyRelease(KeyEvent.VK_ALT);
       
    90         robot.keyRelease(KeyEvent.VK_SHIFT);
       
    91         robot.keyRelease(KeyEvent.VK_CONTROL);
       
    92         robot.waitForIdle();
       
    93     }
       
    94 
       
    95     public static void main(String args[]) {
       
    96        ActionEventTest test = new ActionEventTest();
       
    97        test.performTest();
       
    98        test.dispose();
       
    99     }
       
   100 }