View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.gui.swing;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.awt.GraphicsEnvironment;
22  
23  import org.junit.Test;
24  
25  import ch.elca.el4j.services.gui.swing.ActionsContext;
26  import ch.elca.el4j.tests.gui.swing.actions.ChildActions;
27  import ch.elca.el4j.tests.gui.swing.actions.GrandparentActions;
28  import ch.elca.el4j.tests.gui.swing.actions.ParentActions;
29  
30  /**
31   * Test {@link ActionsContext}s.
32   *
33   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/test/java/ch/elca/el4j/tests/gui/swing/ActionsContextTest.java $
34   *
35   * @author Stefan Wismer (SWI)
36   */
37  public class ActionsContextTest {
38  
39  	/**
40  	 * Test the subclasses support of {@link ActionsContext}.
41  	 */
42  	@Test
43  	public void testSubClasses() {
44  		if (skipTests()) {
45  			// see comment in skipTests()
46  			return;
47  		}
48  		ValueHolder<String> stateHolder = new ValueHolder<String>();
49  		stateHolder.setValue("nothing");
50  		
51  		ChildActions child = new ChildActions("", stateHolder);
52  		ActionsContext actionsContext = ActionsContext.create(child);
53  		
54  		actionsContext.getAction("doA").actionPerformed(null);
55  		assertEquals("Grandparent.doA", stateHolder.getValue());
56  		
57  		actionsContext.getAction("doB").actionPerformed(null);
58  		assertEquals("Parent.doB", stateHolder.getValue());
59  		
60  		actionsContext.getAction("doC").actionPerformed(null);
61  		assertEquals("Parent.doC", stateHolder.getValue());
62  		
63  		actionsContext.getAction("doD").actionPerformed(null);
64  		assertEquals("Child.doD", stateHolder.getValue());
65  		
66  		GrandparentActions grandparent = new GrandparentActions("", stateHolder);
67  		actionsContext.getAction(grandparent, "doB").actionPerformed(null);
68  		assertEquals("Grandparent.doB", stateHolder.getValue());
69  			
70  		actionsContext.getAction(child, "doB").actionPerformed(null);
71  		assertEquals("Parent.doB", stateHolder.getValue());
72  	}
73  	
74  	/**
75  	 * Test the chain support of {@link ActionsContext}.
76  	 */
77  	@Test
78  	public void testChain() {
79  		if (skipTests()) {
80  			// see comment in skipTests()
81  			return;
82  		}
83  		ValueHolder<String> stateHolder = new ValueHolder<String>();
84  		stateHolder.setValue("nothing");
85  		
86  		GrandparentActions grandparent = new GrandparentActions("1_", stateHolder);
87  		ParentActions parent = new ParentActions("2_", stateHolder);
88  		ChildActions child = new ChildActions("3_", stateHolder);
89  		ActionsContext actionsContext = ActionsContext.create(grandparent, parent, child);
90  		
91  		actionsContext.getAction("doA").actionPerformed(null);
92  		assertEquals("1_Grandparent.doA", stateHolder.getValue());
93  		
94  		actionsContext.getAction("doB").actionPerformed(null);
95  		assertEquals("1_Grandparent.doB", stateHolder.getValue());
96  		
97  		actionsContext.getAction("doC").actionPerformed(null);
98  		assertEquals("2_Parent.doC", stateHolder.getValue());
99  		
100 		actionsContext.getAction("doD").actionPerformed(null);
101 		assertEquals("3_Child.doD", stateHolder.getValue());
102 		
103 		actionsContext.getAction(parent, "doB").actionPerformed(null);
104 		assertEquals("2_Parent.doB", stateHolder.getValue());
105 	}
106 	
107 	/**
108 	 * Test the parent context support of {@link ActionsContext}.
109 	 */
110 	@Test
111 	public void testParentContext() {
112 		if (skipTests()) {
113 			// see comment in skipTests()
114 			return;
115 		}
116 		ValueHolder<String> stateHolder = new ValueHolder<String>();
117 		stateHolder.setValue("nothing");
118 		
119 		GrandparentActions grandparent = new GrandparentActions("1_", stateHolder);
120 		ParentActions parent = new ParentActions("2_", stateHolder);
121 		ChildActions child = new ChildActions("3_", stateHolder);
122 		ActionsContext parentContext = ActionsContext.create(parent, child);
123 		ActionsContext actionsContext = ActionsContext.extend(parentContext, grandparent);
124 		
125 		actionsContext.getAction("doA").actionPerformed(null);
126 		assertEquals("1_Grandparent.doA", stateHolder.getValue());
127 		
128 		actionsContext.getAction("doB").actionPerformed(null);
129 		assertEquals("1_Grandparent.doB", stateHolder.getValue());
130 		
131 		actionsContext.getAction("doC").actionPerformed(null);
132 		assertEquals("2_Parent.doC", stateHolder.getValue());
133 		
134 		actionsContext.getAction("doD").actionPerformed(null);
135 		assertEquals("3_Child.doD", stateHolder.getValue());
136 		
137 		actionsContext.getAction(parent, "doB").actionPerformed(null);
138 		assertEquals("2_Parent.doB", stateHolder.getValue());
139 	}
140 	
141 	/**
142 	 * @return    <code>true</code> if tests should be skipped (see comment)
143 	 */
144 	private boolean skipTests() {
145 		// getAction(object, action) makes ResourceMap execute getMenuShortcutKeyMask() on built-in
146 		// "quit"-action, which does not work in headless mode
147 		return GraphicsEnvironment.isHeadless();
148 	}
149 }