Path: blob/master/test/jdk/javax/swing/JTextArea/7049024/bug7049024.java
41154 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2011 IBM Corporation25*/2627/*28* @test29* @key headful30* @bug 704902431* @summary DnD fails with JTextArea and JTextField32* @author Sean Chou33*/3435import javax.swing.*;36import javax.swing.text.DefaultCaret;37import java.awt.*;38import java.awt.datatransfer.Clipboard;39import java.awt.datatransfer.DataFlavor;4041public class bug7049024 {42public static Clipboard clipboard = null;4344public static JTextField textField = null;4546// This button is used to move focus away from textField.47public static JButton button = null;4849public static JFrame frame = null;5051public static DefaultCaret caret = null;5253public static void main(String[] args) throws Exception {5455Robot robot = new Robot();56SwingUtilities.invokeAndWait(new Runnable() {57@Override58public void run() {59frame = new JFrame("Test");60textField = new JTextField("test selection for textfield");61button = new JButton("To compete the focus");6263frame.setLayout(new FlowLayout());64frame.getContentPane().add(textField);65frame.getContentPane().add(button);6667frame.pack();68frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);69frame.setVisible(true);70}71});72robot.waitForIdle();7374clipboard = textField.getToolkit().getSystemSelection();75if (null == clipboard) {76return;77}7879SwingUtilities.invokeAndWait(new Runnable() {80@Override81public void run() {82textField.requestFocusInWindow();83}84});85robot.waitForIdle();8687SwingUtilities.invokeAndWait(new Runnable() {88@Override89public void run() {90caret = (DefaultCaret) textField.getCaret();91caret.setDot(2);92caret.moveDot(4);93}94});95robot.waitForIdle();9697String oldSelection = (String) clipboard.getData(DataFlavor.stringFlavor);98System.out.println("oldSelection is " + oldSelection);99100SwingUtilities.invokeAndWait(new Runnable() {101@Override102public void run() {103button.requestFocusInWindow();104}105});106robot.waitForIdle(); // So JTextField loses the focus.107108SwingUtilities.invokeAndWait(new Runnable() {109@Override110public void run() {111caret.setDot(4);112caret.moveDot(6);113}114});115robot.waitForIdle();116117String newSelection = (String) clipboard.getData(DataFlavor.stringFlavor);118System.out.println("newSelection is " + newSelection);119120boolean passed = newSelection.equals(oldSelection);121122SwingUtilities.invokeAndWait(new Runnable() {123@Override124public void run() {125frame.dispose();126}127});128129if (!passed) {130throw new RuntimeException("The test for bug 7049024 failed");131}132}133}134135136