summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Paige <npaige@google.com>2018-03-20 17:09:03 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2018-03-20 17:09:03 +0000
commit9ba9ad60b67bd3d3892be59c6bc961abbf91a69b (patch)
tree2490034b2e7328d2319b1f007426c759b8c6a3de
parent7d0a11b684a56cd5b23c9e480f94f01a9b6b2122 (diff)
parentdae8e26d82774a50eeef84c578be605ed49e54cf (diff)
downloadswing-testing-9ba9ad60b67bd3d3892be59c6bc961abbf91a69b.tar.gz
Merge "Add pasteText method in robot" into studio-master-dev
-rwxr-xr-xfest-swing/src/main/java/org/fest/swing/core/BasicRobot.java25
-rwxr-xr-xfest-swing/src/main/java/org/fest/swing/core/Robot.java19
2 files changed, 40 insertions, 4 deletions
diff --git a/fest-swing/src/main/java/org/fest/swing/core/BasicRobot.java b/fest-swing/src/main/java/org/fest/swing/core/BasicRobot.java
index a81d847e..75529195 100755
--- a/fest-swing/src/main/java/org/fest/swing/core/BasicRobot.java
+++ b/fest-swing/src/main/java/org/fest/swing/core/BasicRobot.java
@@ -57,6 +57,7 @@ import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
+import java.awt.datatransfer.StringSelection;
import java.awt.event.InvocationEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
@@ -65,10 +66,12 @@ import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
+import javax.swing.text.DefaultEditorKit;
import org.fest.swing.annotation.RunsInCurrentThread;
import org.fest.swing.annotation.RunsInEDT;
@@ -100,6 +103,9 @@ public class BasicRobot implements Robot {
private static final int POPUP_TIMEOUT = 5000;
private static final int WINDOW_DELAY = 20000;
+ /** Up to this length, {@link #enterText} calls {@link #typeText}; otherwise it calls {@link #pasteText}. */
+ private static final int MAX_CHARS_TO_TYPE = 8;
+
private static final ComponentMatcher POPUP_MATCHER = new TypeMatcher(JPopupMenu.class, true);
private volatile boolean active;
@@ -662,7 +668,7 @@ public class BasicRobot implements Robot {
/** {@inheritDoc} */
@RunsInEDT
@Override
- public void enterText(@NotNull String text) {
+ public void typeText(@NotNull String text) {
checkNotNull(text);
for (char character : text.toCharArray()) {
type(character);
@@ -673,6 +679,17 @@ public class BasicRobot implements Robot {
/** {@inheritDoc} */
@RunsInEDT
@Override
+ public void enterText(@NotNull String text) {
+ if (text.length() <= MAX_CHARS_TO_TYPE) {
+ typeText(text);
+ } else {
+ pasteText(text);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @RunsInEDT
+ @Override
public void enterText(@NotNull String text, @NotNull Component c) {
checkNotNull(text);
for (char character : text.toCharArray()) {
@@ -680,6 +697,12 @@ public class BasicRobot implements Robot {
}
}
+ @Override
+ public void pasteText(@NotNull String text) {
+ Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
+ pressAndReleaseKey(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
+ }
+
/** {@inheritDoc} */
@RunsInEDT
@Override
diff --git a/fest-swing/src/main/java/org/fest/swing/core/Robot.java b/fest-swing/src/main/java/org/fest/swing/core/Robot.java
index 445fefb1..64bb965a 100755
--- a/fest-swing/src/main/java/org/fest/swing/core/Robot.java
+++ b/fest-swing/src/main/java/org/fest/swing/core/Robot.java
@@ -327,9 +327,8 @@ public interface Robot {
void jitter(@NotNull Component c, @NotNull Point where);
/**
- * Simulates a user entering the given text. Note that this method the key strokes to the AWT or Swing
- * {@code Component} that has input focus.
- *
+ * Simulates a user typing or pasting the given text into the focused component.
+ *
* @param text the text to enter.
*/
void enterText(@NotNull String text);
@@ -343,6 +342,20 @@ public interface Robot {
void enterText(@NotNull String text, @NotNull Component c);
/**
+ * Simulates a user pasting the given text into the focused component.
+ *
+ * @param text the text to paste.
+ */
+ void pasteText(@NotNull String text);
+
+ /**
+ * Simulates a user typing the given text into the focused component.
+ *
+ * @param text the text to type.
+ */
+ void typeText(@NotNull String text);
+
+ /**
* Types the given character. Note that this method sends the key strokes to the AWT or Swing {@code Component} that
* has input focus.
*