001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.Counter;
006:
007: import javax.swing.event.TreeSelectionEvent;
008: import javax.swing.event.TreeSelectionListener;
009: import java.awt.event.MouseAdapter;
010: import java.awt.event.MouseEvent;
011:
012: public class TreeClickingTest extends TreeTestCase {
013:
014: public void testClickOnlyChangesTheSelectionOnce() throws Exception {
015: checkClickOnlyChangesTheSelectionOnce(new DirectClicker());
016: checkClickOnlyChangesTheSelectionOnce(new TriggerClicker());
017: }
018:
019: private void checkClickOnlyChangesTheSelectionOnce(Clicker clicker)
020: throws Exception {
021: final Counter counter = new Counter();
022: jTree.getSelectionModel().addTreeSelectionListener(
023: new TreeSelectionListener() {
024: public void valueChanged(TreeSelectionEvent e) {
025: counter.increment();
026: }
027: });
028: clicker.click("child1");
029: assertEquals(1, counter.getCount());
030: clicker.click("child2");
031: assertEquals(2, counter.getCount());
032: clicker.click("child1/child1_1");
033: assertEquals(3, counter.getCount());
034: }
035:
036: public void testClickFailsWhenAppliedOnNonExistingPath()
037: throws Exception {
038: checkClickFailsWhenAppliedOnNonExistingPath(new DirectClicker());
039: checkClickFailsWhenAppliedOnNonExistingPath(new TriggerClicker());
040: }
041:
042: private void checkClickFailsWhenAppliedOnNonExistingPath(
043: Clicker clicker) throws Exception {
044: final Counter counter = new Counter();
045: jTree.getSelectionModel().addTreeSelectionListener(
046: new TreeSelectionListener() {
047: public void valueChanged(TreeSelectionEvent e) {
048: counter.increment();
049: }
050: });
051: try {
052: clicker.click("child3");
053: throw new AssertionFailureNotDetectedError();
054: } catch (AssertionFailedError e) {
055: assertEquals(Tree.badTreePath("child3"), e.getMessage());
056: }
057: try {
058: clicker.rightClick("child3");
059: throw new AssertionFailureNotDetectedError();
060: } catch (AssertionFailedError e) {
061: assertEquals(Tree.badTreePath("child3"), e.getMessage());
062: }
063: assertEquals(0, counter.getCount());
064: }
065:
066: public void testRightClickBehaviour() throws Exception {
067: checkRightClickBehaviour(new DirectClicker());
068: checkRightClickBehaviour(new TriggerClicker());
069: }
070:
071: private void checkRightClickBehaviour(Clicker clicker)
072: throws Exception {
073: final Counter counter = new Counter();
074: jTree.addMouseListener(new MouseAdapter() {
075: public void mousePressed(MouseEvent e) {
076: counter.increment();
077: int modifiers = e.getModifiers();
078: assertTrue((modifiers & MouseEvent.BUTTON3_MASK) != 0);
079: }
080: });
081: tree.select("child1");
082: tree.addToSelection("child2");
083: clicker.rightClick("child1");
084: assertTrue(tree.selectionEquals(new String[] { "child1" }));
085: assertEquals(1, counter.getCount());
086:
087: tree.addToSelection("child2");
088: clicker.rightClickInSelection();
089: assertTrue(tree.selectionEquals(new String[] { "child1",
090: "child2" }));
091: assertEquals(2, counter.getCount());
092:
093: clicker.rightClick("child1/child1_1");
094: assertTrue(tree.selectionEquals("child1/child1_1"));
095: assertEquals(3, counter.getCount());
096:
097: clicker.rightClickInSelection();
098: assertTrue(tree.selectionEquals("child1/child1_1"));
099: assertEquals(4, counter.getCount());
100: }
101:
102: public void testRightClickInSelectionNeedsASelection()
103: throws Exception {
104: checkRightClickInSelectionNeedsASelection(new DirectClicker());
105: checkRightClickInSelectionNeedsASelection(new TriggerClicker());
106: }
107:
108: private void checkRightClickInSelectionNeedsASelection(
109: Clicker clicker) throws Exception {
110: try {
111: clicker.rightClickInSelection();
112: throw new AssertionFailureNotDetectedError();
113: } catch (AssertionFailedError e) {
114: assertEquals("There is no current selection", e
115: .getMessage());
116: }
117: }
118:
119: private interface Clicker {
120: public void click(String path) throws Exception;
121:
122: public void rightClick(String path) throws Exception;
123:
124: public void rightClickInSelection() throws Exception;
125: }
126:
127: private class DirectClicker implements Clicker {
128: public void click(String path) {
129: tree.click(path);
130: }
131:
132: public void rightClick(String path) {
133: tree.rightClick(path);
134: }
135:
136: public void rightClickInSelection() throws Exception {
137: tree.rightClickInSelection();
138: }
139: }
140:
141: private class TriggerClicker implements Clicker {
142: public void click(String path) throws Exception {
143: tree.triggerClick(path).run();
144: }
145:
146: public void rightClick(String path) throws Exception {
147: tree.triggerRightClick(path).run();
148: }
149:
150: public void rightClickInSelection() throws Exception {
151: tree.triggerRightClickInSelection().run();
152: }
153: }
154: }
|