Skip to content

Commit

Permalink
Working Mock Backends (U8HFTzEd8f), testing needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed Jun 1, 2023
1 parent 43996db commit 3936b87
Show file tree
Hide file tree
Showing 16 changed files with 876 additions and 167 deletions.
2 changes: 1 addition & 1 deletion External/SimpleOverride
46 changes: 46 additions & 0 deletions Include/ssGUI/Backend/Mocks/BackendDrawingMock.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef SSGUI_BACKEND_DRAWING_MOCK_H
#define SSGUI_BACKEND_DRAWING_MOCK_H

//#define SSGUI_MOCK_ENABLE_LOG
#include "ssGUI/Backend/Mocks/MockMacro.hpp"

#include "ssGUI/Backend/Interfaces/BackendDrawingInterface.hpp"
#include <unordered_set>

Expand Down Expand Up @@ -34,7 +37,50 @@ namespace Backend
public:
BackendDrawingMock(ssGUI::Backend::BackendDrawingInterface* drawingInterface);
~BackendDrawingMock() override;

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendDrawingInterface*, UnderlyingInterface)

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::unordered_set<ssGUI::Backend::BackendImageInterface*>, CachedImage);

//function: GetStateCounter
int GetStateCounter() const;

//function: GetDrawnVertices
const std::vector<glm::vec2>& GetDrawnVertices() const;

//function: GetRenderedVertices
const std::vector<glm::vec2>& GetRenderedVertices() const;

//function: GetDrawnTexCoords
const std::vector<glm::vec2>& GetDrawnTexCoords() const;

//function: GetRenderedTexCoords
const std::vector<glm::vec2>& GetRenderedTexCoords() const;

//function: GetDrawnColors
const std::vector<glm::u8vec4>& GetDrawnColors() const;

//function: GetRenderedColors
const std::vector<glm::u8vec4>& GetRenderedColors() const;

//function: GetDrawnCounts
const std::vector<int>& GetDrawnCounts() const;

//function: GetRenderedCounts
const std::vector<int>& GetRenderedCounts() const;

//function: GetDrawnProperties
const std::vector<ssGUI::DrawingProperty>& GetDrawnProperties() const;

//function: GetRenderedProperties
const std::vector<ssGUI::DrawingProperty>& GetRenderedProperties() const;

//function: GetDrawnClearedColor
const glm::u8vec3& GetDrawnClearedColor() const;

//function: GetRenderedClearedColor
const glm::u8vec3& GetRenderedClearedColor() const;

//function: SaveState
//See <BackendDrawingInterface::SaveState>
void SaveState() override;
Expand Down
12 changes: 10 additions & 2 deletions Include/ssGUI/Backend/Mocks/BackendFontMock.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
#ifndef SSGUI_BACKEND_FONT_MOCK_H
#define SSGUI_BACKEND_FONT_MOCK_H

//#define SSGUI_MOCK_ENABLE_LOG
#include "ssGUI/Backend/Mocks/MockMacro.hpp"

#include "ssGUI/Backend/Interfaces/BackendFontInterface.hpp"
#include "FunctionOverrides.hpp"

