|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2023, 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 | + @test |
| 25 | + @bug 4426750 |
| 26 | + @requires (os.family == "linux") |
| 27 | + @key headful |
| 28 | + @summary tests that middle mouse button click pastes primary selection |
| 29 | +*/ |
| 30 | + |
| 31 | +import java.awt.BorderLayout; |
| 32 | +import java.awt.Dimension; |
| 33 | +import java.awt.EventQueue; |
| 34 | +import java.awt.Frame; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.TextArea; |
| 38 | +import java.awt.TextComponent; |
| 39 | +import java.awt.TextField; |
| 40 | + |
| 41 | +import java.awt.datatransfer.Clipboard; |
| 42 | +import java.awt.datatransfer.StringSelection; |
| 43 | +import java.awt.event.InputEvent; |
| 44 | +import java.awt.Toolkit; |
| 45 | + |
| 46 | +public class MiddleMouseClickPasteTest { |
| 47 | + static final int FRAME_ACTIVATION_TIMEOUT = 1000; |
| 48 | + static final int SELECTION_PASTE_TIMEOUT = 1000; |
| 49 | + static final int CLICK_INTERVAL = 50; |
| 50 | + static final String TEST_TEXT = "TEST TEXT"; |
| 51 | + static Frame frame; |
| 52 | + static TextField tf; |
| 53 | + static TextArea ta; |
| 54 | + static final Clipboard systemSelection = Toolkit.getDefaultToolkit().getSystemSelection(); |
| 55 | + |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + if (systemSelection != null) { |
| 59 | + try { |
| 60 | + EventQueue.invokeAndWait(MiddleMouseClickPasteTest::createAndShowGui); |
| 61 | + Thread.sleep(FRAME_ACTIVATION_TIMEOUT); |
| 62 | + |
| 63 | + checkPaste(tf); |
| 64 | + checkPaste(ta); |
| 65 | + } catch (Exception e) { |
| 66 | + throw new RuntimeException(e); |
| 67 | + } finally { |
| 68 | + EventQueue.invokeAndWait(()-> { |
| 69 | + if (frame != null) { |
| 70 | + frame.dispose(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void createAndShowGui() { |
| 78 | + frame = new Frame(); |
| 79 | + tf = new TextField(); |
| 80 | + ta = new TextArea(); |
| 81 | + |
| 82 | + frame.setLayout(new BorderLayout()); |
| 83 | + frame.add(tf, BorderLayout.NORTH); |
| 84 | + frame.add(ta, BorderLayout.CENTER); |
| 85 | + frame.pack(); |
| 86 | + frame.setLocationRelativeTo(null); |
| 87 | + frame.setVisible(true); |
| 88 | + } |
| 89 | + |
| 90 | + public static void checkPaste(TextComponent textComponent) throws Exception { |
| 91 | + |
| 92 | + final Point sourcePoint = textComponent.getLocationOnScreen(); |
| 93 | + final Dimension d = textComponent.getSize(); |
| 94 | + sourcePoint.translate(d.width / 2, d.height / 2); |
| 95 | + final Robot robot = new Robot(); |
| 96 | + robot.setAutoWaitForIdle(true); |
| 97 | + robot.setAutoDelay(CLICK_INTERVAL); |
| 98 | + |
| 99 | + textComponent.setText(""); |
| 100 | + systemSelection.setContents(new StringSelection(TEST_TEXT), null); |
| 101 | + |
| 102 | + robot.mouseMove(sourcePoint.x, sourcePoint.y); |
| 103 | + robot.mousePress(InputEvent.BUTTON2_DOWN_MASK); |
| 104 | + robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK); |
| 105 | + robot.delay(SELECTION_PASTE_TIMEOUT); |
| 106 | + |
| 107 | + if (!TEST_TEXT.equals(textComponent.getText())) { |
| 108 | + throw new RuntimeException("Primary selection not pasted" + |
| 109 | + " into: " + textComponent); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments