1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37 public class ActionsContextTest {
38
39
40
41
42 @Test
43 public void testSubClasses() {
44 if (skipTests()) {
45
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
76
77 @Test
78 public void testChain() {
79 if (skipTests()) {
80
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
109
110 @Test
111 public void testParentContext() {
112 if (skipTests()) {
113
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
143
144 private boolean skipTests() {
145
146
147 return GraphicsEnvironment.isHeadless();
148 }
149 }