Skip to content

Commit

Permalink
test: add tests for message window widgets
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Takacs <norbert.takacs@commsignia.net>
  • Loading branch information
Norbert Takacs committed Aug 10, 2023
1 parent 9813450 commit c4a7910
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 5 deletions.
169 changes: 168 additions & 1 deletion test/mock_xplane_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <vector>
#include <map>
#include "XPLMGraphics.h"
#include "XPLMPlanes.h"
#include "XPLMDisplay.h"
#include "XPLMDataAccess.h"
#include "XPLMUtilities.h"
#include "XPLMMenus.h"
#include "XPLMProcessing.h"
#include "XPWidgets.h"
#include "XPWidgetDefs.h"
#include "XPStandardWidgets.h"

extern void XPLMSetGraphicsState(
int inEnableFog,
Expand Down Expand Up @@ -90,3 +94,166 @@ extern void XPLMDestroyWindow(
{

}

struct WidgetMock {
WidgetMock()
:callback(NULL),visible(false)
{
properties.clear();
}
std::map<XPWidgetPropertyID, intptr_t> properties;
XPWidgetFunc_t callback;
bool visible;
};

std::vector<WidgetMock> test_widgets;

extern void XPAddWidgetCallback(
XPWidgetID inWidget,
XPWidgetFunc_t inNewCallback)
{
WidgetMock* widget_ptr = (WidgetMock*)inWidget;
widget_ptr->callback = inNewCallback;
}

/* if widget_id is NULL we invoke the callbacks of all widgets */
int test_invoke_widget_callback(XPWidgetMessage message, XPWidgetID widget_id, intptr_t param1, intptr_t param2)
{
WidgetMock* widget_ptr = (WidgetMock*)widget_id;
if (!widget_ptr)
{
for (const auto& widget : test_widgets)
{
if (widget.callback != NULL)
widget.callback(message, (XPWidgetID) &widget, param1, param2);
}
return 1;
}
else
{
if (!widget_ptr->callback)
return 0;

return widget_ptr->callback(message, widget_id, param1, param2);
}
}

extern XPWidgetID XPCreateWidget(
int inLeft,
int inTop,
int inRight,
int inBottom,
int inVisible,
const char* inDescriptor,
int inIsRoot,
XPWidgetID inContainer,
XPWidgetClass inClass)
{
test_widgets.emplace_back();
return (XPWidgetID)&test_widgets.back();
}

extern XPWidgetID XPCreateCustomWidget(
int inLeft,
int inTop,
int inRight,
int inBottom,
int inVisible,
const char* inDescriptor,
int inIsRoot,
XPWidgetID inContainer,
XPWidgetFunc_t inCallback)
{
test_widgets.emplace_back();
test_widgets.back().callback = inCallback;

return (XPWidgetID)&test_widgets.back();
}

extern void XPDestroyWidget(
XPWidgetID inWidget,
int inDestroyChildren)
{

}

extern void XPShowWidget(XPWidgetID inWidget)
{
WidgetMock* widget_ptr = (WidgetMock*)inWidget;
widget_ptr->visible = true;
}

extern void XPHideWidget( XPWidgetID inWidget )
{
WidgetMock* widget_ptr = (WidgetMock*)inWidget;
widget_ptr->visible = false;
}

extern void XPSetWidgetProperty(
XPWidgetID inWidget,
XPWidgetPropertyID inProperty,
intptr_t inValue)
{
WidgetMock* widget_ptr = (WidgetMock*)inWidget;
widget_ptr->properties[inProperty] = inValue;
}

extern intptr_t XPGetWidgetProperty(
XPWidgetID inWidget,
XPWidgetPropertyID inProperty,
int* inExists)
{
WidgetMock* widget_ptr = (WidgetMock*)inWidget;
if (widget_ptr != NULL && widget_ptr->properties.count(inProperty) > 0)
{
if (inExists != NULL)
*inExists = 1;

return widget_ptr->properties[inProperty];
}
else if (inExists != NULL)
{
*inExists = 0;
return 0;
}
else
{
return 0;
}
}

extern void XPLMGetFontDimensions(
XPLMFontID inFontID,
int* outCharWidth, /* Can be NULL */
int* outCharHeight, /* Can be NULL */
int* outDigitsOnly)
{
if (outCharWidth)
*outCharWidth = 5;

if (outCharHeight)
*outCharHeight = 6;

if (outDigitsOnly)
*outDigitsOnly = 0;
}

extern void XPGetWidgetGeometry(
XPWidgetID inWidget,
int* outLeft, /* Can be NULL */
int* outTop, /* Can be NULL */
int* outRight, /* Can be NULL */
int* outBottom)
{
if (outLeft)
*outLeft = 10;

if (outTop)
*outTop = 20;

if (outRight)
*outRight = 610;

if (outBottom)
*outBottom = 620;
}
3 changes: 2 additions & 1 deletion test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ xcopy /y /d "$(SolutionDir)3rdparty\FIP-SDK\fonts\fip-fonts.bmp" "$(OutDir)"
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\src\config-ui\MessageWindow.cpp" />
<ClCompile Include="..\src\core\Action.cpp" />
<ClCompile Include="..\src\core\AgeingCounter.cpp" />
<ClCompile Include="..\src\core\ConfigParser.cpp" />
Expand Down Expand Up @@ -214,6 +213,7 @@ xcopy /y /d "$(SolutionDir)3rdparty\FIP-SDK\fonts\fip-fonts.bmp" "$(OutDir)"
<ClCompile Include="..\src\devices\trc-1000\TRC1000.cpp" />
<ClCompile Include="..\src\devices\trc-1000\TRC1000Audio.cpp" />
<ClCompile Include="..\src\devices\trc-1000\TRC1000PFD.cpp" />
<ClCompile Include="..\src\log-message-window\MessageWindow.cpp" />
<ClCompile Include="mock_FIPDriver.cpp" />
<ClCompile Include="mock_hid_api.cpp" />
<ClCompile Include="mock_xplane.cpp" />
Expand All @@ -224,6 +224,7 @@ xcopy /y /d "$(SolutionDir)3rdparty\FIP-SDK\fonts\fip-fonts.bmp" "$(OutDir)"
<ClCompile Include="test_dynamic_speed.cpp" />
<ClCompile Include="test_FIP.cpp" />
<ClCompile Include="test_generic_display.cpp" />
<ClCompile Include="test_log_window.cpp" />
<ClCompile Include="test_lua.cpp" />
<ClCompile Include="test_multi_panel.cpp" />
<ClCompile Include="test_radio_panel.cpp" />
Expand Down
9 changes: 6 additions & 3 deletions test/test.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@
<ClCompile Include="..\src\core\LuaHelper.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="..\src\config-ui\MessageWindow.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="..\src\core\MultiPurposeDisplay.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
Expand Down Expand Up @@ -150,5 +147,11 @@
<ClCompile Include="..\src\core\XPanel.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="..\src\log-message-window\MessageWindow.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="test_log_window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions test/test_log_window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 Norbert Takacs
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <filesystem>
#include "XPLMDefs.h"
#include "XPLMGraphics.h"
#include "XPLMPlugin.h"
#include "XPLMDisplay.h"
#include "XPWidgetDefs.h"

#include "CppUnitTest.h"
#include "core/ConfigParser.h"
#include "log-message-window/MessageWindow.h"

int test_invoke_widget_callback(XPWidgetMessage message, XPWidgetID widget_id, intptr_t param1, intptr_t param2);

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace test
{
TEST_CLASS(TestLogWindow)
{
private:

public:
TEST_METHOD_INITIALIZE(TestLogWindowInit)
{

}

TEST_METHOD(TestLogWindowCallback)
{
MessageWindow message_window("test window");
message_window.append_line(std::string("test message 1"));
message_window.append_line(std::string("test message 2"));
message_window.show();

Assert::AreEqual(1, test_invoke_widget_callback(xpMsg_Draw, NULL, 0, 0), L"invoke all callback functions: failed", 0);
}

TEST_METHOD_CLEANUP(TestLogWindowCleanup)
{

}
};
}

0 comments on commit c4a7910

Please sign in to comment.