source: trunk/autoquest-plugin-mfc-test/src/test/java/de/ugoe/cs/autoquest/plugin/mfc/EventGenerationRuleTest.java

Last change on this file was 2233, checked in by pharms, 7 years ago
  • solved some findbugs issues
  • Property svn:keywords set to Date Revision Author HeadURL Id
File size: 17.6 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.mfc;
16
17import static org.junit.Assert.*;
18
19import java.io.ByteArrayInputStream;
20import java.io.IOException;
21import java.util.List;
22
23import org.jdom.Document;
24import org.jdom.Element;
25import org.jdom.JDOMException;
26import org.jdom.Namespace;
27import org.jdom.input.SAXBuilder;
28import org.junit.Test;
29
30import de.ugoe.cs.autoquest.plugin.mfc.EventGenerationRule;
31import de.ugoe.cs.autoquest.plugin.mfc.EventGenerationRule.Term;
32import de.ugoe.cs.autoquest.plugin.mfc.eventcore.WindowsMessageType;
33import de.ugoe.cs.util.console.Console;
34
35/**
36 * @author Patrick Harms
37 */
38public class EventGenerationRuleTest {
39
40    /**
41     *
42     */
43    Namespace rulesNamespace = null;
44   
45    /**
46     *
47     */
48    @Test
49    public void testRule1() {
50        String ruleXML =
51            "<rule name='LeftClickButton'>\n" +
52            "  <msg type='&WM_LBUTTONDOWN;'>\n" +
53            "    <store var='clicked'/>\n" +
54            "  </msg>\n" +
55            "  <msg type='&WM_LBUTTONUP;'>\n" +
56            "    <equals>\n" +
57            "      <constValue value='Button'/>\n" +
58            "      <winInfoValue obj='this' winParam='class'/>\n" +
59            "    </equals>\n" +
60            "    <equals>\n" +
61            "      <paramValue obj='clicked' param='window.hwnd'/>\n" +
62            "      <paramValue obj='this' param='window.hwnd'/>\n" +
63            "    </equals>\n" +
64            "  </msg>\n" +
65            "  <genMsg delay='500'>\n" +
66            "    <type>\n" +
67            "      <constValue value='&BM_CLICK;'/>\n" +
68            "    </type>\n" +
69            "    <target>\n" +
70            "      <msgInfoValue obj='clicked' msgParam='target'/>\n" +
71            "    </target>\n" +
72            "  </genMsg>\n" +
73            "</rule>";
74       
75        Element ruleElement = getRuleElement(ruleXML);
76       
77        EventGenerationRule rule = new EventGenerationRule(ruleElement, rulesNamespace);
78       
79        assertEquals("LeftClickButton", rule.getName());
80       
81        assertNotNull(rule.getMessageConditions());
82        assertEquals(2, rule.getMessageConditions().size());
83       
84        // check condition 1
85        EventGenerationRule.MessageCondition condition1 = rule.getMessageConditions().get(0);
86        assertEquals(WindowsMessageType.WM_LBUTTONDOWN, condition1.getMessageType());
87        assertFalse(condition1.matchMultiple());
88        assertNotNull(condition1.getAttributeConditions());
89        assertEquals(0, condition1.getAttributeConditions().size());
90        assertNotNull(condition1.getMessagesToStore());
91        assertEquals(1, condition1.getMessagesToStore().size());
92       
93        assertTerm(condition1.getMessagesToStore().get(0), "store", "clicked", null, null,
94                   null, null, null, null, null, null);
95
96        // check condition 2
97        EventGenerationRule.MessageCondition condition2 = rule.getMessageConditions().get(1);
98        assertEquals(WindowsMessageType.WM_LBUTTONUP, condition2.getMessageType());
99        assertFalse(condition2.matchMultiple());
100        assertNotNull(condition2.getAttributeConditions());
101        assertEquals(2, condition2.getAttributeConditions().size());
102        assertNotNull(condition2.getMessagesToStore());
103        assertEquals(0, condition2.getMessagesToStore().size());
104       
105        EventGenerationRule.AttributeCondition attrCond1 =
106            condition2.getAttributeConditions().get(0);
107       
108        assertTerm(attrCond1.getLeftHandSide(), "constValue", null, null, null,
109                   null, null, null, null, "Button", null);
110
111        assertTerm(attrCond1.getRightHandSide(), "winInfoValue", "this", null, null,
112                   null, null, null, null, null, "class");
113
114        EventGenerationRule.AttributeCondition attrCond2 =
115            condition2.getAttributeConditions().get(1);
116           
117        assertTerm(attrCond2.getLeftHandSide(), "paramValue", "clicked", null, "window.hwnd",
118                   null, null, null, null, null, null);
119
120        assertTerm(attrCond2.getRightHandSide(), "paramValue", "this", null, "window.hwnd",
121                   null, null, null, null, null, null);
122
123        // check event parameters
124        assertNotNull(rule.getEventParameters());
125        assertEquals(0, rule.getEventParameters().size());
126       
127        // check replay specs
128        assertNotNull(rule.getReplayMessageSpecifications());
129        assertEquals(1, rule.getReplayMessageSpecifications().size());
130       
131        // check replay spec 1
132        EventGenerationRule.ReplayMessageSpec replaySpec =
133            rule.getReplayMessageSpecifications().get(0);
134       
135        assertNotNull(replaySpec);
136        assertEquals(500, replaySpec.getDelay());
137        assertTrue(replaySpec.generateSingleMessage());
138        assertNull(replaySpec.getLparam());
139        assertNull(replaySpec.getLparamHiWord());
140        assertNull(replaySpec.getLparamLoWord());
141        assertNull(replaySpec.getReplayObjectId());
142        assertTerm(replaySpec.getTarget(), "msgInfoValue", "clicked", "target", null,
143                   null, null, null, null, null, null);
144        assertTerm(replaySpec.getType(), "constValue", null, null, null,
145                   null, null, null, null, "" + WindowsMessageType.BM_CLICK.getNumber(), null);
146        assertNull(replaySpec.getWparam());
147        assertNull(replaySpec.getWparamHiWord());
148        assertNull(replaySpec.getWparamLoWord());
149       
150    }
151
152    /**
153     *
154     */
155    @Test
156    public void testRule2() {
157       
158        String ruleXML =
159            "<rule name='HScroll_TrackBar'>\n" +
160            "  <msg type='&WM_LBUTTONDOWN;'>\n" +
161            "    <equals>\n" +
162            "      <winInfoValue obj='this' winParam='class'/>\n" +
163            "      <constValue value='msctls_trackbar32'/>\n" +
164            "    </equals>\n" +
165            "    <store var='clicked'/>\n" +
166            "  </msg>\n" +
167            "  <msg type='&WM_HSCROLL;' multiple='true'>\n" +
168            "    <equals>\n" +
169            "      <paramValue obj='this' param='scrollBarHandle'/>\n" +
170            "      <paramValue obj='clicked' param='window.hwnd'/>\n" +
171            "    </equals>\n" +
172            "    <storeSeq varSeq='scrolls'>\n" +
173            "      <resolveHwnd param='scrollBarHandle' storeParam='scrollBarTarget'/>\n" +
174            "    </storeSeq>\n" +
175            "  </msg>\n" +
176            "  <msg type='&WM_LBUTTONUP;'>\n" +
177            "    <equals>\n" +
178            "      <paramValue obj='this' param='window.hwnd'/>\n" +
179            "      <paramValue obj='clicked' param='window.hwnd'/>\n" +
180            "    </equals>\n" +
181            "  </msg>\n" +
182            "  <genMsgSeq delay='50'>\n" +
183            "    <type>\n" +
184            "      <constValue value='&TBM_SETPOS;'/>\n" +
185            "    </type>\n" +
186            "    <target>\n" +
187            "      <seqValue seqObj='scrolls' param='scrollBarTarget'/>\n" +
188            "    </target>\n" +
189            "    <LPARAM>\n" +
190            "      <seqValue seqObj='scrolls' param='scrollPos'/>\n" +
191            "    </LPARAM>\n" +
192            "    <WPARAM>\n" +
193            "      <constValue value='1'/>\n" +
194            "    </WPARAM>\n" +
195            "  </genMsgSeq>\n" +
196            "</rule>\n";
197       
198        Element ruleElement = getRuleElement(ruleXML);
199       
200        EventGenerationRule rule = new EventGenerationRule(ruleElement, rulesNamespace);
201       
202        assertEquals("HScroll_TrackBar", rule.getName());
203       
204        assertNotNull(rule.getMessageConditions());
205        assertEquals(3, rule.getMessageConditions().size());
206       
207        // check condition 1
208        EventGenerationRule.MessageCondition condition1 = rule.getMessageConditions().get(0);
209        assertEquals(WindowsMessageType.WM_LBUTTONDOWN, condition1.getMessageType());
210        assertFalse(condition1.matchMultiple());
211        assertNotNull(condition1.getAttributeConditions());
212        assertEquals(1, condition1.getAttributeConditions().size());
213        EventGenerationRule.AttributeCondition attrCond1 =
214            condition1.getAttributeConditions().get(0);
215           
216        assertTerm(attrCond1.getLeftHandSide(), "winInfoValue", "this", null, null,
217                   null, null, null, null, null, "class");
218
219        assertTerm(attrCond1.getRightHandSide(), "constValue", null, null, null,
220                   null, null, null, null, "msctls_trackbar32", null);
221
222        assertNotNull(condition1.getMessagesToStore());
223        assertEquals(1, condition1.getMessagesToStore().size());
224       
225        assertTerm(condition1.getMessagesToStore().get(0), "store", "clicked", null, null,
226                   null, null, null, null, null, null);
227
228        // check condition 2
229        EventGenerationRule.MessageCondition condition2 = rule.getMessageConditions().get(1);
230        assertEquals(WindowsMessageType.WM_HSCROLL, condition2.getMessageType());
231        assertTrue(condition2.matchMultiple());
232        assertNotNull(condition2.getAttributeConditions());
233        assertEquals(1, condition2.getAttributeConditions().size());
234       
235        EventGenerationRule.AttributeCondition attrCond2 =
236                condition2.getAttributeConditions().get(0);
237       
238        assertTerm(attrCond2.getLeftHandSide(), "paramValue", "this", null, "scrollBarHandle",
239                   null, null, null, null, null, null);
240
241        assertTerm(attrCond2.getRightHandSide(), "paramValue", "clicked", null, "window.hwnd",
242                   null, null, null, null, null, null);
243
244
245        assertNotNull(condition2.getMessagesToStore());
246        assertEquals(1, condition2.getMessagesToStore().size());
247       
248        String[][] resolveHwnd = { { "scrollBarHandle",  "scrollBarTarget" } };
249        assertTerm(condition2.getMessagesToStore().get(0), "storeSeq", null, null, null,
250                   resolveHwnd, "scrolls", null, null, null, null);
251
252        // check condition 3
253        EventGenerationRule.MessageCondition condition3 = rule.getMessageConditions().get(2);
254        assertEquals(WindowsMessageType.WM_LBUTTONUP, condition3.getMessageType());
255        assertFalse(condition3.matchMultiple());
256        assertNotNull(condition3.getAttributeConditions());
257        assertEquals(1, condition3.getAttributeConditions().size());
258        assertNotNull(condition3.getMessagesToStore());
259        assertEquals(0, condition3.getMessagesToStore().size());
260       
261        EventGenerationRule.AttributeCondition attrCond3 =
262            condition3.getAttributeConditions().get(0);
263       
264        assertTerm(attrCond3.getLeftHandSide(), "paramValue", "this", null, "window.hwnd",
265                   null, null, null, null, null, null);
266
267        assertTerm(attrCond3.getRightHandSide(), "paramValue", "clicked", null, "window.hwnd",
268                   null, null, null, null, null, null);
269
270        // check event parameters
271        assertNotNull(rule.getEventParameters());
272        assertEquals(0, rule.getEventParameters().size());
273       
274        // check replay specs
275        assertNotNull(rule.getReplayMessageSpecifications());
276        assertEquals(1, rule.getReplayMessageSpecifications().size());
277       
278        // check replay spec 1
279        EventGenerationRule.ReplayMessageSpec replaySpec =
280            rule.getReplayMessageSpecifications().get(0);
281       
282        assertNotNull(replaySpec);
283        assertEquals(50, replaySpec.getDelay());
284        assertFalse(replaySpec.generateSingleMessage());
285        assertTerm(replaySpec.getLparam(), "seqValue", null, null, null,
286                   null, "scrolls", "scrollPos", null, null, null);
287        assertNull(replaySpec.getLparamHiWord());
288        assertNull(replaySpec.getLparamLoWord());
289        assertEquals("scrolls", replaySpec.getReplayObjectId());
290        assertTerm(replaySpec.getTarget(), "seqValue", null, null, null,
291                   null, "scrolls", "scrollBarTarget", null, null, null);
292        assertTerm(replaySpec.getType(), "constValue", null, null, null,
293                   null, null, null, null, "" + WindowsMessageType.TBM_SETPOS.getNumber(), null);
294        assertTerm(replaySpec.getWparam(), "constValue", null, null, null,
295                   null, null, null, null, "1", null);
296        assertNull(replaySpec.getWparamHiWord());
297        assertNull(replaySpec.getWparamLoWord());
298       
299    }
300   
301    /**
302     *
303     */
304    private void assertTerm(Term       term,
305                            String     name,
306                            String     messageId,
307                            String     messageInfoName,
308                            String     messageParameterName,
309                            String[][] resolveHandles,
310                            String     sequenceId,
311                            String     sequenceParameterName,
312                            String     storeParameterName,
313                            String     value,
314                            String     windowParameterName)
315    {
316        assertNotNull(term);
317       
318        if (name != null) {
319            assertEquals("name does not match", name, term.getName());
320        }
321        else {
322            assertNull("name is not null", term.getName());
323        }
324       
325        if (messageId != null) {
326            assertEquals("messageId does not match", messageId, term.getMessageId());
327        }
328        else {
329            assertNull("messageId is not null", term.getMessageId());
330        }
331       
332        if (messageInfoName != null) {
333            assertEquals
334                ("messageInfoName does not match", messageInfoName, term.getMessageInfoName());
335        }
336        else {
337            assertNull("messageInfoName is not null", term.getMessageInfoName());
338        }
339
340        if (messageParameterName != null) {
341            assertEquals("messageParameterName does not match", messageParameterName,
342                         term.getMessageParameterName());
343        }
344        else {
345            assertNull("messageParameterName is not null", term.getMessageParameterName());
346        }
347       
348        if (resolveHandles != null) {
349            assertEquals("resolveHandles does not match", resolveHandles.length,
350                         term.getResolveHandles().size());
351           
352            for (int i = 0; i < resolveHandles.length; i++) {
353                assertTerm(term.getResolveHandles().get(i), "resolveHwnd", null, null,
354                           resolveHandles[i][0], null, null, null, resolveHandles[i][1], null, null);
355            }
356        }
357        else {
358            assertNull("resolveHandles is not null", term.getResolveHandles());
359        }
360       
361        if (sequenceId != null) {
362            assertEquals("sequenceId does not match", sequenceId, term.getSequenceId());
363        }
364        else {
365            assertNull("sequenceId is not null", term.getSequenceId());
366        }
367       
368        if (sequenceParameterName != null) {
369            assertEquals("sequenceParameter does not match", sequenceParameterName,
370                         term.getSequenceParameterName());
371        }
372        else {
373            assertNull("sequenceParameter is not null", term.getSequenceParameterName());
374        }
375       
376        if (storeParameterName != null) {
377            assertEquals("storeParameterName does not match",
378                         storeParameterName, term.getStoreParameterName());
379        }
380        else {
381            assertNull("storeParameterName is not null", term.getStoreParameterName());
382        }
383       
384        if (value != null) {
385            assertEquals("value does not match", value, term.getValue());
386        }
387        else {
388            assertNull("value is not null", term.getValue());
389        }
390       
391        if (windowParameterName != null) {
392            assertEquals("windowParameterName does not match", windowParameterName,
393                         term.getWindowParameterName());
394        }
395        else {
396            assertNull("windowParameterName is not null", term.getWindowParameterName());
397        }
398    }
399
400    /**
401     *
402     */
403    private Element getRuleElement(String ruleXML) {
404        String fullXML =
405            "<?xml version='1.0' encoding='utf-8'?>\n" +
406            "<!DOCTYPE rules SYSTEM 'data/ruleDoctype.dtd'>\n" +
407            "<rules xmlns='ul:rules' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
408            "                        xsi:schemaLocation='ul:rules ruleSchema.xsd'>\n";
409       
410        fullXML += ruleXML;
411        fullXML += "\n</rules>\n";
412
413        SAXBuilder builder = new SAXBuilder();
414        Document doc = null;
415
416        try {
417            ByteArrayInputStream in = new ByteArrayInputStream(fullXML.getBytes("UTF-8"));
418            doc = builder.build(in);
419            rulesNamespace = Namespace.getNamespace("ul:rules");
420        }
421        catch (JDOMException e) {
422            Console.printerrln("Invalid rules file.");
423            e.printStackTrace();
424            return null;
425        }
426        catch (IOException e) {
427            Console.printerrln("Invalid rules file.");
428            e.printStackTrace();
429            return null;
430        }
431
432        Element rulesRoot = doc.getRootElement();
433       
434        @SuppressWarnings("unchecked")
435        List<Element> ruleElements = rulesRoot.getChildren("rule", rulesNamespace);
436       
437        assertEquals(1, ruleElements.size());
438
439        return ruleElements.get(0);
440    }
441
442}
Note: See TracBrowser for help on using the repository browser.