001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.interception.WindowHandler;
005: import org.uispec4j.interception.WindowInterceptor;
006: import org.uispec4j.utils.AssertionFailureNotDetectedError;
007: import org.uispec4j.xml.EventLogger;
008: import org.uispec4j.xml.XmlAssert;
009:
010: import javax.swing.*;
011: import java.awt.event.ActionEvent;
012:
013: public abstract class MenuItemTestCase extends UIComponentTestCase {
014:
015: public void testGetComponentTypeName() throws Exception {
016: MenuItem item = getBuilder("menuTest").getMenuItem();
017: assertEquals("menu", item.getDescriptionTypeName());
018: }
019:
020: public void testGetDescription() throws Exception {
021: MenuItem item = getBuilder("menuTest").setName("myMenu")
022: .getMenuItem();
023: XmlAssert.assertEquivalent("<menu name='myMenu'/>", item
024: .getDescription());
025: }
026:
027: protected UIComponent createComponent() {
028: return getBuilder("item").getMenuItem();
029: }
030:
031: public void testMenuContent() throws Exception {
032: MenuBuilder rootBuilder = getBuilder("menuTest");
033: rootBuilder.add("item1");
034: MenuBuilder item2Builder = rootBuilder.startSubMenu("item2");
035: item2Builder.add("subItem1");
036: item2Builder.add("subItem2");
037: rootBuilder.add("item3");
038:
039: MenuItem item = rootBuilder.getMenuItem();
040: assertTrue(item.contentEquals("<menu name='menuTest'>"
041: + " <menu name='item1'/>" + " <menu name='item2'>"
042: + " <menu name='subItem1'/>"
043: + " <menu name='subItem2'/>" + " </menu>"
044: + " <menu name='item3'/>" + "</menu>"));
045: }
046:
047: public void testCheckMenu() throws Exception {
048: MenuBuilder builder = getBuilder("menuTest");
049: builder.add("item1");
050: builder.addSeparator();
051: builder.add("item2");
052: MenuItem item = builder.getMenuItem();
053: assertTrue(item.contentEquals("<menu name='menuTest'>"
054: + " <menu name='item1'/>" + " <separator/>"
055: + " <menu name='item2'/>" + "</menu>"));
056: assertTrue(item
057: .contentEquals(new String[] { "item1", "item2" }));
058: }
059:
060: public void testCheckMenuOnMenuItemWithNoError() throws Exception {
061: MenuBuilder builder = getBuilder("root");
062: builder.add("item1");
063: MenuItem item = builder.getMenuItem();
064: MenuItem subMenu = item.getSubMenu("item1");
065: assertTrue(subMenu.contentEquals("<menu name='item1'/>"));
066: assertTrue(item.contentEquals(new String[] { "item1" }));
067: }
068:
069: public void testGetSubMenuWorksWithPartOfItsName() throws Exception {
070: MenuBuilder builder = getBuilder("root");
071: builder.add("one two three...");
072: MenuItem item = builder.getMenuItem();
073: assertNotNull(item.getSubMenu("one two three..."));
074: assertNotNull(item.getSubMenu("one"));
075: assertNotNull(item.getSubMenu("two"));
076: }
077:
078: public void testClickFailsIfTheMenuItemIsNotEnabled()
079: throws Exception {
080: MenuItem item = getBuilder("").setEnabled(false).getMenuItem();
081: try {
082: item.click();
083: throw new AssertionFailureNotDetectedError();
084: } catch (AssertionFailedError e) {
085: assertEquals(
086: "The menu item is not enabled, it cannot be activated",
087: e.getMessage());
088: }
089: }
090:
091: public void testCheckMenuWithOneLevel() throws Exception {
092: MenuItem item = getBuilder("").add("item1").add("item2").add(
093: "item3").getMenuItem();
094: assertTrue(item.contentEquals(new String[] { "item1", "item2",
095: "item3" }));
096: }
097:
098: public void testActivateSimulatesAClickOnTheMenuItem()
099: throws Exception {
100: EventLogger eventLogger = new EventLogger();
101: MenuItem menuItem = createLoggingMenuItem(eventLogger);
102: assertTrue(menuItem.contentEquals(new String[] { "item" }));
103: menuItem.getSubMenu("item").click();
104:
105: eventLogger.assertEquals("<log>" + " <action/>" + "</log>");
106: }
107:
108: public void testTriggerClickWorksAsClick() throws Exception {
109: EventLogger eventLogger = new EventLogger();
110: MenuItem menuItem = createLoggingMenuItem(eventLogger);
111: menuItem.getSubMenu("item").triggerClick().run();
112: eventLogger.assertEquals("<log>" + " <action/>" + "</log>");
113: }
114:
115: public void testGetSubmenuError() throws Exception {
116: checkGetSubmenuError(new String[] { "One", "Two", "Three" },
117: "Four",
118: "There is no menu item matching 'Four' - actual elements: [One,Two,Three]");
119: }
120:
121: public void testGetAmbiguitySubMenu() throws Exception {
122: checkGetSubmenuError(
123: new String[] { "One two three...",
124: "One two three four...", "two three" },
125: "three",
126: "Could not retrieve subMenu item : There are more than one component matching 'three'");
127: }
128:
129: private void checkGetSubmenuError(String[] menus,
130: String searchedItem, String expectedMessage) {
131: MenuBuilder builder = getBuilder("");
132: for (int i = 0; i < menus.length; i++) {
133: builder.add(menus[i]);
134: }
135: MenuItem item = builder.getMenuItem();
136: try {
137: item.getSubMenu(searchedItem);
138: throw new AssertionFailureNotDetectedError();
139: } catch (AssertionFailedError e) {
140: assertEquals(expectedMessage, e.getMessage());
141: }
142: }
143:
144: public void testGetSubMenuWhenSubMenuIsItselfAMenu()
145: throws Exception {
146: MenuBuilder rootBuilder = getBuilder("");
147: rootBuilder.add("item1");
148: rootBuilder.startSubMenu("item2").add("subItem1").add(
149: "subItem2").setName("subMenuItem2");
150: rootBuilder.add("item3");
151: MenuItem subMenuItem = rootBuilder.getMenuItem().getSubMenu(
152: "item2");
153: assertTrue(subMenuItem.contentEquals("<menu name='item2'>"
154: + " <menu name='subItem1'/>"
155: + " <menu name='subItem2'/>" + "</menu>"));
156: }
157:
158: public void testHandlingADialogShownByAPopupMenu() throws Exception {
159: final EventLogger logger = new EventLogger();
160:
161: final JPanel panel = new JPanel();
162: WindowInterceptor.run(new Trigger() {
163: public void run() throws Exception {
164: showDialog(panel);
165: }
166: });
167:
168: final JButton button = new JButton(new AbstractAction("ok") {
169: public void actionPerformed(ActionEvent e) {
170: logger.log("ok");
171: }
172: });
173:
174: MenuItem menuItem = getBuilder("").add(
175: new AbstractAction("menu") {
176: public void actionPerformed(ActionEvent e) {
177: logger.log("dialog");
178: showDialog(button);
179: }
180: }).getMenuItem();
181: WindowInterceptor.init(
182: menuItem.getSubMenu("menu").triggerClick()).process(
183: new WindowHandler() {
184: public Trigger process(Window window)
185: throws Exception {
186: logger.log("handleWindow");
187: return window.getButton("ok").triggerClick();
188: }
189: }).run();
190:
191: logger.assertEquals("<log>" + " <dialog/>"
192: + " <handleWindow/>" + " <ok/>" + "</log>");
193: }
194:
195: private void showDialog(final JComponent component) {
196: JDialog dialog = new JDialog();
197: dialog.getContentPane().add(component);
198: dialog.setVisible(true);
199: }
200:
201: protected abstract MenuItem createLoggingMenuItem(
202: EventLogger eventLogger);
203:
204: protected abstract MenuBuilder getBuilder(String text);
205:
206: public interface MenuBuilder {
207: MenuBuilder add(String item);
208:
209: MenuBuilder add(Action action);
210:
211: MenuBuilder addSeparator();
212:
213: MenuBuilder setEnabled(boolean enabled);
214:
215: MenuBuilder setName(String s);
216:
217: MenuBuilder startSubMenu(String item);
218:
219: MenuItem getMenuItem();
220: }
221: }
|