ui_controls.h revision 868fa2fe829687343ffae624259930155e16dbd8
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef UI_BASE_TEST_UI_CONTROLS_H_ 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define UI_BASE_TEST_UI_CONTROLS_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback_forward.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "build/build_config.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/keycodes/keyboard_codes.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/native_widget_types.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui_controls { 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// A set of utility functions to generate native events in platform 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// independent way. Note that since the implementations depend on a window being 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// top level, these can only be called from test suites that are not sharded. 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// For aura tests, please look into |aura::test:EventGenerator| first. This 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// class provides a way to emulate events in synchronous way and it is often 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// easier to write tests with this class than using |ui_controls|. 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Many of the functions in this class include a variant that takes a Closure. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The version that takes a Closure waits until the generated event is 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// processed. Once the generated event is processed the Closure is Run (and 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deleted). Note that this is a somewhat fragile process in that any event of 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the correct type (key down, mouse click, etc.) will trigger the Closure to be 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// run. Hence a usage such as 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SendKeyPress(...); 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SendKeyPressNotifyWhenDone(..., task); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// might trigger |task| early. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note: Windows does not currently do anything with the |window| argument for 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// these functions, so passing NULL is ok. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Send a key press with/without modifier keys. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If you're writing a test chances are you want the variant in ui_test_utils. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See it for details. 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Per the above comment, these methods can only be called from non-sharded test 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// suites. This method ensures that they're not accidently called by sharded 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// tests. 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void EnableUIControls(); 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendKeyPress(gfx::NativeWindow window, 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ui::KeyboardCode key, 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool control, 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool shift, 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool alt, 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool command); 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ui::KeyboardCode key, 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool control, 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool shift, 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool alt, 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool command, 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Closure& task); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simulate a mouse move. (x,y) are absolute screen coordinates. 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendMouseMove(long x, long y); 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendMouseMoveNotifyWhenDone(long x, 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) long y, 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Closure& task); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum MouseButton { 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LEFT = 0, 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MIDDLE, 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RIGHT, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to indicate the state of the button when generating events. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum MouseButtonState { 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UP = 1, 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DOWN = 2 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sends a mouse down and/or up message. The click will be sent to wherever 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the cursor currently is, so be sure to move the cursor before calling this 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (and be sure the cursor has arrived!). 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendMouseEvents(MouseButton type, int state); 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendMouseEventsNotifyWhenDone(MouseButton type, 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int state, 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Closure& task); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same as SendMouseEvents with UP | DOWN. 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SendMouseClick(MouseButton type); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(TOOLKIT_VIEWS) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Runs |closure| after processing all pending ui events. 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void RunClosureAfterAllPendingUIEvents(const base::Closure& closure); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_AURA) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UIControlsAura; 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InstallUIControlsAura(UIControlsAura* instance); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ui_controls 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif // UI_BASE_TEST_UI_CONTROLS_H_ 103