source: trunk/autoquest-generic-event-monitor-test/src/test/java/de/ugoe/cs/autoquest/genericeventmonitor/GenericEventMonitorTest.java @ 2164

Last change on this file since 2164 was 2164, checked in by pharms, 7 years ago
  • changes for first VR oriented usability evaluation
  • Property svn:mime-type set to text/plain
File size: 22.4 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.genericeventmonitor;
16
17import static org.junit.Assert.*;
18
19import java.io.File;
20
21import org.apache.http.HttpEntity;
22import org.apache.http.HttpResponse;
23import org.apache.http.client.methods.HttpPost;
24import org.apache.http.entity.ContentType;
25import org.apache.http.entity.StringEntity;
26import org.apache.http.impl.client.DefaultHttpClient;
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
30
31import de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitor;
32import de.ugoe.cs.util.console.TextConsole;
33
34/**
35 * 
36 * @author Patrick Harms
37 */
38public class GenericEventMonitorTest {
39
40    /**
41     *
42     */
43    public static final TextConsole CONSOLE = new TextConsole();
44   
45    /**
46     *
47     */
48    private final static String LOG_FILE_DIR = "target/tmp/logfiles/";
49   
50    /**
51     *
52     */
53    private static final int PORT = 19098;
54
55    /**
56     *
57     */
58    private GenericEventMonitor genericEventMonitor;
59
60    /**
61     *
62     */
63    @Before
64    public void setUp() throws Exception {
65        genericEventMonitor = new GenericEventMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
66        genericEventMonitor.init();
67        genericEventMonitor.start();
68    }
69
70    /**
71     *
72     */
73    @After
74    public void tearDown() throws Exception {
75        if (genericEventMonitor != null) {
76            try {
77                genericEventMonitor.stop();
78            }
79            finally {
80                genericEventMonitor = null;
81            }
82        }
83       
84        deleteFiles(new File(LOG_FILE_DIR));
85    }
86
87    /**
88     *
89     */
90    @Test
91    public void testOneSimpleMessage() throws Exception {
92        String clientId = "123";
93        String appId = "456";
94       
95        String message =
96            "{" +
97            "  \"message\": {" +
98            "    \"clientInfos\": {" +
99            "      \"clientId\":\"" + clientId + "\"," +
100            "      \"appId\":\"" + appId + "\"," +
101            "    }," +
102            "    \"targetStructure\": [{" +
103            "      \"targetId\":\"target1\"," +
104            "      \"param1\":\"value1\"," +
105            "      \"param3\":\"value3\"," +
106            "      \"param2\":\"value2\"," +
107            "      \"children\":" +
108            "      [ {" +
109            "          \"targetId\":\"target3\"," +
110            "          \"index\":\"0\"," +
111            "        }," +
112            "        {" +
113            "          \"targetId\":\"target2\"," +
114            "          \"htmlId\":\"gsr\"," +
115            "        }" +
116            "      ]" +
117            "    }]," +
118            "    \"events\":" +
119            "    [ {" +
120            "        \"time\":\"12345\"," +
121            "        \"targetId\":\"target2\"," +
122            "        \"type\":\"gaze\"," +
123            "        \"xcoordinate\": \"194\"," +
124            "        \"ycoordinate\": \"12\"" +
125            "      }" +
126            "    ]" +
127            "  }" +
128            "}";
129
130        sendMessageAndAssertResponse(message);
131       
132        genericEventMonitor.stop();
133        genericEventMonitor = null;
134
135        File logFile = new File(LOG_FILE_DIR + File.separator + appId + File.separator +
136                                clientId + File.separator + "genericevents_" + clientId + "_000.log");
137       
138        assertTrue(logFile.exists());
139       
140        /*HTMLLogParser parser = new HTMLLogParser(null);
141       
142        parser.parseFile(logFile);
143       
144        // check the GUI model
145        GUIModel guiModel = parser.getGuiModel();
146        assertNotNull(guiModel);
147       
148        List<IGUIElement> nodes = guiModel.getRootElements();
149        assertNotNull(nodes);
150        assertEquals(1, nodes.size());
151       
152        // get server node
153        IGUIElement node = nodes.get(0);
154        assertNotNull(node);
155        assertTrue(node instanceof HTMLServer);
156        assertEquals("HTML", node.getPlatform());
157        assertFalse(node.isUsed());
158       
159        nodes = guiModel.getChildren(node);
160        assertNotNull(nodes);
161        assertEquals(1, nodes.size());
162       
163        // get document node
164        node = nodes.get(0);
165        assertNotNull(node);
166        assertTrue(node instanceof HTMLDocument);
167        assertEquals("HTML", node.getPlatform());
168        assertFalse(node.isUsed());
169       
170        nodes = guiModel.getChildren(node);
171        assertNotNull(nodes);
172        assertEquals(1, nodes.size());
173       
174        // get html node
175        node = nodes.get(0);
176        assertNotNull(node);
177        assertTrue(node instanceof HTMLPageElement);
178        assertEquals("HTML", node.getPlatform());
179        assertFalse(node.isUsed());
180       
181        nodes = guiModel.getChildren(node);
182        assertNotNull(nodes);
183        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
184       
185        // get body node
186        node = nodes.get(0);
187        assertNotNull(node);
188        assertTrue(node instanceof HTMLPageElement);
189        assertEquals("HTML", node.getPlatform());
190        assertTrue(node.isUsed());
191
192        nodes = guiModel.getChildren(node);
193        assertNotNull(nodes);
194        assertEquals(0, nodes.size());
195       
196        // check the sequences
197        Collection<List<Event>> sequences = parser.getSequences();
198       
199        assertNotNull(sequences);
200       
201        Iterator<List<Event>> iterator = sequences.iterator();
202        assertTrue(iterator.hasNext());
203       
204        List<Event> sequence = iterator.next();
205        assertFalse(iterator.hasNext());
206       
207        assertNotNull(sequence);
208        assertEquals(1, sequence.size());
209       
210        assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);*/
211    }
212
213    /**
214     *
215     */
216    @Test
217    public void testSeveralMessagesInOneSession() throws Exception {
218        String clientId = "123";
219        String appId = "456";
220       
221        String message =
222            "{" +
223            "  \"message\": {" +
224            "    \"clientInfos\": {" +
225            "      \"clientId\":\"" + clientId + "\"," +
226            "      \"appId\":\"" + appId + "\"," +
227            "    }," +
228            "    \"targetStructure\": [{" +
229            "      \"targetId\":\"html\"," +
230            "      \"index\":\"0\"," +
231            "      \"children\":" +
232            "      [ {" +
233            "          \"targetId\":\"head\"," +
234            "          \"index\":\"0\"," +
235            "        }," +
236            "        {" +
237            "          \"tagName\":\"body\"," +
238            "          \"targetId\":\"gsr\"," +
239            "          \"children\":" +
240            "          [ {" +
241            "              \"tagName\":\"input_button\"," +
242            "              \"targetId\":\"input1\"," +
243            "            }," +
244            "            {" +
245            "              \"tagName\":\"input_button\"," +
246            "              \"targetId\":\"input2\"," +
247            "            }," +
248            "            {" +
249            "              \"tagName\":\"input_button\"," +
250            "              \"targetId\":\"input3\"," +
251            "            }," +
252            "            {" +
253            "              \"tagName\":\"input_button\"," +
254            "              \"targetId\":\"input4\"," +
255            "            }," +
256            "            {" +
257            "              \"tagName\":\"input_button\"," +
258            "              \"targetId\":\"input5\"," +
259            "            }," +
260            "            {" +
261            "              \"tagName\":\"input_button\"," +
262            "              \"targetId\":\"input6\"," +
263            "            }," +
264            "            {" +
265            "              \"tagName\":\"input_button\"," +
266            "              \"targetId\":\"input7\"," +
267            "            }," +
268            "            {" +
269            "              \"tagName\":\"input_button\"," +
270            "              \"targetId\":\"input8\"," +
271            "            }," +
272            "            {" +
273            "              \"tagName\":\"input_button\"," +
274            "              \"targetId\":\"input9\"," +
275            "            }," +
276            "            {" +
277            "              \"tagName\":\"input_button\"," +
278            "              \"targetId\":\"input10\"," +
279            "            }," +
280            "          ]" +
281            "        }" +
282            "      ]" +
283            "    }]," +
284            "    \"events\":" +
285            "    [ {" +
286            "        \"time\":\"1\"," +
287            "        \"targetId\":\"input1\"," +
288            "        \"type\":\"onclick\"," +
289            "        \"coordinates\": \"194\"" +
290            "      }," +
291            "      {" +
292            "        \"time\":\"2\"," +
293            "        \"targetId\":\"input2\"," +
294            "        \"type\":\"ondblclick\"," +
295            "        \"coordinates\": \"194\"" +
296            "      }," +
297            "      {" +
298            "        \"time\":\"3\"," +
299            "        \"targetId\":\"input3\"," +
300            "        \"type\":\"onfocus\"" +
301            "      }," +
302            "      {" +
303            "        \"time\":\"4\"," +
304            "        \"targetId\":\"input4\"," +
305            "        \"type\":\"onclick\"," +
306            "        \"coordinates\": \"255\"" +
307            "      }," +
308            "      {" +
309            "        \"time\":\"5\"," +
310            "        \"targetId\":\"input5\"," +
311            "        \"type\":\"onfocus\"" +
312            "      }," +
313            "      {" +
314            "        \"time\":\"6\"," +
315            "        \"targetId\":\"input6\"," +
316            "        \"type\":\"onfocus\"" +
317            "      }," +
318            "      {" +
319            "        \"time\":\"7\"," +
320            "        \"targetId\":\"input7\"," +
321            "        \"type\":\"onfocus\"" +
322            "      }," +
323            "      {" +
324            "        \"time\":\"8\"," +
325            "        \"targetId\":\"input8\"," +
326            "        \"type\":\"onclick\"," +
327            "        \"coordinates\": \"255\"" +
328            "      }," +
329            "      {" +
330            "        \"time\":\"9\"," +
331            "        \"targetId\":\"input9\"," +
332            "        \"type\":\"onscroll\"," +
333            "        \"scrollPosition\": \"194\"" +
334            "      }," +
335            "      {" +
336            "        \"time\":\"10\"," +
337            "        \"targetId\":\"input10\"," +
338            "        \"type\":\"onclick\"," +
339            "        \"coordinates\": \"194\"" +
340            "      }" +
341            "    ]" +
342            "  }" +
343            "}";
344 
345        sendMessageAndAssertResponse(message);
346       
347        genericEventMonitor.stop();
348        genericEventMonitor = null;
349
350        File logFile = new File(LOG_FILE_DIR + File.separator + appId + File.separator +
351                                clientId + File.separator + "genericevents_" + clientId + "_000.log");
352       
353        assertTrue(logFile.exists());
354       
355        /*HTMLLogParser parser = new HTMLLogParser(null);
356       
357        parser.parseFile(logFile);
358       
359        // check the GUI model
360        GUIModel guiModel = parser.getGuiModel();
361        assertNotNull(guiModel);
362       
363        List<IGUIElement> nodes = guiModel.getRootElements();
364        assertNotNull(nodes);
365        assertEquals(1, nodes.size());
366       
367        // get server node
368        IGUIElement node = nodes.get(0);
369        assertNotNull(node);
370        assertTrue(node instanceof HTMLServer);
371        assertEquals("HTML", node.getPlatform());
372        assertFalse(node.isUsed());
373       
374        nodes = guiModel.getChildren(node);
375        assertNotNull(nodes);
376        assertEquals(1, nodes.size());
377       
378        // get document node
379        node = nodes.get(0);
380        assertNotNull(node);
381        assertTrue(node instanceof HTMLDocument);
382        assertEquals("HTML", node.getPlatform());
383        assertFalse(node.isUsed());
384       
385        nodes = guiModel.getChildren(node);
386        assertNotNull(nodes);
387        assertEquals(1, nodes.size());
388       
389        // get html node
390        node = nodes.get(0);
391        assertNotNull(node);
392        assertTrue(node instanceof HTMLPageElement);
393        assertEquals("HTML", node.getPlatform());
394        assertFalse(node.isUsed());
395       
396        nodes = guiModel.getChildren(node);
397        assertNotNull(nodes);
398        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
399       
400        // get body node
401        IGUIElement body = nodes.get(0);
402        assertNotNull(body);
403        assertTrue(body instanceof HTMLPageElement);
404        assertEquals("HTML", body.getPlatform());
405        assertTrue(body.isUsed());
406
407        nodes = guiModel.getChildren(body);
408        assertNotNull(nodes);
409       
410        // wait for all 10 GUI elements on the same page to be logged although only 9 are used
411        assertEquals(10, nodes.size());
412       
413        Map<String, IGUIElement> inputs = new HashMap<>();
414       
415        // get input nodes
416        for (int i = 0; i < nodes.size(); i++) {
417            node = nodes.get(i);
418            assertNotNull(node);
419            assertTrue(node instanceof HTMLPageElement);
420            assertEquals("HTML", node.getPlatform());
421           
422            if (!"input9".equals(((HTMLPageElement) node).getHtmlId())) {
423                assertTrue(node.isUsed());
424            }
425            else {
426                assertFalse(node.isUsed());
427            }
428           
429            inputs.put(((HTMLPageElement) node).getHtmlId(), node);
430
431            assertNotNull(guiModel.getChildren(node));
432            assertEquals(0, guiModel.getChildren(node).size());
433        }
434       
435        // check the sequences
436        Collection<List<Event>> sequences = parser.getSequences();
437       
438        assertNotNull(sequences);
439       
440        Iterator<List<Event>> iterator = sequences.iterator();
441        assertTrue(iterator.hasNext());
442       
443        List<Event> sequence = iterator.next();
444        assertFalse(iterator.hasNext());
445       
446        assertNotNull(sequence);
447        assertEquals(10, sequence.size());
448       
449        assertEvent(sequence.get(0), 1, MouseClick.class, inputs.get("input1"), 194, 7);
450        assertEvent(sequence.get(1), 2, MouseDoubleClick.class, inputs.get("input2"), 194, 7);
451        assertEvent(sequence.get(2), 3, KeyboardFocusChange.class, inputs.get("input3"), 0, 0);
452        assertEvent(sequence.get(3), 4, MouseClick.class, inputs.get("input4"), 125, 14);
453        assertEvent(sequence.get(4), 5, KeyboardFocusChange.class, inputs.get("input5"), 0, 0);
454        assertEvent(sequence.get(5), 6, KeyboardFocusChange.class, inputs.get("input6"), 0, 0);
455        assertEvent(sequence.get(6), 7, KeyboardFocusChange.class, inputs.get("input7"), 0, 0);
456        assertEvent(sequence.get(7), 8, MouseClick.class, inputs.get("input8"), 255, 4);
457        assertEvent(sequence.get(8), 9, Scroll.class, body, 0, 0);
458        assertEvent(sequence.get(9), 10, MouseClick.class, inputs.get("input10"), 516, 154);*/
459
460    }
461
462    /**
463     *
464     */
465    @Test
466    public void testSeveralSessions() throws Exception {
467        String clientId = "123";
468       
469        String message =
470            "{" +
471            "  \"message\": {" +
472            "    \"clientInfos\": {" +
473            "      \"clientId\":\"" + clientId + "\"," +
474            "      \"userAgent\":\"Agent\"," +
475            "      \"title\":\"Title\"," +
476            "      \"url\":\"http://host/path\"" +
477            "    }," +
478            "    \"guiModel\": {" +
479            "      \"tagName\":\"html\"," +
480            "      \"index\":\"0\"," +
481            "      \"children\":" +
482            "      [ {" +
483            "          \"tagName\":\"head\"," +
484            "          \"index\":\"0\"," +
485            "        }," +
486            "        {" +
487            "          \"tagName\":\"body\"," +
488            "          \"htmlId\":\"gsr\"," +
489            "        }" +
490            "      ]" +
491            "    }," +
492            "    \"events\":" +
493            "    [ {" +
494            "        \"time\":\"12345\"," +
495            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
496            "        \"type\":\"onclick\"" +
497            "        \"coordinates\": [\"194\", \"7\"]" +
498            "      }" +
499            "    ]" +
500            "  }" +
501            "}";
502
503        sendMessageAndAssertResponse(message);
504       
505        int numberOfSessions = 10;
506        for (int i = 0; i < numberOfSessions; i++) {
507            genericEventMonitor.stop();
508            genericEventMonitor =
509                new GenericEventMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
510           
511            genericEventMonitor.init();
512            genericEventMonitor.start();
513            sendMessageAndAssertResponse(message);
514        }
515       
516        genericEventMonitor.stop();
517        genericEventMonitor = null;
518       
519        /*HTMLLogParser parser = new HTMLLogParser(null);
520       
521        // assert 9 already rotated log files
522        for (int i = 0; i < numberOfSessions; i++) {
523            File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
524                                    clientId + File.separator + "htmlmonitor_" + clientId + "_00" +
525                                    i + ".log");
526       
527            assertTrue(logFile.exists());
528       
529            parser.parseFile(logFile);
530        }
531
532        // check the GUI model
533        GUIModel guiModel = parser.getGuiModel();
534        assertNotNull(guiModel);
535       
536        List<IGUIElement> nodes = guiModel.getRootElements();
537        assertNotNull(nodes);
538        assertEquals(1, nodes.size());
539       
540        // get server node
541        IGUIElement node = nodes.get(0);
542        assertNotNull(node);
543        assertTrue(node instanceof HTMLServer);
544        assertEquals("HTML", node.getPlatform());
545        assertFalse(node.isUsed());
546       
547        nodes = guiModel.getChildren(node);
548        assertNotNull(nodes);
549        assertEquals(1, nodes.size());
550       
551        // get document node
552        node = nodes.get(0);
553        assertNotNull(node);
554        assertTrue(node instanceof HTMLDocument);
555        assertEquals("HTML", node.getPlatform());
556        assertFalse(node.isUsed());
557       
558        nodes = guiModel.getChildren(node);
559        assertNotNull(nodes);
560        assertEquals(1, nodes.size());
561       
562        // get html node
563        node = nodes.get(0);
564        assertNotNull(node);
565        assertTrue(node instanceof HTMLPageElement);
566        assertEquals("HTML", node.getPlatform());
567        assertFalse(node.isUsed());
568       
569        nodes = guiModel.getChildren(node);
570        assertNotNull(nodes);
571        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
572       
573        // get body node
574        node = nodes.get(0);
575        assertNotNull(node);
576        assertTrue(node instanceof HTMLPageElement);
577        assertEquals("HTML", node.getPlatform());
578        assertTrue(node.isUsed());
579
580        nodes = guiModel.getChildren(node);
581        assertNotNull(nodes);
582        assertEquals(0, nodes.size());
583       
584        // check the sequences
585        Collection<List<Event>> sequences = parser.getSequences();
586       
587        assertNotNull(sequences);
588        assertEquals(numberOfSessions, sequences.size());
589       
590        Iterator<List<Event>> iterator = sequences.iterator();
591       
592        while (iterator.hasNext()) {
593            List<Event> sequence = iterator.next();
594       
595            assertNotNull(sequence);
596            assertEquals(1, sequence.size());
597       
598            assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);
599        }*/
600    }
601
602    /**
603     *
604     */
605    private void sendMessageAndAssertResponse(String message) throws Exception {
606        DefaultHttpClient httpclient = new DefaultHttpClient();
607        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
608        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
609        httpPost.setEntity(entity);
610       
611        try {
612            HttpResponse response = httpclient.execute(httpPost);
613           
614            // the monitor always returns 200 without any additional information. The client must
615            // never get more or less information. This is especially important for preventing
616            // hackers from finding out more
617            assertEquals(200, response.getStatusLine().getStatusCode());
618            assertTrue
619                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) ||
620                 ((response.getEntity().getContentLength() == 1) &&
621                  (response.getEntity().getContent().read() == ' ')));
622        }
623        finally {
624            httpPost.releaseConnection();
625        }
626    }
627
628    /**
629     *
630     */
631    /*private void assertEvent(Event                       event,
632                             int                         timestamp,
633                             Class<? extends IEventType> eventType,
634                             IGUIElement                 eventTarget,
635                             int                         xCoordinate,
636                             int                         yCoordinate)
637    {
638        assertEquals(timestamp, event.getTimestamp());
639        assertTrue(eventType.isInstance(event.getType()));
640        assertEquals(eventTarget, event.getTarget());
641       
642        if (event.getType() instanceof MouseButtonInteraction) {
643            assertEquals(xCoordinate, ((MouseButtonInteraction) event.getType()).getX());
644            assertEquals(yCoordinate, ((MouseButtonInteraction) event.getType()).getY());
645        }
646    }
647
648    /**
649     *
650     */
651    private void deleteFiles(File file) {
652        if (file.exists()) {
653            if (file.isDirectory()) {
654                for (File child : file.listFiles()) {
655                    deleteFiles(child);
656                }
657            }
658           
659            try {
660                file.delete();
661            }
662            catch (Exception e) {
663                // ignore and delete as much as possible
664            }
665        }
666    }
667
668}
Note: See TracBrowser for help on using the repository browser.