#include "config.h"
#include "WebXRTest.h"
#if ENABLE(WEBXR)
#include "JSWebFakeXRDevice.h"
#include "JSXRReferenceSpaceType.h"
#include "UserGestureIndicator.h"
#include "WebXRSystem.h"
#include "XRSessionMode.h"
namespace WebCore {
WebXRTest::~WebXRTest() = default;
void WebXRTest::simulateDeviceConnection(ScriptExecutionContext& context, const FakeXRDeviceInit& init, WebFakeXRDevicePromise&& promise)
{
context.postTask([this, init, promise = WTFMove(promise)] (ScriptExecutionContext& context) mutable {
auto device = WebFakeXRDevice::create();
auto& simulatedDevice = device->simulatedXRDevice();
for (auto& viewInit : init.views) {
auto view = WebFakeXRDevice::parseView(viewInit);
if (view.hasException()) {
promise.reject(view.releaseException());
return;
}
simulatedDevice.views().append(view.releaseReturnValue());
}
Vector<XRReferenceSpaceType> features;
if (init.supportedFeatures) {
if (auto* globalObject = context.execState()) {
for (auto& feature : init.supportedFeatures.value()) {
if (auto referenceSpaceType = parseEnumeration<XRReferenceSpaceType>(*globalObject, feature))
features.append(referenceSpaceType.value());
}
}
}
if (init.boundsCoordinates) {
if (init.boundsCoordinates->size() < 3) {
promise.reject(Exception { TypeError });
return;
}
simulatedDevice.setNativeBoundsGeometry(init.boundsCoordinates.value());
}
if (init.viewerOrigin)
device->setViewerOrigin(init.viewerOrigin.value());
if (init.floorOrigin)
device->setFloorOrigin(init.floorOrigin.value());
Vector<XRSessionMode> supportedModes;
if (init.supportedModes) {
supportedModes = init.supportedModes.value();
if (supportedModes.isEmpty())
supportedModes.append(XRSessionMode::Inline);
} else {
supportedModes.append(XRSessionMode::Inline);
if (init.supportsImmersive)
supportedModes.append(XRSessionMode::ImmersiveVr);
}
for (auto& mode : supportedModes)
simulatedDevice.setEnabledFeatures(mode, features);
m_context->registerSimulatedXRDeviceForTesting(simulatedDevice);
promise.resolve(device.get());
m_devices.append(WTFMove(device));
});
}
void WebXRTest::simulateUserActivation(Document& document, XRSimulateUserActivationFunction& function)
{
UserGestureIndicator gestureIndicator(ProcessingUserGesture, &document);
function.handleEvent();
}
void WebXRTest::disconnectAllDevices(DOMPromiseDeferred<void>&& promise)
{
for (auto& device : m_devices)
m_context->unregisterSimulatedXRDeviceForTesting(device->simulatedXRDevice());
m_devices.clear();
promise.resolve();
}
}
#endif // ENABLE(WEBXR)