3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "client/activeobjectmgr.h"
26 class TestClientActiveObject : public ClientActiveObject
29 TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
30 ~TestClientActiveObject() = default;
31 ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
32 virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {}
35 class TestClientActiveObjectMgr : public TestBase
38 TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
39 const char *getName() { return "TestClientActiveObjectMgr"; }
41 void runTests(IGameDef *gamedef);
44 void testRegisterObject();
45 void testRemoveObject();
48 static TestClientActiveObjectMgr g_test_instance;
50 void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
53 TEST(testRegisterObject)
54 TEST(testRemoveObject)
57 ////////////////////////////////////////////////////////////////////////////////
59 void TestClientActiveObjectMgr::testFreeID()
61 client::ActiveObjectMgr caomgr;
62 std::vector<u16> aoids;
64 u16 aoid = caomgr.getFreeId();
65 // Ensure it's not the same id
66 UASSERT(caomgr.getFreeId() != aoid);
68 aoids.push_back(aoid);
70 // Register basic objects, ensure we never found
71 for (u8 i = 0; i < UINT8_MAX; i++) {
73 auto tcao = new TestClientActiveObject();
74 caomgr.registerObject(tcao);
75 aoids.push_back(tcao->getId());
77 // Ensure next id is not in registered list
78 UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
85 void TestClientActiveObjectMgr::testRegisterObject()
87 client::ActiveObjectMgr caomgr;
88 auto tcao = new TestClientActiveObject();
89 UASSERT(caomgr.registerObject(tcao));
91 u16 id = tcao->getId();
93 auto tcaoToCompare = caomgr.getActiveObject(id);
94 UASSERT(tcaoToCompare->getId() == id);
95 UASSERT(tcaoToCompare == tcao);
97 tcao = new TestClientActiveObject();
98 UASSERT(caomgr.registerObject(tcao));
99 UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
100 UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
105 void TestClientActiveObjectMgr::testRemoveObject()
107 client::ActiveObjectMgr caomgr;
108 auto tcao = new TestClientActiveObject();
109 UASSERT(caomgr.registerObject(tcao));
111 u16 id = tcao->getId();
112 UASSERT(caomgr.getActiveObject(id) != nullptr)
114 caomgr.removeObject(tcao->getId());
115 UASSERT(caomgr.getActiveObject(id) == nullptr)