source: trunk/quest-core-usability-test/src/test/java/de/ugoe/cs/quest/usability/AbstractUsabilityEvaluationTC.java @ 655

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
File size: 6.5 KB
Line 
1package de.ugoe.cs.quest.usability;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.fail;
5
6import java.util.logging.Logger;
7import java.util.regex.Matcher;
8import java.util.regex.Pattern;
9
10import org.junit.Before;
11
12import de.ugoe.cs.quest.tasktrees.testutils.DummyGUIElement;
13import de.ugoe.cs.quest.tasktrees.testutils.DummyInteraction;
14import de.ugoe.cs.quest.tasktrees.testutils.DummyTextField;
15import de.ugoe.cs.quest.tasktrees.testutils.SimpleLogFormatter;
16import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
17import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
18import de.ugoe.cs.quest.tasktrees.treeifc.ISequence;
19import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTree;
20import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder;
21import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
22import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory;
23import de.ugoe.cs.quest.tasktrees.treeifc.ITextInputEventTask;
24import de.ugoe.cs.quest.tasktrees.treeimpl.TaskTreeBuilder;
25import de.ugoe.cs.quest.tasktrees.treeimpl.TaskTreeNodeFactory;
26
27/**
28 * TODO comment
29 *
30 * @version $Revision: $ $Date: 18.07.2012$
31 * @author 2012, last modified by $Author: pharms$
32 */
33public class AbstractUsabilityEvaluationTC {
34
35    /** */
36    private ITaskTreeBuilder taskTreeBuilder = new TaskTreeBuilder();
37
38    /** */
39    private ITaskTreeNodeFactory taskTreeNodeFactory = new TaskTreeNodeFactory();
40
41    /**
42   *
43   */
44    @Before
45    public void setUp() {
46        Logger.getLogger("").getHandlers()[0].setFormatter(new SimpleLogFormatter());
47    }
48
49    /**
50   *
51   */
52    protected ITaskTree createTaskTree(String spec) {
53        Matcher matcher =
54            Pattern.compile("(\\s*(\\w+)\\s*(\\(((\\w*\\s*)*)\\))?\\s*(\\{))|(\\})").matcher(spec);
55
56        if (!matcher.find()) {
57            if (!matcher.hitEnd()) {
58                throw new IllegalArgumentException("could not parse task specification");
59            }
60        }
61
62        return taskTreeNodeFactory.createTaskTree(parseTask(matcher, new int[1]));
63    }
64
65    /**
66     * TODO: comment
67     *
68     * @param expectedDefects
69     * @param evaluateUsability
70     */
71    protected void assertUsabilityEvaluationResult(UsabilityDefect[]         expectedDefects,
72                                                   UsabilityEvaluationResult evaluationResult)
73    {
74        assertEquals(expectedDefects.length, evaluationResult.getAllDefects().size());
75
76        EXPECTED_DEFECT_ITERATION:
77        for (UsabilityDefect expectedDefect : expectedDefects) {
78            for (UsabilityDefect defect : evaluationResult.getAllDefects()) {
79                if (expectedDefect.equals(defect)) {
80                    System.err.println(defect.getParameterizedDescription());
81                    continue EXPECTED_DEFECT_ITERATION;
82                }
83            }
84
85            for (UsabilityDefect defect : evaluationResult.getAllDefects()) {
86                System.err.println(defect);
87            }
88
89            fail("expected defect " + expectedDefect + " not found in evaluation result");
90        }
91    }
92
93    /**
94   *
95   */
96    private ITaskTreeNode parseTask(Matcher tokenMatcher, int[] typeNumbers) {
97        String firstToken = tokenMatcher.group();
98
99        if ("}".equals(firstToken)) {
100            throw new IllegalArgumentException("found a closing bracket at an unexpected place");
101        }
102
103        ITaskTreeNode treeNode = instantiateTreeNode(tokenMatcher, typeNumbers);
104
105        if (!tokenMatcher.find()) {
106            throw new IllegalArgumentException("could not parse task specification");
107        }
108
109        firstToken = tokenMatcher.group();
110
111        if (!"}".equals(firstToken)) {
112            ITaskTreeNode child = null;
113
114            do {
115                child = parseTask(tokenMatcher, typeNumbers);
116
117                if (child != null) {
118                    addChild(treeNode, child);
119
120                    if (!tokenMatcher.find()) {
121                        throw new IllegalArgumentException("could not parse task specification");
122                    }
123
124                    firstToken = tokenMatcher.group();
125
126                    if ("}".equals(firstToken)) {
127                        break;
128                    }
129                }
130
131            }
132            while (child != null);
133
134        }
135
136        return treeNode;
137    }
138
139    /**
140     * TODO: comment
141     *
142     * @param group
143     * @return
144     */
145    private ITaskTreeNode instantiateTreeNode(Matcher tokenMatcher, int[] typeNumbers) {
146        String type = tokenMatcher.group(2);
147        String additionalInfo = tokenMatcher.group(4);
148
149        if ("Interaction".equals(type)) {
150            return taskTreeNodeFactory.createNewEventTask
151                (new DummyInteraction("dummy", typeNumbers[0]++), new DummyGUIElement("dummy"));
152        }
153        else if ("Sequence".equals(type)) {
154            return taskTreeNodeFactory.createNewSequence();
155        }
156        else if ("Iteration".equals(type)) {
157            return taskTreeNodeFactory.createNewIteration();
158        }
159        else if ("Selection".equals(type)) {
160            return taskTreeNodeFactory.createNewSelection();
161        }
162        else if ("TextInput".equals(type)) {
163            if (additionalInfo == null) {
164                fail("no simulated text provided for text input interactin task");
165            }
166
167            ITextInputEventTask task =
168                taskTreeNodeFactory.createNewTextInputEventTask(new DummyTextField(additionalInfo));
169
170            task.setEnteredText(additionalInfo);
171            return task;
172        }
173        else {
174            fail("invalid type of task tree node: " + type);
175            return null;
176        }
177    }
178
179    /**
180     * TODO: comment
181     *
182     * @param treeNode
183     * @param child
184     */
185    private void addChild(ITaskTreeNode parent, ITaskTreeNode child) {
186        if (parent instanceof ISequence) {
187            taskTreeBuilder.addChild((ISequence) parent, child);
188        }
189        else if (parent instanceof IIteration) {
190            if (parent.getChildren().size() <= 0) {
191                taskTreeBuilder.setChild((IIteration) parent, child);
192            }
193            else {
194                fail("can not add more than one child to an iteration");
195            }
196        }
197        else if (parent instanceof ISelection) {
198            taskTreeBuilder.addChild((ISelection) parent, child);
199        }
200        else if (parent instanceof ITextInputEventTask) {
201            taskTreeBuilder.addChild((ITextInputEventTask) parent, child);
202        }
203        else {
204            fail("can not add children to parent task tree node of type " +
205                 parent.getClass().getName());
206        }
207    }
208
209}
Note: See TracBrowser for help on using the repository browser.