📄 panelcomponentsearchtest.java
字号:
package org.uispec4j.finder;
import org.uispec4j.Button;
import org.uispec4j.*;
import org.uispec4j.Panel;
import org.uispec4j.utils.ComponentUtils;
import org.uispec4j.utils.UIComponentAnalyzer;
import org.uispec4j.utils.UIComponentFactory;
import org.uispec4j.xml.XmlAssert;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
public class PanelComponentSearchTest extends PanelComponentFinderTestCase {
private static final Class[] COMPONENT_CLASSES = new Class[]{
Button.class,
CheckBox.class,
ComboBox.class,
org.uispec4j.Desktop.class,
ListBox.class,
TabGroup.class,
TextBox.class,
PasswordField.class,
Panel.class,
Spinner.class,
Slider.class,
ProgressBar.class,
RadioButton.class};
private Map componentAccessors;
protected void setUp() throws Exception {
super.setUp();
componentAccessors = createAccessors(panel);
}
public void testGetComponentTypeName() {
assertEquals("panel", UIComponentFactory.createUIComponent(new JPanel()).getDescriptionTypeName());
}
public void testGetComponentWithText() throws Exception {
checkGetComponentWithText(JButton.class, Button.TYPE_NAME);
checkGetComponentWithText(JCheckBox.class, CheckBox.TYPE_NAME);
checkGetComponentWithText(JLabel.class, TextBox.TYPE_NAME);
checkGetComponentWithText(JRadioButton.class, RadioButton.TYPE_NAME);
}
public void testGetComponentWithClassAndName() throws Exception {
for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
checkGetComponentWithClassAndName(COMPONENT_CLASSES[i]);
}
}
public void testGetComponentWithClass() throws Exception {
for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
if (COMPONENT_CLASSES[i] != Panel.class) {
checkGetComponentWithClass(COMPONENT_CLASSES[i]);
}
}
}
public void testGetComponentWithCustomMatcher() throws Exception {
for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
if (COMPONENT_CLASSES[i] != Panel.class) {
checkGetComponentWithCustomMatcher(COMPONENT_CLASSES[i]);
}
}
}
public void testComponentNotFoundErrors() throws Exception {
for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
checkComponentNotFound(UIComponentAnalyzer.getTypeName(COMPONENT_CLASSES[i]));
}
}
public void testComponentTypeMismatch() throws Exception {
checkComponentTypeMismatch("name1", JList.class, Button.TYPE_NAME);
checkComponentTypeMismatch("name2", JList.class, TextBox.TYPE_NAME);
checkComponentTypeMismatch("name3", JList.class, CheckBox.TYPE_NAME);
checkComponentTypeMismatch("name5", JList.class, TextBox.TYPE_NAME);
checkComponentTypeMismatch("name6", JList.class, TabGroup.TYPE_NAME);
checkComponentTypeMismatch("name7", JList.class, ComboBox.TYPE_NAME);
checkComponentTypeMismatch("name8", JList.class, RadioButton.TYPE_NAME);
}
public void testPasswordField() throws Exception {
JPasswordField jPasswordField = new JPasswordField();
jPanel.add(jPasswordField);
PasswordField passwordField = panel.getPasswordField();
assertSame(jPasswordField, passwordField.getAwtComponent());
}
public void testSearchTraversalByClassExploresInDepthAllSubComponents() throws Exception {
JTextField innerTextField = new JTextField("hello");
JPanel innerPanel = new JPanel();
innerPanel.add(innerTextField);
jPanel.add(innerPanel);
JTextField textField = new JTextField("world");
jPanel.add(textField);
TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
try {
labelAccessor.getComponent();
fail();
}
catch (Exception e) {
assertEquals(Messages.computeAmbiguityMessage(new String[]{"world", "hello"}, TextBox.TYPE_NAME, null),
e.getMessage());
}
TestUtils.assertUIComponentRefersTo(textField, labelAccessor.getComponent("world"));
TestUtils.assertUIComponentRefersTo(innerTextField, labelAccessor.getComponent("hello"));
}
public void testSearchTraversalByNameExploresInDepthAllSubComponents() throws Exception {
JLabel innerLabel = new JLabel();
innerLabel.setName("Hello Marc");
JPanel innerPanel = new JPanel();
innerPanel.add(innerLabel);
jPanel.add(innerPanel);
JLabel label = new JLabel();
addComponentToPanelWithName(label, "Hello Regis");
TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
try {
labelAccessor.getComponent("hello");
fail();
}
catch (Exception e) {
assertEquals(Messages.computeAmbiguityMessage(new String[]{"Hello Regis", "Hello Marc"}, TextBox.TYPE_NAME, "hello"),
e.getMessage());
}
TestUtils.assertUIComponentRefersTo(innerLabel, labelAccessor.getComponent("Hello marc"));
TestUtils.assertUIComponentRefersTo(label, labelAccessor.getComponent("Hello regis"));
}
public void testSearchByNameStrategyIsFirstOnDisplayedName() throws Exception {
JLabel label = new JLabel("label toto");
JLabel labelWithInnerName = new JLabel("do not chose me");
labelWithInnerName.setName("toto");
jPanel.add(label);
jPanel.add(labelWithInnerName);
TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
TestUtils.assertUIComponentRefersTo(label, labelAccessor.getComponent("toto"));
}
public void testSearchWithinAComplexPanel() throws Exception {
JPanel main = new JPanel();
for (int i = 0; i < 10; i++) {
JPanel containedPanel = new JPanel();
containedPanel.add(new JButton("button" + i));
addWithScrollPane(containedPanel, main);
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
JTable table = new JTable();
table.setName("table" + i);
addWithScrollPane(table, topPanel);
JTree tree = new JTree();
tree.setName("tree" + i);
addWithScrollPane(tree, topPanel);
bottomPanel.add(new JLabel("label" + i));
addWithScrollPane(split, main);
for (int j = 0; j < 5; j++) {
JPanel sub = new JPanel();
sub.add(new JCheckBox("checkBox" + i + "." + j));
addWithScrollPane(sub, bottomPanel);
JPanel subSub = new JPanel();
subSub.add(new JRadioButton("radio" + i + "." + j));
addWithScrollPane(subSub, sub);
}
}
Panel mainPanel = new Panel(main);
assertNotNull(mainPanel.getTextBox("label5"));
assertNotNull(mainPanel.getButton("button7"));
assertNotNull(mainPanel.getTable("table6"));
assertNotNull(mainPanel.getTree("tree5"));
assertNotNull(mainPanel.getCheckBox("checkBox3.4"));
assertNotNull(mainPanel.getRadioButton("radio2.3"));
}
public void testComponentNameAmbiguityException() throws Exception {
jPanel.add(new JButton("myButton1"));
jPanel.add(new JButton("myButton2"));
checkComponentNameAmbiguity("button", Button.TYPE_NAME, new String[]{"myButton1", "myButton2"});
jPanel.add(new JLabel("myLabel1"));
jPanel.add(new JLabel("myLabel2"));
checkComponentNameAmbiguity("label", TextBox.TYPE_NAME, new String[]{"myLabel1", "myLabel2"});
jPanel.add(new JCheckBox("myCheckBox1"));
jPanel.add(new JCheckBox("myCheckBox2"));
checkComponentNameAmbiguity("myCheckBox", CheckBox.TYPE_NAME, new String[]{"myCheckBox1", "myCheckBox2"});
createTwoComponentsWithSameName(JPanel.class, "myPanel");
checkComponentNameAmbiguity("panel", Panel.TYPE_NAME, new String[]{"myPanel", "myPanel"});
createTwoComponentsWithSameName(JTextField.class, "myText");
checkComponentNameAmbiguity("text", TextBox.TYPE_NAME, new String[]{"myText", "myText"});
createTwoComponentsWithSameName(JTabbedPane.class, "myTabbed");
checkComponentNameAmbiguity("myTabbed", TabGroup.TYPE_NAME, new String[]{"myTabbed", "myTabbed"});
createTwoComponentsWithSameName(JComboBox.class, "myCombo");
checkComponentNameAmbiguity("myCombo", ComboBox.TYPE_NAME, new String[]{"myCombo", "myCombo"});
}
public void testAmbiguityExceptionIsDetectedBetweenLabelAndTextComponents() throws Exception {
JLabel jLabel = new JLabel("myText1");
jPanel.add(jLabel);
JTextField jTextField = new JTextField("myText2");
jTextField.setText("myText2");
jPanel.add(jTextField);
checkComponentNameAmbiguity("text", TextBox.TYPE_NAME, new String[]{"myText1", "myText2"});
panel = new Panel(jPanel);
try {
panel.getTextBox();
fail();
}
catch (ComponentAmbiguityException e) {
assertEquals(Messages.computeAmbiguityMessage(new String[]{"myText1", "myText2"}, TextBox.TYPE_NAME, null),
e.getMessage());
}
}
public void testComponentsWithSameNameAreFoundAccordingToTheirType() throws Exception {
JLabel label = new JLabel("test");
JTable table = new JTable();
table.setName("test");
jPanel.add(label);
jPanel.add(table);
TestUtils.assertUIComponentRefersTo(label, panel.getTextBox("test"));
TestUtils.assertUIComponentRefersTo(table, panel.getTable("test"));
}
public void testComponentsWithSamePatternInNameAreFoundAccordingToTheirType() throws Exception {
JLabel label = new JLabel("another label");
JTable table = new JTable();
table.setName("another table");
jPanel.add(label);
jPanel.add(table);
TestUtils.assertUIComponentRefersTo(label, panel.getTextBox("another"));
TestUtils.assertUIComponentRefersTo(table, panel.getTable("another"));
}
public void testComponentThatShouldBeUniqueInPanel() throws Exception {
checkComponentUnicityAmbiguity(JTable.class, Table.TYPE_NAME, "myTable");
checkComponentUnicityAmbiguity(JList.class, ListBox.TYPE_NAME, "myList");
checkComponentUnicityAmbiguity(JDesktopPane.class, org.uispec4j.Desktop.TYPE_NAME, "myDesktop");
checkComponentUnicityAmbiguity(JTextField.class, TextBox.TYPE_NAME, "myText");
checkComponentUnicityAmbiguity(JTree.class, Tree.TYPE_NAME, "myTree");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -