Утверждая на отображении jdialogsJAVA

Программисты JAVA общаются здесь
Anonymous
Утверждая на отображении jdialogs

Сообщение Anonymous »

Как я могу утверждать на модальных диалогах Swing? the dialog's contents immediately after (since it may not fully initialize at that point) nor Robot#waitForIdle (since it throws if invoked from the EDT — all Swing tests should run in the EDT).

MRE:

Код: Выделить всё

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class SimpleDialog extends JDialog {

private JTextField textField;

public SimpleDialog(String title) {
this(null, title);
}

public SimpleDialog(Window owner, String title) {
super(owner, title);
add(createMainPanel());
}

// our dialogs have a unique showing method
public void doModal() {
/*
in a real case, it would typically contain some init logic
and return a constant (OK, CANCEL, etc.)
*/
setVisible(true);
}

private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.add(createNameField());
// imagine there's an app requirement that the field should have focus right away
panel.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
textField.requestFocus();
}
});
return panel;
}

private JTextField createNameField() {
textField = new JTextField(10);
textField.setName("nameField");
return textField;
}
}
< /code>
import org.fest.swing.core.BasicRobot;
import org.fest.swing.core.ComponentFinder;
import org.fest.swing.core.Robot;
import org.fest.swing.core.matcher.JTextComponentMatcher;
import org.junit.jupiter.api.Test;

import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SimpleDialogTest {

Robot robot = BasicRobot.robotWithCurrentAwtHierarchy();
ComponentFinder finder = robot.finder();

@Test
void onceDialogDisplayed_nameFieldHasFocus() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
SimpleDialog dialog = new SimpleDialog("Enter your name");
JTextComponent nameField = finder.find(JTextComponentMatcher.withName("nameField"));

SwingUtilities.invokeLater(dialog::doModal);
assertTrue(nameField.hasFocus());  // fails
});
}
}
< /code>
        
org.junit.jupiter
junit-jupiter-engine
5.9.3
test



org.easytesting
fest-swing
1.2.1
test

Или вся моя идея провести целые тесты в EDT, обреченную с самого начала (я надеялся аннотировать тестовый класс и вообще забыть о проблемах, связанных с EDT)? PrettyPrint-Override ">

Код: Выделить всё

// works, not executed in the EDT as a whole
@Test
void onceDialogDisplayed_nameFieldHasFocus() throws InterruptedException, InvocationTargetException {
DialogFixture dialogFixture = createDialogFixture();
dialogFixture.show();
JTextComponent nameField = finder.find(JTextComponentMatcher.withName("nameField"));
assertTrue(nameField.hasFocus());
}

private DialogFixture createDialogFixture() {
SimpleDialog dialog = createDialog();
DialogFixture dialogFixture = new DialogFixture(robot, dialog);
return dialogFixture;
}

private static SimpleDialog createDialog() {
SimpleDialog dialog = GuiActionRunner.execute(new GuiQuery() {
@Override
protected SimpleDialog executeInEDT() {
SimpleDialog dialog = new SimpleDialog("Enter your name");
return dialog;
}
});
return dialog;
}
java 8.


Подробнее здесь: https://stackoverflow.com/questions/797 ... d-jdialogs

Вернуться в «JAVA»