1
0
forked from 0ad/0ad

Change the GuiManager tests to use promises

This commit is contained in:
phosit 2024-09-21 20:22:55 +02:00
parent b28b2343d8
commit 883f093cb9
11 changed files with 92 additions and 18 deletions

View File

@ -0,0 +1,4 @@
function init()
{
return new Promise(resolve => global.closePage = resolve);
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<objects>
<script file="gui/regainFocus/emptyPage.js"/>
</objects>

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<page>
<include>common/styles.xml</include>
<include>regainFocus/emptyPage.xml</include>
</page>

View File

@ -1 +1,4 @@
Engine.PushGuiPage("regainFocus/page_emptyPage.xml").then(Engine.PopGuiPage);
function init()
{
return Engine.PushGuiPage("regainFocus/page_emptyPage.xml");
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<page>
<include>common/styles.xml</include>
<include>resolveReject/resolveReject.xml</include>
</page>

View File

@ -0,0 +1,5 @@
async function init(reject)
{
if (reject)
throw undefined;
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<objects>
<script file="gui/resolveReject/resolveReject.js"/>
</objects>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<page>
<include>common/styles.xml</include>
<include>sequential/sequential.xml</include>
</page>

View File

@ -0,0 +1,5 @@
async function init()
{
await Engine.PushGuiPage("regainFocus/page_emptyPage.xml");
await Engine.PushGuiPage("regainFocus/page_emptyPage.xml");
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<objects>
<script file="gui/sequential/sequential.js"/>
</objects>

View File

@ -26,12 +26,14 @@
#include "ps/GameSetup/GameSetup.h"
#include "ps/Hotkey.h"
#include "ps/XML/Xeromyces.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/ScriptContext.h"
#include "scriptinterface/ScriptRequest.h"
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/StructuredClone.h"
#include "scriptinterface/Object.h"
#include "js/Promise.h"
#include <memory>
#include <optional>
@ -204,37 +206,69 @@ public:
static void CloseTopmostPage()
{
g_GUI->PopPage(JS::NullHandleValue);
ScriptRequest rq{g_GUI->GetActiveGUI()->GetScriptInterface()};
JS::RootedValue global{rq.cx, rq.globalValue()};
TS_ASSERT(ScriptFunction::CallVoid(rq, global, "closePage"));
// Check if some promise are setteled and flush the promise-jobQueue.
g_GUI->TickObjects();
}
void test_PageRegainedFocusEvent()
{
// Load up a test page.
ScriptRequest rq{g_GUI->GetScriptInterface()};
JS::RootedValue val(rq.cx);
Script::CreateObject(rq, &val);
const Script::StructuredClone undefined{
Script::WriteStructuredClone(rq, JS::UndefinedHandleValue)};
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 0);
Script::StructuredClone data = Script::WriteStructuredClone(rq, JS::NullHandleValue);
g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", data);
const ScriptInterface& pageScriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface());
ScriptRequest prq(pageScriptInterface);
JS::RootedValue global(prq.cx, prq.globalValue());
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 1);
g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", data);
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 2);
CloseTopmostPage();
g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", undefined);
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 1);
// This page instantly pushes an empty page with a callback that pops another page again.
g_GUI->PushPage(L"regainFocus/page_pushWithPopOnInit.xml", data);
g_GUI->PushPage(L"regainFocus/page_pushWithPopOnInit.xml", undefined);
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 3);
// Pop the empty page
// Pop the empty page and execute the continuation.
CloseTopmostPage();
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 1);
CloseTopmostPage();
TS_ASSERT_EQUALS(g_GUI->GetPageCount(), 0);
}
void test_ResolveReject()
{
TestLogger logger;
constexpr std::array<std::tuple<bool, JS::PromiseState>, 2> testSteps{{
{false, JS::PromiseState::Fulfilled},
{true, JS::PromiseState::Rejected}}};
const ScriptRequest rq{g_GUI->GetScriptInterface()};
const Script::StructuredClone undefined{
Script::WriteStructuredClone(rq, JS::UndefinedHandleValue)};
g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", undefined);
for (const auto& [reject, result] : testSteps)
{
const JS::RootedValue value{rq.cx, JS::BooleanValue(reject)};
const Script::StructuredClone clonedValue{Script::WriteStructuredClone(rq, value)};
const JS::RootedValue promise{rq.cx,
g_GUI->PushPage(L"resolveReject/page_resolveReject.xml", clonedValue)};
// Check if some promise are setteled and flush the promise-jobQueue.
g_GUI->TickObjects();
const JS::RootedObject promiseObject{rq.cx, &promise.toObject()};
TS_ASSERT_EQUALS(JS::GetPromiseState(promiseObject), result);
}
}
void test_Sequential()
{
const ScriptRequest rq{g_GUI->GetScriptInterface()};
const Script::StructuredClone undefined{
Script::WriteStructuredClone(rq, JS::UndefinedHandleValue)};
g_GUI->PushPage(L"sequential/page_sequential.xml", undefined);
}
};