Class MockOpenXREnvironment
Sets up and manages a mock XR environment used for testing, using the OpenXR Mock Runtime feature.
Implements
Inherited Members
Namespace: UnityEngine.XR.OpenXR.TestTooling
Assembly: Unity.XR.OpenXR.TestTooling.dll
Syntax
public class MockOpenXREnvironment : IDisposable
Remarks
Use this class for testing low-level OpenXR features, such as OpenXR-based loaders, performing calls to the Mock Runtime, or testing lifecycle events.
Note that only one MockOpenXREnvironment instance should be created and started at a time, otherwise settings and resources may conflict.
Fields
Settings
Resources and settings used by this environment to run the Mock Runtime.
Declaration
public readonly MockOpenXREnvironmentSettings Settings
Field Value
Type | Description |
---|---|
MockOpenXREnvironmentSettings |
Remarks
Use this object to add or remove features and extensions to be used by the environment.
Methods
AddSupportedExtension(string, uint)
Adds an extension to the set of underlying mock runtime extensions that are provided by the runtime when the OpenXR xrEnumerateInstanceExtensionProperties() call is made. Throws an 'InvalidOperationException' if this is called while the MockOpenXREnvironment is running. NOTE: Any added mocked extension will not work when writing tests which rely on the generic Khronos loader for the mock runtime when executing on device.
Declaration
public void AddSupportedExtension(string extensionName, uint extensionVersion)
Parameters
Type | Name | Description |
---|---|---|
string | extensionName | The name of the extension to add. For example 'XR_FB_spatial_entity'. |
uint | extensionVersion | The version number of the extension to mock, usually defined in openxr.h For example, XR_FB_spatial_entity_SPEC_VERSION. |
CreateEnvironment()
Builds a new MockOpenXREnvironment instance and sets it up for testing.
Declaration
public static MockOpenXREnvironment CreateEnvironment()
Returns
Type | Description |
---|---|
MockOpenXREnvironment | A new MockOpenXREnvironment instance. |
Remarks
This method stops any running loader instances and initializes the Mock Runtime, to make a clean and stable environment for running.
The method runs automatically if the environment is built with a using statement or declaration.
When calling this method while running the tests in the Editor, any open Project Settings window will be closed to avoid changes in the settings, which may result in the settings files receiving unitnended changes and tests failing.
Examples
Example of creating a new environment for testing with a using statement:
using (var env = MockOpenXREnvironment.CreateEnvironment())
{
env.Start();
// Run tests
env.Stop();
// The environment will be disposed automatically after the using block
}
Dispose()
Tears down the environment and its resources.
Declaration
public void Dispose()
Remarks
This method stops the Mock Runtime and disposes of the environment settings. After calling this method, the environment object is in a disposed state and can't be used anymore.
EnqueueMockEventData(IntPtr)
Places a mocked C# event structure converted to a marshaled native pointer into the mock runtime's event queue that will be polled through xrPollEvent.
Declaration
public void EnqueueMockEventData(IntPtr eventData)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | eventData | The marshaled event structure data. |
ProcessEventQueue()
Processes events on the event queue by virtue of calling xrPollEvent until the event queue is empty
Declaration
public void ProcessEventQueue()
SetFunctionForInterceptor(string, IntPtr)
Binds a C# mock function to a function name that corresponds with an extension-specific and platform-specific function.
Declaration
public void SetFunctionForInterceptor(string methodName, IntPtr function)
Parameters
Type | Name | Description |
---|---|---|
string | methodName | The name of the OpenXR function to bind to the mock function. |
IntPtr | function | The C# mock function to call for the function name. Use null to remove it. |
SetSysPropertiesFunctionForXrStructureType(uint, IntPtr)
Passes a mocked C# function that to the native Mock Runtime for handling the retrieval of system properties for an extension-specific/type-specific system properties structure.
Declaration
public void SetSysPropertiesFunctionForXrStructureType(uint type, IntPtr function)
Parameters
Type | Name | Description |
---|---|---|
uint | type | An OpenXR structure type ID which identifies the actual structure definition type. |
IntPtr | function | A pointer to a C# mock function capable of modifying the typed system properties structure. Note: if IntPtr.Zero is passed, then that means to disregard usage of a mock function for the provided type. |
Start()
Starts the Mock Runtime and initializes the XR subsystems for testing.
Declaration
public void Start()
Remarks
The method does nothing if the environment has been disposed.
If the Mock Runtime is already running, the method calls Stop() before restarting and reinitializing it.
Stop()
Stops the Mock Runtime and deinitializes the XR subsystems.
Declaration
public void Stop()
Remarks
The method does nothing if the environment has been disposed.