yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
overworld_ui_tests.cc
Go to the documentation of this file.
2
3#if defined(YAZE_ENABLE_IMGUI_TEST_ENGINE) && YAZE_ENABLE_IMGUI_TEST_ENGINE
4
5#include "imgui.h"
6#if __has_include("imgui_test_engine/imgui_te_context.h")
7#include "imgui_test_engine/imgui_te_context.h"
8#elif __has_include("imgui_te_context.h")
9#include "imgui_te_context.h"
10#else
11#error "ImGui Test Engine context header not found"
12#endif
13
14#if __has_include("imgui_test_engine/imgui_te_engine.h")
15#include "imgui_test_engine/imgui_te_engine.h"
16#elif __has_include("imgui_te_engine.h")
17#include "imgui_te_engine.h"
18#else
19#error "ImGui Test Engine engine header not found"
20#endif
21
22namespace yaze::test {
23
24// ============================================================================
25// Test: Keyboard shortcut mode switching
26//
27// The overworld editor uses number keys (1, 2) and letter keys (B, F)
28// to switch between Mouse, Draw Tile, and Fill Tile modes. These shortcuts
29// are handled in OverworldEditor::HandleKeyboardShortcuts() and delegate
30// to TilePaintingManager for B/F.
31//
32// These tests verify that pressing each shortcut key doesn't crash the
33// application. Actual mode state verification would require accessing
34// the OverworldEditor's internal state, which we defer to unit tests.
35// ============================================================================
36
37static void RegisterKeyboardShortcutTests(ImGuiTestEngine* engine) {
38 // Test: Key '1' (Mouse mode) doesn't crash
39 {
40 ImGuiTest* test =
41 IM_REGISTER_TEST(engine, "overworld_keys", "mouse_mode_key_1");
42 test->TestFunc = [](ImGuiTestContext* ctx) {
43 ctx->KeyPress(ImGuiKey_1);
44 ctx->Yield(3);
45 };
46 }
47
48 // Test: Key '2' (Draw Tile mode) doesn't crash
49 {
50 ImGuiTest* test =
51 IM_REGISTER_TEST(engine, "overworld_keys", "draw_mode_key_2");
52 test->TestFunc = [](ImGuiTestContext* ctx) {
53 ctx->KeyPress(ImGuiKey_2);
54 ctx->Yield(3);
55 // Restore mouse mode
56 ctx->KeyPress(ImGuiKey_1);
57 ctx->Yield(1);
58 };
59 }
60
61 // Test: Key 'B' (Brush toggle) doesn't crash
62 {
63 ImGuiTest* test =
64 IM_REGISTER_TEST(engine, "overworld_keys", "brush_toggle_key_b");
65 test->TestFunc = [](ImGuiTestContext* ctx) {
66 ctx->KeyPress(ImGuiKey_B);
67 ctx->Yield(3);
68 // Toggle back
69 ctx->KeyPress(ImGuiKey_B);
70 ctx->Yield(1);
71 };
72 }
73
74 // Test: Key 'F' (Fill tool) doesn't crash
75 {
76 ImGuiTest* test =
77 IM_REGISTER_TEST(engine, "overworld_keys", "fill_tool_key_f");
78 test->TestFunc = [](ImGuiTestContext* ctx) {
79 ctx->KeyPress(ImGuiKey_F);
80 ctx->Yield(3);
81 // Toggle back
82 ctx->KeyPress(ImGuiKey_F);
83 ctx->Yield(1);
84 };
85 }
86
87 // Test: Key 'I' (Eyedropper / pick tile16) doesn't crash
88 {
89 ImGuiTest* test =
90 IM_REGISTER_TEST(engine, "overworld_keys", "eyedropper_key_i");
91 test->TestFunc = [](ImGuiTestContext* ctx) {
92 ctx->KeyPress(ImGuiKey_I);
93 ctx->Yield(3);
94 };
95 }
96
97 // Test: Ctrl+L (Lock map toggle) doesn't crash
98 {
99 ImGuiTest* test =
100 IM_REGISTER_TEST(engine, "overworld_keys", "lock_toggle_ctrl_l");
101 test->TestFunc = [](ImGuiTestContext* ctx) {
102 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_L);
103 ctx->Yield(3);
104 // Toggle back
105 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_L);
106 ctx->Yield(1);
107 };
108 }
109
110 // Test: F11 (Fullscreen toggle) doesn't crash
111 {
112 ImGuiTest* test =
113 IM_REGISTER_TEST(engine, "overworld_keys", "fullscreen_toggle_f11");
114 test->TestFunc = [](ImGuiTestContext* ctx) {
115 ctx->KeyPress(ImGuiKey_F11);
116 ctx->Yield(3);
117 // Toggle back
118 ctx->KeyPress(ImGuiKey_F11);
119 ctx->Yield(1);
120 };
121 }
122
123 // Test: Ctrl+Z (Undo) doesn't crash when no undo history
124 {
125 ImGuiTest* test =
126 IM_REGISTER_TEST(engine, "overworld_keys", "undo_no_crash");
127 test->TestFunc = [](ImGuiTestContext* ctx) {
128 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_Z);
129 ctx->Yield(3);
130 };
131 }
132
133 // Test: Ctrl+Y (Redo) doesn't crash when no redo history
134 {
135 ImGuiTest* test =
136 IM_REGISTER_TEST(engine, "overworld_keys", "redo_no_crash");
137 test->TestFunc = [](ImGuiTestContext* ctx) {
138 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_Y);
139 ctx->Yield(3);
140 };
141 }
142
143 // Test: Ctrl+T (Toggle Tile16 Editor panel) doesn't crash
144 {
145 ImGuiTest* test =
146 IM_REGISTER_TEST(engine, "overworld_keys", "tile16_editor_ctrl_t");
147 test->TestFunc = [](ImGuiTestContext* ctx) {
148 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_T);
149 ctx->Yield(3);
150 // Toggle back
151 ctx->KeyPress(ImGuiMod_Ctrl | ImGuiKey_T);
152 ctx->Yield(1);
153 };
154 }
155}
156
157// ============================================================================
158// Test: Entity editing mode shortcuts (3-8 keys)
159//
160// Keys 3-8 activate different entity editing modes:
161// 3 = Entrances, 4 = Exits, 5 = Items,
162// 6 = Sprites, 7 = Transports, 8 = Music
163// ============================================================================
164
165static void RegisterEntityModeTests(ImGuiTestEngine* engine) {
166 // Test: Entity mode keys (3-8) don't crash
167 {
168 ImGuiTest* test =
169 IM_REGISTER_TEST(engine, "overworld_entity", "mode_keys_3_to_8");
170 test->TestFunc = [](ImGuiTestContext* ctx) {
171 // Cycle through all entity modes
172 ctx->KeyPress(ImGuiKey_3); // Entrances
173 ctx->Yield(2);
174 ctx->KeyPress(ImGuiKey_4); // Exits
175 ctx->Yield(2);
176 ctx->KeyPress(ImGuiKey_5); // Items
177 ctx->Yield(2);
178 ctx->KeyPress(ImGuiKey_6); // Sprites
179 ctx->Yield(2);
180 ctx->KeyPress(ImGuiKey_7); // Transports
181 ctx->Yield(2);
182 ctx->KeyPress(ImGuiKey_8); // Music
183 ctx->Yield(2);
184 // Return to mouse mode
185 ctx->KeyPress(ImGuiKey_1);
186 ctx->Yield(1);
187 };
188 }
189}
190
191// ============================================================================
192// Test: Mode switching round-trips
193//
194// Verifies that cycling through all editing modes and returning to Mouse
195// mode doesn't leave the editor in a broken state.
196// ============================================================================
197
198static void RegisterModeRoundTripTests(ImGuiTestEngine* engine) {
199 // Test: Full mode cycle (Mouse -> Draw -> Fill -> Mouse) is stable
200 {
201 ImGuiTest* test =
202 IM_REGISTER_TEST(engine, "overworld_modes", "full_mode_cycle");
203 test->TestFunc = [](ImGuiTestContext* ctx) {
204 // Start in mouse mode
205 ctx->KeyPress(ImGuiKey_1);
206 ctx->Yield(2);
207
208 // Switch to draw tile
209 ctx->KeyPress(ImGuiKey_2);
210 ctx->Yield(2);
211
212 // Switch to fill via F
213 ctx->KeyPress(ImGuiKey_F);
214 ctx->Yield(2);
215
216 // Toggle brush via B (should go to draw tile)
217 ctx->KeyPress(ImGuiKey_B);
218 ctx->Yield(2);
219
220 // Toggle brush again (should go back to mouse)
221 ctx->KeyPress(ImGuiKey_B);
222 ctx->Yield(2);
223
224 // Back to mouse mode explicitly
225 ctx->KeyPress(ImGuiKey_1);
226 ctx->Yield(3);
227 };
228 }
229
230 // Test: Rapid mode switching doesn't crash
231 {
232 ImGuiTest* test =
233 IM_REGISTER_TEST(engine, "overworld_modes", "rapid_mode_switch");
234 test->TestFunc = [](ImGuiTestContext* ctx) {
235 for (int i = 0; i < 10; ++i) {
236 ctx->KeyPress(ImGuiKey_B);
237 ctx->Yield(1);
238 }
239 // Settle back to mouse
240 ctx->KeyPress(ImGuiKey_1);
241 ctx->Yield(3);
242 };
243 }
244}
245
246// ============================================================================
247// Test: Canvas navigation smoke tests
248//
249// These exercise CanvasNavigationManager methods indirectly through the
250// keyboard shortcuts and UI interactions. The actual methods tested:
251// - HandleOverworldPan (via middle-click drag simulation)
252// - HandleOverworldZoom (via the zoom step mechanism)
253// - ResetOverworldView (not directly bound to a key, but used internally)
254//
255// Note: Mouse-based pan/zoom requires pixel-precise ImGui interaction that
256// the test engine handles through its coroutine system.
257// ============================================================================
258
259static void RegisterCanvasNavigationTests(ImGuiTestEngine* engine) {
260 // Test: Multiple frames of rendering without interaction is stable
261 {
262 ImGuiTest* test =
263 IM_REGISTER_TEST(engine, "overworld_canvas", "idle_frames_stable");
264 test->TestFunc = [](ImGuiTestContext* ctx) {
265 // Just yield several frames to verify the overworld editor
266 // renders stably without any interaction
267 ctx->Yield(10);
268 };
269 }
270}
271
272// ============================================================================
273// Test: World combo selector smoke tests
274//
275// The toolbar has a world selector combo (Light World, Dark World, Special).
276// These tests verify the combo exists but don't change world since that
277// triggers heavy map reloading that depends on ROM state.
278// ============================================================================
279
280static void RegisterWorldSelectorTests(ImGuiTestEngine* engine) {
281 // Test: Overworld editor remains stable after multiple frame yields
282 {
283 ImGuiTest* test =
284 IM_REGISTER_TEST(engine, "overworld_toolbar", "toolbar_renders_stable");
285 test->TestFunc = [](ImGuiTestContext* ctx) {
286 // Render several frames to verify toolbar doesn't crash
287 ctx->Yield(5);
288 // The toolbar "CanvasToolbar" table renders every frame when
289 // ROM is loaded and overworld is initialized
290 };
291 }
292}
293
294// ============================================================================
295// Public registration entry point
296// ============================================================================
297
298void RegisterOverworldUITests(ImGuiTestEngine* engine) {
299 if (engine == nullptr)
300 return;
301 RegisterKeyboardShortcutTests(engine);
302 RegisterEntityModeTests(engine);
303 RegisterModeRoundTripTests(engine);
304 RegisterCanvasNavigationTests(engine);
305 RegisterWorldSelectorTests(engine);
306}
307
308} // namespace yaze::test
309
310#else // !YAZE_ENABLE_IMGUI_TEST_ENGINE
311
312namespace yaze::test {
313void RegisterOverworldUITests(ImGuiTestEngine* /*engine*/) {}
314} // namespace yaze::test
315
316#endif // YAZE_ENABLE_IMGUI_TEST_ENGINE
void RegisterOverworldUITests(ImGuiTestEngine *)