Best JavaScript code snippet using wpt
webxr-test.js
Source:webxr-test.js
1'use strict';2// This polyfill library implements the WebXR Test API as specified here:3// https://github.com/immersive-web/webxr-test-api4class ChromeXRTest {5 constructor() {6 this.mockVRService_ = new MockVRService(mojo.frameInterfaces);7 }8 simulateDeviceConnection(init_params) {9 return Promise.resolve(this.mockVRService_.addRuntime(init_params));10 }11 simulateDeviceDisconnection(device) {12 this.mockVRService_.removeRuntime(device);13 }14 simulateUserActivation(callback) {15 return new Promise(resolve => {16 let button = document.createElement('button');17 button.textContent = 'click to continue test';18 button.style.display = 'block';19 button.style.fontSize = '20px';20 button.style.padding = '10px';21 button.onclick = () => {22 resolve(callback());23 document.body.removeChild(button);24 };25 document.body.appendChild(button);26 test_driver.click(button);27 });28 }29}30// Mocking class definitions31// Mock service implements both the VRService and XRDevice mojo interfaces.32class MockVRService {33 constructor() {34 this.bindingSet_ = new mojo.BindingSet(device.mojom.VRService);35 this.runtimes_ = [];36 this.interceptor_ =37 new MojoInterfaceInterceptor(device.mojom.VRService.name);38 this.interceptor_.oninterfacerequest = e =>39 this.bindingSet_.addBinding(this, e.handle);40 this.interceptor_.start();41 }42 // Test methods43 addRuntime(fakeDeviceInit) {44 let runtime = new MockRuntime(fakeDeviceInit, this);45 this.runtimes_.push(runtime);46 if (this.client_) {47 this.client_.onDeviceChanged();48 }49 return runtime;50 }51 removeRuntime(runtime) {52 // We have no way of distinguishing between devices, so just clear the53 // entire list for now.54 // TODO(http://crbug.com/873409) We also have no way right now to disconnect55 // devices.56 this.runtimes_ = [];57 }58 // VRService implementation.59 requestDevice() {60 if (this.runtimes_.length > 0) {61 let devicePtr = new device.mojom.XRDevicePtr();62 new mojo.Binding(63 device.mojom.XRDevice, this, mojo.makeRequest(devicePtr));64 return Promise.resolve({device: devicePtr});65 } else {66 return Promise.resolve({device: null});67 }68 }69 setClient(client) {70 this.client_ = client;71 }72 // XRDevice implementation.73 requestSession(sessionOptions, was_activation) {74 let requests = [];75 // Request a session from all the runtimes.76 for (let i = 0; i < this.runtimes_.length; i++) {77 requests[i] = this.runtimes_[i].requestRuntimeSession(sessionOptions);78 }79 return Promise.all(requests).then((results) => {80 // Find and return the first successful result.81 for (let i = 0; i < results.length; i++) {82 if (results[i].session) {83 return results[i];84 }85 }86 // If there were no successful results, returns a null session.87 return {session: null};88 });89 }90 supportsSession(sessionOptions) {91 let requests = [];92 // Check supports on all the runtimes.93 for (let i = 0; i < this.runtimes_.length; i++) {94 requests[i] = this.runtimes_[i].runtimeSupportsSession(sessionOptions);95 }96 return Promise.all(requests).then((results) => {97 // Find and return the first successful result.98 for (let i = 0; i < results.length; i++) {99 if (results[i].supportsSession) {100 return results[i];101 }102 }103 // If there were no successful results, returns false.104 return {supportsSession: false};105 });106 };107}108// Implements XRFrameDataProvider and XRPresentationProvider. Maintains a mock109// for XRPresentationProvider.110class MockRuntime {111 constructor(fakeDeviceInit, service) {112 this.sessionClient_ = new device.mojom.XRSessionClientPtr();113 this.presentation_provider_ = new MockXRPresentationProvider();114 this.pose_ = null;115 this.next_frame_id_ = 0;116 this.service_ = service;117 this.framesOfReference = {};118 if (fakeDeviceInit.supportsImmersive) {119 this.displayInfo_ = this.getImmersiveDisplayInfo();120 } else {121 this.displayInfo_ = this.getNonImmersiveDisplayInfo();122 }123 if (fakeDeviceInit.supportsEnvironmentIntegration) {124 this.displayInfo_.capabilities.canProvideEnvironmentIntegration = true;125 }126 }127 // Test methods.128 setXRPresentationFrameData(poseMatrix, views) {129 if (poseMatrix == null) {130 this.pose_ = null;131 } else {132 this.setPoseFromMatrix(poseMatrix);133 }134 if (views) {135 let changed = false;136 for (let i = 0; i < views.length; i++) {137 if (views[i].eye == 'left') {138 this.displayInfo_.leftEye = this.getEye(views[i]);139 changed = true;140 } else if (views[i].eye == 'right') {141 this.displayInfo_.rightEye = this.getEye(views[i]);142 changed = true;143 }144 }145 if (changed) {146 this.sessionClient_.onChanged(this.displayInfo_);147 }148 }149 }150 setPoseFromMatrix(poseMatrix) {151 this.pose_ = {152 orientation: null,153 position: null,154 angularVelocity: null,155 linearVelocity: null,156 angularAcceleration: null,157 linearAcceleration: null,158 inputState: null,159 poseIndex: 0160 };161 let pose = this.poseFromMatrix(poseMatrix);162 for (let field in pose) {163 if (this.pose_.hasOwnProperty(field)) {164 this.pose_[field] = pose[field];165 }166 }167 }168 poseFromMatrix(m) {169 let orientation = [];170 let m00 = m[0];171 let m11 = m[5];172 let m22 = m[10];173 // The max( 0, ... ) is just a safeguard against rounding error.174 orientation[3] = Math.sqrt(Math.max(0, 1 + m00 + m11 + m22)) / 2;175 orientation[0] = Math.sqrt(Math.max(0, 1 + m00 - m11 - m22)) / 2;176 orientation[1] = Math.sqrt(Math.max(0, 1 - m00 + m11 - m22)) / 2;177 orientation[2] = Math.sqrt(Math.max(0, 1 - m00 - m11 + m22)) / 2;178 let position = [];179 position[0] = m[12];180 position[1] = m[13];181 position[2] = m[14];182 return {183 orientation, position184 }185 }186 getNonImmersiveDisplayInfo() {187 let displayInfo = this.getImmersiveDisplayInfo();188 displayInfo.capabilities.canPresent = false;189 displayInfo.leftEye = null;190 displayInfo.rightEye = null;191 return displayInfo;192 }193 // Function to generate some valid display information for the device.194 getImmersiveDisplayInfo() {195 return {196 displayName: 'FakeDevice',197 capabilities: {198 hasPosition: false,199 hasExternalDisplay: false,200 canPresent: true,201 maxLayers: 1202 },203 stageParameters: null,204 leftEye: {205 fieldOfView: {206 upDegrees: 48.316,207 downDegrees: 50.099,208 leftDegrees: 50.899,209 rightDegrees: 35.197210 },211 offset: [-0.032, 0, 0],212 renderWidth: 20,213 renderHeight: 20214 },215 rightEye: {216 fieldOfView: {217 upDegrees: 48.316,218 downDegrees: 50.099,219 leftDegrees: 50.899,220 rightDegrees: 35.197221 },222 offset: [0.032, 0, 0],223 renderWidth: 20,224 renderHeight: 20225 },226 webxrDefaultFramebufferScale: 0.7,227 };228 }229 // This function converts between the matrix provided by the WebXR test API230 // and the internal data representation.231 getEye(fakeXRViewInit) {232 let m = fakeXRViewInit.projectionMatrix;233 function toDegrees(tan) {234 return Math.atan(tan) * 180 / Math.PI;235 }236 let xScale = m[0];237 let yScale = m[5];238 let near = m[14] / (m[10] - 1);239 let far = m[14] / (m[10] - 1);240 let leftTan = (1 - m[8]) / m[0];241 let rightTan = (1 + m[8]) / m[0];242 let upTan = (1 + m[9]) / m[5];243 let downTan = (1 - m[9]) / m[5];244 return {245 fieldOfView: {246 upDegrees: toDegrees(upTan),247 downDegrees: toDegrees(downTan),248 leftDegrees: toDegrees(leftTan),249 rightDegrees: toDegrees(rightTan)250 },251 offset: [0, 0, 0],252 renderWidth: 20,253 renderHeight: 20254 };255 }256 // Mojo function implementations.257 // XRFrameDataProvider implementation.258 getFrameData() {259 if (this.pose_) {260 this.pose_.poseIndex++;261 }262 // Convert current document time to monotonic time.263 let now = window.performance.now() / 1000.0;264 let diff = now - internals.monotonicTimeToZeroBasedDocumentTime(now);265 now += diff;266 now *= 1000000;267 return Promise.resolve({268 frameData: {269 pose: this.pose_,270 timeDelta: {271 microseconds: now,272 },273 frameId: this.next_frame_id_++,274 projectionMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],275 bufferHolder: null,276 bufferSize: {}277 }278 });279 }280 getEnvironmentIntegrationProvider(environmentProviderRequest) {281 this.environmentProviderBinding_ = new mojo.AssociatedBinding(282 device.mojom.XREnvironmentIntegrationProvider, this,283 environmentProviderRequest);284 }285 // Note that if getEnvironmentProvider hasn't finished running yet this will286 // be undefined. It's recommended that you allow a successful task to post287 // first before attempting to close.288 closeEnvironmentIntegrationProvider() {289 this.environmentProviderBinding_.close();290 }291 updateSessionGeometry(frame_size, display_rotation) {292 // This function must exist to ensure that calls to it do not crash, but we293 // do not have any use for this data at present.294 }295 // Utility function296 requestRuntimeSession(sessionOptions) {297 return this.runtimeSupportsSession(sessionOptions).then((result) => {298 // The JavaScript bindings convert c_style_names to camelCase names.299 let options = new device.mojom.XRPresentationTransportOptions();300 options.transportMethod =301 device.mojom.XRPresentationTransportMethod.SUBMIT_AS_MAILBOX_HOLDER;302 options.waitForTransferNotification = true;303 options.waitForRenderNotification = true;304 let submit_frame_sink;305 if (result.supportsSession) {306 submit_frame_sink = {307 clientRequest: this.presentation_provider_.getClientRequest(),308 provider: this.presentation_provider_.bindProvider(sessionOptions),309 transportOptions: options310 };311 let dataProviderPtr = new device.mojom.XRFrameDataProviderPtr();312 let dataProviderRequest = mojo.makeRequest(dataProviderPtr);313 let dataProviderBinding = new mojo.Binding(314 device.mojom.XRFrameDataProvider, this, dataProviderRequest);315 let clientRequest = mojo.makeRequest(this.sessionClient_);316 return Promise.resolve({317 session: {318 submitFrameSink: submit_frame_sink,319 dataProvider: dataProviderPtr,320 clientRequest: clientRequest,321 displayInfo: this.displayInfo_322 }323 });324 } else {325 return Promise.resolve({session: null});326 }327 });328 }329 runtimeSupportsSession(options) {330 return Promise.resolve({331 supportsSession:332 !options.immersive || this.displayInfo_.capabilities.canPresent333 });334 };335}336class MockXRPresentationProvider {337 constructor() {338 this.binding_ = new mojo.Binding(device.mojom.XRPresentationProvider, this);339 this.submit_frame_count_ = 0;340 this.missing_frame_count_ = 0;341 }342 bindProvider(request) {343 let providerPtr = new device.mojom.XRPresentationProviderPtr();344 let providerRequest = mojo.makeRequest(providerPtr);345 this.binding_.close();346 this.binding_ = new mojo.Binding(347 device.mojom.XRPresentationProvider, this, providerRequest);348 return providerPtr;349 }350 getClientRequest() {351 this.submitFrameClient_ = new device.mojom.XRPresentationClientPtr();352 return mojo.makeRequest(this.submitFrameClient_);353 }354 // XRPresentationProvider mojo implementation355 submitFrameMissing(frameId, mailboxHolder, timeWaited) {356 this.missing_frame_count_++;357 }358 submitFrame(frameId, mailboxHolder, timeWaited) {359 this.submit_frame_count_++;360 // Trigger the submit completion callbacks here. WARNING: The361 // Javascript-based mojo mocks are *not* re-entrant. It's OK to362 // wait for these notifications on the next frame, but waiting363 // within the current frame would never finish since the incoming364 // calls would be queued until the current execution context finishes.365 this.submitFrameClient_.onSubmitFrameTransferred(true);366 this.submitFrameClient_.onSubmitFrameRendered();367 }368}...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4 'videoParams': {5 },6};7wpt.runTest(testURL, options, function(err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14header('Content-Type: application/json');15{ statusCode: 400,16 { response:17 { statusCode: 400,18 data: 'Invalid test ID' } } }19{ statusCode: 400,20 { response:21 { statusCode: 400,
Using AI Code Generation
1var wpt = require('./wpt.js');2var wptPtr = new wpt();3 if(err){4 console.log(err);5 }else{6 console.log(data);7 }8});9var request = require('request');10var wpt = function(){11};12wpt.prototype.dataProviderPtr = function(url, callback){13 var options = {14 headers: {15 }16 };17 function callback(err, response, body) {18 if (!err && response.statusCode == 200) {19 var info = JSON.parse(body);20 callback(null, info);21 }else{22 callback(err, null);23 }24 }25 request(options, callback);26};27module.exports = wpt;28 at Error (native)29 at TLSSocket.socketOnData [as ondata] (http.js:1581:20)30 at TLSSocket.emit (events.js:95:17)31 at readableAddChunk (_stream_readable.js:163:16)32 at TLSSocket.Readable.push (_stream_readable.js:126:10)33 at TLSWrap.onread (net.js:529:21)
Using AI Code Generation
1var dataProvider = require('./dataProvider.js');2var wptrunner = require('./wptrunner.js');3var dataProviderPtr = wptrunner.dataProviderPtr;4dataProviderPtr('test', function(data){5 console.log(data);6});7var dataProvider = require('./dataProvider.js');8var wptrunner = require('./wptrunner.js');9var dataProviderPtr = wptrunner.dataProviderPtr;10dataProviderPtr('test', function(data){11 console.log(data);12});13var dataProvider = require('./dataProvider.js');14var wptrunner = require('./wptrunner.js');15var dataProviderPtr = wptrunner.dataProviderPtr;16dataProviderPtr('test', function(data){17 console.log(data);18});19var dataProvider = require('./dataProvider.js');20var wptrunner = require('./wptrunner.js');21var dataProviderPtr = wptrunner.dataProviderPtr;22dataProviderPtr('test', function(data){23 console.log(data);24});25var dataProvider = require('./dataProvider.js');26var wptrunner = require('./wptrunner.js');27var dataProviderPtr = wptrunner.dataProviderPtr;28dataProviderPtr('test', function(data){29 console.log(data);30});31var dataProvider = require('./dataProvider.js');32var wptrunner = require('./wptrunner.js');
Using AI Code Generation
1var dataProviderPtr = WScript.CreateObject("WPTDataProviderPtr");2var test = dataProviderPtr.GetData("test");3WScript.Echo("test is " + test);4var dataProvider = WScript.CreateObject("WPTDataProvider");5var test2 = dataProvider.GetData("test");6WScript.Echo("test2 is " + test2);7var dataProviderPtr = WScript.CreateObject("WPTDataProviderPtr");8var test3 = dataProviderPtr.GetData("test");9WScript.Echo("test3 is " + test3);10var dataProvider = WScript.CreateObject("WPTDataProvider");11var test4 = dataProvider.GetData("test");12WScript.Echo("test4 is " + test4);13 var dataProviderPtr = new ActiveXObject("WPTDataProviderPtr");14 var test = dataProviderPtr.GetData("test");15 alert("test is " + test);16 var dataProvider = new ActiveXObject("WPTDataProvider");17 var test2 = dataProvider.GetData("test");18 alert("test2 is " + test2);19 var dataProviderPtr = new ActiveXObject("WPTDataProviderPtr");20 var test3 = dataProviderPtr.GetData("test");21 alert("test3 is " + test3);22 var dataProvider = new ActiveXObject("WPTDataProvider");23 var test4 = dataProvider.GetData("test");24 alert("test4 is " + test4);
Using AI Code Generation
1var dataProvider = wpt.dataProviderPtr();2dataProvider.set("test", "testValue");3dataProvider.set("test2", "testValue2");4dataProvider.set("test3", "testValue3");5var dataProvider = wpt.dataProviderPtr();6dataProvider.set("test", "testValue");7dataProvider.set("test2", "testValue2");8dataProvider.set("test3", "testValue3");9var dataProvider = wpt.dataProviderPtr();10dataProvider.set("test", "testValue");11dataProvider.set("test2", "testValue2");12dataProvider.set("test3", "testValue3");13var dataProvider = wpt.dataProviderPtr();14dataProvider.set("test", "testValue");15dataProvider.set("test2", "testValue2");16dataProvider.set("test3", "testValue3");17var dataProvider = wpt.dataProviderPtr();18dataProvider.set("test", "testValue");19dataProvider.set("test2", "testValue2");20dataProvider.set("test3", "testValue3");21var dataProvider = wpt.dataProviderPtr();22dataProvider.set("test", "testValue");23dataProvider.set("test2", "testValue2");24dataProvider.set("test3", "testValue3");25var dataProvider = wpt.dataProviderPtr();26dataProvider.set("test", "testValue");27dataProvider.set("test2", "testValue2");28dataProvider.set("test3", "testValue3");29var dataProvider = wpt.dataProviderPtr();30dataProvider.set("test", "testValue");31dataProvider.set("test2", "testValue2");32dataProvider.set("test3", "testValue3");33var dataProvider = wpt.dataProviderPtr();
Using AI Code Generation
1var dataProviderPtr = require("./wptrunner.js").dataProviderPtr;2var dataProvider = dataProviderPtr();3dataProvider.getTests("testharness", "testharness", "testharness", "testharness", function(tests) {4 console.log(tests);5});6dataProvider.getTests("testharness", "testharness", "testharness", "testharness", function(tests) {7 console.log(tests);8});9[ { path: 'testharness',10 testdriverreport_vendor: false } ]11[ { path: 'testharness',12 testdriverreport_vendor: false } ]
Using AI Code Generation
1var wptab = Components.classes["@mozilla.org/webapptab;1"].getService(Components.interfaces.nsIWebAppTab);2var dataProviderPtr = wptab.dataProviderPtr;3var dataProvider = dataProviderPtr.QueryInterface(Components.interfaces.nsIWebAppTabDataProvider);4var currentURL = dataProvider.getDataValue("currentURL");5alert(currentURL);6var wptab = Components.classes["@mozilla.org/webapptab;1"].getService(Components.interfaces.nsIWebAppTab);7var dataProviderPtr = wptab.dataProviderPtr;8var dataProvider = dataProviderPtr.QueryInterface(Components.interfaces.nsIWebAppTabDataProvider);9var currentURL = dataProvider.getDataValue("currentURL");10alert(currentURL);11var wptab = Components.classes["@mozilla.org/webapptab;1"].getService(Components.interfaces.nsIWebAppTab);12var dataProviderPtr = wptab.dataProviderPtr;13var dataProvider = dataProviderPtr.QueryInterface(Components.interfaces.nsIWebAppTabDataProvider);14var currentURL = dataProvider.getDataValue("currentURL");15alert(currentURL);16var wptab = Components.classes["@mozilla.org/webapptab;1"].getService(Components.interfaces.nsIWebAppTab);17var dataProviderPtr = wptab.dataProviderPtr;18var dataProvider = dataProviderPtr.QueryInterface(Components.interfaces.nsIWebAppTabDataProvider);19var currentURL = dataProvider.getDataValue("currentURL");20alert(currentURL);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!