Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTextArea/7049024/bug7049024.java
38853 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/* @test28* @bug 704902429* @summary DnD fails with JTextArea and JTextField30* @author Sean Chou31*/3233import javax.swing.*;34import javax.swing.text.DefaultCaret;35import java.awt.*;36import java.awt.datatransfer.Clipboard;37import java.awt.datatransfer.DataFlavor;3839public class bug7049024 {40public static Clipboard clipboard = null;4142public static JTextField textField = null;4344// This button is used to move focus away from textField.45public static JButton button = null;4647public static JFrame frame = null;4849public static DefaultCaret caret = null;5051public static void main(String[] args) throws Exception {5253Robot robot = new Robot();54SwingUtilities.invokeAndWait(new Runnable() {55@Override56public void run() {57frame = new JFrame("Test");58textField = new JTextField("test selection for textfield");59button = new JButton("To compete the focus");6061frame.setLayout(new FlowLayout());62frame.getContentPane().add(textField);63frame.getContentPane().add(button);6465frame.pack();66frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);67frame.setVisible(true);68}69});70robot.waitForIdle();7172clipboard = textField.getToolkit().getSystemSelection();73if (null == clipboard) {74return;75}7677SwingUtilities.invokeAndWait(new Runnable() {78@Override79public void run() {80textField.requestFocusInWindow();81}82});83robot.waitForIdle();8485SwingUtilities.invokeAndWait(new Runnable() {86@Override87public void run() {88caret = (DefaultCaret) textField.getCaret();89caret.setDot(2);90caret.moveDot(4);91}92});93robot.waitForIdle();9495String oldSelection = (String) clipboard.getData(DataFlavor.stringFlavor);96System.out.println("oldSelection is " + oldSelection);9798SwingUtilities.invokeAndWait(new Runnable() {99@Override100public void run() {101button.requestFocusInWindow();102}103});104robot.waitForIdle(); // So JTextField loses the focus.105106SwingUtilities.invokeAndWait(new Runnable() {107@Override108public void run() {109caret.setDot(4);110caret.moveDot(6);111}112});113robot.waitForIdle();114115String newSelection = (String) clipboard.getData(DataFlavor.stringFlavor);116System.out.println("newSelection is " + newSelection);117118boolean passed = newSelection.equals(oldSelection);119120SwingUtilities.invokeAndWait(new Runnable() {121@Override122public void run() {123frame.dispose();124}125});126127if (!passed) {128throw new RuntimeException("The test for bug 7049024 failed");129}130}131}132133134