001: package org.uispec4j.finder;
002:
003: import org.uispec4j.ComponentAmbiguityException;
004: import org.uispec4j.ItemNotFoundException;
005: import org.uispec4j.TestUtils;
006:
007: import javax.swing.*;
008: import java.awt.*;
009:
010: public class ComponentMatchersTest extends PanelComponentFinderTestCase {
011: private JButton button1;
012: private JButton button2;
013: private JTextField textField;
014:
015: protected void setUp() throws Exception {
016: super .setUp();
017: button1 = (JButton) addComponent(JButton.class, "displayed1");
018: button1.setName("inner1");
019: button2 = (JButton) addComponent(JButton.class, "displayed2");
020: button2.setName("inner2");
021: textField = (JTextField) addComponent(JTextField.class,
022: "displayed1");
023: textField.setName("inner1");
024: Component component = addComponent(JButton.class, "other");
025: component.setName("else");
026: }
027:
028: public void testClassComponentMatcher() throws Exception {
029: TestUtils.assertSwingComponentsEquals(
030: new Component[] { textField }, panel
031: .getSwingComponents(ComponentMatchers
032: .fromClass(JTextField.class)));
033: }
034:
035: public void testDisplayedNameIdentity() throws Exception {
036: TestUtils.assertUIComponentRefersTo(button2, panel
037: .getButton(ComponentMatchers
038: .displayedNameIdentity("displayed2")));
039:
040: try {
041: panel.getButton(ComponentMatchers
042: .displayedNameIdentity("displayed"));
043: fail();
044: } catch (ItemNotFoundException e) {
045: assertEquals("No component found", e.getMessage());
046: }
047:
048: try {
049: panel.getButton(ComponentMatchers
050: .displayedNameIdentity("inner2"));
051: fail();
052: } catch (ItemNotFoundException e) {
053: assertEquals("No component found", e.getMessage());
054: }
055:
056: TestUtils.assertSwingComponentsEquals(new Component[] {
057: button1, textField }, panel
058: .getSwingComponents(ComponentMatchers
059: .displayedNameIdentity("displayed1")));
060: }
061:
062: public void testDisplayedNameSubstring() throws Exception {
063: TestUtils.assertUIComponentRefersTo(button2, panel
064: .getButton(ComponentMatchers
065: .displayedNameSubstring("displayed2")));
066:
067: try {
068: panel.getButton(ComponentMatchers
069: .displayedNameSubstring("displayed"));
070: fail();
071: } catch (ComponentAmbiguityException e) {
072: assertEquals(Messages.computeAmbiguityMessage(
073: new Component[] { button1, button2 }, null, null),
074: e.getMessage());
075: }
076:
077: assertNull(panel.findSwingComponent(ComponentMatchers
078: .displayedNameSubstring("inner")));
079:
080: TestUtils.assertSwingComponentsEquals(new Component[] {
081: button1, button2, textField }, panel
082: .getSwingComponents(ComponentMatchers
083: .displayedNameSubstring("displayed")));
084: }
085:
086: public void testDisplayedNameRegexp() throws Exception {
087: TestUtils.assertUIComponentRefersTo(button2, panel
088: .getButton(ComponentMatchers
089: .displayedNameRegexp("displaye.?2")));
090:
091: try {
092: panel.getButton(ComponentMatchers
093: .displayedNameRegexp("displayed.?"));
094: fail();
095: } catch (ComponentAmbiguityException e) {
096: assertEquals(Messages.computeAmbiguityMessage(
097: new Component[] { button1, button2 }, null, null),
098: e.getMessage());
099: }
100:
101: try {
102: panel.getButton(ComponentMatchers
103: .displayedNameRegexp("inn.*"));
104: fail();
105: } catch (ItemNotFoundException e) {
106: assertEquals("No component found", e.getMessage());
107: }
108:
109: TestUtils.assertSwingComponentsEquals(new Component[] {
110: button1, button2, textField }, panel
111: .getSwingComponents(ComponentMatchers
112: .displayedNameRegexp("dis.*")));
113: }
114:
115: public void testInnerNameIdentity() throws Exception {
116: TestUtils.assertUIComponentRefersTo(button2, panel
117: .getButton(ComponentMatchers
118: .innerNameIdentity("inner2")));
119:
120: }
121:
122: public void testInnerNameSubstring() throws Exception {
123: TestUtils.assertSwingComponentsEquals(new Component[] {
124: button1, button2, textField }, panel
125: .getSwingComponents(ComponentMatchers
126: .innerNameSubstring("inner")));
127: }
128:
129: public void testInnerNameRegexp() throws Exception {
130: TestUtils.assertUIComponentRefersTo(button2,
131: panel.getButton(ComponentMatchers
132: .innerNameRegexp("inne.?2")));
133:
134: try {
135: panel
136: .getButton(ComponentMatchers
137: .innerNameRegexp("disp.*"));
138: fail();
139: } catch (ItemNotFoundException e) {
140: assertEquals("No component found", e.getMessage());
141: }
142:
143: TestUtils.assertSwingComponentsEquals(new Component[] {
144: button1, button2, textField }, panel
145: .getSwingComponents(ComponentMatchers
146: .innerNameRegexp("inn.*")));
147: }
148: }
|