diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/src/org/netbeans/jemmy/operators/JTextComponentOperator.java b/src/org/netbeans/jemmy/operators/JTextComponentOperator.java index daa2690..09d136f 100644 --- a/src/org/netbeans/jemmy/operators/JTextComponentOperator.java +++ b/src/org/netbeans/jemmy/operators/JTextComponentOperator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, 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 @@ -828,10 +828,8 @@ public String toString() { public Hashtable getDump() { Hashtable result = super.getDump(); result.put(TEXT_DPROP, ((JTextComponent) getSource()).getText()); - if (((JTextComponent) getSource()).getSelectedText() != null - && !((JTextComponent) getSource()).getSelectedText().equals("")) { - result.put(SELECTED_TEXT_DPROP, ((JTextComponent) getSource()).getSelectedText()); - } + String selected = ((JTextComponent) getSource()).getSelectedText(); + result.put(SELECTED_TEXT_DPROP, (selected != null) ? selected : ""); result.put(IS_EDITABLE_DPROP, ((JTextComponent) getSource()).isEditable() ? "true" : "false"); return result; } diff --git a/src/org/netbeans/jemmy/operators/TextComponentOperator.java b/src/org/netbeans/jemmy/operators/TextComponentOperator.java index f61f926..5a698b1 100644 --- a/src/org/netbeans/jemmy/operators/TextComponentOperator.java +++ b/src/org/netbeans/jemmy/operators/TextComponentOperator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, 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 @@ -58,6 +58,13 @@ public class TextComponentOperator extends ComponentOperator */ public static final String TEXT_DPROP = "Text"; + /** + * Identifier for a "selected text" property. + * + * @see #getDump + */ + public static final String SELECTED_TEXT_DPROP = "Selected text"; + private final static long PUSH_KEY_TIMEOUT = 0; private final static long BETWEEN_KEYS_TIMEOUT = 0; private final static long CHANGE_CARET_POSITION_TIMEOUT = 60000; @@ -505,6 +512,8 @@ public String toString() { public Hashtable getDump() { Hashtable result = super.getDump(); result.put(TEXT_DPROP, ((TextComponent) getSource()).getText()); + String selected = ((TextComponent) getSource()).getSelectedText(); + result.put(SELECTED_TEXT_DPROP, (selected != null) ? selected : ""); return result; } diff --git a/test/org/netbeans/jemmy/operators/JTextComponentOperatorTest.java b/test/org/netbeans/jemmy/operators/JTextComponentOperatorTest.java new file mode 100644 index 0000000..93101b2 --- /dev/null +++ b/test/org/netbeans/jemmy/operators/JTextComponentOperatorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ +package org.netbeans.jemmy.operators; + +import org.testng.annotations.Test; + +import javax.swing.JTextField; +import javax.swing.text.JTextComponent; + +import static org.testng.Assert.assertEquals; + +public class JTextComponentOperatorTest { + @Test + public void selection() { + JTextComponent text = new JTextField("0123456789"); + JTextComponentOperator textOper = new JTextComponentOperator(text); + assertEquals(textOper.getDump().get(JTextComponentOperator.SELECTED_TEXT_DPROP), ""); + text.select(1,1); + assertEquals(textOper.getDump().get(JTextComponentOperator.SELECTED_TEXT_DPROP), ""); + text.select(1,2); + assertEquals(textOper.getDump().get(JTextComponentOperator.SELECTED_TEXT_DPROP), "1"); + } +} diff --git a/test/org/netbeans/jemmy/operators/TextComponentOperatorTest.java b/test/org/netbeans/jemmy/operators/TextComponentOperatorTest.java new file mode 100644 index 0000000..73cad83 --- /dev/null +++ b/test/org/netbeans/jemmy/operators/TextComponentOperatorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ +package org.netbeans.jemmy.operators; + +import org.testng.annotations.Test; + +import java.awt.TextComponent; +import java.awt.TextField; + +import static org.testng.Assert.assertEquals; + +public class TextComponentOperatorTest { + @Test + public void selection() { + TextComponent text = new TextField("0123456789"); + TextComponentOperator textOper = new TextComponentOperator(text); + assertEquals(textOper.getDump().get(TextComponentOperator.SELECTED_TEXT_DPROP), ""); + text.select(1,1); + assertEquals(textOper.getDump().get(TextComponentOperator.SELECTED_TEXT_DPROP), ""); + text.select(1,2); + assertEquals(textOper.getDump().get(TextComponentOperator.SELECTED_TEXT_DPROP), "1"); + } +}