File: MouseDragTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (215 lines) | stat: -rw-r--r-- 6,899 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

/*
 * @test
 * @bug 4035189
 * @summary Test to verify that Drag events go to wrong component
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @run main/manual MouseDragTest
 */

class HeavySquare extends Canvas {
    private final Color colorNormal;
    private boolean gotADragEvent;

    public HeavySquare(Color color) {
        colorNormal = color;
        setBackground(colorNormal);
        new MouseChecker(this);
        addMouseMotionListener(new DragAdapter());
        addMouseListener(new PressReleaseAdapter());
    }

    class DragAdapter extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent ev) {
            if (gotADragEvent)
                return;

            Point mousePt = ev.getPoint();
            Dimension csize = getSize();
            boolean inBounds =
                    (mousePt.x >= 0 && mousePt.x <= csize.width &&
                            mousePt.y >= 0 && mousePt.y <= csize.height);
            if (!inBounds) {
                setBackground(Color.green);
            }
            gotADragEvent = true;
        }
    }

    class PressReleaseAdapter extends MouseAdapter {
        public void mousePressed(MouseEvent ev) {
            gotADragEvent = false;
        }

        public void mouseReleased(MouseEvent ev) {
            setBackground(colorNormal);
        }
    }

    public Dimension preferredSize() {
        return new Dimension(50, 50);
    }
}

class MouseFrame extends Frame {
    public MouseFrame() {
        super("MouseDragTest");
        new MouseChecker(this);
        setLayout(new FlowLayout());
        add(new HeavySquare(Color.red));
        add(new HeavySquare(Color.blue));
        setBounds(new Rectangle(20, 20, 400, 300));
    }
}

public class MouseDragTest {
    static Frame TestFrame;

    public MouseDragTest() {
        TestFrame = new MouseFrame();
    }

    public static void main(String[] args) throws Exception {
        String INSTRUCTIONS = """
                1. A frame with two boxes will appear. Click and drag _very_ quickly
                   off one of the components. You will know you were quick enough
                   when the component you dragged off of turns green
                2. Repeat this several times on both boxes, ensuring you get them
                   to turn green. The components should revert to their original
                   color when you release the mouse
                3. The test FAILS if the component doesn't revert to original
                   color, else PASS.
                """;
        PassFailJFrame.builder()
                .title("Test Instructions")
                .instructions(INSTRUCTIONS)
                .rows((int) INSTRUCTIONS.lines().count() + 2)
                .columns(35)
                .testUI(new MouseFrame())
                .build()
                .awaitAndCheck();
    }
}

class MouseChecker implements MouseListener, MouseMotionListener {
    private boolean isPressed = false;
    private MouseEvent evPrev = null;
    private MouseEvent evPrevPrev = null;

    public MouseChecker(Component comp) {
        comp.addMouseListener(this);
        comp.addMouseMotionListener(this);
    }

    private void recordEv(MouseEvent ev) {
        evPrevPrev = evPrev;
        evPrev = ev;
    }

    private synchronized void failure(String str) {
        PassFailJFrame.forceFail("Test Failed : "+str);
    }

    public void mouseClicked(MouseEvent ev) {
        if (!(evPrev.getID() == MouseEvent.MOUSE_RELEASED &&
                evPrevPrev.getID() == MouseEvent.MOUSE_PRESSED)) {
            failure("Got mouse click without press/release preceding.");
        }
        recordEv(ev);
    }

    public void mousePressed(MouseEvent ev) {
        recordEv(ev);
        if (isPressed) {
            failure("Got two mouse presses without a release.");
        }
        isPressed = true;
    }

    public void mouseReleased(MouseEvent ev) {
        recordEv(ev);
        if (!isPressed) {
            failure("Got mouse release without being pressed.");
        }
        isPressed = false;
    }

    public void mouseEntered(MouseEvent ev) {
        recordEv(ev);
        Point mousePt = ev.getPoint();
        Component comp = (Component) ev.getSource();
        Dimension size = comp.getSize();
        boolean inBounds =
                (mousePt.x >= 0 && mousePt.x <= size.width &&
                        mousePt.y >= 0 && mousePt.y <= size.height);

        if (!inBounds) {
            failure("Got mouse entered, but mouse not inside component.");
        }
    }

    public void mouseExited(MouseEvent ev) {
        recordEv(ev);
        Point mousePt = ev.getPoint();
        Component comp = (Component) ev.getSource();
        if (comp instanceof Frame) {
            return;
        }
        Dimension size = comp.getSize();
        boolean isOnChild = (comp != comp.getComponentAt(mousePt));
        boolean inBounds =
                (mousePt.x >= 0 && mousePt.x <= size.width &&
                        mousePt.y >= 0 && mousePt.y <= size.height);
        if (!isOnChild && inBounds) {
            failure("Got mouse exit, but mouse still inside component.");
        }
    }

    public void mouseDragged(MouseEvent ev) {
        recordEv(ev);
        if (!isPressed) {
            failure("Got drag without a press first.");
        }
    }

    public void mouseMoved(MouseEvent ev) {
        recordEv(ev);
    }
}