2026-03-05 03:31:47 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2023-07-18 13:01:22 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2023-07-18 13:01:22 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2023-07-18 13:01:22 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2023-07-18 13:01:22 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "lib/self_test.h"
|
|
|
|
|
|
|
|
|
|
#include "gui/CGUI.h"
|
|
|
|
|
#include "gui/CGUISetting.h"
|
|
|
|
|
#include "gui/ObjectBases/IGUIObject.h"
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
#include "gui/SettingTypes/CGUISize.h"
|
2025-04-23 08:51:40 -07:00
|
|
|
#include "i18n/L10n.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "lib/file/file_system.h"
|
|
|
|
|
#include "lib/file/vfs/vfs.h"
|
|
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "maths/Rect.h"
|
2023-07-18 13:01:22 -07:00
|
|
|
#include "ps/CLogger.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/CStr.h"
|
2023-07-18 13:01:22 -07:00
|
|
|
#include "ps/ConfigDB.h"
|
|
|
|
|
#include "ps/Filesystem.h"
|
|
|
|
|
#include "ps/ProfileViewer.h"
|
|
|
|
|
#include "ps/VideoMode.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2023-07-18 13:01:22 -07:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
2023-07-18 13:01:22 -07:00
|
|
|
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <js/PropertyAndElement.h>
|
|
|
|
|
#include <js/PropertyDescriptor.h>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <map>
|
2023-07-18 13:01:22 -07:00
|
|
|
#include <memory>
|
2024-09-18 09:17:04 -07:00
|
|
|
#include <optional>
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <string>
|
2023-07-18 13:01:22 -07:00
|
|
|
#include <type_traits>
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <utility>
|
2023-07-18 13:01:22 -07:00
|
|
|
|
|
|
|
|
class TestGUISetting : public CxxTest::TestSuite
|
|
|
|
|
{
|
2024-09-18 09:17:04 -07:00
|
|
|
std::optional<CXeromycesEngine> m_XeromycesEngine;
|
2023-07-18 13:01:22 -07:00
|
|
|
std::unique_ptr<CProfileViewer> m_Viewer;
|
|
|
|
|
std::unique_ptr<CRenderer> m_Renderer;
|
2025-04-23 08:51:40 -07:00
|
|
|
std::unique_ptr<L10n> m_L10n;
|
2023-07-18 13:01:22 -07:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
class TestGUIObject : public IGUIObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TestGUIObject(CGUI& gui) : IGUIObject(gui) {}
|
|
|
|
|
|
|
|
|
|
void Draw(CCanvas2D&) {}
|
2026-03-05 03:31:47 -08:00
|
|
|
void RecalculateActualSize() const {
|
|
|
|
|
cachedSizeRecalculated = true;
|
|
|
|
|
IGUIObject::RecalculateActualSize();
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CGUISimpleSetting<CGUISize>* GetSizeSetting() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<CGUISimpleSetting<CGUISize>*>(m_Settings.at("size"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 03:31:47 -08:00
|
|
|
mutable bool cachedSizeRecalculated{false};
|
2023-07-18 13:01:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
|
{
|
|
|
|
|
g_VFS = CreateVfs();
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.gui" / "", VFS_MOUNT_MUST_EXIST));
|
2023-07-18 13:01:22 -07:00
|
|
|
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.minimal" / "", VFS_MOUNT_MUST_EXIST));
|
|
|
|
|
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY));
|
|
|
|
|
|
2024-09-18 09:17:04 -07:00
|
|
|
m_XeromycesEngine.emplace();
|
2023-07-18 13:01:22 -07:00
|
|
|
|
|
|
|
|
// The renderer spews messages.
|
|
|
|
|
TestLogger logger;
|
|
|
|
|
|
|
|
|
|
// We need to initialise the renderer to initialise the font manager.
|
|
|
|
|
// TODO: decouple this.
|
|
|
|
|
CConfigDB::Initialise();
|
|
|
|
|
CConfigDB::Instance()->SetValueString(CFG_SYSTEM, "rendererbackend", "dummy");
|
|
|
|
|
g_VideoMode.InitNonSDL();
|
|
|
|
|
g_VideoMode.CreateBackendDevice(false);
|
|
|
|
|
m_Viewer = std::make_unique<CProfileViewer>();
|
|
|
|
|
m_Renderer = std::make_unique<CRenderer>(g_VideoMode.GetBackendDevice());
|
2025-04-23 08:51:40 -07:00
|
|
|
m_L10n = std::make_unique<L10n>();
|
2023-07-18 13:01:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
|
{
|
2025-04-23 08:51:40 -07:00
|
|
|
m_L10n.reset();
|
2023-07-18 13:01:22 -07:00
|
|
|
m_Renderer.reset();
|
|
|
|
|
m_Viewer.reset();
|
|
|
|
|
g_VideoMode.Shutdown();
|
|
|
|
|
CConfigDB::Shutdown();
|
2024-09-18 09:17:04 -07:00
|
|
|
m_XeromycesEngine.reset();
|
2023-07-18 13:01:22 -07:00
|
|
|
g_VFS.reset();
|
|
|
|
|
DeleteDirectory(DataDir() / "_testcache");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_movability()
|
|
|
|
|
{
|
2024-06-27 12:09:30 -07:00
|
|
|
CGUI gui{*g_ScriptContext};
|
2023-07-18 13:01:22 -07:00
|
|
|
TestGUIObject object(gui);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_move_constructible_v<CGUISimpleSetting<CStr>>);
|
|
|
|
|
static_assert(!std::is_move_assignable_v<CGUISimpleSetting<CStr>>);
|
|
|
|
|
|
|
|
|
|
CGUISimpleSetting<CStr> settingA(&object, "A");
|
|
|
|
|
TS_ASSERT(settingA->empty());
|
|
|
|
|
TS_ASSERT(object.SettingExists("A"));
|
|
|
|
|
object.SetSettingFromString("A", L"ValueA", false);
|
|
|
|
|
TS_ASSERT_EQUALS(*settingA, "ValueA");
|
|
|
|
|
|
|
|
|
|
CGUISimpleSetting<CStr> settingB(std::move(settingA));
|
|
|
|
|
TS_ASSERT(object.SettingExists("A"));
|
|
|
|
|
object.SetSettingFromString("A", L"ValueB", false);
|
|
|
|
|
TS_ASSERT_EQUALS(*settingB, "ValueB");
|
|
|
|
|
}
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
|
|
|
|
|
void test_setting_cguisize()
|
|
|
|
|
{
|
|
|
|
|
CGUI gui{*g_ScriptContext};
|
|
|
|
|
gui.AddObjectTypes();
|
|
|
|
|
TestGUIObject object{gui};
|
|
|
|
|
|
|
|
|
|
CGUISimpleSetting<CGUISize>* setting{object.GetSizeSetting()};
|
|
|
|
|
object.SetSettingFromString("size", L"2 2 20 20", false);
|
|
|
|
|
|
|
|
|
|
ScriptRequest rq{gui.GetScriptInterface()};
|
|
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
val.setObject(*object.GetJSObject());
|
|
|
|
|
JS::RootedObject global(rq.cx, rq.glob);
|
|
|
|
|
JS_DefineProperty(rq.cx, global, "testObject", val, JSPROP_ENUMERATE);
|
|
|
|
|
|
|
|
|
|
// Lazy assigment.
|
|
|
|
|
TS_ASSERT(gui.GetScriptInterface()->LoadGlobalScriptFile(L"gui/settings/cguisize/lazyassign.js"));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().pixel, (CRect{5, 2, 20, 20}));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().percent, (CRect{0, 0, 0, 0}));
|
2026-03-05 03:31:47 -08:00
|
|
|
TS_ASSERT(!object.cachedSizeRecalculated);
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
|
2026-03-05 03:31:47 -08:00
|
|
|
// This should finally recalculate the dirty (cached) actual size in order to return an up-to-date value.
|
|
|
|
|
TS_ASSERT_EQUALS(object.GetActualSize(), (CRect{5, 2, 20, 20}));
|
|
|
|
|
TS_ASSERT(object.cachedSizeRecalculated);
|
|
|
|
|
|
|
|
|
|
object.cachedSizeRecalculated = false;
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
|
|
|
|
|
// Compound assignment operator.
|
|
|
|
|
TS_ASSERT(gui.GetScriptInterface()->LoadGlobalScriptFile(L"gui/settings/cguisize/compoundassignmentoperator.js"));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().pixel, (CRect{10, 2, 20, 20}));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().percent, (CRect{0, 0, 0, 0}));
|
|
|
|
|
|
|
|
|
|
// Object assignment.
|
|
|
|
|
TS_ASSERT(gui.GetScriptInterface()->LoadGlobalScriptFile(L"gui/settings/cguisize/objectassign.js"));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().pixel, (CRect{10, 2, 20, 20}));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().percent, (CRect{4, 0, 0, 20}));
|
|
|
|
|
|
|
|
|
|
// assign
|
|
|
|
|
TS_ASSERT(gui.GetScriptInterface()->LoadGlobalScriptFile(L"gui/settings/cguisize/assign.js"));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().pixel, (CRect{3, 0, 0, 2}));
|
|
|
|
|
TS_ASSERT_EQUALS(setting->GetMutable().percent, (CRect{0, 0, 0, 0}));
|
2026-03-05 03:31:47 -08:00
|
|
|
|
|
|
|
|
TS_ASSERT(!object.cachedSizeRecalculated);
|
|
|
|
|
// Call the getComputedSize method on it, which should finally recalculate the dirty (cached) actual size in
|
|
|
|
|
// order to return an up-to-date value.
|
|
|
|
|
TS_ASSERT(gui.GetScriptInterface()->LoadGlobalScriptFile(L"gui/settings/cguisize/getcomputedsize.js"));
|
|
|
|
|
TS_ASSERT(object.cachedSizeRecalculated);
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
}
|
2023-07-18 13:01:22 -07:00
|
|
|
};
|