tests/gui/test_drop_target.cpp

Go to the documentation of this file.
00001 /* $Id: test_drop_target.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Pauli Nieminen <paniemin@cc.hut.fi>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-test"
00017 
00018 #include <boost/test/unit_test.hpp>
00019 
00020 #include <boost/bind.hpp>
00021 
00022 #include "sdl_utils.hpp"
00023 #include "widgets/drop_target.hpp"
00024 
00025 BOOST_AUTO_TEST_SUITE( test_drop_target )
00026 
00027     /**
00028      * Specialized testing class so unit test
00029      * can call protected member functions to
00030      * simulate drop operation
00031      **/
00032     class test_drop_target : public gui::drop_target {
00033         public:
00034         test_drop_target(gui::drop_group_manager_ptr group, const SDL_Rect& loc) : gui::drop_target(group, loc)
00035         {}
00036 
00037         int handle_drop() {
00038             return gui::drop_target::handle_drop();
00039         }
00040         static bool empty() {
00041             return gui::drop_target::empty();
00042         };
00043     };
00044 
00045 BOOST_AUTO_TEST_CASE( test_create_group )
00046 {
00047 
00048     gui::drop_group_manager group0;
00049     gui::drop_group_manager* group1 = new gui::drop_group_manager();
00050 
00051     BOOST_CHECK_EQUAL(group0.get_group_id(), 0);
00052     BOOST_CHECK_EQUAL(group1->get_group_id(), 1);
00053 
00054     delete group1;
00055 
00056     gui::drop_group_manager_ptr group2(new gui::drop_group_manager());
00057 
00058 
00059     BOOST_CHECK_EQUAL(group2->get_group_id(), 2);
00060 }
00061 
00062 typedef std::vector<gui::drop_target_ptr> target_store;
00063 
00064 static void create_drop_targets(const SDL_Rect& loc, gui::drop_group_manager_ptr group, target_store& targets, int& id_counter)
00065 {
00066     gui::drop_target_ptr new_target(new test_drop_target(group, loc));
00067     BOOST_CHECK_EQUAL(id_counter++, new_target->get_id());
00068     // Test that drop gives -1 correctly for non overlapping
00069     // targets
00070     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(new_target.get())->handle_drop(), -1);
00071     targets.push_back(new_target);
00072 }
00073 
00074 BOOST_AUTO_TEST_CASE( test_create_drop_targets )
00075 {
00076     gui::drop_group_manager_ptr group(new gui::drop_group_manager());
00077     BOOST_CHECK(group->get_group_id() > 0);
00078 
00079     typedef std::vector<SDL_Rect> location_store;
00080     location_store locations;
00081 
00082     // Create rectangles for drop targets
00083     locations.push_back(create_rect(50,50,20,20));
00084     locations.push_back(create_rect(50,100,20,20));
00085     locations.push_back(create_rect(50,150,20,20));
00086     locations.push_back(create_rect(50,200,20,20));
00087     locations.push_back(create_rect(50,250,20,20));
00088     locations.push_back(create_rect(50,300,20,20));
00089 
00090     target_store targets;
00091 
00092     int id_counter = 0;
00093 
00094     std::for_each(locations.begin(), locations.end(),
00095             boost::bind(create_drop_targets,_1, group, boost::ref(targets), boost::ref(id_counter)));
00096 
00097     BOOST_CHECK_EQUAL(targets.size(), locations.size());
00098 
00099     // Modify 3rd rectangle to overlap with 4th
00100     locations[2].y = 190;
00101 
00102     // Check for correct drop results
00103     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[2].get())->handle_drop(), 3);
00104     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[3].get())->handle_drop(), 2);
00105     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[1].get())->handle_drop(), -1);
00106     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[4].get())->handle_drop(), -1);
00107 }
00108 
00109 BOOST_AUTO_TEST_CASE( check_memory_leaks )
00110 {
00111     BOOST_CHECK(test_drop_target::empty());
00112 }
00113 
00114 BOOST_AUTO_TEST_CASE( test_multiple_drop_groups )
00115 {
00116     gui::drop_group_manager_ptr group(new gui::drop_group_manager());
00117     gui::drop_group_manager_ptr group2(new gui::drop_group_manager());
00118     BOOST_CHECK(group->get_group_id() > 0);
00119     BOOST_CHECK(group2->get_group_id() > 0);
00120 
00121     typedef std::vector<SDL_Rect> location_store;
00122     location_store locations;
00123     location_store locations2;
00124 
00125     // Create rectangles for drop targets
00126     locations.push_back(create_rect(50,50,20,20));
00127     locations.push_back(create_rect(50,100,20,20));
00128     locations.push_back(create_rect(50,150,20,20));
00129     locations.push_back(create_rect(50,200,20,20));
00130     locations.push_back(create_rect(50,250,20,20));
00131     locations.push_back(create_rect(50,300,20,20));
00132 
00133     locations2.push_back(create_rect(50,50,20,20));
00134     locations2.push_back(create_rect(100,50,20,20));
00135     locations2.push_back(create_rect(150,50,20,20));
00136     locations2.push_back(create_rect(200,50,20,20));
00137     locations2.push_back(create_rect(250,50,20,20));
00138     locations2.push_back(create_rect(300,50,20,20));
00139 
00140 
00141     target_store targets;
00142     target_store targets2;
00143 
00144     int id_counter = 0;
00145 
00146     std::for_each(locations.begin(), locations.end(),
00147             boost::bind(create_drop_targets,_1, group, boost::ref(targets), boost::ref(id_counter)));
00148     id_counter = 0;
00149     std::for_each(locations2.begin(), locations2.end(),
00150             boost::bind(create_drop_targets,_1, group2, boost::ref(targets2), boost::ref(id_counter)));
00151 
00152     BOOST_CHECK_EQUAL(targets.size(), locations.size());
00153     BOOST_CHECK_EQUAL(targets2.size(), locations2.size());
00154 
00155     // Modify 3rd rectangle to overlap with 4th
00156     locations[2].y = 190;
00157 
00158     // Check for correct drop results
00159     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[2].get())->handle_drop(), 3);
00160     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[3].get())->handle_drop(), 2);
00161     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[1].get())->handle_drop(), -1);
00162     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets[4].get())->handle_drop(), -1);
00163 
00164     locations2[2].y = 180;
00165     locations2[2].x = 50;
00166 
00167     BOOST_CHECK_EQUAL(static_cast<test_drop_target*>(targets2[2].get())->handle_drop(), -1);
00168 
00169 }
00170 
00171 /* vim: set ts=4 sw=4: */
00172 BOOST_AUTO_TEST_SUITE_END()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:11 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs