From cf31daa35ac163986158b6f95df06fd7b43f6e79 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 18 Dec 2017 17:23:51 +0100 Subject: [PATCH] test basic interaction --- src/app/MasterState.hpp | 3 ++ tst/app/MasterTest.cpp | 102 ++++++++++++++++++++++++++++++++++++++++ tst/app/MasterTest.hpp | 2 + tst/event.hpp | 77 ++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 tst/event.hpp diff --git a/src/app/MasterState.hpp b/src/app/MasterState.hpp index cb95215..77d00cf 100644 --- a/src/app/MasterState.hpp +++ b/src/app/MasterState.hpp @@ -41,6 +41,9 @@ public: graphics::Camera &GetCamera() noexcept { return cam; } const graphics::Camera &GetCamera() const noexcept { return cam; } + ui::BodyPanel &GetBodyPanel() noexcept { return bp; } + const ui::BodyPanel &GetBodyPanel() const noexcept { return bp; } + ui::CreaturePanel &GetCreaturePanel() noexcept { return cp; } const ui::CreaturePanel &GetCreaturePanel() const noexcept { return cp; } diff --git a/tst/app/MasterTest.cpp b/tst/app/MasterTest.cpp index c9ab2d9..1624efa 100644 --- a/tst/app/MasterTest.cpp +++ b/tst/app/MasterTest.cpp @@ -1,14 +1,21 @@ #include "MasterTest.hpp" +#include "../event.hpp" + #include "app/Application.hpp" #include "app/Assets.hpp" #include "app/init.hpp" #include "app/MasterState.hpp" #include "creature/Creature.hpp" +#include "world/Planet.hpp" #include "world/Simulation.hpp" CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(blobs::app::test::MasterTest, "headed"); +using blobs::test::FakeKeyPress; +using blobs::test::FakeMouseClick; +using blobs::test::FakeQuit; + namespace blobs { namespace app { @@ -47,6 +54,101 @@ void MasterTest::testOneSecond() { } } +void MasterTest::testBasicInteraction() { + Init init(false, 1); + Assets assets; + + world::Simulation sim(assets); + assets.LoadUniverse("universe", sim); + + auto blob = new creature::Creature(sim); + blob->Name(assets.name.Sequential()); + Spawn(*blob, sim.PlanetByName("Planet")); + // decrease chances of ur-blob dying without splitting + blob->GetProperties().Fertility() = 1.0; + blob->BuildVAO(); + + app::MasterState state(assets, sim); + state.Show(*blob); + + app::Application app(init.window, init.viewport); + app.PushState(&state); + + // skip first 200ms to let camera settle + for (int t = 0; t < 200; t += 17) { + app.Loop(17); + } + + CPPUNIT_ASSERT_MESSAGE( + "creature panel not shown", + state.GetCreaturePanel().Shown() + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "creature panel showing wrong creature", + const_cast(blob), &state.GetCreaturePanel().GetCreature() + ); + CPPUNIT_ASSERT_MESSAGE( + "records panel not shown", + state.GetRecordsPanel().Shown() + ); + CPPUNIT_ASSERT_MESSAGE( + "body panel shown", + !state.GetBodyPanel().Shown() + ); + + // hide records panel + FakeKeyPress(SDLK_F1); + app.Loop(17); + CPPUNIT_ASSERT_MESSAGE( + "records panel shown", + !state.GetRecordsPanel().Shown() + ); + + // show records panel + FakeKeyPress(SDLK_F1); + app.Loop(17); + CPPUNIT_ASSERT_MESSAGE( + "records panel not shown", + state.GetRecordsPanel().Shown() + ); + + // click on blob + FakeMouseClick(SDL_BUTTON_LEFT, init.viewport.Width() / 2, init.viewport.Height() / 2); + app.Loop(17); + CPPUNIT_ASSERT_MESSAGE( + "creature panel not shown", + state.GetCreaturePanel().Shown() + ); + CPPUNIT_ASSERT_MESSAGE( + "body panel shown", + !state.GetBodyPanel().Shown() + ); + + // click on planet + FakeMouseClick(SDL_BUTTON_LEFT, init.viewport.Width() / 3, init.viewport.Height() / 2); + app.Loop(17); + CPPUNIT_ASSERT_MESSAGE( + "creature panel shown", + !state.GetCreaturePanel().Shown() + ); + CPPUNIT_ASSERT_MESSAGE( + "body panel not shown", + state.GetBodyPanel().Shown() + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "body panel showing wrong body", + static_cast(&sim.PlanetByName("Planet")), &state.GetBodyPanel().GetBody() + ); + + // close window + FakeQuit(); + app.Loop(17); + CPPUNIT_ASSERT_MESSAGE( + "app didn't exit after quit event", + !app.HasState() + ); +} + } } } diff --git a/tst/app/MasterTest.hpp b/tst/app/MasterTest.hpp index 14df37d..ecccafe 100644 --- a/tst/app/MasterTest.hpp +++ b/tst/app/MasterTest.hpp @@ -14,6 +14,7 @@ class MasterTest CPPUNIT_TEST_SUITE(MasterTest); CPPUNIT_TEST(testOneSecond); +CPPUNIT_TEST(testBasicInteraction); CPPUNIT_TEST_SUITE_END(); @@ -22,6 +23,7 @@ public: void tearDown(); void testOneSecond(); + void testBasicInteraction(); }; diff --git a/tst/event.hpp b/tst/event.hpp new file mode 100644 index 0000000..315d0d4 --- /dev/null +++ b/tst/event.hpp @@ -0,0 +1,77 @@ +#ifndef BLOBS_TEST_EVENT_HPP_ +#define BLOBS_TEST_EVENT_HPP_ + +#include + + +namespace blobs { +namespace test { + +inline void FakeKeyDown(int sym) { + SDL_Event e; + e.type = SDL_KEYDOWN; + e.key.keysym.sym = sym; + SDL_PushEvent(&e); +} + +inline void FakeKeyUp(int sym) { + SDL_Event e; + e.type = SDL_KEYUP; + e.key.keysym.sym = sym; + SDL_PushEvent(&e); +} + +inline void FakeKeyPress(int sym) { + FakeKeyDown(sym); + FakeKeyUp(sym); +} + +inline void FakeMouseDown(int button = SDL_BUTTON_LEFT, int x = 0, int y = 0) { + SDL_Event e; + e.type = SDL_MOUSEBUTTONDOWN; + e.button.button = button; + e.button.x = x; + e.button.y = y; + SDL_PushEvent(&e); +} + +inline void FakeMouseUp(int button = SDL_BUTTON_LEFT, int x = 0, int y = 0) { + SDL_Event e; + e.type = SDL_MOUSEBUTTONUP; + e.button.button = button; + e.button.x = x; + e.button.y = y; + SDL_PushEvent(&e); +} + +inline void FakeMouseClick(int button = SDL_BUTTON_LEFT, int x = 0, int y = 0) { + FakeMouseDown(button, x, y); + FakeMouseUp(button, x, y); +} + +inline void FakeMouseMotion(int xrel = 0, int yrel = 0) { + SDL_Event e; + e.type = SDL_MOUSEMOTION; + e.motion.xrel = xrel; + e.motion.yrel = yrel; + SDL_PushEvent(&e); +} + +inline void FakeMouseWheel(int y = 0, int x = 0) { + SDL_Event e; + e.type = SDL_MOUSEWHEEL; + e.wheel.x = x; + e.wheel.y = y; + SDL_PushEvent(&e); +} + +inline void FakeQuit() { + SDL_Event e; + e.type = SDL_QUIT; + SDL_PushEvent(&e); +} + +} +} + +#endif -- 2.39.2