Skip to content

Commit

Permalink
level info test and rendered text hook
Browse files Browse the repository at this point in the history
  • Loading branch information
chreden committed Feb 20, 2022
1 parent 1639fa5 commit da46300
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 48 deletions.
2 changes: 2 additions & 0 deletions external/imgui/imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3185,12 +3185,14 @@ extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect&
extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags);
extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...);
extern void ImGuiTrviewTestEngineHook_ItemText(ImGuiContext* ctx, ImGuiID id, const char* buf);
extern void ImGuiTrviewTestEngineHook_RenderedText(ImGuiContext* ctx, ImGuiID id, const char* buf);
extern const char* ImGuiTestEngine_FindItemDebugLabel(ImGuiContext* ctx, ImGuiID id);

#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box
#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional)
#define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log
#define IMGUI_TRVIEW_TEST_ENGINE_ITEM_TEXT(_ID,_BUF) if (g.TestEngineHookItems) ImGuiTrviewTestEngineHook_ItemText(&g, _ID, _BUF);
#define IMGUI_TRVIEW_TEST_ENGINE_RENDERED_TEXT(_ID,_BUF) if (g.TestEngineHookItems) ImGuiTrviewTestEngineHook_RenderedText(&g, _ID, _BUF);
#else
#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)0)
#define IMGUI_TRVIEW_TEST_ENGINE_ITEM_TEXT(_ID,_BUF) ((void)0)
Expand Down
2 changes: 2 additions & 0 deletions external/imgui/imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags)
return;
ImGuiContext& g = *GImGui;

IMGUI_TRVIEW_TEST_ENGINE_RENDERED_TEXT(window->IDStack.back(), text);

// Accept null ranges
if (text == text_end)
text = text_end = "";
Expand Down
22 changes: 22 additions & 0 deletions trview.app.tests/TestImgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ void ImGuiTrviewTestEngineHook_ItemText(ImGuiContext* ctx, ImGuiID id, const cha
test->add_item_text(id, buf);
}

void ImGuiTrviewTestEngineHook_RenderedText(ImGuiContext* ctx, ImGuiID id, const char* buf)
{
TestImgui* test = static_cast<TestImgui*>(ctx->TestEngine);
if (!test)
{
return;
}

test->add_rendered_text(id, buf);
}

namespace trview
{
namespace tests
Expand Down Expand Up @@ -184,6 +195,11 @@ namespace trview
_item_text[id] = text;
}

void TestImgui::add_rendered_text(ImGuiID id, const std::string& text)
{
_item_rendered_text[id].push_back(text);
}

void TestImgui::add_style_colours(ImGuiID id, const std::array<ImVec4, ImGuiCol_COUNT>& colours)
{
_item_colours[id] = colours;
Expand All @@ -204,6 +220,11 @@ namespace trview
return _item_text.find(id.id())->second;
}

std::vector<std::string> TestImgui::rendered_text(TestImGuiId id) const
{
return _item_rendered_text.find(id.id())->second;
}

void TestImgui::render(const std::function<void()>& pre_render_callback)
{
_backend.new_frame();
Expand Down Expand Up @@ -240,6 +261,7 @@ namespace trview
_item_flags.clear();
_item_colours.clear();
_item_text.clear();
_item_rendered_text.clear();
_tracking_id = 0;
}