namespace ssGUI
{

//namespace: ssGUI::Backend
namespace Backend
{
//TODO: Allow returning custom info
//class: ssGUI::Backend::BackendFontMock
class BackendFontMock : public BackendFontInterface
{
private:
FO_DECLARE_INSTNACE(OverrideObject);

ssGUI::Backend::BackendFontInterface* UnderlyingInterface;
bool Valid;

BackendFontMock& operator=(BackendFontMock const& other);

protected:
BackendFontMock(BackendFontMock const& other);

public:
FO_DECLARE_OVERRIDE_METHODS(OverrideObject)

BackendFontMock(ssGUI::Backend::BackendFontInterface* fontInterface);
~BackendFontMock() override;

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendFontInterface*, UnderlyingInterface)

//function: IsValid
//See <BackendFontInterface::IsValid>
Expand Down
10 changes: 9 additions & 1 deletion Include/ssGUI/Backend/Mocks/BackendImageMock.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef SSGUI_BACKEND_IMAGE_MOCK_H
#define SSGUI_BACKEND_IMAGE_MOCK_H

//#define SSGUI_MOCK_ENABLE_LOG
#include "ssGUI/Backend/Mocks/MockMacro.hpp"

#include "FunctionOverrides.hpp"
#include "ssGUI/Backend/Interfaces/BackendImageInterface.hpp"
#include <vector>

Expand All @@ -10,13 +14,13 @@ namespace ssGUI
//namespace: ssGUI::Backend
namespace Backend
{
//TODO: Allow returning custom info
//class: ssGUI::Backend::BackendImageMock
class BackendImageMock : public BackendImageInterface
{
private:
ssGUI::Backend::BackendImageInterface* UnderlyingInterface;
std::vector<ssGUI::Backend::BackendDrawingInterface*> LinkedBackendDrawing; //See <AddBackendDrawingLinking>
FO_DECLARE_INSTNACE(OverrideObject);

BackendImageMock& operator=(BackendImageMock const& other);

Expand All @@ -27,6 +31,10 @@ namespace Backend
BackendImageMock(ssGUI::Backend::BackendImageInterface* imageInterface);
~BackendImageMock() override;

FO_DECLARE_OVERRIDE_METHODS(OverrideObject)

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendImageInterface*, UnderlyingInterface)

//function: GetRawHandle
//See <BackendImageInterface::GetRawHandle>
void* GetRawHandle() override;
Expand Down
55 changes: 54 additions & 1 deletion Include/ssGUI/Backend/Mocks/BackendMainWindowMock.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef SSGUI_BACKEND_MAIN_WINDOW_MOCK_H
#define SSGUI_BACKEND_MAIN_WINDOW_MOCK_H

//#define SSGUI_MOCK_ENABLE_LOG
#include "ssGUI/Backend/Mocks/MockMacro.hpp"

#include "FunctionOverrides.hpp"
#include "ssGUI/Backend/Interfaces/BackendMainWindowInterface.hpp"

namespace ssGUI
Expand All @@ -9,12 +13,32 @@ namespace ssGUI
//namespace: ssGUI::Backend
namespace Backend
{
//TODO: Allow returning custom info
//class: ssGUI::Backend::BackendMainWindowMock
class BackendMainWindowMock : public BackendMainWindowInterface
{
private:
ssGUI::Backend::BackendMainWindowInterface* UnderlyingInterface;
glm::ivec2 WindowPosition;
glm::ivec2 PositionOffset;
glm::ivec2 WindowSize;
glm::ivec2 RenderSize;
std::vector<std::pair<std::function<void()>, bool>> OnCloseListeners;
bool Closed;
bool ClosingAborted;
std::wstring Title;
ssGUI::Backend::BackendImageInterface* IconImage;
bool Visible;
bool VSync;
bool Focused;
bool FocusSetByExternal;
std::vector<std::pair<std::function<void(bool)>, bool>> FocusChangedListeners;
int MSAA;
bool Titlebar;
bool Resizable;
bool CloseButton;
ssGUI::Enums::WindowMode WindowMode;

FO_DECLARE_INSTNACE(OverrideObject);

BackendMainWindowMock& operator=(BackendMainWindowMock const& other);

Expand All @@ -25,6 +49,35 @@ namespace Backend
BackendMainWindowMock(ssGUI::Backend::BackendMainWindowInterface* mainWindowInterface);
~BackendMainWindowMock() override;

FO_DECLARE_OVERRIDE_METHODS(OverrideObject)

void SetMockPositionOffset(glm::ivec2 offset);

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendMainWindowInterface*, UnderlyingInterface)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, WindowPosition)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, PositionOffset)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, WindowSize)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, RenderSize)

using CloseListeners = std::vector<std::pair<std::function<void()>, bool>>;
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(CloseListeners, OnCloseListeners)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, Closed)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, ClosingAborted)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::wstring, Title)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendImageInterface*, IconImage)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, Visible)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, VSync)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, Focused)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, FocusSetByExternal)

using FocusListeners = std::vector<std::pair<std::function<void(bool)>, bool>>;
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(FocusListeners, FocusChangedListeners)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(int, MSAA)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, Titlebar)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, Resizable)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(bool, CloseButton)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Enums::WindowMode, WindowMode)

//function: SetWindowPosition
//See <BackendMainWindowInterface::SetWindowPosition>
void SetWindowPosition(glm::ivec2 pos) override;
Expand Down
61 changes: 61 additions & 0 deletions Include/ssGUI/Backend/Mocks/BackendSystemInputMock.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@
#ifndef SSGUI_BACKEND_SYSTEM_INPUT_MOCK_H
#define SSGUI_BACKEND_SYSTEM_INPUT_MOCK_H

#include "FunctionOverrides.hpp"
#include "ssGUI/Backend/Interfaces/BackendSystemInputInterface.hpp"

//#define SSGUI_MOCK_ENABLE_LOG
#include "ssGUI/Backend/Mocks/MockMacro.hpp"

