001: package org.uispec4j.interception;
002:
003: import org.uispec4j.MenuItem;
004: import org.uispec4j.Trigger;
005: import org.uispec4j.UISpec4J;
006: import org.uispec4j.Window;
007: import org.uispec4j.utils.AssertionFailureNotDetectedError;
008: import org.uispec4j.utils.Functor;
009: import org.uispec4j.utils.Utils;
010:
011: import javax.swing.*;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.MouseAdapter;
014: import java.awt.event.MouseEvent;
015: import java.io.IOException;
016:
017: public class PopupMenuInterceptorTest extends InterceptionTestCase {
018:
019: public void testStandardUsageWithHeavyweightPopup()
020: throws Exception {
021: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
022: checkStandardUsage();
023: }
024:
025: public void testStandardUsageWithLightweightPopup()
026: throws Exception {
027: JPopupMenu.setDefaultLightWeightPopupEnabled(true);
028: checkStandardUsage();
029: }
030:
031: private void checkStandardUsage() {
032: PopupDisplayTrigger trigger = getPopupTrigger();
033: launchInterception(trigger);
034: logger.assertEquals("<log>" + "<click popupMenuItem='item 1'/>"
035: + "</log>");
036: }
037:
038: public void testRetryStrategy() throws Exception {
039: checkInterceptionWithDelay(100);
040: logger.assertEquals("<log>" + "<click popupMenuItem='item 1'/>"
041: + "</log>");
042: }
043:
044: public void testRetryStrategyWithAnotherTimeout() throws Exception {
045: checkAssertionFailedError(new Functor() {
046: public void run() throws Exception {
047: checkInterceptionWithDelay(0);
048: }
049: }, "No popup was shown");
050: }
051:
052: private void checkInterceptionWithDelay(int delayForPopup)
053: throws InterruptedException {
054: final PopupDisplayTrigger trigger = getPopupTrigger();
055: final Thread thread = new Thread() {
056: public void run() {
057: Utils.sleep(20);
058: try {
059: trigger.run();
060: } catch (Exception e) {
061: throw new RuntimeException(e);
062: }
063: }
064: };
065:
066: UISpec4J.setWindowInterceptionTimeLimit(delayForPopup);
067: try {
068: launchInterception(new Trigger() {
069: public void run() throws Exception {
070: thread.start();
071: }
072: });
073: } finally {
074: thread.join();
075: }
076: }
077:
078: private PopupDisplayTrigger getPopupTrigger() {
079: final JButton button = new JButton();
080: showLargeDialogWithComponent(button);
081: return new PopupDisplayTrigger(button);
082: }
083:
084: public void testAnErrorIsRaisedIfTheTriggerDoesNotPopupAMenu()
085: throws Exception {
086: checkAssertionFailedError(new Functor() {
087: public void run() throws Exception {
088: PopupMenuInterceptor.run(Trigger.DO_NOTHING);
089: }
090: }, "No popup was shown");
091: }
092:
093: public void testNothingHappensWhenAPopupAppearsWithoutInterception()
094: throws Exception {
095: final JButton button = new JButton();
096: showLargeDialogWithComponent(button);
097: new PopupDisplayTrigger(button).run();
098: }
099:
100: public void testExceptionsRaisedByTheTriggerAreConvertedIntoRuntimeExceptions()
101: throws Exception {
102: try {
103: PopupMenuInterceptor.run(new Trigger() {
104: public void run() throws Exception {
105: throw new IOException("msg");
106: }
107: });
108: throw new AssertionFailureNotDetectedError();
109: } catch (RuntimeException e) {
110: assertEquals("msg", e.getCause().getMessage());
111: }
112: }
113:
114: public void testHandlingADialogShownByAPopupMenu() throws Exception {
115: final JPanel panel = new JPanel();
116: showLargeDialogWithComponent(panel);
117:
118: final JButton button = new JButton(new AbstractAction("ok") {
119: public void actionPerformed(ActionEvent e) {
120: logger.log("ok");
121: }
122: });
123:
124: WindowInterceptor.init(PopupMenuInterceptor.run(new Trigger() {
125: public void run() throws Exception {
126: JPopupMenu menu = new JPopupMenu();
127: menu.add(new AbstractAction("menu") {
128: public void actionPerformed(ActionEvent e) {
129: logger.log("dialog");
130: JDialog dialog = new JDialog();
131: dialog.getContentPane().add(button);
132: dialog.setVisible(true);
133: }
134: });
135: menu.show(panel, 10, 10);
136: }
137: }).getSubMenu("menu").triggerClick()).process(
138: new WindowHandler() {
139: public Trigger process(Window window)
140: throws Exception {
141: logger.log("handleWindow");
142: return window.getButton("ok").triggerClick();
143: }
144: }).run();
145:
146: logger.assertEquals("<log>" + " <dialog/>"
147: + " <handleWindow/>" + " <ok/>" + "</log>");
148: }
149:
150: public void testPopupMenuShownFromATree() throws Exception {
151: final JTree jTree = new JTree();
152: jTree.addMouseListener(new MouseAdapter() {
153: public void mouseReleased(MouseEvent e) {
154: JPopupMenu menu = new JPopupMenu();
155: menu.add(new AbstractAction("Run") {
156: public void actionPerformed(ActionEvent e) {
157: logger.log("click");
158: }
159: });
160: menu.show(jTree, 5, 5);
161: }
162: });
163: final JDialog dialog = new JDialog(new JFrame(), "dialog");
164: addHideButton(dialog, "Close");
165: dialog.getContentPane().add(jTree);
166:
167: WindowInterceptor.init(new Trigger() {
168: public void run() throws Exception {
169: dialog.setVisible(true);
170: }
171: }).process(new WindowHandler() {
172: public Trigger process(final Window window)
173: throws Exception {
174: PopupMenuInterceptor.run(
175: window.getTree().triggerClick("")).getSubMenu(
176: "Run").click();
177: return window.getButton("Close").triggerClick();
178: }
179: }).run();
180:
181: logger.assertEquals("<log>" + " <click/>"
182: + " <click button='Close'/>" + "</log>");
183: }
184:
185: private void launchInterception(Trigger trigger) {
186: MenuItem menu = PopupMenuInterceptor.run(trigger);
187: assertNotNull(menu);
188: assertTrue(menu
189: .contentEquals(new String[] { "item 1", "item 2" }));
190: menu.getSubMenu("item 1").click();
191: }
192:
193: private void showLargeDialogWithComponent(final JComponent component) {
194: WindowInterceptor.run(new Trigger() {
195: public void run() throws Exception {
196: JDialog dialog = new JDialog();
197: dialog.getContentPane().add(component);
198: // Setting a large size for the dialog forces the use of lightweight popups
199: // if this feature was enabled
200: dialog.setSize(2000, 2000);
201: dialog.setVisible(true);
202: }
203: });
204: }
205:
206: private class PopupDisplayTrigger implements Trigger {
207: private JComponent component;
208:
209: public PopupDisplayTrigger(JComponent c) {
210: component = c;
211: }
212:
213: public void run() throws Exception {
214: JPopupMenu menu = new JPopupMenu();
215: menu.add(new AbstractAction("item 1") {
216: public void actionPerformed(ActionEvent e) {
217: logger.log("click").add("popupMenuItem", "item 1");
218: }
219: });
220: menu.add("item 2");
221: menu.show(component, 10, 10);
222: }
223: }
224: }
|