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/manual ActionEventTest
  29  */
  30 
  31 import java.awt.AWTException;
  32 import java.awt.FlowLayout;
  33 import java.awt.Frame;
  34 import java.awt.Button;
  35 import java.awt.TextArea;
  36 import java.awt.Robot;
  37 import java.awt.Point;
  38 import java.awt.event.InputEvent;
  39 import java.awt.event.ActionEvent;
  40 import java.awt.event.ActionListener;
  41 import java.awt.event.KeyEvent;
  42 
  43 public class ActionEventTest extends Frame {
  44     Button button;
  45     Robot robot;
  46     TextArea instructions;
  47     public static boolean isProgInterruption = false;
  48     static Thread mainThread = null;
  49     static int sleepTime = 300000;
  50 
  51     public ActionEventTest() {
  52         try {
  53             robot = new Robot();
  54         } catch(AWTException e) {
  55             throw new RuntimeException(e.getMessage());
  56         }
  57 
  58         button = new Button("ClickMe");
  59         button.setEnabled(true);
  60 
  61         instructions = new TextArea(10, 50);
  62         instructions.setText(
  63         " This is a manual test\n" +
  64         " Keep the Alt, Shift & Ctrl Keys pressed &\n" +
  65         " Click 'ClickMe' button with left mouse button\n" +
  66         " Test exits automatically after mouse click.");
  67 
  68         add(button);
  69         add(instructions);
  70         setSize(400,400);
  71         setLayout(new FlowLayout());
  72         pack();
  73         setVisible(true);
  74         robot.waitForIdle();
  75 
  76         button.addActionListener(new ActionListener() {
  77             @Override
  78             public void actionPerformed(ActionEvent ae) {
  79                 int md = ae.getModifiers();
  80                 int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
  81                         | ActionEvent.SHIFT_MASK;
  82 
  83                 isProgInterruption = true;
  84                 mainThread.interrupt();
  85                 if ((md & expectedMask) != expectedMask) {
  86                     throw new RuntimeException("Action Event modifiers"
  87                         + " are not set correctly.");
  88                 }
  89             }
  90         });
  91     }
  92 
  93     public static void main(String args[]) throws Exception {
  94         mainThread = Thread.currentThread();
  95         ActionEventTest test = new ActionEventTest();
  96         try {
  97             mainThread.sleep(sleepTime);
  98         } catch (InterruptedException e) {
  99             if (!isProgInterruption) {
 100                 throw e;
 101             }
 102         }
 103         test.dispose();
 104         if (!isProgInterruption) {
 105             throw new RuntimeException("Timed out after " + sleepTime / 1000
 106                     + " seconds");
 107         }
 108     }
 109 }