Add example for different component lookup methods.

This commit is contained in:
Christian Rösch 2014-08-16 16:59:42 +02:00
parent e2e2f6388b
commit 74354e88a8
6 changed files with 185 additions and 1 deletions

View File

@ -0,0 +1,51 @@
package org.assertj.swing.aut.lookup;
import static org.assertj.swing.aut.util.swing.ButtonUtil.newButton;
import static org.assertj.swing.aut.util.swing.LabelUtil.newLabel;
import static org.assertj.swing.aut.util.swing.TextFieldUtil.newPasswordField;
import static org.assertj.swing.aut.util.swing.TextFieldUtil.newTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.LC;
import org.assertj.swing.aut.components.SampleFrame;
public class LoginFrame extends SampleFrame {
private static final long serialVersionUID = 1L;
public LoginFrame() {
setMiglayout(new LC().wrapAfter(2), new AC().align("right"), new AC());
final JLabel nameLabel = newLabel("username", "Username");
final JTextField textField = newTextField("username");
final JLabel pwLabel = newLabel("password", "Password");
final JTextField pwField = newPasswordField("password");
JButton loginButton = newButton("login", "Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LoginFrame.this.dispose();
JFrame frame = new MainFrame(textField.getText());
frame.setVisible(true);
}
});
add(nameLabel);
add(textField);
add(pwLabel);
add(pwField);
add(loginButton);
pack();
}
}

View File

@ -0,0 +1,20 @@
package org.assertj.swing.aut.lookup;
import static org.assertj.swing.aut.util.swing.LabelUtil.newLabel;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.LC;
import org.assertj.swing.aut.components.SampleFrame;
public class MainFrame extends SampleFrame {
private static final long serialVersionUID = 1L;
public MainFrame(String name) {
setMiglayout(new LC().wrapAfter(1), new AC().align("center"), new AC());
add(newLabel("name", "The user has the name: '" + name + "'"));
add(newLabel("pw", "and the password: '***"));
pack();
}
}

View File

@ -4,7 +4,11 @@ import javax.swing.JLabel;
public class LabelUtil {
public static JLabel newLabel(String name) {
JLabel label = new JLabel(" ");
return newLabel(name, " ");
}
public static JLabel newLabel(String name, String text) {
JLabel label = new JLabel(text);
label.setName(name);
return label;
}

View File

@ -1,5 +1,6 @@
package org.assertj.swing.aut.util.swing;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class TextFieldUtil {
@ -8,4 +9,10 @@ public class TextFieldUtil {
field.setName(name);
return field;
}
public static JTextField newPasswordField(String name) {
JTextField field = new JPasswordField(20);
field.setName(name);
return field;
}
}

View File

@ -0,0 +1,51 @@
package org.assertj.swing.junit.examples.lookup;
import javax.swing.JButton;
import org.assertj.swing.aut.components.SampleFrame;
import org.assertj.swing.aut.lookup.LoginFrame;
import org.assertj.swing.core.GenericTypeMatcher;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JButtonFixture;
import org.assertj.swing.junit.SwingJUnitExamples;
import org.junit.Test;
public class LoginFrame_ComponentLookup_Test extends SwingJUnitExamples {
private FrameFixture window;
@Override
protected void onSetUp() {
SampleFrame frame = GuiActionRunner.execute(new GuiQuery<SampleFrame>() {
protected SampleFrame executeInEDT() {
return new LoginFrame();
}
});
window = new FrameFixture(robot(), frame);
window.show();
}
@Test
public void shouldLookupByName() {
JButtonFixture button = window.button("login");
button.requireVisible();
}
@Test
public void shouldLookupByType() {
JButtonFixture button = window.button();
button.requireVisible();
}
@Test
public void shouldLookupByCustomSearchCriteria() {
JButtonFixture button = window.button(new GenericTypeMatcher<JButton>(JButton.class) {
@Override
protected boolean isMatching(JButton button) {
return "Login".equals(button.getText());
}
});
button.requireVisible();
}
}

View File

@ -0,0 +1,51 @@
package org.assertj.swing.testng.examples.lookup;
import javax.swing.JButton;
import org.assertj.swing.aut.components.SampleFrame;
import org.assertj.swing.aut.lookup.LoginFrame;
import org.assertj.swing.core.GenericTypeMatcher;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JButtonFixture;
import org.assertj.swing.testng.SwingTestNGExamples;
import org.testng.annotations.Test;
public class LoginFrame_ComponentLookup_Test extends SwingTestNGExamples {
private FrameFixture window;
@Override
protected void onSetUp() {
SampleFrame frame = GuiActionRunner.execute(new GuiQuery<SampleFrame>() {
protected SampleFrame executeInEDT() {
return new LoginFrame();
}
});
window = new FrameFixture(robot(), frame);
window.show();
}
@Test
public void shouldLookupByName() {
JButtonFixture button = window.button("login");
button.requireVisible();
}
@Test
public void shouldLookupByType() {
JButtonFixture button = window.button();
button.requireVisible();
}
@Test
public void shouldLookupByCustomSearchCriteria() {
JButtonFixture button = window.button(new GenericTypeMatcher<JButton>(JButton.class) {
@Override
protected boolean isMatching(JButton button) {
return "Login".equals(button.getText());
}
});
button.requireVisible();
}
}