Skip to content

Commit

Permalink
#5310: Add unit tests covering the commands SetActiveCameraPosition a…
Browse files Browse the repository at this point in the history
…nd SetActiveCameraAngles.
  • Loading branch information
codereader committed Oct 3, 2020
1 parent 41e3b4c commit debae72
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 27 deletions.
53 changes: 29 additions & 24 deletions test/CSG.cpp
Expand Up @@ -5,6 +5,9 @@
#include "entitylib.h"
#include "algorithm/Scene.h"

namespace test
{

TEST_F(RadiantTest, CSGMergeTwoRegularWorldspawnBrushes)
{
loadMap("csg_merge.map");
Expand All @@ -13,8 +16,8 @@ TEST_F(RadiantTest, CSGMergeTwoRegularWorldspawnBrushes)
auto worldspawn = GlobalMapModule().getWorldspawn();

// Try to merge the two brushes with the "1" and "2" materials
auto firstBrush = test::algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto secondBrush = test::algorithm::findFirstBrushWithMaterial(worldspawn, "2");
auto firstBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto secondBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "2");

ASSERT_TRUE(Node_getIBrush(firstBrush)->getNumFaces() == 5);
ASSERT_TRUE(Node_getIBrush(secondBrush)->getNumFaces() == 5);
Expand All @@ -32,8 +35,8 @@ TEST_F(RadiantTest, CSGMergeTwoRegularWorldspawnBrushes)
ASSERT_TRUE(secondBrush->getParent() == nullptr);

// The merged brush will carry both materials
auto brushWithMaterial1 = test::algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto brushWithMaterial2 = test::algorithm::findFirstBrushWithMaterial(worldspawn, "2");
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto brushWithMaterial2 = algorithm::findFirstBrushWithMaterial(worldspawn, "2");

ASSERT_TRUE(brushWithMaterial1 == brushWithMaterial2);
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
Expand All @@ -48,10 +51,10 @@ TEST_F(RadiantTest, CSGMergeFourRegularWorldspawnBrushes)

// Try to merge the two brushes with the "1" and "2" materials
std::vector<scene::INodePtr> brushes = {
test::algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
test::algorithm::findFirstBrushWithMaterial(worldspawn, "2"),
test::algorithm::findFirstBrushWithMaterial(worldspawn, "3"),
test::algorithm::findFirstBrushWithMaterial(worldspawn, "4")
algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
algorithm::findFirstBrushWithMaterial(worldspawn, "2"),
algorithm::findFirstBrushWithMaterial(worldspawn, "3"),
algorithm::findFirstBrushWithMaterial(worldspawn, "4")
};

// Check the correct setup
Expand All @@ -77,7 +80,7 @@ TEST_F(RadiantTest, CSGMergeFourRegularWorldspawnBrushes)
}

// The combined brush should be a 6-sided cuboid
auto brushWithMaterial1 = test::algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
}

Expand All @@ -92,8 +95,8 @@ TEST_F(RadiantTest, CSGMergeTwoFuncStaticBrushes)
auto entity = walker.getEntityNode();

// Try to merge the two brushes with the "1" and "2" materials
auto firstBrush = test::algorithm::findFirstBrushWithMaterial(entity, "1");
auto secondBrush = test::algorithm::findFirstBrushWithMaterial(entity, "2");
auto firstBrush = algorithm::findFirstBrushWithMaterial(entity, "1");
auto secondBrush = algorithm::findFirstBrushWithMaterial(entity, "2");

ASSERT_TRUE(Node_getIBrush(firstBrush)->getNumFaces() == 5);
ASSERT_TRUE(Node_getIBrush(secondBrush)->getNumFaces() == 5);
Expand All @@ -109,10 +112,10 @@ TEST_F(RadiantTest, CSGMergeTwoFuncStaticBrushes)
// The two brushes should be gone, replaced by a new one
ASSERT_TRUE(firstBrush->getParent() == nullptr);
ASSERT_TRUE(secondBrush->getParent() == nullptr);

// The merged brush will carry both materials
auto brushWithMaterial1 = test::algorithm::findFirstBrushWithMaterial(entity, "1");
auto brushWithMaterial2 = test::algorithm::findFirstBrushWithMaterial(entity, "2");
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(entity, "1");
auto brushWithMaterial2 = algorithm::findFirstBrushWithMaterial(entity, "2");

ASSERT_TRUE(brushWithMaterial1 == brushWithMaterial2);
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
Expand All @@ -137,10 +140,10 @@ TEST_F(RadiantTest, CSGMergeBrushesOfMixedEntitySelection)

// Select the mergeable brushes of both entities carrying the "1" and "2" materials
std::vector<scene::INodePtr> brushes = {
test::algorithm::findFirstBrushWithMaterial(entity, "1"),
test::algorithm::findFirstBrushWithMaterial(entity, "2"),
test::algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
test::algorithm::findFirstBrushWithMaterial(worldspawn, "2")
algorithm::findFirstBrushWithMaterial(entity, "1"),
algorithm::findFirstBrushWithMaterial(entity, "2"),
algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
algorithm::findFirstBrushWithMaterial(worldspawn, "2")
};

// Check the correct setup
Expand All @@ -167,16 +170,16 @@ TEST_F(RadiantTest, CSGMergeBrushesOfMixedEntitySelection)
}

