1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.usability.rules.metrics; |
---|
16 | |
---|
17 | import static org.junit.Assert.assertEquals; |
---|
18 | import static org.junit.Assert.fail; |
---|
19 | |
---|
20 | import java.util.ArrayList; |
---|
21 | import java.util.logging.Level; |
---|
22 | import java.util.regex.Matcher; |
---|
23 | import java.util.regex.Pattern; |
---|
24 | |
---|
25 | import org.junit.Before; |
---|
26 | |
---|
27 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
28 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeBuilder; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeNodeFactory; |
---|
39 | import de.ugoe.cs.autoquest.test.DummyGUIElement; |
---|
40 | import de.ugoe.cs.autoquest.test.DummyInteraction; |
---|
41 | import de.ugoe.cs.autoquest.test.DummyTextField; |
---|
42 | import de.ugoe.cs.autoquest.usability.UsabilityDefect; |
---|
43 | import de.ugoe.cs.autoquest.usability.UsabilityEvaluationResult; |
---|
44 | import de.ugoe.cs.util.console.TextConsole; |
---|
45 | |
---|
46 | /** |
---|
47 | * TODO comment |
---|
48 | * |
---|
49 | * @version $Revision: $ $Date: 18.07.2012$ |
---|
50 | * @author 2012, last modified by $Author: pharms$ |
---|
51 | */ |
---|
52 | public class AbstractUsabilityEvaluationTC { |
---|
53 | |
---|
54 | /** */ |
---|
55 | private ITaskTreeBuilder taskTreeBuilder = new TaskTreeBuilder(); |
---|
56 | |
---|
57 | /** */ |
---|
58 | private ITaskTreeNodeFactory taskTreeNodeFactory = new TaskTreeNodeFactory(); |
---|
59 | |
---|
60 | /** |
---|
61 | * |
---|
62 | */ |
---|
63 | @Before |
---|
64 | public void setUp() { |
---|
65 | new TextConsole(Level.FINEST); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * |
---|
70 | */ |
---|
71 | protected ITaskTree createTaskTree(String spec) { |
---|
72 | Matcher matcher = |
---|
73 | Pattern.compile("(\\s*(\\w+)\\s*(\\(((\\w*\\s*)*)\\))?\\s*(\\{))|(\\})").matcher(spec); |
---|
74 | |
---|
75 | if (!matcher.find()) { |
---|
76 | if (!matcher.hitEnd()) { |
---|
77 | throw new IllegalArgumentException("could not parse task specification"); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | return taskTreeNodeFactory.createTaskTree(parseTask(matcher, new int[1])); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * TODO: comment |
---|
86 | * |
---|
87 | * @param expectedDefects |
---|
88 | * @param evaluateUsability |
---|
89 | */ |
---|
90 | protected void assertUsabilityEvaluationResult(UsabilityDefect[] expectedDefects, |
---|
91 | UsabilityEvaluationResult evaluationResult) |
---|
92 | { |
---|
93 | assertEquals(expectedDefects.length, evaluationResult.getAllDefects().size()); |
---|
94 | |
---|
95 | EXPECTED_DEFECT_ITERATION: for (UsabilityDefect expectedDefect : expectedDefects) { |
---|
96 | for (UsabilityDefect defect : evaluationResult.getAllDefects()) { |
---|
97 | if (expectedDefect.equals(defect)) { |
---|
98 | System.err.println(defect.getParameterizedDescription()); |
---|
99 | continue EXPECTED_DEFECT_ITERATION; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | for (UsabilityDefect defect : evaluationResult.getAllDefects()) { |
---|
104 | System.err.println(defect); |
---|
105 | } |
---|
106 | |
---|
107 | fail("expected defect " + expectedDefect + " not found in evaluation result"); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * |
---|
113 | */ |
---|
114 | private ITaskTreeNode parseTask(Matcher tokenMatcher, int[] typeNumbers) { |
---|
115 | String firstToken = tokenMatcher.group(); |
---|
116 | |
---|
117 | if ("}".equals(firstToken)) { |
---|
118 | throw new IllegalArgumentException("found a closing bracket at an unexpected place"); |
---|
119 | } |
---|
120 | |
---|
121 | ITaskTreeNode treeNode = instantiateTreeNode(tokenMatcher, typeNumbers); |
---|
122 | |
---|
123 | if (!tokenMatcher.find()) { |
---|
124 | throw new IllegalArgumentException("could not parse task specification"); |
---|
125 | } |
---|
126 | |
---|
127 | firstToken = tokenMatcher.group(); |
---|
128 | |
---|
129 | if (!"}".equals(firstToken)) { |
---|
130 | ITaskTreeNode child = null; |
---|
131 | |
---|
132 | do { |
---|
133 | child = parseTask(tokenMatcher, typeNumbers); |
---|
134 | |
---|
135 | if (child != null) { |
---|
136 | addChild(treeNode, child); |
---|
137 | |
---|
138 | if (!tokenMatcher.find()) { |
---|
139 | throw new IllegalArgumentException("could not parse task specification"); |
---|
140 | } |
---|
141 | |
---|
142 | firstToken = tokenMatcher.group(); |
---|
143 | |
---|
144 | if ("}".equals(firstToken)) { |
---|
145 | break; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|
150 | while (child != null); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | return treeNode; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * TODO: comment |
---|
159 | * |
---|
160 | * @param group |
---|
161 | * @return |
---|
162 | */ |
---|
163 | private ITaskTreeNode instantiateTreeNode(Matcher tokenMatcher, int[] typeNumbers) { |
---|
164 | String type = tokenMatcher.group(2); |
---|
165 | String additionalInfo = tokenMatcher.group(4); |
---|
166 | |
---|
167 | if ("Interaction".equals(type)) { |
---|
168 | return taskTreeNodeFactory.createNewEventTask(new DummyInteraction("dummy", |
---|
169 | typeNumbers[0]++), |
---|
170 | new DummyGUIElement("dummy")); |
---|
171 | } |
---|
172 | else if ("Sequence".equals(type)) { |
---|
173 | return taskTreeNodeFactory.createNewSequence(); |
---|
174 | } |
---|
175 | else if ("Iteration".equals(type)) { |
---|
176 | return taskTreeNodeFactory.createNewIteration(); |
---|
177 | } |
---|
178 | else if ("Selection".equals(type)) { |
---|
179 | return taskTreeNodeFactory.createNewSelection(); |
---|
180 | } |
---|
181 | else if ("TextInput".equals(type)) { |
---|
182 | if (additionalInfo == null) { |
---|
183 | fail("no simulated text provided for text input interactin task"); |
---|
184 | } |
---|
185 | |
---|
186 | TextInput textInput = new TextInput(additionalInfo, new ArrayList<Event>()); |
---|
187 | |
---|
188 | IEventTask task = |
---|
189 | taskTreeNodeFactory.createNewEventTask(textInput, |
---|
190 | new DummyTextField(additionalInfo)); |
---|
191 | |
---|
192 | return task; |
---|
193 | } |
---|
194 | else { |
---|
195 | fail("invalid type of task tree node: " + type); |
---|
196 | return null; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * TODO: comment |
---|
202 | * |
---|
203 | * @param treeNode |
---|
204 | * @param child |
---|
205 | */ |
---|
206 | private void addChild(ITaskTreeNode parent, ITaskTreeNode child) { |
---|
207 | if (parent instanceof ISequence) { |
---|
208 | taskTreeBuilder.addChild((ISequence) parent, child); |
---|
209 | } |
---|
210 | else if (parent instanceof IIteration) { |
---|
211 | if (parent.getChildren().size() <= 0) { |
---|
212 | taskTreeBuilder.setChild((IIteration) parent, child); |
---|
213 | } |
---|
214 | else { |
---|
215 | fail("can not add more than one child to an iteration"); |
---|
216 | } |
---|
217 | } |
---|
218 | else if (parent instanceof ISelection) { |
---|
219 | taskTreeBuilder.addChild((ISelection) parent, child); |
---|
220 | } |
---|
221 | else { |
---|
222 | fail("can not add children to parent task tree node of type " + |
---|
223 | parent.getClass().getName()); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|