01: package org.uispec4j.finder;
02:
03: import org.uispec4j.ComponentAmbiguityException;
04: import org.uispec4j.ItemNotFoundException;
05: import org.uispec4j.TestUtils;
06:
07: import javax.swing.*;
08: import java.awt.*;
09:
10: public class PanelSwingComponentFinderTest extends
11: PanelComponentFinderTestCase {
12: private JButton button1;
13: private JButton button2;
14:
15: protected void setUp() throws Exception {
16: super .setUp();
17: button1 = (JButton) addComponent(JButton.class, "button1");
18: button2 = (JButton) addComponent(JButton.class, "button2");
19: button2.setEnabled(false);
20: }
21:
22: public void testGetSwingComponentWithCustomComponentMatcher()
23: throws Exception {
24: assertSame(button1, panel
25: .findSwingComponent(new ComponentMatcher() {
26: public boolean matches(Component component) {
27: return component.isEnabled();
28: }
29: }));
30:
31: try {
32: panel.findSwingComponent(new ComponentMatcher() {
33: public boolean matches(Component component) {
34: return component instanceof JTextField;
35: }
36: });
37: } catch (ItemNotFoundException e) {
38: assertEquals("No component found", e.getMessage());
39: }
40:
41: try {
42: panel.findSwingComponent(new ComponentMatcher() {
43: public boolean matches(Component component) {
44: return component instanceof JButton;
45: }
46: });
47: } catch (ComponentAmbiguityException e) {
48: assertEquals(Messages.computeAmbiguityMessage(
49: new Component[] { button1, button2 }, null, null),
50: e.getMessage());
51: }
52: }
53:
54: public void testFindComponentsWithCustomComponentMatcher()
55: throws Exception {
56: TestUtils.assertSwingComponentsEquals(
57: new Component[] { button1 }, panel
58: .getSwingComponents(new ComponentMatcher() {
59: public boolean matches(Component component) {
60: return component.isEnabled();
61: }
62: }));
63:
64: TestUtils.assertSwingComponentsEquals(new Component[0], panel
65: .getSwingComponents(new ComponentMatcher() {
66: public boolean matches(Component component) {
67: return component instanceof JTextField;
68: }
69: }));
70:
71: TestUtils.assertSwingComponentsEquals(new Component[] {
72: button1, button2 }, panel
73: .getSwingComponents(new ComponentMatcher() {
74: public boolean matches(Component component) {
75: return component instanceof JButton;
76: }
77: }));
78: }
79: }
|