The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_drop_target.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Pauli Nieminen <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "utils/functional.hpp"
20 
21 #include "sdl/rect.hpp"
22 #include "sdl/utils.hpp"
23 #include "widgets/drop_target.hpp"
24 
26 
27  /**
28  * Specialized testing class so unit test
29  * can call protected member functions to
30  * simulate drop operation
31  **/
32  class test_drop_target : public gui::drop_target {
33  public:
34  test_drop_target(gui::drop_group_manager_ptr group, const SDL_Rect& loc) : gui::drop_target(group, loc)
35  {}
36 
37  int handle_drop() {
39  }
40  static bool empty() {
41  return gui::drop_target::empty();
42  }
43  };
44 
45 BOOST_AUTO_TEST_CASE( test_create_group )
46 {
47 
50 
51  BOOST_CHECK_EQUAL(group0.get_group_id(), 0);
52  BOOST_CHECK_EQUAL(group1->get_group_id(), 1);
53 
54  delete group1;
55 
57 
58 
59  BOOST_CHECK_EQUAL(group2->get_group_id(), 2);
60 }
61 
62 typedef std::vector<gui::drop_target_ptr> target_store;
63 
64 static void create_drop_targets(const SDL_Rect& loc, gui::drop_group_manager_ptr group, target_store& targets, int& id_counter)
65 {
66  gui::drop_target_ptr new_target(new test_drop_target(group, loc));
67  BOOST_CHECK_EQUAL(id_counter++, new_target->get_id());
68  // Test that drop gives -1 correctly for non overlapping
69  // targets
70  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(new_target.get())->handle_drop(), -1);
71  targets.push_back(new_target);
72 }
73 
74 BOOST_AUTO_TEST_CASE( test_create_drop_targets )
75 {
77  BOOST_CHECK(group->get_group_id() > 0);
78 
79  typedef std::vector<SDL_Rect> location_store;
80  location_store locations;
81 
82  // Create rectangles for drop targets
83  locations.push_back(sdl::create_rect(50,50,20,20));
84  locations.push_back(sdl::create_rect(50,100,20,20));
85  locations.push_back(sdl::create_rect(50,150,20,20));
86  locations.push_back(sdl::create_rect(50,200,20,20));
87  locations.push_back(sdl::create_rect(50,250,20,20));
88  locations.push_back(sdl::create_rect(50,300,20,20));
89 
90  target_store targets;
91 
92  int id_counter = 0;
93 
94  std::for_each(locations.begin(), locations.end(),
95  std::bind(create_drop_targets,_1, group, std::ref(targets), std::ref(id_counter)));
96 
97  BOOST_CHECK_EQUAL(targets.size(), locations.size());
98 
99  // Modify 3rd rectangle to overlap with 4th
100  locations[2].y = 190;
101 
102  // Check for correct drop results
103  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[2].get())->handle_drop(), 3);
104  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[3].get())->handle_drop(), 2);
105  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[1].get())->handle_drop(), -1);
106  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[4].get())->handle_drop(), -1);
107 }
108 
109 BOOST_AUTO_TEST_CASE( check_memory_leaks )
110 {
111  BOOST_CHECK(test_drop_target::empty());
112 }
113 
114 BOOST_AUTO_TEST_CASE( test_multiple_drop_groups )
115 {
118  BOOST_CHECK(group->get_group_id() > 0);
119  BOOST_CHECK(group2->get_group_id() > 0);
120 
121  typedef std::vector<SDL_Rect> location_store;
122  location_store locations;
123  location_store locations2;
124 
125  // Create rectangles for drop targets
126  locations.push_back(sdl::create_rect(50,50,20,20));
127  locations.push_back(sdl::create_rect(50,100,20,20));
128  locations.push_back(sdl::create_rect(50,150,20,20));
129  locations.push_back(sdl::create_rect(50,200,20,20));
130  locations.push_back(sdl::create_rect(50,250,20,20));
131  locations.push_back(sdl::create_rect(50,300,20,20));
132 
133  locations2.push_back(sdl::create_rect(50,50,20,20));
134  locations2.push_back(sdl::create_rect(100,50,20,20));
135  locations2.push_back(sdl::create_rect(150,50,20,20));
136  locations2.push_back(sdl::create_rect(200,50,20,20));
137  locations2.push_back(sdl::create_rect(250,50,20,20));
138  locations2.push_back(sdl::create_rect(300,50,20,20));
139 
140 
141  target_store targets;
142  target_store targets2;
143 
144  int id_counter = 0;
145 
146  std::for_each(locations.begin(), locations.end(),
147  std::bind(create_drop_targets,_1, group, std::ref(targets), std::ref(id_counter)));
148  id_counter = 0;
149  std::for_each(locations2.begin(), locations2.end(),
150  std::bind(create_drop_targets,_1, group2, std::ref(targets2), std::ref(id_counter)));
151 
152  BOOST_CHECK_EQUAL(targets.size(), locations.size());
153  BOOST_CHECK_EQUAL(targets2.size(), locations2.size());
154 
155  // Modify 3rd rectangle to overlap with 4th
156  locations[2].y = 190;
157 
158  // Check for correct drop results
159  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[2].get())->handle_drop(), 3);
160  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[3].get())->handle_drop(), 2);
161  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[1].get())->handle_drop(), -1);
162  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[4].get())->handle_drop(), -1);
163 
164  locations2[2].y = 180;
165  locations2[2].x = 50;
166 
167  BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets2[2].get())->handle_drop(), -1);
168 
169 }
170 
171 /* vim: set ts=4 sw=4: */
172 BOOST_AUTO_TEST_SUITE_END()
drop_target_group get_group_id() const
static void create_drop_targets(const SDL_Rect &loc, gui::drop_group_manager_ptr group, target_store &targets, int &id_counter)
static bool empty()
General purpose widgets.
BOOST_AUTO_TEST_SUITE(test_map_location)
test_drop_target(gui::drop_group_manager_ptr group, const SDL_Rect &loc)
int handle_drop()
Called by widget object when droping happens.
Definition: drop_target.cpp:60
std::vector< gui::drop_target_ptr > target_store
BOOST_AUTO_TEST_CASE(test_create_group)
GLsizei const GLint * locations
Definition: glew.h:11075
static bool empty()
Used to check if static storages are empty.
Definition: drop_target.hpp:61
Used to create and destroy drop groups.
Definition: drop_target.hpp:95
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
Contains the SDL_Rect helper code.
GLenum GLint ref
Definition: glew.h:1813
GLboolean GLuint group
Definition: glew.h:2589
Specialized testing class so unit test can call protected member functions to simulate drop operation...