Files
dusk/test/script/test_moduleui.c
T
YourWishes b639bc6c4f Reimplement the archived UI widget stack on the new element-tree system
The old widget system (archive/dusk/ui/) depended on infrastructure this
rewrite deleted -- a static X-macro element list, a 9-slice UI_FRAME, and
a global gamepad/keyboard focus stack -- so everything below is rebuilt
on top of the new native uielement_t pool + parent/child tree instead,
gamepad/keyboard-only (no mouse support exists in the input system).

New JS widgets (assets/scripts/), all pure composites of Label/Rectangle
following the Button.js pattern: Checkbox, Tab, Slider, Dropdown. Slider/
Dropdown expose a directionInput(dx, dy) hook so a menu can hand them
LEFT/RIGHT before falling back to cursor movement.

New Menu.js replaces the old global uifocus_t stack: a small push/pop
stack of Menu instances where only the topmost consumes CANCEL/ACCEPT/
direction input each tick (needed for nested modals -- a Settings
sub-page menu plus a "discard changes?" Confirm can be open at once).
Held-direction repeat timing matches the old 0.5s delay / 0.1s repeat.

New overlay composites (added via a new UI.addOverlay()/UI.removeOverlay(),
always drawn/updated after normal roots): FpsCounter, ConsoleOverlay
(one Label per console history line, driven directly rather than via
add() since History (16) exceeds the 8-children-per-element cap), Crop
(letterbox/pillarbox bars), and Fullbox (a reusable tweened full-screen
fade covering both the old fullbox and transition effects -- reuses
shared instances rather than allocate-and-dispose, since disposing an
element from inside its own render() callback risks the same JerryScript
refcount corruption hit earlier in this rewrite).

New Confirm.js (Yes/No dialog) and Settings.js (+ SettingsGeneral/
SettingsInput sub-pages) built on Menu. Display/Audio settings pages are
intentionally not ported -- neither had a real backing engine system
even in the old code. Frame is a flat Rectangle for now, not real 9-slice
(a genuinely bigger native lift, deliberately deferred).

Small native binding additions needed by the above, each with its own
unit test: Time.renderDelta, Console.lineCount/getLine/visible/
consumeDirty, a new Screen module, a new Locale module, Input.deadzone,
a new Save module, and Label.color (was missing entirely). Also fixes
a real gap from the UI.addOverlay() work: uiElementDispose() wasn't
removing disposed elements from the new overlay root array, which would
have left dangling ids behind.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-14 09:05:11 -05:00

170 lines
4.8 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "console/console.h"
#include "script/scriptmanager.h"
#include "ui/ui.h"
#include "util/memory.h"
static int ui_setup(void **state) {
consoleInit();
memoryZero(UI_ELEMENTS, sizeof(UI_ELEMENTS));
memoryZero(&UI, sizeof(UI));
for(uint8_t i = 0; i < UI_ROOT_MAX; i++) UI.root[i] = UI_ELEMENT_ID_INVALID;
for(uint8_t i = 0; i < UI_OVERLAY_ROOT_MAX; i++) {
UI.overlayRoot[i] = UI_ELEMENT_ID_INVALID;
}
errorret_t ret = scriptManagerInit();
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
return 0;
}
static int ui_teardown(void **state) {
errorret_t ret = scriptManagerDispose();
if(errorIsNotOk(ret)) errorCatch(ret);
consoleDispose();
return 0;
}
static void exec(const char_t *script) {
jerry_value_t result;
errorret_t ret = scriptManagerExec(script, &result);
if(errorIsNotOk(ret)) {
errorCatch(errorPrint(ret));
fail_msg("Script execution failed (see printed error above)");
}
assert_true(errorIsOk(ret));
jerry_value_free(result);
}
static void test_ui_add_requires_an_argument(void **state) {
errorret_t ret = scriptManagerExec("UI.add();", NULL);
assert_true(errorIsNotOk(ret));
errorCatch(ret);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_ui_add_requires_an_object(void **state) {
errorret_t ret = scriptManagerExec("UI.add('button');", NULL);
assert_true(errorIsNotOk(ret));
errorCatch(ret);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_ui_add_requires_a_uielement(void **state) {
errorret_t ret = scriptManagerExec("UI.add({});", NULL);
assert_true(errorIsNotOk(ret));
errorCatch(ret);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_ui_remove_of_untracked_element_is_a_noop(void **state) {
jerry_value_t result;
errorret_t ret = scriptManagerExec(
"UI.remove(new UIElement());", &result
);
assert_true(errorIsOk(ret));
jerry_value_free(result);
}
static void test_ui_add_overlay_and_remove_overlay(void **state) {
assert_true(UI.overlayRoot[0] == UI_ELEMENT_ID_INVALID);
exec(
"globalThis.el = new UIElement();"
"el.x = 3;"
"UI.addOverlay(el);"
);
assert_true(UI.overlayRoot[0] != UI_ELEMENT_ID_INVALID);
assert_true(UI_ELEMENTS[UI.overlayRoot[0]].type == UI_ELEMENT_TYPE_SCRIPTED);
assert_float_equal(UI_ELEMENTS[UI.overlayRoot[0]].x, 3.0f, 0.001f);
exec("UI.removeOverlay(el);");
assert_true(UI.overlayRoot[0] == UI_ELEMENT_ID_INVALID);
}
static void test_ui_add_overlay_detaches_from_parent(void **state) {
exec(
"globalThis.parent = new UIElement();"
"globalThis.child = new UIElement();"
"parent.add(child);"
"if(parent.childCount !== 1) throw new Error('expected 1 child');"
"UI.addOverlay(child);"
"if(parent.childCount !== 0) {"
" throw new Error('expected 0 children after UI.addOverlay');"
"}"
"if(child.parent !== undefined) throw new Error('child.parent should be undefined');"
);
}
static void test_ui_dispose_removes_overlay_root(void **state) {
exec(
"globalThis.el = new UIElement();"
"UI.addOverlay(el);"
);
assert_true(UI.overlayRoot[0] != UI_ELEMENT_ID_INVALID);
exec("el.dispose();");
assert_true(UI.overlayRoot[0] == UI_ELEMENT_ID_INVALID);
}
static void test_ui_overlay_root_is_independent_of_root(void **state) {
// A normal root and an overlay root are two separate arrays -- adding
// to one must never touch the other.
exec(
"globalThis.a = new UIElement();"
"globalThis.b = new UIElement();"
"UI.add(a);"
"UI.addOverlay(b);"
);
assert_true(UI.root[0] != UI_ELEMENT_ID_INVALID);
assert_true(UI.overlayRoot[0] != UI_ELEMENT_ID_INVALID);
assert_true(UI.root[0] != UI.overlayRoot[0]);
}
int main(void) {
assertInit();
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(
test_ui_add_requires_an_argument, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_add_requires_an_object, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_add_requires_a_uielement, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_remove_of_untracked_element_is_a_noop, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_add_overlay_and_remove_overlay, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_add_overlay_detaches_from_parent, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_dispose_removes_overlay_root, ui_setup, ui_teardown
),
cmocka_unit_test_setup_teardown(
test_ui_overlay_root_is_independent_of_root, ui_setup, ui_teardown
),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}