/
fire_event.cpp
149 lines (123 loc) · 4.2 KB
/
fire_event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright (C) 2011 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
// In this domain since it compares a shared string from this domain.
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "config_cache.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/label.hpp"
#include <boost/test/unit_test.hpp>
#include "utils/functional.hpp"
#include <sstream>
static void print(std::stringstream& sstr
, const std::string& queue
, const std::string& id)
{
sstr << queue << ':' << id << '\n';
}
template<gui2::event::ui_event E>
void connect_queue(
std::stringstream& sstr
, gui2::widget& widget)
{
widget.connect_signal<E>(
std::bind(print, std::ref(sstr), "pre", widget.id())
, gui2::event::dispatcher::back_pre_child);
widget.connect_signal<E>(
std::bind(print, std::ref(sstr), "child", widget.id())
, gui2::event::dispatcher::back_child);
widget.connect_signal<E>(
std::bind(print, std::ref(sstr), "post", widget.id())
, gui2::event::dispatcher::back_post_child);
}
static void connect_signals(
std::stringstream& sstr
, gui2::widget& widget)
{
/** @todo Add the rest of the events. */
connect_queue<gui2::event::DRAW>(sstr, widget);
connect_queue<gui2::event::CLOSE_WINDOW>(sstr, widget);
connect_queue<gui2::event::MOUSE_ENTER>(sstr, widget);
connect_queue<gui2::event::MOUSE_LEAVE>(sstr, widget);
connect_queue<gui2::event::LEFT_BUTTON_DOWN>(sstr, widget);
connect_queue<gui2::event::LEFT_BUTTON_UP>(sstr, widget);
connect_queue<gui2::event::LEFT_BUTTON_CLICK>(sstr, widget);
connect_queue<gui2::event::LEFT_BUTTON_DOUBLE_CLICK>(sstr, widget);
connect_queue<gui2::event::MIDDLE_BUTTON_DOWN>(sstr, widget);
connect_queue<gui2::event::MIDDLE_BUTTON_UP>(sstr, widget);
connect_queue<gui2::event::MIDDLE_BUTTON_CLICK>(sstr, widget);
connect_queue<gui2::event::MIDDLE_BUTTON_DOUBLE_CLICK>(sstr, widget);
connect_queue<gui2::event::RIGHT_BUTTON_DOWN>(sstr, widget);
connect_queue<gui2::event::RIGHT_BUTTON_UP>(sstr, widget);
connect_queue<gui2::event::RIGHT_BUTTON_CLICK>(sstr, widget);
connect_queue<gui2::event::RIGHT_BUTTON_DOUBLE_CLICK>(sstr, widget);
}
static void add_widget(gui2::grid& grid
, gui2::widget_ptr widget
, const std::string& id
, const unsigned row
, const unsigned column)
{
BOOST_REQUIRE_NE(widget.get(), static_cast<gui2::widget*>(nullptr));
widget->set_id(id);
grid.set_child(widget
, row
, column
, gui2::grid::VERTICAL_GROW_SEND_TO_CLIENT
| gui2::grid::HORIZONTAL_GROW_SEND_TO_CLIENT
, 0);
}
static std::string set_event_order()
{
return
"pre:root\n"
"pre:level 1\n"
"pre:level 2\n"
"child:level 2\n"
"post:level 2\n"
"post:level 1\n"
"post:root\n";
}
/** @todo Add the rest of the events. */
static void validate_draw(std::stringstream& sstr)
{
BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
}
static void validate_right_button_down(std::stringstream& sstr)
{
BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
}
BOOST_AUTO_TEST_CASE(test_fire_event)
{
/**** Initialize the environment. *****/
game_config::config_cache& cache = game_config::config_cache::instance();
cache.clear_defines();
cache.add_define("EDITOR");
cache.add_define("MULTIPLAYER");
std::stringstream sstr;
/**** Initialize the grid. *****/
gui2::grid grid(1, 1);
grid.set_id("root");
connect_signals(sstr, grid);
auto child_grid = std::make_shared<gui2::grid>(1, 1);
add_widget(grid, child_grid, "level 1", 0, 0);
connect_signals(sstr, *child_grid);
auto child = std::make_shared<gui2::grid>(1, 1);
add_widget(*child_grid, child, "level 2", 0, 0);
connect_signals(sstr, *child);
/** @todo Add the rest of the events. */
sstr.str("");
grid.fire(gui2::event::DRAW, *child);
validate_draw(sstr);
sstr.str("");
grid.fire(gui2::event::RIGHT_BUTTON_DOWN, *child);
validate_right_button_down(sstr);
}