下面列出了java.awt.Robot#waitForIdle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
robot = new Robot();
String name = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(name);
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException e) {
throw new RuntimeException("Test Failed");
}
createUI();
robot.waitForIdle();
executeTest();
if (!"".equals(errorMessage)) {
throw new RuntimeException(errorMessage);
}
}
public static void main(String[] args) throws Exception {
robot = new Robot();
String name = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(name);
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException e) {
throw new RuntimeException("Test Failed");
}
createUI();
robot.waitForIdle();
executeTest();
if (!"".equals(errorMessage)) {
throw new RuntimeException(errorMessage);
}
}
public static void main(String[] args) throws Throwable {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
constructTestUI();
}
});
try {
testRobot = new Robot();
} catch (AWTException ex) {
throw new RuntimeException("Could not initiate a drag operation");
}
testRobot.waitForIdle();
// Method performing auto test operation
boolean result = test();
disposeTestUI();
if (result == false) {
throw new RuntimeException("Test FAILED!");
}
}
public static void main(String[] args) throws Exception {
robot = new Robot();
String name = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(name);
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException e) {
throw new RuntimeException("Test Failed");
}
createUI();
robot.waitForIdle();
executeTest();
if (!"".equals(errorMessage)) {
throw new RuntimeException(errorMessage);
}
}
public static void main(String[] args) throws Exception {
robot = new Robot();
String name = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(name);
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException e) {
throw new RuntimeException("Test Failed");
}
createUI();
robot.waitForIdle();
executeTest();
if (!"".equals(errorMessage)) {
throw new RuntimeException(errorMessage);
}
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createAndShowUI();
}
});
robot.waitForIdle();
clickCell(robot, 1, 1);
Util.hitKeys(robot, KeyEvent.VK_BACK_SPACE, KeyEvent.VK_BACK_SPACE,
KeyEvent.VK_BACK_SPACE);
robot.waitForIdle();
clickColumnHeader(robot, 1);
robot.waitForIdle();
clickColumnHeader(robot, 1);
}
static void testFocus() throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
Frame frame = new Frame();
frame.setSize(100, 100);
String test = "123";
TextField textField = new TextField(test);
textField.selectAll();
frame.add(textField);
frame.setVisible(true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyRelease(KeyEvent.VK_B);
robot.waitForIdle();
frame.dispose();
if (!textField.getText().equals("ab")) {
throw new RuntimeException("Focus is lost!");
}
}
public static void main(String[] args) throws Exception {
Test8013370 task = new Test8013370();
invokeAndWait(task);
Robot robot = new Robot();
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.waitForIdle();
invokeAndWait(task);
task.validate();
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.waitForIdle();
// move mouse outside menu to prevent auto selection
robot.mouseMove(100,100);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new bug4458079().createAndShowGUI();
}
});
robot.setAutoDelay(50);
Util.hitMnemonics(robot, KeyEvent.VK_M);
robot.waitForIdle();
Thread.sleep(1000);
Util.hitKeys(robot, KeyEvent.VK_DOWN);
Util.hitKeys(robot, KeyEvent.VK_ENTER);
robot.waitForIdle();
Thread.sleep(1000);
if (!itemASelected) {
throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!");
}
}
public static void main(final String[] args) throws Exception {
robot = new Robot();
EventQueue.invokeAndWait(DockIconRepaint::createUI);
try {
robot.waitForIdle();
color = Color.BLUE;
test();
color = Color.RED;
test();
color = Color.GREEN;
test();
} finally {
frame.dispose();
}
}
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.waitForIdle();
UIManager.LookAndFeelInfo[] lookAndFeelArray
= UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
executeCase(lookAndFeelItem.getClassName(),
lookAndFeelItem.getName());
}
if (!"".equals(errorString)) {
System.err.println(errorString);
}
}
public static void main(final String[] args) throws Exception {
robot = new Robot();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
JOptionPane optionPane = new JOptionPane("JOptionPane",
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
null,
new String[]{"3", "2", "1"},
null);
dialog = optionPane.createDialog("JOptionPane");
int width = 0;
Component[] comps = optionPane.getComponents();
for (Component comp : comps) {
if (comp instanceof JPanel) {
Component[] child = ((JPanel) comp).getComponents();
for (Component c : child) {
if (c instanceof JButton) {
width += c.getWidth();
}
}
}
}
Insets in = optionPane.getInsets();
width += in.left + in.right;
if (width > optionPane.getWidth()) {
testFailed = true;
}
} finally {
dialog.dispose();
}
}
});
robot.waitForIdle();
if (testFailed) {
throw new RuntimeException("Test Failed");
}
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
for (int i = 0; i < TAB_COUNT; i++) {
tabbedPane.add("Header " + i, new JLabel("Content: " + i));
}
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
frame.setVisible(true);
});
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
for (int j = 0; j < ITERATIONS; j++) {
for (int i = 0; i < TAB_COUNT; i++) {
tabbedPane.setTitleAt(i, getHtmlText(j * TAB_COUNT + i));
}
}
});
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> frame.dispose());
}
public static void main(String[] args) throws Exception {
Test8013370 task = new Test8013370();
invokeAndWait(task);
Robot robot = new Robot();
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.waitForIdle();
invokeAndWait(task);
task.validate();
}
public static void main(String[] args) throws Exception {
GraphicsDevice[] sds = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
for (final GraphicsDevice gd : sds) {
fail = true;
Robot robot = new Robot(gd);
robot.setAutoDelay(100);
robot.setAutoWaitForIdle(true);
Frame frame = new Frame(gd.getDefaultConfiguration());
frame.setUndecorated(true);
frame.setSize(400, 400);
frame.setVisible(true);
robot.waitForIdle();
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("e = " + e);
fail = false;
}
});
Rectangle bounds = frame.getBounds();
robot.mouseMove(bounds.x + bounds.width / 2,
bounds.y + bounds.height / 2);
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
frame.dispose();
if (fail) {
System.err.println("Frame bounds = " + bounds);
throw new RuntimeException("Click in the wrong location");
}
}
}
private static void test(final int key) throws Exception {
passed = false;
try {
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame();
JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
JPanel panel = new JPanel();
panel.add(combo);
combo.requestFocusInWindow();
frame.setBounds(100, 150, 300, 100);
addAction(panel, key);
frame.add(panel);
frame.setVisible(true);
});
Robot robot = new Robot();
robot.waitForIdle();
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
robot.keyPress(key);
robot.waitForIdle();
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
robot.keyRelease(key);
robot.waitForIdle();
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
if (!passed) {
throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
}
} finally {
if (frame != null) {
frame.dispose();
}
}
}
private static void hitKey(Robot robot, int keycode) {
robot.keyPress(keycode);
robot.keyRelease(keycode);
robot.waitForIdle();
}
public static void performTasks(Robot robotForKeyInput) throws Exception {
lblTestStatus.setText("Running Tests..");
robotForKeyInput.setAutoDelay(500);
ArrayList<Integer> keyCodesToUse = new ArrayList<Integer>();
keyCodesToUse.add(KeyEvent.VK_A);
keyCodesToUse.add(KeyEvent.VK_B);
keyCodesToUse.add(KeyEvent.VK_E);
keyCodesToUse.add(KeyEvent.VK_SPACE);
keyCodesToUse.add(KeyEvent.VK_SPACE);
keyCodesToUse.add(KeyEvent.VK_ENTER);
keyCodesToUse.add(KeyEvent.VK_S);
keyCodesToUse.add(KeyEvent.VK_I);
keyCodesToUse.add(KeyEvent.VK_N);
keyCodesToUse.add(KeyEvent.VK_Z);
keyCodesToUse.add(KeyEvent.VK_O);
keyCodesToUse.add(KeyEvent.VK_U);
keyCodesToUse.add(KeyEvent.VK_SPACE);
keyCodesToUse.add(KeyEvent.VK_ENTER);
textFieldMain.requestFocusInWindow();
robotForKeyInput.waitForIdle();
enterInput(robotForKeyInput, keyCodesToUse);
SwingUtilities.invokeAndWait(() -> {
readingPass1 = textFieldReading.getText();
});
if (setTaskStatus(readingPass1, 1)) {
keyCodesToUse.remove((Integer) KeyEvent.VK_ENTER);
enterInput(robotForKeyInput, keyCodesToUse);
SwingUtilities.invokeAndWait(() -> {
readingPass2 = textFieldReading.getText();
});
if (setTaskStatus(readingPass2, 2)) {
if (readingPass1.equals(readingPass2)) {
testPassed = true;
testResult = "Test Passed : Same reading attribute "
+ "obtained from both passes ";
lblTestStatus.setText(testResult);
} else {
testResult = "Test Failed : Reading attribute from Pass 1 <"
+ readingPass1 + "> != Reading attribute "
+ "from Pass 2 <" + readingPass2 + ">";
}
}
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
spinner = new JSpinner();
frame.getContentPane().add(spinner);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
spinner.addChangeListener((ChangeListener)listener);
spinner.addFocusListener((FocusListener)listener);
spinner.requestFocus();
}
});
synchronized(listener) {
if (!bFocusGained) {
System.out.println("waiting focusGained...");
try { listener.wait(5000); } catch (InterruptedException e) {}
}
}
boolean hasFocus = Util.invokeOnEDT(new java.util.concurrent.Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return spinner.hasFocus();
}
});
if (!bFocusGained && !hasFocus) {
throw new RuntimeException("Couldn't request focus for spinner");
}
Robot robot = new Robot();
robot.setAutoDelay(50);
Util.hitKeys(robot, KeyEvent.VK_UP);
robot.waitForIdle();
Thread.sleep(1000);
if (!bStateChanged) {
throw new RuntimeException("Up arrow key button doesn't work for a spinner in Synth L&F");
}
bStateChanged = false;
Util.hitKeys(robot, KeyEvent.VK_DOWN);
robot.waitForIdle();
Thread.sleep(1000);
if (!bStateChanged) {
throw new RuntimeException("Down arrow key button doesn't work for a spinner in Synth L&F");
}
}
public static void main(String[] args) throws Exception {
final String ABSOLUTE_FILE_PATH = ImageViewTest.class.getResource("circle.png").getPath();
System.out.println(ABSOLUTE_FILE_PATH);
Robot r = new Robot();
final JEditorPane[] editorPanes = new JEditorPane[11];
SwingUtilities.invokeAndWait(() -> {
editorPanes[0] = new JEditorPane("text/html",
"<img height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[1] = new JEditorPane("text/html",
"<img width=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[2] = new JEditorPane("text/html",
"<img width=\"200\" height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[3] = new JEditorPane("text/html",
"<img src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[4] = new JEditorPane("text/html",
"<img width=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[5] = new JEditorPane("text/html",
"<img height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[6] = new JEditorPane("text/html",
"<img width=\"100\" height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[7] = new JEditorPane("text/html",
"<img width=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[8] = new JEditorPane("text/html",
"<img height=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[9] = new JEditorPane("text/html",
"<img width=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
editorPanes[10] = new JEditorPane("text/html",
"<img height=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
});
r.waitForIdle();
System.out.println("Test with only height set to 200");
test(r, editorPanes[0], 200, 200);
System.out.println("Test with only width set to 200");
test(r, editorPanes[1], 200, 200);
System.out.println("Test with both of them set");
test(r, editorPanes[2], 200, 200);
System.out.println("Test with none of them set to 200");
test(r, editorPanes[3], 200, 200);
System.out.println("Test with only width set to 100");
test(r, editorPanes[4], 100, 100);
System.out.println("Test with only height set to 100");
test(r, editorPanes[5], 100, 100);
System.out.println("Test with both width and height set to 100");
test(r, editorPanes[6], 100, 100);
System.out.println("Test with only width set to 50");
test(r, editorPanes[7], 50, 50);
System.out.println("Test with only height set to 50");
test(r, editorPanes[8], 50, 50);
System.out.println("Test with only width set to 300");
test(r, editorPanes[9], 300, 300);
System.out.println("Test with only height set to 300");
test(r, editorPanes[10], 300, 300);
System.out.println("Test Passed.");
}