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