Repository navigation
Expand file tree
/
Copy pathcanvas_tests.cpp
More file actions
188 lines (168 loc) · 7.2 KB
/
Copy pathcanvas_tests.cpp
File metadata and controls
188 lines (168 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//------------------------------------------------------------------------------
// The editor in host windows that clip or skip its canvas.
//------------------------------------------------------------------------------
# include "harness.h"
# include <cfloat>
# include <cstring>
# include <functional>
TEST_CASE(ClippedEditor, "canvas.clipped_editor_is_not_drawn_untransformed", "CNV-2", test::TestFlags_None)
{
test::Storage storage;
test::Session session(&storage);
session.HostPos = ImVec2(0.0f, 0.0f);
session.HostSize = ImVec2(400.0f, 300.0f);
session.HostFlags = ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar;
session.EditorSize = ImVec2(380.0f, 250.0f);
// The editor sits below the visible area of the scrolling host window.
int verticesBefore = 0;
float editorTop = 0.0f;
ImVec2 cursorAfter;
ed::NodeId hovered;
session.BeforeEditor = [&]
{
ImGui::TextUnformatted("Scroll down to see the node editor");
ImGui::Dummy(ImVec2(10.0f, 800.0f));
verticesBefore = session.DrawList->VtxBuffer.Size;
editorTop = ImGui::GetCursorScreenPos().y;
};
ImVec2 editorMin, editorMax;
session.AfterEditor = [&]
{
cursorAfter = ImGui::GetCursorScreenPos();
hovered = session.Use([] { return ed::GetHoveredNode(); });
editorMin = ImVec2(FLT_MAX, FLT_MAX);
editorMax = ImVec2(-FLT_MAX, -FLT_MAX);
for (int i = verticesBefore; i < session.DrawList->VtxBuffer.Size; ++i)
{
editorMin = ImMin(editorMin, session.DrawList->VtxBuffer[i].pos);
editorMax = ImMax(editorMax, session.DrawList->VtxBuffer[i].pos);
}
};
bool suspended = false;
test::MouseMove(ImVec2(140.0f, 120.0f));
for (int i = 0; i < 6; ++i)
session.Frame([&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Node 1");
ed::EndNode();
ed::Suspend();
suspended = ed::IsSuspended();
ed::Resume();
});
test::Check(editorMin.y >= editorTop, "editor draws nothing above its widget: geometry starts at y=%.0f, widget at y=%.0f", editorMin.y, editorTop);
test::Check(!hovered, "node isn't hovered through the visible part of the host window: hovered node %d", (int)hovered.Get());
test::Check(cursorAfter.y >= editorTop + session.EditorSize.y, "layout continues below the editor: cursor at y=%.0f, editor ends at y=%.0f", cursorAfter.y, editorTop + session.EditorSize.y);
test::Check(suspended, "IsSuspended() is true after Suspend()");
}
namespace {
// Shows a 400 x 250 group, hides the editor with 'setHidden' for a few frames
// while the application keeps submitting it, and checks the group afterwards.
void CheckGroupSurvivesHiding(test::Session& session, test::Storage& storage, const std::function<void(bool hidden)>& setHidden)
{
ImVec2 size;
float zoom = 0.0f;
auto scene = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Group");
ed::Group(ImVec2(400.0f, 250.0f));
ed::EndNode();
size = ed::GetNodeSize(1);
zoom = test::CurrentZoom();
};
for (int i = 0; i < 5; ++i)
session.Frame(scene);
const ImVec2 sizeBefore = size;
const float zoomBefore = zoom;
setHidden(true);
for (int i = 0; i < 3; ++i)
session.Frame(scene);
const float zoomHidden = zoom;
setHidden(false);
for (int i = 0; i < 5; ++i)
session.Frame(scene);
test::Check(zoomHidden == zoomBefore && zoom == zoomBefore, "view zoom kept while hidden: %g, before %g, after %g", zoomHidden, zoomBefore, zoom);
test::Check(size.x == sizeBefore.x && size.y == sizeBefore.y, "group size after the editor was hidden: (%g, %g), before (%g, %g)", size.x, size.y, sizeBefore.x, sizeBefore.y);
test::Check(strstr(storage.Global.c_str(), "\"group_size\":{\"x\":400") != nullptr, "saved group size unchanged: %s", storage.Global.c_str());
}
} // namespace
TEST_CASE(CollapsedHost, "canvas.collapsed_host_keeps_group_size", "CORE-2", test::TestFlags_None)
{
test::Storage storage;
test::Session session(&storage);
session.HostPos = ImVec2(0.0f, 0.0f);
session.HostSize = ImVec2(1000.0f, 700.0f);
session.HostFlags = ImGuiWindowFlags_NoSavedSettings;
CheckGroupSurvivesHiding(session, storage, [&](bool hidden) { session.HostCollapsed = hidden; });
}
TEST_CASE(ScrolledOutChild, "canvas.child_scrolled_out_of_view_keeps_group_size", "CORE-2", test::TestFlags_None)
{
// The editor sits in a child window, which content above it pushes out of view.
test::Storage storage;
test::Session session(&storage);
session.HostPos = ImVec2(0.0f, 0.0f);
session.HostSize = ImVec2(1000.0f, 700.0f);
bool pushedOut = false;
session.BeforeEditor = [&]
{
if (pushedOut)
ImGui::Dummy(ImVec2(10.0f, 2000.0f));
ImGui::BeginChild("Child", ImVec2(900.0f, 600.0f));
};
session.AfterEditor = []
{
ImGui::EndChild();
};
CheckGroupSurvivesHiding(session, storage, [&](bool hidden) { pushedOut = hidden; });
}
TEST_CASE(HiddenDockTab, "canvas.hidden_dock_tab_keeps_group_size", "CORE-2", test::TestFlags_None)
{
# if defined(IMGUI_HAS_DOCK)
// The host window is docked with another one, whose tab is selected for a while.
test::Storage storage;
test::Session session(&storage);
session.PlaceHost = false;
ImGui::SetCurrentContext(session.HostContext.Context);
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// Docked in the second frame, once both windows exist.
bool showOther = false;
session.BeforeHost = [&]
{
if (session.FrameIndex != 1)
return;
const ImGuiID node = ImGui::DockBuilderAddNode();
ImGui::DockBuilderSetNodePos(node, ImVec2(0.0f, 0.0f));
ImGui::DockBuilderSetNodeSize(node, ImVec2(1000.0f, 700.0f));
ImGui::DockBuilderDockWindow("Host", node);
ImGui::DockBuilderDockWindow("Other", node);
ImGui::DockBuilderFinish(node);
};
session.AfterHost = [&]
{
ImGui::Begin("Other");
ImGui::TextUnformatted("Other tab");
ImGui::End();
ImGui::SetWindowFocus(showOther ? "Other" : "Host");
};
CheckGroupSurvivesHiding(session, storage, [&](bool hidden) { showOther = hidden; });
# else
test::Skip("needs a docking build of Dear ImGui");
# endif
}
TEST_CASE(FirstFrameLayout, "canvas.first_frame_layout_matches_later_frames", "CNV-9", test::TestFlags_None)
{
// An editor 300 by 200, with content after it in the host window.
test::Storage storage;
test::Session session(&storage);
session.EditorSize = ImVec2(300.0f, 200.0f);
ImVec2 after[3];
session.AfterEditor = [&] { after[session.FrameIndex] = ImGui::GetCursorScreenPos(); };
session.Run(3);
test::Check(after[0].x == after[2].x && after[0].y == after[2].y,
"content after the editor starts at (%g, %g) on the first frame, as on later ones: (%g, %g)", after[0].x, after[0].y, after[2].x, after[2].y);
}