1 /*
2  * Copyright (c) 2011, 2012, 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  * Portions Copyright (c) 2011 IBM Corporation
26  */
27 
28 /*
29  * @test
30  * @key headful
31  * @bug 7049024
32  * @summary DnD fails with JTextArea and JTextField
33  * @author Sean Chou
34  */
35 
36 import javax.swing.*;
37 import javax.swing.text.DefaultCaret;
38 import java.awt.*;
39 import java.awt.datatransfer.Clipboard;
40 import java.awt.datatransfer.DataFlavor;
41 
42 public class bug7049024 {
43     public static Clipboard clipboard = null;
44 
45     public static JTextField textField = null;
46 
47     // This button is used to move focus away from textField.
48     public static JButton button = null;
49 
50     public static JFrame frame = null;
51 
52     public static DefaultCaret caret = null;
53 
main(String[] args)54     public static void main(String[] args) throws Exception {
55 
56         Robot robot = new Robot();
57         SwingUtilities.invokeAndWait(new Runnable() {
58             @Override
59             public void run() {
60                 frame = new JFrame("Test");
61                 textField = new JTextField("test selection for textfield");
62                 button = new JButton("To compete the focus");
63 
64                 frame.setLayout(new FlowLayout());
65                 frame.getContentPane().add(textField);
66                 frame.getContentPane().add(button);
67 
68                 frame.pack();
69                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70                 frame.setVisible(true);
71             }
72         });
73         robot.waitForIdle();
74 
75         clipboard = textField.getToolkit().getSystemSelection();
76         if (null == clipboard) {
77             return;
78         }
79 
80         SwingUtilities.invokeAndWait(new Runnable() {
81             @Override
82             public void run() {
83                 textField.requestFocusInWindow();
84             }
85         });
86         robot.waitForIdle();
87 
88         SwingUtilities.invokeAndWait(new Runnable() {
89             @Override
90             public void run() {
91                 caret = (DefaultCaret) textField.getCaret();
92                 caret.setDot(2);
93                 caret.moveDot(4);
94             }
95         });
96         robot.waitForIdle();
97 
98         String oldSelection = (String) clipboard.getData(DataFlavor.stringFlavor);
99         System.out.println("oldSelection is " + oldSelection);
100 
101         SwingUtilities.invokeAndWait(new Runnable() {
102             @Override
103             public void run() {
104                 button.requestFocusInWindow();
105             }
106         });
107         robot.waitForIdle(); // So JTextField loses the focus.
108 
109         SwingUtilities.invokeAndWait(new Runnable() {
110             @Override
111             public void run() {
112                 caret.setDot(4);
113                 caret.moveDot(6);
114             }
115         });
116         robot.waitForIdle();
117 
118         String newSelection = (String) clipboard.getData(DataFlavor.stringFlavor);
119         System.out.println("newSelection is " + newSelection);
120 
121         boolean passed = newSelection.equals(oldSelection);
122 
123         SwingUtilities.invokeAndWait(new Runnable() {
124             @Override
125             public void run() {
126                 frame.dispose();
127             }
128         });
129 
130         if (!passed) {
131             throw new RuntimeException("The test for bug 7049024 failed");
132         }
133     }
134 }
135