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