1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32  EventSender class:
33  Bound to a JavaScript window.eventSender object using
34  CppBoundClass::bindToJavascript(), this allows layout tests to fire DOM events.
35*/
36
37#ifndef EventSender_h
38#define EventSender_h
39
40#include "CppBoundClass.h"
41#include "Task.h"
42#include "WebDragOperation.h"
43#include "WebInputEvent.h"
44#include "WebPoint.h"
45
46class TestShell;
47
48namespace WebKit {
49class WebDragData;
50class WebView;
51}
52
53class EventSender : public CppBoundClass {
54public:
55    // Builds the property and method lists needed to bind this class to a JS
56    // object.
57    EventSender(TestShell*);
58
59    // Resets some static variable state.
60    void reset();
61
62    // Simulate drag&drop system call.
63    void doDragDrop(const WebKit::WebDragData&, WebKit::WebDragOperationsMask);
64
65    // JS callback methods.
66    void mouseDown(const CppArgumentList&, CppVariant*);
67    void mouseUp(const CppArgumentList&, CppVariant*);
68    void mouseMoveTo(const CppArgumentList&, CppVariant*);
69    void leapForward(const CppArgumentList&, CppVariant*);
70    void keyDown(const CppArgumentList&, CppVariant*);
71    void dispatchMessage(const CppArgumentList&, CppVariant*);
72    void textZoomIn(const CppArgumentList&, CppVariant*);
73    void textZoomOut(const CppArgumentList&, CppVariant*);
74    void zoomPageIn(const CppArgumentList&, CppVariant*);
75    void zoomPageOut(const CppArgumentList&, CppVariant*);
76    void mouseScrollBy(const CppArgumentList&, CppVariant*);
77    void continuousMouseScrollBy(const CppArgumentList&, CppVariant*);
78    void scheduleAsynchronousClick(const CppArgumentList&, CppVariant*);
79    void beginDragWithFiles(const CppArgumentList&, CppVariant*);
80    CppVariant dragMode;
81
82    void addTouchPoint(const CppArgumentList&, CppVariant*);
83    void cancelTouchPoint(const CppArgumentList&, CppVariant*);
84    void clearTouchPoints(const CppArgumentList&, CppVariant*);
85    void releaseTouchPoint(const CppArgumentList&, CppVariant*);
86    void setTouchModifier(const CppArgumentList&, CppVariant*);
87    void touchCancel(const CppArgumentList&, CppVariant*);
88    void touchEnd(const CppArgumentList&, CppVariant*);
89    void touchMove(const CppArgumentList&, CppVariant*);
90    void touchStart(const CppArgumentList&, CppVariant*);
91    void updateTouchPoint(const CppArgumentList&, CppVariant*);
92
93    // Unimplemented stubs
94    void contextClick(const CppArgumentList&, CppVariant*);
95    void enableDOMUIEventLogging(const CppArgumentList&, CppVariant*);
96    void fireKeyboardEventsToElement(const CppArgumentList&, CppVariant*);
97    void clearKillRing(const CppArgumentList&, CppVariant*);
98
99    // Properties used in layout tests.
100#if defined(OS_WIN)
101    CppVariant wmKeyDown;
102    CppVariant wmKeyUp;
103    CppVariant wmChar;
104    CppVariant wmDeadChar;
105    CppVariant wmSysKeyDown;
106    CppVariant wmSysKeyUp;
107    CppVariant wmSysChar;
108    CppVariant wmSysDeadChar;
109#endif
110
111    TaskList* taskList() { return &m_taskList; }
112
113private:
114    // Returns the test shell's webview.
115    WebKit::WebView* webview();
116
117    // Returns true if dragMode is true.
118    bool isDragMode() { return dragMode.isBool() && dragMode.toBoolean(); }
119
120    // Sometimes we queue up mouse move and mouse up events for drag drop
121    // handling purposes.  These methods dispatch the event.
122    void doMouseMove(const WebKit::WebMouseEvent&);
123    void doMouseUp(const WebKit::WebMouseEvent&);
124    static void doLeapForward(int milliseconds);
125    void replaySavedEvents();
126
127    // Helper to return the button type given a button code
128    static WebKit::WebMouseEvent::Button getButtonTypeFromButtonNumber(int);
129
130    // Helper to extract the button number from the optional argument in
131    // mouseDown and mouseUp
132    static int getButtonNumberFromSingleArg(const CppArgumentList&);
133
134    // Returns true if the specified key code passed in needs a shift key
135    // modifier to be passed into the generated event.
136    bool needsShiftModifier(int);
137
138    void updateClickCountForButton(WebKit::WebMouseEvent::Button);
139
140    // Compose a touch event from the current touch points and send it.
141    void sendCurrentTouchEvent(const WebKit::WebInputEvent::Type);
142
143    // Handle a request to send a wheel event.
144    void handleMouseWheel(const CppArgumentList&, CppVariant*, bool continuous);
145
146    TaskList m_taskList;
147
148    // Non-owning pointer.  The EventSender is owned by the TestShell.
149    TestShell* m_shell;
150
151    // Location of last mouseMoveTo event.
152    static WebKit::WebPoint lastMousePos;
153
154    // Currently pressed mouse button (Left/Right/Middle or None)
155    static WebKit::WebMouseEvent::Button pressedButton;
156
157    // The last button number passed to mouseDown and mouseUp.
158    // Used to determine whether the click count continues to
159    // increment or not.
160    static WebKit::WebMouseEvent::Button lastButtonType;
161};
162
163#endif // EventSender_h
164