001: package abbot.tester;
002:
003: import java.awt.*;
004: import javax.swing.*;
005:
006: import junit.extensions.abbot.*;
007: import abbot.Log;
008:
009: /** Unit test to verify the JComponentTester class.<p> */
010:
011: public class JComponentTesterTest extends ComponentTestFixture {
012:
013: private JComponentTester tester;
014:
015: /** Create a new test case with the given name. */
016: public JComponentTesterTest(String name) {
017: super (name);
018: }
019:
020: protected void setUp() {
021: tester = (JComponentTester) ComponentTester
022: .getTester(JComponent.class);
023: }
024:
025: private void scrollAndAssertVisible(JList list, int row) {
026: Rectangle rect = list.getCellBounds(row, row);
027: tester.actionScrollToVisible(list, rect.x, rect.y, rect.width,
028: rect.height);
029: tester.actionWaitForIdle();
030: Rectangle visible = list.getVisibleRect();
031: Log.debug("Visible rect: " + visible);
032: assertTrue("Horizontal scroll to element " + row + " failed ("
033: + visible.x + " <= " + rect.x + " < "
034: + (visible.x + visible.width) + ")",
035: visible.x <= rect.x
036: && rect.x < visible.x + visible.width);
037: assertTrue("Vertical scroll to element " + row + " failed ("
038: + visible.y + " <= " + rect.y + " < "
039: + (visible.y + visible.height) + ")",
040: visible.y <= rect.y
041: && rect.y < visible.y + visible.height);
042: }
043:
044: /** Test scrolling for JComponents within a scroll pane. */
045: // FIXME really needs to scroll horizontally as well
046: public void testScroll() {
047: String[] data = { "zero", "one", "two", "three", "four",
048: "five", "six", "seven", "eight", "nine", "zero", "one",
049: "two", "three", "four", "five", "six", "seven",
050: "eight", "nine", "zero", "one", "two", "three", "four",
051: "five", "six", "seven", "eight", "nine", "zero", "one",
052: "two", "three", "four", "five", "six", "seven",
053: "eight", "nine", "zero", "one", "two", "three", "four",
054: "five", "six", "seven", "eight", "nine", };
055: JList list = new JList(data);
056: JScrollPane scroll = new JScrollPane(list);
057: showFrame(scroll);
058:
059: scrollAndAssertVisible(list, 0);
060: scrollAndAssertVisible(list, data.length / 4);
061: scrollAndAssertVisible(list, data.length / 2);
062: scrollAndAssertVisible(list, data.length * 3 / 4);
063: scrollAndAssertVisible(list, data.length / 2);
064: scrollAndAssertVisible(list, data.length / 4);
065: scrollAndAssertVisible(list, 0);
066: }
067:
068: public void testActionMap() {
069: // Use something for which we know the action/mappings
070: JTextField tf = new JTextField("Some text");
071: showFrame(tf);
072: tester.actionFocus(tf);
073: tester.actionActionMap(tf, "select-all");
074: assertEquals("Selection should start at beginning of text", 0,
075: tf.getSelectionStart());
076: assertEquals("Selection should end at end of text", tf
077: .getText().length(), tf.getSelectionEnd());
078: }
079:
080: public void testActionOnObscured() {
081: JPanel panel = new JPanel() {
082: public Dimension getPreferredSize() {
083: return new Dimension(400, 400);
084: }
085: };
086: showFrame(panel, new Dimension(100, 100));
087: try {
088: tester.actionClick(panel, 300, 300);
089: fail("Action should fail if outside of JComponent visible rect");
090: } catch (ActionFailedException e) {
091: }
092: }
093:
094: public void testScrollToVisible() {
095: JLabel label = new JLabel(getName());
096: JPanel p = new JPanel();
097: p.setPreferredSize(new Dimension(200, 400));
098: JPanel scrolled = new JPanel(new BorderLayout());
099: scrolled.add(p);
100: scrolled.add(label, BorderLayout.SOUTH);
101: showFrame(new JScrollPane(scrolled), new Dimension(200, 200));
102:
103: Rectangle visible = label.getVisibleRect();
104: Rectangle empty = new Rectangle(0, 0, 0, 0);
105: assertTrue("Component should not be visible", visible == null
106: || visible.equals(empty));
107:
108: tester.actionScrollToVisible(label, new ComponentLocation());
109: visible = label.getVisibleRect();
110: assertFalse("Component should be visible", visible == null
111: || visible.equals(empty));
112: }
113:
114: public static void main(String[] args) {
115: RepeatHelper.runTests(args, JComponentTesterTest.class);
116: }
117: }
|