namespace ssGUI
{

//namespace: ssGUI::Backend
namespace Backend
{
struct MockCursorData
{
ssGUI::Backend::BackendImageInterface* CursorImage = nullptr;
std::string CursorName = "";
glm::ivec2 CursorSize;
glm::ivec2 Hotspot;

MockCursorData( ssGUI::Backend::BackendImageInterface* cursorImage,
std::string cursorName,
glm::ivec2 cursorSize,
glm::ivec2 hotspot) : CursorImage(cursorImage),
CursorName(cursorName),
CursorSize(cursorSize),
Hotspot(hotspot)
{}
};


//class: ssGUI::Backend::BackendSystemInputMock
class BackendSystemInputMock : public BackendSystemInputInterface
{
private:
ssGUI::Backend::BackendSystemInputInterface* UnderlyingInterface;
std::vector<ssGUI::Enums::GenericButtonAndKeyInput> LastKeyPresses;
std::vector<ssGUI::Enums::GenericButtonAndKeyInput> CurrentKeyPresses;

glm::ivec2 LastMousePosition;
glm::ivec2 CurrentMousePosition;
glm::vec2 ScrollDelta;
std::vector<ssGUI::RealtimeInputInfo> LastRealtimeInputs;
std::vector<ssGUI::RealtimeInputInfo> CurrentRealtimeInputs;
std::wstring CurrentTextInput;
ssGUI::Enums::CursorType CurrentCursorType;

std::vector<MockCursorData> CustomCursors;
int CurrentCursorIndex;
int RawEventHandleNextId;

std::wstring ClipboardText;
BackendImageInterface* ClipboardImg;
std::chrono::high_resolution_clock::time_point StartTime;


FO_DECLARE_INSTNACE(OverrideObject);

BackendSystemInputMock& operator=(BackendSystemInputMock const& other);

Expand All @@ -25,6 +67,25 @@ namespace Backend
BackendSystemInputMock(ssGUI::Backend::BackendSystemInputInterface* systemInputInterface);
~BackendSystemInputMock() override;

FO_DECLARE_OVERRIDE_METHODS(OverrideObject)

SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Backend::BackendSystemInputInterface*, UnderlyingInterface)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::vector<ssGUI::Enums::GenericButtonAndKeyInput>, LastKeyPresses)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::vector<ssGUI::Enums::GenericButtonAndKeyInput>, CurrentKeyPresses)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, LastMousePosition)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::ivec2, CurrentMousePosition)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(glm::vec2, ScrollDelta)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::vector<ssGUI::RealtimeInputInfo>, LastRealtimeInputs)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::vector<ssGUI::RealtimeInputInfo>, CurrentRealtimeInputs)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::wstring, CurrentTextInput)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(ssGUI::Enums::CursorType, CurrentCursorType)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::vector<MockCursorData>, CustomCursors)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(int, CurrentCursorIndex)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(int, RawEventHandleNextId)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::wstring, ClipboardText)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(BackendImageInterface*, ClipboardImg)
SSGUI_MOCK_DECLARE_VARIABLE_GETTER(std::chrono::high_resolution_clock::time_point, StartTime)

//function: UpdateInput
//See <BackendSystemInputInterface::UpdateInput>
void UpdateInput() override;
Expand Down
41 changes: 40 additions & 1 deletion Include/ssGUI/Backend/Mocks/MockMacro.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SSGUI_MOCK_MACRO_H
#define SSGUI_MOCK_MACRO_H

#include "ssLogger/ssLog.hpp"

#define SSGUI_MOCK_PASSTHROUGH(funcOp)\
do\
{\
Expand All @@ -13,7 +15,31 @@ while(0)
do\
{\
if(UnderlyingInterface != nullptr)\
return UnderlyingInterface->funcOp;\
{\
UnderlyingInterface->funcOp;\
return;\
}\
}\
while(0)


//#define SSGUI_MOCK_ENABLE_LOG
#ifdef SSGUI_MOCK_ENABLE_LOG
#define SSGUI_MOCK_INTERNAL_LOG_RESULT(res) ssLOG_LINE("Mock function returned with: "<<res)
#else
#define SSGUI_MOCK_INTERNAL_LOG_RESULT(res)
#endif


#define SSGUI_MOCK_PASSTHROUGH_AND_RETURN_FUNC(funcOp)\
do\
{\
if(UnderlyingInterface != nullptr)\
{\
auto result = UnderlyingInterface->funcOp;\
SSGUI_MOCK_INTERNAL_LOG_RESULT(result);\
return result;\
}\
}\
while(0)

Expand All @@ -24,9 +50,22 @@ do\
{\
bool result = UnderlyingInterface->funcOp;\
if(!result)\
{\
SSGUI_MOCK_INTERNAL_LOG_RESULT(result);\
return false;\
}\
}\
}\
while(0)

#ifdef SSGUI_MOCK_ENABLE_LOG
#define SSGUI_MOCK_LOG_FUNCTION_CALL() ssLOG_LINE("Mock function called");
#else
#define SSGUI_MOCK_LOG_FUNCTION_CALL()
#endif


#define SSGUI_MOCK_DECLARE_VARIABLE_GETTER(variableType, variableName)\
inline variableType& GetMock ## variableName () { return variableName; }

#endif
Loading

0 comments on commit 3936b87

Please sign in to comment.