01: /*
02: * This file is not part of the ItsNat framework.
03: *
04: * Original source code use and closed source derivatives are authorized
05: * to third parties with no restriction or fee.
06: * The original source code is owned by the author.
07: *
08: * This program is distributed AS IS in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * Author: Jose Maria Arranz Santamaria
13: * (C) Innowhere Software Services S.L., Spanish company, year 2007
14: */
15:
16: package org.itsnat.feashow.features.components.functest;
17:
18: import org.itsnat.core.ItsNatDocument;
19: import org.w3c.dom.Element;
20: import org.w3c.dom.events.DocumentEvent;
21: import org.w3c.dom.events.Event;
22: import org.w3c.dom.events.EventTarget;
23: import org.w3c.dom.events.MouseEvent;
24: import org.w3c.dom.views.AbstractView;
25: import org.w3c.dom.views.DocumentView;
26:
27: public class FuncTestUsingBrowserUtil {
28: protected ItsNatDocument itsNatDoc;
29:
30: public FuncTestUsingBrowserUtil(ItsNatDocument itsNatDoc) {
31: this .itsNatDoc = itsNatDoc;
32: }
33:
34: public void sendHTMLEvent(Element elem, String type) {
35: Event event;
36:
37: synchronized (itsNatDoc) {
38: event = createHTMLEvent(type);
39: }
40:
41: dispatchEvent(elem, event);
42: }
43:
44: public void clickElement(Element elem) {
45: sendMouseEvent(elem, "click");
46: }
47:
48: public void sendMouseEvent(Element elem, String type) {
49: sendMouseEvent(elem, type, false);
50: }
51:
52: public void sendMouseEvent(Element elem, String type,
53: boolean shiftKey) {
54: Event event;
55: synchronized (itsNatDoc) {
56: event = createMouseEvent(type, shiftKey);
57: }
58:
59: dispatchEvent(elem, event);
60: }
61:
62: private void dispatchEvent(Element elem, Event event) {
63: EventTarget target;
64: synchronized (itsNatDoc) {
65: target = (EventTarget) itsNatDoc.getItsNatNode(elem);
66: }
67:
68: target.dispatchEvent(event);
69: try {
70: Thread.currentThread().sleep(200);
71: } catch (InterruptedException ex) {
72: throw new RuntimeException(ex);
73: }
74: }
75:
76: private MouseEvent createMouseEvent(String type, boolean shiftKey) {
77: DocumentEvent docEvent = (DocumentEvent) itsNatDoc;
78: MouseEvent event = (MouseEvent) docEvent
79: .createEvent("MouseEvents");
80: AbstractView view = ((DocumentView) itsNatDoc).getDefaultView();
81: event.initMouseEvent(type, true, true, view, 0, 0, 0, 0, 0,
82: false, false, shiftKey, false,
83: (short) 0/*left button*/, null);
84: return event;
85: }
86:
87: private Event createHTMLEvent(String type) {
88: DocumentEvent docEvent = (DocumentEvent) itsNatDoc;
89: Event event = docEvent.createEvent("HTMLEvents");
90: event.initEvent(type, true, true);
91: return event;
92: }
93:
94: }
|