Repository navigation
Expand file tree
/
Copy pathapi_tests.cpp
More file actions
516 lines (471 loc) · 19.1 KB
/
Copy pathapi_tests.cpp
File metadata and controls
516 lines (471 loc) · 19.1 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//------------------------------------------------------------------------------
// Public API misuse.
//------------------------------------------------------------------------------
# include "harness.h"
# include <type_traits>
# include <limits>
TEST_CASE(DestroyCurrentEditor, "api.destroying_current_editor_clears_it", "API-8", test::TestFlags_None)
{
test::Host host;
test::Storage storage;
const auto config = test::MakeConfig(&storage);
auto first = ed::CreateEditor(&config);
auto second = ed::CreateEditor(&config);
ed::SetCurrentEditor(first);
ed::DestroyEditor(second);
test::Check(ed::GetCurrentEditor() == first, "destroying another editor keeps the current one");
ed::DestroyEditor(first);
test::Check(ed::GetCurrentEditor() == nullptr, "destroying the current editor clears it");
test::Check(ed::GetConfig().UserPointer == nullptr, "GetConfig() without a current editor returns an empty config");
}
TEST_CASE(SelectTwiceDeselectOnce, "api.node_selected_twice_deselected_once", "CORE-14", test::TestFlags_None)
{
// The selection border is drawn magenta, which nothing else uses.
const ImU32 selectionColor = IM_COL32(255, 0, 255, 0);
test::Storage storage;
test::Session session(&storage);
session.DefaultContent = [&](int frame)
{
if (frame == 0)
{
ed::GetStyle().Colors[ed::StyleColor_SelNodeBorder] = ImColor(selectionColor | IM_COL32_A_MASK);
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
}
ed::BeginNode(1);
ImGui::TextUnformatted("Node");
ed::EndNode();
};
session.Run(3);
session.Use([]
{
ed::SelectNode(1, true);
ed::SelectNode(1, true);
ed::DeselectNode(1);
});
session.Run(2);
int selectionVertices = 0;
for (auto color : test::Capture(session.DrawList).Colors)
if ((color & ~IM_COL32_A_MASK) == selectionColor)
++selectionVertices;
const bool selected = session.Use([] { return ed::IsNodeSelected(1); });
const int count = session.Use([] { return ed::GetSelectedObjectCount(); });
test::Check(!selected && count == 0, "node 1 not selected: IsNodeSelected() %d, %d selected objects", selected ? 1 : 0, count);
test::Check(selectionVertices == 0, "node 1 not drawn as selected: %d selection border vertices", selectionVertices);
}
TEST_CASE(BackgroundDrawListOutsideFrame, "api.node_background_draw_list_between_frames", "API-13", test::TestFlags_None)
{
test::Storage storage;
test::Session session(&storage);
ImDrawList* inFrame = nullptr;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Node");
ed::EndNode();
inFrame = ed::GetNodeBackgroundDrawList(1);
};
session.Run(3);
ImDrawList* betweenFrames = session.Use([] { return ed::GetNodeBackgroundDrawList(1); });
test::Check(inFrame != nullptr, "available after EndNode()");
test::Check(betweenFrames == nullptr, "nullptr between frames");
}
TEST_CASE(PinSubmittedTwice, "api.pin_submitted_twice_in_a_node", "API-4", test::TestFlags_None)
{
// One pin ID used for an input and an output of the same node.
test::ExpectAsserts(4);
test::Storage storage;
test::Session session(&storage);
ImVec2 nodeCenter;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Node");
ed::BeginPin(2, ed::PinKind::Input);
ImGui::TextUnformatted("> in");
ed::EndPin();
ed::BeginPin(2, ed::PinKind::Output);
ImGui::TextUnformatted("out >");
ed::EndPin();
ed::EndNode();
nodeCenter = ed::CanvasToScreen(ed::GetNodePosition(1) + ed::GetNodeSize(1) * 0.5f);
};
// The editor walks the node's pins while the mouse is over it.
session.Run(2);
test::MouseMove(nodeCenter);
session.Run(2);
test::Check(true, "ed::End() returns with the mouse over the node");
}
TEST_CASE(NodePaddingScopedToBegin, "api.node_padding_scoped_to_begin_or_end_node", "API-5", test::TestFlags_None)
{
// ImGui's idiom of scoping a style var to the one call it affects.
test::Storage storage;
test::Session session(&storage);
ImVec2 sizes[2];
session.DefaultContent = [&](int frame)
{
if (frame == 0)
{
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::SetNodePosition(2, ImVec2(400.0f, 100.0f));
}
ed::PushStyleVar(ed::StyleVar_NodePadding, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
ed::BeginNode(1);
ed::PopStyleVar();
ImGui::TextUnformatted("Node");
ed::EndNode();
sizes[0] = ed::GetNodeSize(1);
ed::BeginNode(2);
ImGui::TextUnformatted("Node");
ed::PushStyleVar(ed::StyleVar_NodePadding, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
ed::EndNode();
ed::PopStyleVar();
sizes[1] = ed::GetNodeSize(2);
};
session.Run(3);
const ImVec2 textSize = ImGui::CalcTextSize("Node");
test::Check(sizes[0].x == ImFloor(textSize.x) && sizes[0].y == ImFloor(textSize.y), "node 1 has no padding: %g x %g", sizes[0].x, sizes[0].y);
test::Check(sizes[1].x > textSize.x && sizes[1].y > textSize.y, "node 2 has the default padding: %g x %g", sizes[1].x, sizes[1].y);
}
TEST_CASE(DrawListsInsideNode, "api.node_draw_lists_requested_inside_a_node", "API-6", test::TestFlags_None)
{
// Three frames, each asking for a node background draw list and a group hint inside a node.
test::ExpectAsserts(6);
test::Storage storage;
test::Session session(&storage);
ImDrawList* inside = nullptr;
ImDrawList* after = nullptr;
bool hint = true;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Node");
inside = ed::GetNodeBackgroundDrawList(1);
hint = ed::BeginGroupHint(1);
ed::EndNode();
after = ed::GetNodeBackgroundDrawList(1);
};
session.Run(3);
test::Check(inside == nullptr && !hint, "nothing returned inside the node: draw list %p, group hint %d", (void*)inside, hint ? 1 : 0);
test::Check(after != nullptr, "background draw list available after EndNode()");
}
TEST_CASE(StyleStackMisuse, "api.style_stack_misuse_asserts", "API-7", test::TestFlags_None)
{
// An extra pop of each stack, and a float pushed to the ImVec4 NodePadding.
test::ExpectAsserts(3);
test::Storage storage;
test::Session session(&storage);
ImVec4 padding;
session.Frame([&](int)
{
ed::PopStyleColor();
ed::PopStyleVar();
ed::PushStyleVar(ed::StyleVar_NodePadding, 1.0f);
padding = ed::GetStyle().NodePadding;
});
const ImVec4 defaultPadding = ed::Style().NodePadding;
test::Check(padding.x == defaultPadding.x && padding.y == defaultPadding.y && padding.z == defaultPadding.z && padding.w == defaultPadding.w,
"NodePadding unchanged by the wrong push: (%g, %g, %g, %g)", padding.x, padding.y, padding.z, padding.w);
}
TEST_CASE(StyleStackUnbalanced, "api.style_stack_unbalanced_in_frame_asserts", "API-7", test::TestFlags_None)
{
// Pushed before ed::Begin() and popped after ed::End() is fine; a push left inside is not.
test::ExpectAsserts(1);
test::Storage storage;
test::Session session(&storage);
session.Use([] { ed::PushStyleColor(ed::StyleColor_Bg, ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); });
session.Frame([](int) {});
session.Use([] { ed::PopStyleColor(); });
session.Frame([](int) { ed::PushStyleVar(ed::StyleVar_NodeRounding, 2.0f); });
session.Use([] { ed::PopStyleVar(); });
}
TEST_CASE(CallWithoutEditor, "api.call_without_current_editor_asserts", "API-8", test::TestFlags_None)
{
test::ExpectAsserts(1);
test::Host host;
ed::SetCurrentEditor(nullptr);
bool asserted = false;
try
{
test::ThrowOnAssert throwOnAssert;
ed::GetStyle();
}
catch (const test::AssertFailure&)
{
asserted = true;
}
test::Check(asserted, "ed::GetStyle() without a current editor asserts");
}
TEST_CASE(NonFinitePositions, "api.non_finite_positions_are_ignored", "CORE-17", test::TestFlags_None)
{
test::Storage storage;
test::Session session(&storage);
session.DefaultContent = [](int frame)
{
if (frame == 0)
{
ed::SetNodePosition(1, ImVec2(100.0f, 50.0f));
ed::SetGroupSize(1, ImVec2(200.0f, 120.0f));
}
ed::BeginNode(1);
ImGui::TextUnformatted("Group");
ed::Group(ImVec2(200.0f, 120.0f));
ed::EndNode();
};
session.Run(3);
const auto before = session.Use([] { return ed::GetNodePosition(1); });
session.Use([]
{
const auto nan = std::numeric_limits<float>::quiet_NaN();
const auto inf = std::numeric_limits<float>::infinity();
ed::SetNodePosition(1, ImVec2(nan, 0.0f));
ed::SetNodePosition(1, ImVec2(0.0f, inf));
ed::SetNodePosition(2, ImVec2(-inf, nan));
ed::SetGroupSize(1, ImVec2(nan, nan));
});
session.Run(2);
const auto after = session.Use([] { return ed::GetNodePosition(1); });
const auto other = session.Use([] { return ed::GetNodePosition(2); });
test::Check(after.x == before.x && after.y == before.y, "node 1 stays at (%g, %g): at (%g, %g)", before.x, before.y, after.x, after.y);
test::Check(other.x == FLT_MAX && other.y == FLT_MAX, "node 2 isn't created: GetNodePosition() reports (%g, %g)", other.x, other.y);
}
TEST_CASE(OrderedNodeCount, "api.ordered_node_ids_count_without_an_array", "API-13", test::TestFlags_None)
{
// Three nodes, counted without an array as GetSelectedNodes() counts.
test::Storage storage;
test::Session session(&storage);
int counts[2] = {};
session.DefaultContent = [&](int)
{
for (int id : { 1, 2, 3 })
{
ed::BeginNode(id);
ImGui::Text("Node %d", id);
ed::EndNode();
}
};
session.Run(2);
counts[0] = session.Use([] { return ed::GetOrderedNodeIds(nullptr, 0); });
counts[1] = session.Use([] { return ed::GetOrderedNodeIds(nullptr, 10); });
test::Check(counts[0] == 3 && counts[1] == 3, "the count is 3 whatever the size: %d and %d", counts[0], counts[1]);
}
TEST_CASE(MisnestedBuilding, "api.misnested_building_is_ignored", "API-13", test::TestFlags_None)
{
// Seven misuses, each asserting on both frames, before nodes 1 and 2 linked by
// link 100.
test::ExpectAsserts(14);
test::Storage storage;
test::Session session(&storage);
bool linked = false;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
{
ed::SetNodePosition(1, ImVec2(0.0f, 0.0f));
ed::SetNodePosition(2, ImVec2(300.0f, 0.0f));
ed::SetNodePosition(3, ImVec2(0.0f, 200.0f));
}
ed::BeginPin(5, ed::PinKind::Input); // outside a node
ed::EndPin();
ed::EndNode(); // without BeginNode()
ed::EndPin(); // without BeginPin()
ed::PinRect(ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f)); // outside a pin
ed::BeginNode(1);
ed::BeginNode(4); // inside another node
ImGui::TextUnformatted("Nested");
ed::EndNode();
ed::BeginPin(11, ed::PinKind::Output);
ed::BeginPin(12, ed::PinKind::Output); // inside another pin
ed::EndPin();
ImGui::TextUnformatted("Out");
ed::EndPin();
ed::EndNode();
ed::BeginNode(3);
ImGui::TextUnformatted("Group");
ed::Group(ImVec2(100.0f, 50.0f));
ed::BeginPin(31, ed::PinKind::Input); // in a group
ed::EndPin();
ed::EndNode();
ed::BeginNode(2);
ed::BeginPin(21, ed::PinKind::Input);
ImGui::TextUnformatted("In");
ed::EndPin();
ed::EndNode();
linked = ed::Link(100, 11, 21);
};
session.Run(2);
const auto nested = session.Use([] { return ed::GetNodePosition(4); });
const auto size = session.Use([] { return ed::GetNodeSize(1); });
test::Check(nested.x == FLT_MAX, "the nested node isn't created: at (%g, %g)", nested.x, nested.y);
test::Check(size.x > 0.0f && size.y > 0.0f, "the node around it is built: %g by %g", size.x, size.y);
test::Check(linked, "link 100 joins the pins built after the misuse");
}
TEST_CASE(GroupHintAlpha, "api.group_hint_multiplies_style_alpha", "API-13", test::TestFlags_None)
{
// At zoom 0.6 a group hint fades to 0.6; the application's style alpha is 0.5.
test::Storage storage;
storage.Global = R"({"view":{"scroll":{"x":0,"y":0},"zoom":0.6}})";
test::Session session(&storage);
ImGui::GetStyle().Alpha = 0.5f;
float alpha = -1.0f;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(10, ImVec2(0.0f, 0.0f));
ed::BeginNode(10);
ImGui::TextUnformatted("Group");
ed::Group(ImVec2(300.0f, 200.0f));
ed::EndNode();
if (ed::BeginGroupHint(10))
{
alpha = ImGui::GetStyle().Alpha;
ed::EndGroupHint();
}
};
session.Run(3);
test::Check(ImFabs(alpha - 0.3f) < 1e-4f, "the hint draws at 0.5 times 0.6: %g", alpha);
}
TEST_CASE(PinAlpha, "api.pin_colors_follow_style_alpha", "API-13", test::TestFlags_None)
{
// A hovered pin draws its rectangle in opaque red, with the style alpha at 0.5.
test::Storage storage;
test::Session session(&storage);
ImGui::GetStyle().Alpha = 0.5f;
ImVec2 pinCenter;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ed::PushStyleColor(ed::StyleColor_PinRect, ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
ed::BeginPin(11, ed::PinKind::Output);
ImGui::TextUnformatted("Out ->");
pinCenter = ImGui::GetItemRectMin() + ImGui::GetItemRectSize() * 0.5f;
ed::EndPin();
ed::PopStyleColor();
ed::EndNode();
};
session.Run(2);
test::MouseMove(session.Use([&] { return ed::CanvasToScreen(pinCenter); }));
session.Run(2);
const bool hovered = session.Use([] { return ed::GetHoveredPin() == ed::PinId(11); });
int opaque = 0, halved = 0;
for (auto color : test::Capture(session.DrawList).Colors)
{
if ((color & ~IM_COL32_A_MASK) != IM_COL32(255, 0, 0, 0) || (color & IM_COL32_A_MASK) == 0)
continue;
if ((color >> IM_COL32_A_SHIFT) == 255)
++opaque;
else if ((color >> IM_COL32_A_SHIFT) == 128)
++halved;
}
test::Check(hovered, "the pin is hovered");
test::Check(halved > 0 && opaque == 0, "the pin rectangle is drawn at half alpha: %d vertices at 128, %d at 255", halved, opaque);
}
TEST_CASE(AltClickWithoutShortcuts, "api.alt_click_deletion_follows_enable_shortcuts", "API-13", test::TestFlags_None)
{
// Alt+click in the middle of link 100, with shortcuts off and then on.
bool offered[2] = {};
for (int enabled = 0; enabled < 2; ++enabled)
{
test::Storage storage;
test::Session session(&storage);
session.DefaultContent = [&](int frame)
{
ed::EnableShortcuts(enabled != 0);
const ImVec2 pivots[2] = { ImVec2(100.0f, 100.0f), ImVec2(400.0f, 100.0f) };
for (int n = 0; n < 2; ++n)
{
if (frame == 0)
ed::SetNodePosition(n + 1, ImVec2(n * 300.0f, 300.0f));
ed::BeginNode(n + 1);
ed::BeginPin(11 + n, n == 0 ? ed::PinKind::Output : ed::PinKind::Input);
ImGui::TextUnformatted("Pin");
ed::PinPivotRect(pivots[n], pivots[n]);
ed::EndPin();
ed::EndNode();
}
ed::Link(100, 11, 12);
if (ed::BeginDelete())
{
ed::LinkId linkId;
while (ed::QueryDeletedLink(&linkId))
{
offered[enabled] = offered[enabled] || linkId == ed::LinkId(100);
ed::RejectDeletedItem();
}
}
ed::EndDelete();
};
session.Run(2);
const auto middle = session.Use([] { return ed::CanvasToScreen(ImVec2(250.0f, 100.0f)); });
ImGui::GetIO().AddKeyEvent(ImGuiMod_Alt, true);
session.Click(middle);
session.Run(3);
ImGui::GetIO().AddKeyEvent(ImGuiMod_Alt, false);
session.Run(1);
}
test::Check(!offered[0], "with shortcuts off, Alt+click doesn't offer the link for deletion");
test::Check(offered[1], "with shortcuts on, it does");
}
TEST_CASE(SelectionChangeInFrame, "api.selection_change_is_seen_between_begin_and_end", "CORE-13", test::TestFlags_None)
{
// A click selects node 1; the content asks HasSelectionChanged() every frame.
test::Storage storage;
test::Session session(&storage);
int changedFrames = 0;
ImVec2 center;
session.DefaultContent = [&](int frame)
{
if (frame == 0)
ed::SetNodePosition(1, ImVec2(100.0f, 100.0f));
ed::BeginNode(1);
ImGui::TextUnformatted("Node");
ed::EndNode();
center = ed::GetNodePosition(1) + ed::GetNodeSize(1) * 0.5f;
if (ed::HasSelectionChanged())
++changedFrames;
};
session.Run(2);
session.Click(session.Use([&] { return ed::CanvasToScreen(center); }));
session.Run(3);
const bool selected = session.Use([] { return ed::IsNodeSelected(1); });
test::Check(selected, "the click selects node 1");
test::Check(changedFrames == 1, "the change is seen on one frame between Begin() and End(): %d", changedFrames);
}
TEST_CASE(LinkQueryPhase, "api.link_queries_see_links_submitted_so_far", "CORE-13", test::TestFlags_None)
{
// Link 100 connects pins 11 and 21, and is submitted after the nodes.
test::Storage storage;
test::Session session(&storage);
bool inPin = true, hadInPin = false, afterLinks = false;
session.DefaultContent = [&](int frame)
{
for (int n = 1; n <= 2; ++n)
{
if (frame == 0)
ed::SetNodePosition(n, ImVec2(n * 200.0f, 100.0f));
ed::BeginNode(n);
ed::BeginPin(n * 10 + 1, n == 1 ? ed::PinKind::Output : ed::PinKind::Input);
if (n == 1 && frame == 2)
{
inPin = ed::HasAnyLinks(ed::PinId(11));
hadInPin = ed::PinHadAnyLinks(ed::PinId(11));
}
ImGui::TextUnformatted("Pin");
ed::EndPin();
ed::EndNode();
}
ed::Link(100, 11, 21);
if (frame == 2)
afterLinks = ed::HasAnyLinks(ed::PinId(11));
};
session.Run(3);
test::Check(!inPin && hadInPin, "inside the pin, before the frame's links, HasAnyLinks() is false and PinHadAnyLinks() true");
test::Check(afterLinks, "after the frame's links, HasAnyLinks() is true");
}