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