Expand Down
3 changes: 3 additions & 0 deletions trview.app.tests/TestImgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ namespace trview
void add_status_flag(ImGuiID id, ImGuiItemStatusFlags flags);
void add_item_flag(ImGuiID id, ImGuiItemFlags flags);
void add_item_text(ImGuiID id, const std::string& text);
void add_rendered_text(ImGuiID id, const std::string& text);
void add_style_colours(ImGuiID id, const std::array<ImVec4, ImGuiCol_COUNT>& colours);
bool element_present(TestImGuiId id) const;
ImGuiItemStatusFlags status_flags(TestImGuiId id) const;
ImGuiItemFlags item_flags(TestImGuiId id) const;
std::string item_text(TestImGuiId id) const;
std::vector<std::string> rendered_text(TestImGuiId id) const;
Colour style_colour(TestImGuiId id, ImGuiCol colour) const;
void show_context_menu(const std::string& window_name);
void render();
Expand All @@ -55,6 +57,7 @@ namespace trview
std::unordered_map<ImGuiID, ImGuiItemFlags> _item_flags;
std::unordered_map<ImGuiID, std::array<ImVec4, ImGuiCol_COUNT>> _item_colours;
std::unordered_map<ImGuiID, std::string> _item_text;
std::unordered_map<ImGuiID, std::vector<std::string>> _item_rendered_text;
ImGuiID _tracking_id{ 0 };
ImGuiContext* _context{ nullptr };
NullImGuiBackend _backend;
Expand Down
52 changes: 4 additions & 48 deletions trview.app.tests/UI/LevelInfoTests.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
#include <trview.app/UI/LevelInfo.h>
#include <trview.common/Mocks/Windows/IShell.h>
#include <trview.app/Mocks/Graphics/ITextureStorage.h>
#include "TestImgui.h"

using namespace trview;
using namespace trview::mocks;
using namespace trview::tests;
using testing::Return;
/*

TEST(LevelInfo, NameUpdated)
{
MockTextureStorage texture_storage;
LevelInfo info(texture_storage);
// info.toggle_visibility();

TestImgui imgui([&]() { info.render(); });
// ui::Window parent(Size(), Colour::White);
// MockTextureStorage texture_storage;
// LevelInfo info(parent, texture_storage, JsonLoader(std::make_shared<MockShell>()));
info.set_level("test");
imgui.render();

ASSERT_EQ(imgui.item_text("LevelInfo", { }), "test");
auto label = parent.find<Label>(LevelInfo::Names::name);
ASSERT_NE(label, nullptr);
ASSERT_EQ(label->text(), L"test");
auto rendered = imgui.rendered_text(imgui.id("LevelInfo").id(""));
ASSERT_THAT(rendered, testing::Contains("test"));
}
*/

TEST(LevelInfo, OnToggleSettingsRaised)
{
Expand All @@ -45,36 +34,3 @@ TEST(LevelInfo, OnToggleSettingsRaised)
imgui.click_element(imgui.id("LevelInfo").id("Settings"));
ASSERT_TRUE(raised);
}

/*
TEST(LevelInfo, VersionImageChanged)
{
ui::Window parent(Size(), Colour::White);
MockTextureStorage texture_storage;
graphics::Texture texture;
texture.set_name("TR3");
ON_CALL(texture_storage, lookup("tomb3_icon")).WillByDefault(Return(texture));
LevelInfo info(parent, texture_storage, JsonLoader(std::make_shared<MockShell>()));
info.set_level_version(trlevel::LevelVersion::Tomb3);
auto image = parent.find<Image>(LevelInfo::Names::version);
ASSERT_NE(image, nullptr);
ASSERT_EQ(image->texture().name(), "TR3");
}
TEST(LevelInfo, CentresWhenParentResizes)
{
ui::Window parent(Size(400, 400), Colour::White);
MockTextureStorage texture_storage;
LevelInfo info(parent, texture_storage, JsonLoader(std::make_shared<MockShell>()));
auto window = parent.child_elements()[0];
ASSERT_NE(window, nullptr);
ASSERT_EQ(window->position().x, parent.size().width / 2 - window->size().width / 2);
parent.set_size(Size(800, 400));
ASSERT_EQ(window->position().x, parent.size().width / 2 - window->size().width / 2);
}
*/
5 changes: 5 additions & 0 deletions trview/trview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ void ImGuiTrviewTestEngineHook_ItemText(ImGuiContext* ctx, ImGuiID id, const cha
{
}

void ImGuiTrviewTestEngineHook_RenderedText(ImGuiContext* ctx, ImGuiID id, const char*)
{
}


int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int nCmdShow)
{
auto window = trview::create_window(hInstance, nCmdShow);
Expand Down

0 comments on commit da46300

Please sign in to comment.