xr_layer_promise_test.js (3757B)
1 'use strict'; 2 3 // A test function that runs the common steps for requesting an XR session. 4 // After the session is created, it is initialize the XRWebGLBinding 5 // and local XRSpace objects for the session. These components are essential 6 // for tests involving WebXR layers. 7 function xr_layer_promise_test( 8 name, func, fakeDeviceInit, sessionMode, sessionInit, properties, 9 glcontextPropertiesParam) { 10 const glcontextProperties = (glcontextPropertiesParam) ? glcontextPropertiesParam : {}; 11 12 function runTest(t, glContext) { 13 let testSession; 14 let testDeviceController; 15 let sessionObjects = {gl: glContext}; 16 17 // Ensure that any pending sessions are ended when done. This needs to 18 // use a cleanup function to ensure proper sequencing. If this were 19 // done in a .then() for the success case, a test that expected 20 // failure would already be marked done at the time that runs, and the 21 // shutdown would interfere with the next test which may have started. 22 t.add_cleanup(async () => { 23 // If a session was created, end it. 24 if (testSession) { 25 await testSession.end().catch(() => {}); 26 } 27 }); 28 29 return navigator.xr.test.simulateDeviceConnection(fakeDeviceInit) 30 .then((controller) => { 31 testDeviceController = controller; 32 return sessionObjects.gl.makeXRCompatible(); 33 }) 34 .then(() => new Promise((resolve, reject) => { 35 // Perform the session request in a user gesture. 36 xr_debug(name, 'simulateUserActivation'); 37 navigator.xr.test.simulateUserActivation(() => { 38 xr_debug(name, 'document.hasFocus()=' + document.hasFocus()); 39 navigator.xr.requestSession(sessionMode, sessionInit || {}) 40 .then(async (session) => { 41 xr_debug(name, 'session start'); 42 testSession = session; 43 session.mode = sessionMode; 44 session.sessionInit = sessionInit; 45 // This method creates test specific session objects. 46 sessionObjects.xrBinding = new XRWebGLBinding(session, sessionObjects.gl); 47 // Request a 'local' reference space which is required for layers creation. 48 sessionObjects.xrSpace = await session.requestReferenceSpace('local'); 49 if (!sessionObjects.xrSpace) { 50 reject("Local space is required for layers test."); 51 return; 52 } 53 xr_debug(name, 'session.visibilityState=' + session.visibilityState); 54 try { 55 resolve(func(session, testDeviceController, t, sessionObjects)); 56 } catch(err) { 57 reject("Test function failed with: " + err); 58 } 59 }) 60 .catch((err) => { 61 xr_debug(name, 'error: ' + err); 62 reject( 63 'Session with params ' + 64 JSON.stringify(sessionMode) + 65 ' was rejected on device ' + 66 JSON.stringify(fakeDeviceInit) + 67 ' with error: ' + err); 68 }); 69 }); 70 })); 71 } 72 73 xr_promise_test( 74 name + ' - webgl', 75 runTest, 76 properties, 77 'webgl', 78 {alpha: false, antialias: false, ...glcontextProperties} 79 ); 80 xr_promise_test( 81 name + ' - webgl2', 82 runTest, 83 properties, 84 'webgl2', 85 {alpha: false, antialias: false, ...glcontextProperties} 86 ); 87 }