MouseDraggedTest.java revision 12677:a4299d47bd00
1/*
2 * Copyright (c) 2015, 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
24import java.awt.*;
25import java.awt.event.*;
26import javax.swing.*;
27/*
28 * @test
29 * @bug 8080137
30 * @summary Dragged events for extra mouse buttons (4,5,6) are not generated
31 *            on JSplitPane
32 * @author alexandr.scherbatiy area=awt.event
33 * @run main MouseDraggedTest
34 */
35public class MouseDraggedTest {
36
37    private static JFrame frame;
38    private static Rectangle frameBounds;
39    private static volatile boolean mouseDragged;
40
41    public static void main(String[] args) throws Exception {
42        try {
43            Robot robot = new Robot();
44            robot.setAutoDelay(50);
45
46            SwingUtilities.invokeAndWait(MouseDraggedTest::createAndShowGUI);
47            robot.waitForIdle();
48
49            SwingUtilities.invokeAndWait(() -> frameBounds = frame.getBounds());
50            robot.waitForIdle();
51
52            for (int i = 1; i <= MouseInfo.getNumberOfButtons(); i++) {
53                testMouseDrag(i, robot);
54            }
55        } finally {
56            SwingUtilities.invokeLater(() -> {
57                if (frame != null) {
58                    frame.dispose();
59                }
60            });
61        }
62    }
63
64    private static void testMouseDrag(int button, Robot robot) {
65
66        mouseDragged = false;
67        int x1 = frameBounds.x + frameBounds.width / 4;
68        int y1 = frameBounds.y + frameBounds.height / 4;
69        int x2 = frameBounds.x + frameBounds.width / 2;
70        int y2 = frameBounds.y + frameBounds.height / 2;
71
72        robot.mouseMove(x1, y1);
73        robot.waitForIdle();
74
75        int buttonMask = InputEvent.getMaskForButton(button);
76        robot.mousePress(buttonMask);
77        robot.mouseMove(x2, y2);
78        robot.mouseRelease(buttonMask);
79        robot.waitForIdle();
80
81        if (!mouseDragged) {
82            throw new RuntimeException("Mouse button " + button
83                    + " is not dragged");
84        }
85    }
86
87    static void createAndShowGUI() {
88
89        frame = new JFrame();
90        frame.setSize(400, 400);
91        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
92        JPanel panel = new JPanel(new BorderLayout());
93        panel.addMouseMotionListener(new MouseAdapter() {
94
95            @Override
96            public void mouseDragged(MouseEvent e) {
97                mouseDragged = true;
98            }
99        });
100        frame.add(panel, BorderLayout.CENTER);
101        frame.setVisible(true);
102    }
103}
104