Added support for fake mouse events in unit tests

fixed warning about redefined preprocessor macro in wesconfig.h
This commit is contained in:
Pauli Nieminen 2008-08-04 07:27:20 +00:00
parent 3e3103f7a7
commit b65325235f
3 changed files with 102 additions and 1 deletions

View file

@ -72,6 +72,20 @@ namespace test_utils {
SDL_WarpMouse(event_.motion.x,event_.motion.y);
}
event_node_mouse_click::event_node_mouse_click(size_t time, SDL_Event& event) : event_node(time,event)
{}
void event_node_mouse_click::fire_event()
{
// We have to use temporaries because of difference
// in types for mouse position.
int x, y;
SDL_GetMouseState(&x, &y);
event_.button.x = x;
event_.button.y = y;
event_node::fire_event();
}
fake_event_source::fake_event_source() : frame_count_(0)
{
}
@ -131,6 +145,34 @@ namespace test_utils {
return new_move;
}
SDL_Event fake_event_source::make_mouse_click_event(const Uint8 type, const Uint8 button)
{
SDL_Event event;
event.type = type;
if (type == SDL_MOUSEBUTTONDOWN)
event.button.state = SDL_PRESSED;
else
event.button.state = SDL_RELEASED;
event.button.button = button;
return event;
}
event_node_ptr fake_event_source::mouse_press(const size_t time, const Uint8 button)
{
SDL_Event event = make_mouse_click_event(SDL_MOUSEBUTTONDOWN, button);
event_node_ptr new_click(new event_node_mouse_click(time, event));
add_event(new_click);
return new_click;
}
event_node_ptr fake_event_source::mouse_release(const size_t time, const Uint8 button)
{
SDL_Event event = make_mouse_click_event(SDL_MOUSEBUTTONDOWN, button);
event_node_ptr new_click(new event_node_mouse_click(time, event));
add_event(new_click);
return new_click;
}
event_node_ptr fake_event_source::press_key(const size_t time, const SDLKey key, const SDLMod mod)
{
SDL_Event event = make_key_event(SDL_KEYDOWN, key, mod);

View file

@ -48,12 +48,19 @@ namespace test_utils {
event_node(const size_t time, const SDL_Event& event);
virtual ~event_node();
/**
* Used to fire sdl event stored in this object.
* Child class may extend or override functionality
**/
virtual void fire_event();
/**
* @return true if this should stop firing events
* Test if this event should fire now
**/
bool test_if_should_fire(const size_t frame_count ) const;
/**
* @return true if this event has fired
**/
bool is_fired() const;
/**
@ -62,18 +69,36 @@ namespace test_utils {
bool operator<(const event_node& o) const;
};
/**
* modifies SDL_GetKeyState table to have
* correct state.
**/
class event_node_keyboard : public event_node {
public:
event_node_keyboard(size_t time, SDL_Event& event);
virtual void fire_event();
};
/**
* Uses special SDL_WarpMouse function to
* generate mouse move events
**/
class event_node_mouse_motion : public event_node {
public:
event_node_mouse_motion(size_t time, SDL_Event& event);
virtual void fire_event();
};
/**
* Used to create SDL_MOUSEBUTTONDOWN/UP events
* with correct x,y values
**/
class event_node_mouse_click : public event_node {
public:
event_node_mouse_click(size_t time, SDL_Event& event);
virtual void fire_event();
};
typedef boost::shared_ptr<event_node> event_node_ptr;
/**
@ -93,20 +118,50 @@ namespace test_utils {
event_queue queue_;
SDL_Event make_key_event(Uint8 type, const SDLKey key, const SDLMod mod);
SDL_Event make_mouse_click_event(const Uint8 type, const Uint8 button);
public:
fake_event_source();
~fake_event_source();
/**
* adds a generic event to queue
**/
void add_event(const size_t time, const SDL_Event& event);
/**
* adds any type of event to queue
**/
void add_event(event_node_ptr new_node);
/**
* Sets event time source back to zero
**/
void start();
/**
* adds keyboard press event to queue
**/
event_node_ptr press_key(const size_t time, const SDLKey key, const SDLMod mod = KMOD_NONE);
/**
* adds keyboard release event to queue
**/
event_node_ptr release_key(const size_t time, const SDLKey key, const SDLMod mod =KMOD_NONE);
/**
* Adds mouse motion event to queue
**/
event_node_ptr move_mouse(const size_t time, const int x, const int y);
/**
* adds mouse button click event to queue
**/
event_node_ptr mouse_press(const size_t time, const Uint8 button);
/**
* adds mouse button realease event to queue
**/
event_node_ptr mouse_release(const size_t time, const Uint8 button);
/**
* Called by events::pump() to fire events
**/
void process(events::pump_info& /*info*/);
};
}

View file

@ -22,8 +22,12 @@
# define LOCALEDIR "translations"
# endif
#endif
#ifndef VERSION
#define VERSION "1.5.2+svn"
#endif
#ifndef PACKAGE
#define PACKAGE "wesnoth"
#endif
/**
* Some older savegames of Wesnoth cannot be loaded anymore,