// The merged brush will carry both materials
auto funcBrush1 = test::algorithm::findFirstBrushWithMaterial(entity, "1");
auto funcBrush2 = test::algorithm::findFirstBrushWithMaterial(entity, "2");
auto funcBrush1 = algorithm::findFirstBrushWithMaterial(entity, "1");
auto funcBrush2 = algorithm::findFirstBrushWithMaterial(entity, "2");

ASSERT_TRUE(funcBrush1);
ASSERT_TRUE(funcBrush1 == funcBrush2);
ASSERT_TRUE(Node_getIBrush(funcBrush1)->getNumFaces() == 6);

// Same for the worldspawn entity
auto worldBrush1 = test::algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto worldBrush2 = test::algorithm::findFirstBrushWithMaterial(worldspawn, "2");
auto worldBrush1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto worldBrush2 = algorithm::findFirstBrushWithMaterial(worldspawn, "2");

ASSERT_TRUE(worldBrush1);
ASSERT_TRUE(worldBrush1 == worldBrush2);
Expand All @@ -189,7 +192,7 @@ TEST_F(RadiantTest, CSGMergeWithFuncStatic)
loadMap("csg_merge_with_func_static.map");

// Locate the first worldspawn brush
auto firstBrush = test::algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0);
auto firstBrush = algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0);
ASSERT_TRUE(firstBrush);

// Locate the func_static in the map
Expand All @@ -210,11 +213,13 @@ TEST_F(RadiantTest, CSGMergeWithFuncStatic)
// No merge should have happened since the brushes
// are not part of the same entity
// So assume the scene didn't change
ASSERT_TRUE(test::algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0) == firstBrush);
ASSERT_TRUE(algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0) == firstBrush);

EntityNodeFindByClassnameWalker walker2("func_static");
GlobalSceneGraph().root()->traverse(walker2);

ASSERT_TRUE(walker.getEntityNode());
ASSERT_TRUE(walker.getEntityNode()->hasChildNodes());
}

}
59 changes: 59 additions & 0 deletions test/Camera.cpp
@@ -0,0 +1,59 @@
#include "RadiantTest.h"

#include "icameraview.h"
#include "icommandsystem.h"
#include "render/View.h"

namespace test
{

TEST_F(RadiantTest, SetCameraPositionAndAngles)
{
// Assume that we don't have any camera at first
bool exceptionThrown = false;

try
{
GlobalCameraManager().getActiveView();
}
catch (const std::runtime_error&)
{
exceptionThrown = true;
}

ASSERT_TRUE(exceptionThrown);

render::View view;
auto allocatedCam = GlobalCameraManager().createCamera(view, [](bool) {});

// We should be able to get an active camera now
exceptionThrown = false;
try
{
auto& camera = GlobalCameraManager().getActiveView();
auto oldPosition = camera.getCameraOrigin();
auto oldAngles = camera.getCameraAngles();

// Run the command, and compare the camera's angles
GlobalCommandSystem().executeCommand("SetActiveCameraPosition", cmd::Argument(oldPosition + Vector3(50,50,50)));

auto newPosition = camera.getCameraOrigin();
ASSERT_TRUE(oldPosition != newPosition); // position changed
ASSERT_TRUE(camera.getCameraAngles() == oldAngles); // angles unchanged

GlobalCommandSystem().executeCommand("SetActiveCameraAngles", cmd::Argument(oldAngles + Vector3(10,10,10)));

ASSERT_TRUE(camera.getCameraOrigin() == newPosition); // position unchanged
ASSERT_TRUE(camera.getCameraAngles() != oldAngles); // angles changed
}
catch (const std::runtime_error&)
{
exceptionThrown = true;
}

ASSERT_TRUE(exceptionThrown == false);

GlobalCameraManager().destroyCamera(allocatedCam);
}

}
11 changes: 8 additions & 3 deletions test/RadiantTest.h
Expand Up @@ -14,6 +14,9 @@
#include "module/CoreModule.h"
#include "messages/GameConfigNeededMessage.h"

namespace test
{

/**
* Test fixture setting up the application context and
* the radiant core module.
Expand Down Expand Up @@ -66,8 +69,8 @@ class RadiantTest :
// Wire up the game-config-needed handler, we need to respond
_gameSetupListener = _coreModule->get()->getMessageBus().addListener(
radiant::IMessage::Type::GameConfigNeeded,
radiant::TypeListener<game::ConfigurationNeeded>(
sigc::mem_fun(this, &RadiantTest::handleGameConfigMessage)));
radiant::TypeListener<game::ConfigurationNeeded>(
sigc::mem_fun(this, &RadiantTest::handleGameConfigMessage)));

try
{
Expand Down Expand Up @@ -123,4 +126,6 @@ class RadiantTest :
message.setConfig(config);
message.setHandled(true);
}
};
};

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -61,6 +61,7 @@
<ClInclude Include="..\..\..\test\TestContext.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\test\Camera.cpp" />
<ClCompile Include="..\..\..\test\CSG.cpp" />
<ClCompile Include="..\..\..\test\HeadlessOpenGLContext.cpp" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -3,6 +3,7 @@
<ItemGroup>
<ClCompile Include="..\..\..\test\CSG.cpp" />
<ClCompile Include="..\..\..\test\HeadlessOpenGLContext.cpp" />
<ClCompile Include="..\..\..\test\Camera.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit debae72

Please sign in to comment.