forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebXRLayers.test.ts
More file actions
113 lines (94 loc) · 3.75 KB
/
Copy pathwebXRLayers.test.ts
File metadata and controls
113 lines (94 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @vitest-environment jsdom
*/
import { NullEngine } from "core/Engines/nullEngine";
import { Scene } from "core/scene";
import { WebXRLayers } from "core/XR/features/WebXRLayers";
import { WebXRSessionManager } from "core/XR/webXRSessionManager";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
interface ProjectionLayerBindingConstructor {
prototype: {
createProjectionLayer?: () => void;
};
}
type TestGlobals = typeof globalThis & {
XRGPUBinding?: ProjectionLayerBindingConstructor;
XRWebGLBinding?: ProjectionLayerBindingConstructor;
};
describe("WebXRLayers", () => {
let engine: NullEngine;
let scene: Scene;
let sessionManager: WebXRSessionManager;
const testGlobals = globalThis as TestGlobals;
let originalGPUBinding: ProjectionLayerBindingConstructor | undefined;
let originalWebGLBinding: ProjectionLayerBindingConstructor | undefined;
beforeEach(() => {
engine = new NullEngine({
renderHeight: 256,
renderWidth: 256,
textureSize: 256,
deterministicLockstep: false,
lockstepMaxSteps: 1,
});
scene = new Scene(engine);
sessionManager = new WebXRSessionManager(scene);
originalGPUBinding = testGlobals.XRGPUBinding;
originalWebGLBinding = testGlobals.XRWebGLBinding;
});
afterEach(() => {
if (originalGPUBinding) {
testGlobals.XRGPUBinding = originalGPUBinding;
} else {
delete testGlobals.XRGPUBinding;
}
if (originalWebGLBinding) {
testGlobals.XRWebGLBinding = originalWebGLBinding;
} else {
delete testGlobals.XRWebGLBinding;
}
vi.restoreAllMocks();
scene.dispose();
engine.dispose();
});
function setEnvironment(isNative: boolean, isWebGPU: boolean): void {
vi.spyOn(sessionManager, "isNative", "get").mockReturnValue(isNative);
vi.spyOn(engine, "isWebGPU", "get").mockReturnValue(isWebGPU);
}
function installGPUBinding(): void {
const binding = vi.fn() as unknown as ProjectionLayerBindingConstructor;
binding.prototype.createProjectionLayer = vi.fn();
testGlobals.XRGPUBinding = binding;
}
function installWebGLBinding(): void {
const binding = vi.fn() as unknown as ProjectionLayerBindingConstructor;
binding.prototype.createProjectionLayer = vi.fn();
testGlobals.XRWebGLBinding = binding;
}
describe("isCompatible", () => {
it("accepts native WebGPU when XRGPUBinding exposes projection layers", () => {
setEnvironment(true, true);
installGPUBinding();
expect(new WebXRLayers(sessionManager).isCompatible()).toBe(true);
});
it("rejects native WebGPU when XRGPUBinding is absent", () => {
setEnvironment(true, true);
delete testGlobals.XRGPUBinding;
expect(new WebXRLayers(sessionManager).isCompatible()).toBe(false);
});
it("keeps native WebGL on the legacy render-target path", () => {
setEnvironment(true, false);
installWebGLBinding();
expect(new WebXRLayers(sessionManager).isCompatible()).toBe(false);
});
it("accepts browser WebGPU when XRGPUBinding exposes projection layers", () => {
setEnvironment(false, true);
installGPUBinding();
expect(new WebXRLayers(sessionManager).isCompatible()).toBe(true);
});
it("accepts browser WebGL when XRWebGLBinding exposes projection layers", () => {
setEnvironment(false, false);
installWebGLBinding();
expect(new WebXRLayers(sessionManager).isCompatible()).toBe(true);
});
});
});