1 /* 2 * Copyright (c) 2011, 2013, 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 @summary <rdar://problem/3284378> Robot generates incorrect events for BUTTON2/3_MASK 27 @summary com.apple.junit.java.awt.Event; 28 @run main RobotROMouse3284378 29 */ 30 31 import junit.framework.*; 32 33 import javax.swing.*; 34 import java.awt.*; 35 import java.awt.event.*; 36 import java.util.HashMap; 37 38 public class RobotROMouse3284378 extends TestCase { 39 private JFrame frame = null; 40 private JLabel label = null; 41 private static String resultString = null; 42 43 private static HashMap<Integer, String> map = new HashMap<Integer, String>(); 44 45 private static class Watcher extends MouseAdapter { 46 public void mousePressed(MouseEvent ev) { 47 resultString = (String) map.get(new Integer(ev.getModifiers())); 48 // System.out.println("received button press " + resultString); 49 } 50 } 51 52 public void testROMouse() throws Exception { 53 map.put(new Integer(InputEvent.BUTTON1_MASK), "button 1"); 54 map.put(new Integer(InputEvent.BUTTON2_MASK), "button 2"); 55 map.put(new Integer(InputEvent.BUTTON3_MASK), "button 3"); 56 57 frame.setVisible(true); 58 Thread.sleep(1000); 59 Robot robot = new Robot(); 60 Point pt = label.getLocationOnScreen(); 61 robot.setAutoDelay(100); 62 robot.mouseMove(pt.x + label.getWidth()/2, 63 pt.y + label.getHeight()/2); 64 // System.out.println("roboto press button 2"); 65 // We no longer test button 2, as it is now mapped to Dashboard by default. 4207099 66 // robot.mousePress(InputEvent.BUTTON2_MASK); 67 // robot.mouseRelease(InputEvent.BUTTON2_MASK); 68 // robot.waitForIdle(); 69 // assertSame("These should both be \"button 2\"", resultString, map.get(new Integer(InputEvent.BUTTON2_MASK))); 70 71 //System.out.println("roboto press button 3"); 72 robot.mousePress(InputEvent.BUTTON3_MASK); 73 robot.mouseRelease(InputEvent.BUTTON3_MASK); 74 robot.waitForIdle(); 75 assertSame("These should both be \"button 3\"", resultString, map.get(new Integer(InputEvent.BUTTON3_MASK))); 76 77 Thread.sleep(1000); 78 } 79 80 protected void setUp() { 81 frame = new JFrame("Modifier test"); 82 label = new JLabel("Modifier test"); 83 label.addMouseListener(new Watcher()); 84 frame.getContentPane().add(label); 85 frame.pack(); 86 assertNotNull( frame); 87 } 88 89 protected void tearDown() { 90 if (frame != null) { 91 frame.dispose(); 92 } 93 frame=null; 94 } 95 96 public static Test suite() { 97 return new TestSuite( RobotROMouse3284378.class); 98 } 99 100 public static void main (String[] args) throws RuntimeException { 101 TestResult tr = junit.textui.TestRunner.run(suite()); 102 if((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 103 throw new RuntimeException("### Unexpected JUnit errors or failures."); 104 } 105 } 106 107 } 108