1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file. 4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef UI_EVENTS_TEST_EVENT_GENERATOR_H_ 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define UI_EVENTS_TEST_EVENT_GENERATOR_H_ 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <list> 9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <vector> 10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/basictypes.h" 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/callback.h" 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_ptr.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/time/time.h" 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/events/event_constants.h" 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/events/keycodes/keyboard_codes.h" 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/gfx/native_widget_types.h" 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/gfx/point.h" 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace base { 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TickClock; 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace ui { 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass Event; 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass EventProcessor; 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass EventSource; 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass EventTarget; 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass KeyEvent; 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass MouseEvent; 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ScrollEvent; 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TouchEvent; 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace test { 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 36116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef base::Callback<void(EventType, const gfx::Vector2dF&)> 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ScrollStepCallback; 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class EventGenerator; 406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// A delegate interface for EventGenerator to abstract platform-specific event 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// targeting and coordinate conversion. 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass EventGeneratorDelegate { 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual ~EventGeneratorDelegate() {} 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Set the context of the delegate, whilst it is being used by an active 486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // EventGenerator. 496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) virtual void SetContext(EventGenerator* owner, 506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) gfx::NativeWindow root_window, 516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) gfx::NativeWindow window) {} 526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The ui::EventTarget at the given |location|. 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual EventTarget* GetTargetAt(const gfx::Point& location) = 0; 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The ui::EventSource for the given |target|. 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual EventSource* GetEventSource(EventTarget* target) = 0; 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Helper functions to determine the center point of |target| or |window|. 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual gfx::Point CenterOfTarget(const EventTarget* target) const = 0; 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual gfx::Point CenterOfWindow(gfx::NativeWindow window) const = 0; 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Convert a point between API's coordinates and |target|'s coordinates. 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void ConvertPointFromTarget(const EventTarget* target, 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point* point) const = 0; 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void ConvertPointToTarget(const EventTarget* target, 67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point* point) const = 0; 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Convert a point from the coordinate system in the host that contains 70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |hosted_target| into the root window's coordinate system. 71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void ConvertPointFromHost(const EventTarget* hosted_target, 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point* point) const = 0; 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// ui::test::EventGenerator is a tool that generates and dispatches events. 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Unlike |ui_controls| package in ui/base/test, this does not use platform 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// native message loops. Instead, it sends events to the event dispatcher 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// synchronously. 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This class is not suited for the following cases: 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 1) If your test depends on native events (ui::Event::native_event()). 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This return is empty/NULL event with EventGenerator. 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 2) If your test involves nested message loop, such as 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// menu or drag & drop. Because this class directly 86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// post an event to WindowEventDispatcher, this event will not be 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// handled in the nested message loop. 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 3) Similarly, |base::MessagePumpObserver| will not be invoked. 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 4) Any other code that requires native message loops, such as 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// tests for WindowTreeHostWin/WindowTreeHostX11. 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// If one of these applies to your test, please use |ui_controls| 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// package instead. 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Note: The coordinates of the points in API is determined by the 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// EventGeneratorDelegate. 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass EventGenerator { 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Creates an EventGenerator with the mouse/touch location (0,0), 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // which uses the |root_window|'s coordinates and the default delegate for 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // this platform. 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch explicit EventGenerator(gfx::NativeWindow root_window); 103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Create an EventGenerator with EventGeneratorDelegate, 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // which uses the coordinates conversions and targeting provided by 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |delegate|. 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch explicit EventGenerator(EventGeneratorDelegate* delegate); 108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Creates an EventGenerator with the mouse/touch location 110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // at |initial_location|, which uses the |root_window|'s coordinates. 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EventGenerator(gfx::NativeWindow root_window, 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point& initial_location); 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Creates an EventGenerator with the mouse/touch location centered over 115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |window|. This is currently the only constructor that works on Mac, since 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // a specific window is required (and there is no root window). 117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EventGenerator(gfx::NativeWindow root_window, gfx::NativeWindow window); 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual ~EventGenerator(); 120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Explicitly sets the location used by mouse/touch events. This is set by the 122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // various methods that take a location but can be manipulated directly, 123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // typically for touch. 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void set_current_location(const gfx::Point& location) { 125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch current_location_ = location; 126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point& current_location() const { return current_location_; } 128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void set_async(bool async) { async_ = async; } 130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool async() const { return async_; } 131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Resets the event flags bitmask. 133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void set_flags(int flags) { flags_ = flags; } 134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int flags() const { return flags_; } 135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a left button press event. 137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressLeftButton(); 138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a left button release event. 140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseLeftButton(); 141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to click (press, release) left button. 143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ClickLeftButton(); 144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a double click event using the left button. 146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DoubleClickLeftButton(); 147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a right button press event. 149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressRightButton(); 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a right button release event. 152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseRightButton(); 153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Moves the mouse wheel by |delta_x|, |delta_y|. 155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseWheel(int delta_x, int delta_y); 156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a mouse exit. 158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void SendMouseExit(); 159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to move mouse to be the given |point| in the 161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |current_root_window_|'s host window coordinates. 162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseToInHost(const gfx::Point& point_in_host); 163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseToInHost(int x, int y) { 164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MoveMouseToInHost(gfx::Point(x, y)); 165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to move mouse to be the given |point| in screen 168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // coordinates. 169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseTo(const gfx::Point& point_in_screen, int count); 170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseTo(const gfx::Point& point_in_screen) { 171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MoveMouseTo(point_in_screen, 1); 172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseTo(int x, int y) { 174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MoveMouseTo(gfx::Point(x, y)); 175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to move mouse to be the given |point| in |window|'s 178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // coordinates. 179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseRelativeTo(const EventTarget* window, const gfx::Point& point); 180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseRelativeTo(const EventTarget* window, int x, int y) { 181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MoveMouseRelativeTo(window, gfx::Point(x, y)); 182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseBy(int x, int y) { 185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MoveMouseTo(current_location_ + gfx::Vector2d(x, y)); 186116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to drag mouse to given |point|. 189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DragMouseTo(const gfx::Point& point); 190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DragMouseTo(int x, int y) { 192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DragMouseTo(gfx::Point(x, y)); 193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DragMouseBy(int dx, int dy) { 196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DragMouseTo(current_location_ + gfx::Vector2d(dx, dy)); 197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 199116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates events to move the mouse to the center of the window. 200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveMouseToCenterOf(EventTarget* window); 201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 202116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a touch press event. 203116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressTouch(); 204116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a touch press event with |touch_id|. 206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressTouchId(int touch_id); 207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a ET_TOUCH_MOVED event to |point|. 209116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveTouch(const gfx::Point& point); 210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a ET_TOUCH_MOVED event to |point| with |touch_id|. 212116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MoveTouchId(const gfx::Point& point, int touch_id); 213116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a touch release event. 215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseTouch(); 216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a touch release event with |touch_id|. 218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseTouchId(int touch_id); 219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press, move and release event to move touch 221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // to be the given |point|. 222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressMoveAndReleaseTouchTo(const gfx::Point& point); 223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressMoveAndReleaseTouchTo(int x, int y) { 225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch PressMoveAndReleaseTouchTo(gfx::Point(x, y)); 226116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressMoveAndReleaseTouchBy(int x, int y) { 229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch PressMoveAndReleaseTouchTo(current_location_ + gfx::Vector2d(x, y)); 230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press, move and release events to move touch 233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // to the center of the window. 234116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressMoveAndReleaseTouchToCenterOf(EventTarget* window); 235116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 236116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates and dispatches a Win8 edge-swipe event (swipe up from bottom or 237116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // swipe down from top). Note that it is not possible to distinguish between 238116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the two edges with this event. 239116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureEdgeSwipe(); 240116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 241116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates and dispatches touch-events required to generate a TAP gesture. 242116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Note that this can generate a number of other gesture events at the same 243116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // time (e.g. GESTURE_BEGIN, TAP_DOWN, END). 244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureTapAt(const gfx::Point& point); 245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press and release touch-events to generate a TAP_DOWN event, but 247116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // without generating any scroll or tap events. This can also generate a few 248116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // other gesture events (e.g. GESTURE_BEGIN, END). 249116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureTapDownAndUp(const gfx::Point& point); 250116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 251116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press, move, release touch-events to generate a sequence of 252116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // scroll events. |duration| and |steps| affect the velocity of the scroll, 253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // and depending on these values, this may also generate FLING scroll 254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // gestures. If velocity/fling is irrelevant for the test, then any non-zero 255116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // values for these should be sufficient. 256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureScrollSequence(const gfx::Point& start, 257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point& end, 258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::TimeDelta& duration, 259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int steps); 260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The same as GestureScrollSequence(), with the exception that |callback| is 262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // called at each step of the scroll sequence. |callback| is called at the 263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // start of the sequence with ET_GESTURE_SCROLL_BEGIN, followed by one or more 264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // ET_GESTURE_SCROLL_UPDATE and ends with an ET_GESTURE_SCROLL_END. 265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureScrollSequenceWithCallback(const gfx::Point& start, 266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point& end, 267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::TimeDelta& duration, 268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int steps, 269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ScrollStepCallback& callback); 270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press, move, release touch-events to generate a sequence of 272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // multi-finger scroll events. |count| specifies the number of touch-points 273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // that should generate the scroll events. |start| are the starting positions 274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // of all the touch points. |steps| and |event_separation_time_ms| are 275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // relevant when testing velocity/fling/swipe, otherwise these can be any 276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // non-zero value. |delta_x| and |delta_y| are the amount that each finger 277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // should be moved. Internally calls GestureMultiFingerScrollWithDelays 278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // with zeros as |delay_adding_finger_ms| forcing all touch down events to be 279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // immediate. 280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureMultiFingerScroll(int count, 281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point start[], 282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int event_separation_time_ms, 283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int steps, 284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int move_x, 285116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int move_y); 286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates press, move, release touch-events to generate a sequence of 288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // multi-finger scroll events. |count| specifies the number of touch-points 289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // that should generate the scroll events. |start| are the starting positions 290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // of all the touch points. |delay_adding_finger_ms| are delays in ms from the 291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // starting time till touching down of each finger. |delay_adding_finger_ms| 292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // is useful when testing complex gestures that start with 1 or 2 fingers and 293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // add fingers with a delay. |steps| and |event_separation_time_ms| are 294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // relevant when testing velocity/fling/swipe, otherwise these can be any 295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // non-zero value. |delta_x| and |delta_y| are the amount that each finger 296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // should be moved. 297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GestureMultiFingerScrollWithDelays(int count, 298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const gfx::Point start[], 299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const int delay_adding_finger_ms[], 300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int event_separation_time_ms, 301116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int steps, 302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int move_x, 303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int move_y); 304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates scroll sequences of a FlingCancel, Scrolls, FlingStart, with 306116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // constant deltas to |x_offset| and |y_offset| in |steps|. 307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ScrollSequence(const gfx::Point& start, 308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::TimeDelta& step_delay, 309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch float x_offset, 310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch float y_offset, 311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int steps, 312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int num_fingers); 313116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates scroll sequences of a FlingCancel, Scrolls, FlingStart, sending 315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // scrolls of each of the values in |offsets|. 316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ScrollSequence(const gfx::Point& start, 317116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::TimeDelta& step_delay, 318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::vector<gfx::Point>& offsets, 319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int num_fingers); 320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 321116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a key press event. On platforms except Windows and X11, a key 322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // event without native_event() is generated. Note that ui::EF_ flags should 323116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. 324116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(yusukes): Support native_event() on all platforms. 325116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressKey(KeyboardCode key_code, int flags); 326116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 327116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Generates a key release event. On platforms except Windows and X11, a key 328116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // event without native_event() is generated. Note that ui::EF_ flags should 329116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. 330116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(yusukes): Support native_event() on all platforms. 331116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseKey(KeyboardCode key_code, int flags); 332116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 333116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Dispatch the event to the WindowEventDispatcher. 334116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void Dispatch(Event* event); 335116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 336116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void set_current_target(EventTarget* target) { 337116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch current_target_ = target; 338116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 339116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 340116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Specify an alternative tick clock to be used for simulating time in tests. 341116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void SetTickClock(scoped_ptr<base::TickClock> tick_clock); 342116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 343116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Get the current time from the tick clock. 344116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::TimeDelta Now(); 345116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 3466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Default delegate set by a platform-specific GeneratorDelegate singleton. 3476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) static EventGeneratorDelegate* default_delegate; 3486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 349116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private: 3506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Set up the test context using the delegate. 3516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) void Init(gfx::NativeWindow root_window, gfx::NativeWindow window_context); 352116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 353116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Dispatch a key event to the WindowEventDispatcher. 354116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DispatchKeyEvent(bool is_press, KeyboardCode key_code, int flags); 355116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 356116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void UpdateCurrentDispatcher(const gfx::Point& point); 357116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void PressButton(int flag); 358116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ReleaseButton(int flag); 359116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 360116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point GetLocationInCurrentRoot() const; 361116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point CenterOfWindow(const EventTarget* window) const; 362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 363116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DispatchNextPendingEvent(); 364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void DoDispatchEvent(Event* event, bool async); 365116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 3666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) const EventGeneratorDelegate* delegate() const; 3676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) EventGeneratorDelegate* delegate(); 3686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<EventGeneratorDelegate> delegate_; 370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch gfx::Point current_location_; 371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EventTarget* current_target_; 372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int flags_; 373116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool grab_; 374116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::list<Event*> pending_events_; 375116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Set to true to cause events to be posted asynchronously. 376116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool async_; 377116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<base::TickClock> tick_clock_; 378116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 379116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DISALLOW_COPY_AND_ASSIGN(EventGenerator); 380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace test 383116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace ui 384116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 385116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif // UI_EVENTS_TEST_EVENT_GENERATOR_H_ 386