Best JavaScript code snippet using wpt
webxr-test.js
Source:webxr-test.js
...758 let input_state = null;759 if (this.input_sources_.size > 0) {760 input_state = [];761 for (const input_source of this.input_sources_.values()) {762 input_state.push(input_source._getInputSourceState());763 }764 }765 let frame_views = this.primaryViews_;766 for (let i = 0; i < this.primaryViews_.length; i++) {767 this.primaryViews_[i].mojoFromView =768 this._getMojoFromViewerWithOffset(this.primaryViews_[i].viewOffset);769 }770 if (this.enabledFeatures_.includes(vrMojom.XRSessionFeature.SECONDARY_VIEWS)) {771 for (let i = 0; i < this.secondaryViews_.length; i++) {772 this.secondaryViews_[i].mojoFromView =773 this._getMojoFromViewerWithOffset(this.secondaryViews_[i].viewOffset);774 }775 frame_views = frame_views.concat(this.secondaryViews_);776 }777 const frameData = {778 mojoFromViewer: this.pose_,779 views: frame_views,780 mojoSpaceReset: mojo_space_reset,781 inputState: input_state,782 timeDelta: {783 // window.performance.now() is in milliseconds, so convert to microseconds.784 microseconds: BigInt(Math.floor(window.performance.now() * 1000)),785 },786 frameId: this.next_frame_id_,787 bufferHolder: null,788 bufferSize: {},789 renderingTimeRatio: 0,790 stageParameters: this.stageParameters_,791 stageParametersId: this.stageParametersId_,792 lightEstimationData: this.light_estimate_793 };794 this.next_frame_id_++;795 this._calculateHitTestResults(frameData);796 this._calculateAnchorInformation(frameData);797 this._calculateDepthInformation(frameData);798 this._injectAdditionalFrameData(options, frameData);799 resolve({frameData});800 };801 if(this.sessionOptions_.mode == vrMojom.XRSessionMode.kInline) {802 // Inline sessions should not have a delay introduced since it causes them803 // to miss a vsync blink-side and delays propagation of changes that happened804 // within a rAFcb by one frame (e.g. setViewerOrigin() calls would take 2 frames805 // to propagate).806 populatePose();807 } else {808 // For immerive sessions, add additional delay to allow for anchor creation809 // promises to run.810 setTimeout(populatePose, 3); // note: according to MDN, the timeout is not exact811 }812 });813 }814 getEnvironmentIntegrationProvider(environmentProviderRequest) {815 if (this.environmentProviderReceiver_) {816 this.environmentProviderReceiver_.$.close();817 }818 this.environmentProviderReceiver_ =819 new vrMojom.XREnvironmentIntegrationProviderReceiver(this);820 this.environmentProviderReceiver_.$.bindHandle(821 environmentProviderRequest.handle);822 }823 setInputSourceButtonListener(listener) { listener.$.close(); }824 // XREnvironmentIntegrationProvider implementation:825 subscribeToHitTest(nativeOriginInformation, entityTypes, ray) {826 if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {827 // Reject outside of AR.828 return Promise.resolve({829 result : vrMojom.SubscribeToHitTestResult.FAILURE_GENERIC,830 subscriptionId : 0n831 });832 }833 if (!this._nativeOriginKnown(nativeOriginInformation)) {834 return Promise.resolve({835 result : vrMojom.SubscribeToHitTestResult.FAILURE_GENERIC,836 subscriptionId : 0n837 });838 }839 // Reserve the id for hit test source:840 const id = this.next_hit_test_id_++;841 const hitTestParameters = { isTransient: false, profileName: null };842 const controller = new FakeXRHitTestSourceController(id);843 return this._shouldHitTestSourceCreationSucceed(hitTestParameters, controller)844 .then((succeeded) => {845 if(succeeded) {846 // Store the subscription information as-is (including controller):847 this.hitTestSubscriptions_.set(id, { nativeOriginInformation, entityTypes, ray, controller });848 return Promise.resolve({849 result : vrMojom.SubscribeToHitTestResult.SUCCESS,850 subscriptionId : id851 });852 } else {853 return Promise.resolve({854 result : vrMojom.SubscribeToHitTestResult.FAILURE_GENERIC,855 subscriptionId : 0n856 });857 }858 });859 }860 subscribeToHitTestForTransientInput(profileName, entityTypes, ray){861 if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {862 // Reject outside of AR.863 return Promise.resolve({864 result : vrMojom.SubscribeToHitTestResult.FAILURE_GENERIC,865 subscriptionId : 0n866 });867 }868 const id = this.next_hit_test_id_++;869 const hitTestParameters = { isTransient: true, profileName: profileName };870 const controller = new FakeXRHitTestSourceController(id);871 // Check if we have hit test source creation callback.872 // If yes, ask it if the hit test source creation should succeed.873 // If no, for back-compat, assume the hit test source creation succeeded.874 return this._shouldHitTestSourceCreationSucceed(hitTestParameters, controller)875 .then((succeeded) => {876 if(succeeded) {877 // Store the subscription information as-is (including controller):878 this.transientHitTestSubscriptions_.set(id, { profileName, entityTypes, ray, controller });879 return Promise.resolve({880 result : vrMojom.SubscribeToHitTestResult.SUCCESS,881 subscriptionId : id882 });883 } else {884 return Promise.resolve({885 result : vrMojom.SubscribeToHitTestResult.FAILURE_GENERIC,886 subscriptionId : 0n887 });888 }889 });890 }891 unsubscribeFromHitTest(subscriptionId) {892 let controller = null;893 if(this.transientHitTestSubscriptions_.has(subscriptionId)){894 controller = this.transientHitTestSubscriptions_.get(subscriptionId).controller;895 this.transientHitTestSubscriptions_.delete(subscriptionId);896 } else if(this.hitTestSubscriptions_.has(subscriptionId)){897 controller = this.hitTestSubscriptions_.get(subscriptionId).controller;898 this.hitTestSubscriptions_.delete(subscriptionId);899 }900 if(controller) {901 controller.deleted = true;902 }903 }904 createAnchor(nativeOriginInformation, nativeOriginFromAnchor) {905 return new Promise((resolve) => {906 if(this.anchor_creation_callback_ == null) {907 resolve({908 result : vrMojom.CreateAnchorResult.FAILURE,909 anchorId : 0n910 });911 return;912 }913 const mojoFromNativeOrigin = this._getMojoFromNativeOrigin(nativeOriginInformation);914 if(mojoFromNativeOrigin == null) {915 resolve({916 result : vrMojom.CreateAnchorResult.FAILURE,917 anchorId : 0n918 });919 return;920 }921 const mojoFromAnchor = XRMathHelper.mul4x4(mojoFromNativeOrigin, nativeOriginFromAnchor);922 const anchorCreationParameters = {923 requestedAnchorOrigin: mojoFromAnchor,924 isAttachedToEntity: false,925 };926 const anchorController = new FakeXRAnchorController();927 this.anchor_creation_callback_(anchorCreationParameters, anchorController)928 .then((result) => {929 if(result) {930 // If the test allowed the anchor creation,931 // store the anchor controller & return success.932 const anchor_id = this.next_anchor_id_;933 this.next_anchor_id_++;934 this.anchor_controllers_.set(anchor_id, anchorController);935 anchorController.device = this;936 anchorController.id = anchor_id;937 resolve({938 result : vrMojom.CreateAnchorResult.SUCCESS,939 anchorId : anchor_id940 });941 } else {942 // The test has rejected anchor creation.943 resolve({944 result : vrMojom.CreateAnchorResult.FAILURE,945 anchorId : 0n946 });947 }948 })949 .catch(() => {950 // The test threw an error, treat anchor creation as failed.951 resolve({952 result : vrMojom.CreateAnchorResult.FAILURE,953 anchorId : 0n954 });955 });956 });957 }958 createPlaneAnchor(planeFromAnchor, planeId) {959 return new Promise((resolve) => {960 // Not supported yet.961 resolve({962 result : vrMojom.CreateAnchorResult.FAILURE,963 anchorId : 0n,964 });965 });966 }967 detachAnchor(anchorId) {}968 // Utility function969 _requestRuntimeSession(sessionOptions) {970 return this._runtimeSupportsSession(sessionOptions).then((result) => {971 // The JavaScript bindings convert c_style_names to camelCase names.972 const options = {973 transportMethod:974 vrMojom.XRPresentationTransportMethod.SUBMIT_AS_MAILBOX_HOLDER,975 waitForTransferNotification: true,976 waitForRenderNotification: true,977 waitForGpuFence: false,978 };979 let submit_frame_sink;980 if (result.supportsSession) {981 submit_frame_sink = {982 clientReceiver: this.presentation_provider_._getClientReceiver(),983 provider: this.presentation_provider_._bindProvider(sessionOptions),984 transportOptions: options985 };986 const dataProviderPtr = new vrMojom.XRFrameDataProviderRemote();987 this.dataProviderReceiver_ =988 new vrMojom.XRFrameDataProviderReceiver(this);989 this.dataProviderReceiver_.$.bindHandle(990 dataProviderPtr.$.bindNewPipeAndPassReceiver().handle);991 this.sessionOptions_ = sessionOptions;992 this.sessionClient_ = new vrMojom.XRSessionClientRemote();993 const clientReceiver = this.sessionClient_.$.bindNewPipeAndPassReceiver();994 const enabled_features = [];995 for (let i = 0; i < sessionOptions.requiredFeatures.length; i++) {996 if (this.supportedFeatures_.indexOf(sessionOptions.requiredFeatures[i]) !== -1) {997 enabled_features.push(sessionOptions.requiredFeatures[i]);998 } else {999 return Promise.resolve({session: null});1000 }1001 }1002 for (let i =0; i < sessionOptions.optionalFeatures.length; i++) {1003 if (this.supportedFeatures_.indexOf(sessionOptions.optionalFeatures[i]) !== -1) {1004 enabled_features.push(sessionOptions.optionalFeatures[i]);1005 }1006 }1007 this.enabledFeatures_ = enabled_features;1008 return Promise.resolve({1009 session: {1010 submitFrameSink: submit_frame_sink,1011 dataProvider: dataProviderPtr,1012 clientReceiver: clientReceiver,1013 displayInfo: this.displayInfo_,1014 enabledFeatures: enabled_features,1015 deviceConfig: {1016 usesInputEventing: false,1017 defaultFramebufferScale: this.defaultFramebufferScale_,1018 supportsViewportScaling: true,1019 depthConfiguration:1020 enabled_features.includes(vrMojom.XRSessionFeature.DEPTH) ? {1021 depthUsage: vrMojom.XRDepthUsage.kCPUOptimized,1022 depthDataFormat: vrMojom.XRDepthDataFormat.kLuminanceAlpha,1023 } : null,1024 },1025 enviromentBlendMode: this.enviromentBlendMode_,1026 interactionMode: this.interactionMode_1027 }1028 });1029 } else {1030 return Promise.resolve({session: null});1031 }1032 });1033 }1034 _runtimeSupportsSession(options) {1035 let result = this.supportedModes_.includes(options.mode);1036 if (options.requiredFeatures.includes(vrMojom.XRSessionFeature.DEPTH)1037 || options.optionalFeatures.includes(vrMojom.XRSessionFeature.DEPTH)) {1038 result &= options.depthOptions.usagePreferences.includes(vrMojom.XRDepthUsage.kCPUOptimized);1039 result &= options.depthOptions.dataFormatPreferences.includes(vrMojom.XRDepthDataFormat.kLuminanceAlpha);1040 }1041 return Promise.resolve({1042 supportsSession: result,1043 });1044 }1045 // Private functions - utilities:1046 _nativeOriginKnown(nativeOriginInformation){1047 if (nativeOriginInformation.inputSourceSpaceInfo !== undefined) {1048 if (!this.input_sources_.has(nativeOriginInformation.inputSourceSpaceInfo.inputSourceId)) {1049 // Unknown input source.1050 return false;1051 }1052 return true;1053 } else if (nativeOriginInformation.referenceSpaceType !== undefined) {1054 // Bounded_floor & unbounded ref spaces are not yet supported for AR:1055 if (nativeOriginInformation.referenceSpaceType == vrMojom.XRReferenceSpaceType.kUnbounded1056 || nativeOriginInformation.referenceSpaceType == vrMojom.XRReferenceSpaceType.kBoundedFloor) {1057 return false;1058 }1059 return true;1060 } else {1061 // Planes and anchors are not yet supported by the mock interface.1062 return false;1063 }1064 }1065 // Private functions - anchors implementation:1066 // Modifies passed in frameData to add anchor information.1067 _calculateAnchorInformation(frameData) {1068 if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {1069 return;1070 }1071 frameData.anchorsData = {allAnchorsIds: [], updatedAnchorsData: []};1072 for(const [id, controller] of this.anchor_controllers_) {1073 frameData.anchorsData.allAnchorsIds.push(id);1074 // Send the entire anchor data over if there was a change since last GetFrameData().1075 if(controller.dirty) {1076 const anchorData = {id};1077 if(!controller.paused) {1078 anchorData.mojoFromAnchor = getPoseFromTransform(1079 XRMathHelper.decomposeRigidTransform(1080 controller._getAnchorOrigin()));1081 }1082 controller._markProcessed();1083 frameData.anchorsData.updatedAnchorsData.push(anchorData);1084 }1085 }1086 }1087 // Private functions - depth sensing implementation:1088 // Modifies passed in frameData to add anchor information.1089 _calculateDepthInformation(frameData) {1090 if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {1091 return;1092 }1093 if (!this.enabledFeatures_.includes(vrMojom.XRSessionFeature.DEPTH)) {1094 return;1095 }1096 // If we don't have a current depth data, we'll return null1097 // (i.e. no data is not a valid data, so it cannot be "StillValid").1098 if (this.depthSensingData_ == null) {1099 frameData.depthData = null;1100 return;1101 }1102 if(!this.depthSensingDataDirty_) {1103 frameData.depthData = { dataStillValid: {}};1104 return;1105 }1106 frameData.depthData = {1107 updatedDepthData: {1108 timeDelta: frameData.timeDelta,1109 normTextureFromNormView: this.depthSensingData_.normDepthBufferFromNormView,1110 rawValueToMeters: this.depthSensingData_.rawValueToMeters,1111 size: { width: this.depthSensingData_.width, height: this.depthSensingData_.height },1112 pixelData: { bytes: this.depthSensingData_.depthData }1113 }1114 };1115 this.depthSensingDataDirty_ = false;1116 }1117 // Private functions - hit test implementation:1118 // Returns a Promise<bool> that signifies whether hit test source creation should succeed.1119 // If we have a hit test source creation callback installed, invoke it and return its result.1120 // If it's not installed, for back-compat just return a promise that resolves to true.1121 _shouldHitTestSourceCreationSucceed(hitTestParameters, controller) {1122 if(this.hit_test_source_creation_callback_) {1123 return this.hit_test_source_creation_callback_(hitTestParameters, controller);1124 } else {1125 return Promise.resolve(true);1126 }1127 }1128 // Modifies passed in frameData to add hit test results.1129 _calculateHitTestResults(frameData) {1130 if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {1131 return;1132 }1133 frameData.hitTestSubscriptionResults = {results: [],1134 transientInputResults: []};1135 if (!this.world_) {1136 return;1137 }1138 // Non-transient hit test:1139 for (const [id, subscription] of this.hitTestSubscriptions_) {1140 const mojo_from_native_origin = this._getMojoFromNativeOrigin(subscription.nativeOriginInformation);1141 if (!mojo_from_native_origin) continue;1142 const [mojo_ray_origin, mojo_ray_direction] = this._transformRayToMojoSpace(1143 subscription.ray,1144 mojo_from_native_origin1145 );1146 const results = this._hitTestWorld(mojo_ray_origin, mojo_ray_direction, subscription.entityTypes);1147 frameData.hitTestSubscriptionResults.results.push(1148 {subscriptionId: id, hitTestResults: results});1149 }1150 // Transient hit test:1151 const mojo_from_viewer = this._getMojoFromViewer();1152 for (const [id, subscription] of this.transientHitTestSubscriptions_) {1153 const result = {subscriptionId: id,1154 inputSourceIdToHitTestResults: new Map()};1155 // Find all input sources that match the profile name:1156 const matching_input_sources = Array.from(this.input_sources_.values())1157 .filter(input_source => input_source.profiles_.includes(subscription.profileName));1158 for (const input_source of matching_input_sources) {1159 const mojo_from_native_origin = input_source._getMojoFromInputSource(mojo_from_viewer);1160 const [mojo_ray_origin, mojo_ray_direction] = this._transformRayToMojoSpace(1161 subscription.ray,1162 mojo_from_native_origin1163 );1164 const results = this._hitTestWorld(mojo_ray_origin, mojo_ray_direction, subscription.entityTypes);1165 result.inputSourceIdToHitTestResults.set(input_source.source_id_, results);1166 }1167 frameData.hitTestSubscriptionResults.transientInputResults.push(result);1168 }1169 }1170 // Returns 2-element array [origin, direction] of a ray in mojo space.1171 // |ray| is expressed relative to native origin.1172 _transformRayToMojoSpace(ray, mojo_from_native_origin) {1173 const ray_origin = {1174 x: ray.origin.x,1175 y: ray.origin.y,1176 z: ray.origin.z,1177 w: 11178 };1179 const ray_direction = {1180 x: ray.direction.x,1181 y: ray.direction.y,1182 z: ray.direction.z,1183 w: 01184 };1185 const mojo_ray_origin = XRMathHelper.transform_by_matrix(1186 mojo_from_native_origin,1187 ray_origin);1188 const mojo_ray_direction = XRMathHelper.transform_by_matrix(1189 mojo_from_native_origin,1190 ray_direction);1191 return [mojo_ray_origin, mojo_ray_direction];1192 }1193 // Hit tests the passed in ray (expressed as origin and direction) against the mocked world data.1194 _hitTestWorld(origin, direction, entityTypes) {1195 let result = [];1196 for (const region of this.world_.hitTestRegions) {1197 const partial_result = this._hitTestRegion(1198 region,1199 origin, direction,1200 entityTypes);1201 result = result.concat(partial_result);1202 }1203 return result.sort((lhs, rhs) => lhs.distance - rhs.distance).map((hitTest) => {1204 delete hitTest.distance;1205 return hitTest;1206 });1207 }1208 // Hit tests the passed in ray (expressed as origin and direction) against world region.1209 // |entityTypes| is a set of FakeXRRegionTypes.1210 // |region| is FakeXRRegion.1211 // Returns array of XRHitResults, each entry will be decorated with the distance from the ray origin (along the ray).1212 _hitTestRegion(region, origin, direction, entityTypes) {1213 const regionNameToMojoEnum = {1214 "point": vrMojom.EntityTypeForHitTest.POINT,1215 "plane": vrMojom.EntityTypeForHitTest.PLANE,1216 "mesh":null1217 };1218 if (!entityTypes.includes(regionNameToMojoEnum[region.type])) {1219 return [];1220 }1221 const result = [];1222 for (const face of region.faces) {1223 const maybe_hit = this._hitTestFace(face, origin, direction);1224 if (maybe_hit) {1225 result.push(maybe_hit);1226 }1227 }1228 // The results should be sorted by distance and there should be no 2 entries with1229 // the same distance from ray origin - that would mean they are the same point.1230 // This situation is possible when a ray intersects the region through an edge shared1231 // by 2 faces.1232 return result.sort((lhs, rhs) => lhs.distance - rhs.distance)1233 .filter((val, index, array) => index === 0 || val.distance !== array[index - 1].distance);1234 }1235 // Hit tests the passed in ray (expressed as origin and direction) against a single face.1236 // |face|, |origin|, and |direction| are specified in world (aka mojo) coordinates.1237 // |face| is an array of DOMPointInits.1238 // Returns null if the face does not intersect with the ray, otherwise the result is1239 // an XRHitResult with matrix describing the pose of the intersection point.1240 _hitTestFace(face, origin, direction) {1241 const add = XRMathHelper.add;1242 const sub = XRMathHelper.sub;1243 const mul = XRMathHelper.mul;1244 const normalize = XRMathHelper.normalize;1245 const dot = XRMathHelper.dot;1246 const cross = XRMathHelper.cross;1247 const neg = XRMathHelper.neg;1248 //1. Calculate plane normal in world coordinates.1249 const point_A = face.vertices[0];1250 const point_B = face.vertices[1];1251 const point_C = face.vertices[2];1252 const edge_AB = sub(point_B, point_A);1253 const edge_AC = sub(point_C, point_A);1254 const normal = normalize(cross(edge_AB, edge_AC));1255 const numerator = dot(sub(point_A, origin), normal);1256 const denominator = dot(direction, normal);1257 if (Math.abs(denominator) < XRMathHelper.EPSILON) {1258 // Planes are nearly parallel - there's either infinitely many intersection points or 0.1259 // Both cases signify a "no hit" for us.1260 return null;1261 } else {1262 // Single intersection point between the infinite plane and the line (*not* ray).1263 // Need to calculate the hit test matrix taking into account the face vertices.1264 const distance = numerator / denominator;1265 if (distance < 0) {1266 // Line - plane intersection exists, but not the half-line - plane does not.1267 return null;1268 } else {1269 const intersection_point = add(origin, mul(distance, direction));1270 // Since we are treating the face as a solid, flip the normal so that its1271 // half-space will contain the ray origin.1272 const y_axis = denominator > 0 ? neg(normal) : normal;1273 let z_axis = null;1274 const cos_direction_and_y_axis = dot(direction, y_axis);1275 if (Math.abs(cos_direction_and_y_axis) > (1 - XRMathHelper.EPSILON)) {1276 // Ray and the hit test normal are co-linear - try using the 'up' or 'right' vector's projection on the face plane as the Z axis.1277 // Note: this edge case is currently not covered by the spec.1278 const up = {x: 0.0, y: 1.0, z: 0.0, w: 0.0};1279 const right = {x: 1.0, y: 0.0, z: 0.0, w: 0.0};1280 z_axis = Math.abs(dot(up, y_axis)) > (1 - XRMathHelper.EPSILON)1281 ? sub(up, mul(dot(right, y_axis), y_axis)) // `up is also co-linear with hit test normal, use `right`1282 : sub(up, mul(dot(up, y_axis), y_axis)); // `up` is not co-linear with hit test normal, use it1283 } else {1284 // Project the ray direction onto the plane, negate it and use as a Z axis.1285 z_axis = neg(sub(direction, mul(cos_direction_and_y_axis, y_axis))); // Z should point towards the ray origin, not away.1286 }1287 z_axis = normalize(z_axis);1288 const x_axis = normalize(cross(y_axis, z_axis));1289 // Filter out the points not in polygon.1290 if (!XRMathHelper.pointInFace(intersection_point, face)) {1291 return null;1292 }1293 const hitResult = {planeId: 0n};1294 hitResult.distance = distance; // Extend the object with additional information used by higher layers.1295 // It will not be serialized over mojom.1296 const matrix = new Array(16);1297 matrix[0] = x_axis.x;1298 matrix[1] = x_axis.y;1299 matrix[2] = x_axis.z;1300 matrix[3] = 0;1301 matrix[4] = y_axis.x;1302 matrix[5] = y_axis.y;1303 matrix[6] = y_axis.z;1304 matrix[7] = 0;1305 matrix[8] = z_axis.x;1306 matrix[9] = z_axis.y;1307 matrix[10] = z_axis.z;1308 matrix[11] = 0;1309 matrix[12] = intersection_point.x;1310 matrix[13] = intersection_point.y;1311 matrix[14] = intersection_point.z;1312 matrix[15] = 1;1313 hitResult.mojoFromResult = getPoseFromTransform(1314 XRMathHelper.decomposeRigidTransform(matrix));1315 return hitResult;1316 }1317 }1318 }1319 _getMojoFromViewer() {1320 if (!this.pose_) {1321 return XRMathHelper.identity();1322 }1323 const transform = {1324 position: [1325 this.pose_.position.x,1326 this.pose_.position.y,1327 this.pose_.position.z],1328 orientation: [1329 this.pose_.orientation.x,1330 this.pose_.orientation.y,1331 this.pose_.orientation.z,1332 this.pose_.orientation.w],1333 };1334 return getMatrixFromTransform(transform);1335 }1336 _getMojoFromViewerWithOffset(viewOffset) {1337 return { matrix: XRMathHelper.mul4x4(this._getMojoFromViewer(), viewOffset.matrix) };1338 }1339 _getMojoFromNativeOrigin(nativeOriginInformation) {1340 const mojo_from_viewer = this._getMojoFromViewer();1341 if (nativeOriginInformation.inputSourceSpaceInfo !== undefined) {1342 if (!this.input_sources_.has(nativeOriginInformation.inputSourceSpaceInfo.inputSourceId)) {1343 return null;1344 } else {1345 const inputSource = this.input_sources_.get(nativeOriginInformation.inputSourceSpaceInfo.inputSourceId);1346 return inputSource._getMojoFromInputSource(mojo_from_viewer);1347 }1348 } else if (nativeOriginInformation.referenceSpaceType !== undefined) {1349 switch (nativeOriginInformation.referenceSpaceType) {1350 case vrMojom.XRReferenceSpaceType.kLocal:1351 return XRMathHelper.identity();1352 case vrMojom.XRReferenceSpaceType.kLocalFloor:1353 if (this.stageParameters_ == null || this.stageParameters_.mojoFromFloor == null) {1354 console.warn("Standing transform not available.");1355 return null;1356 }1357 return this.stageParameters_.mojoFromFloor.matrix;1358 case vrMojom.XRReferenceSpaceType.kViewer:1359 return mojo_from_viewer;1360 case vrMojom.XRReferenceSpaceType.kBoundedFloor:1361 return null;1362 case vrMojom.XRReferenceSpaceType.kUnbounded:1363 return null;1364 default:1365 throw new TypeError("Unrecognized XRReferenceSpaceType!");1366 }1367 } else {1368 // Anchors & planes are not yet supported for hit test.1369 return null;1370 }1371 }1372}1373class MockXRInputSource {1374 constructor(fakeInputSourceInit, id, pairedDevice) {1375 this.source_id_ = id;1376 this.pairedDevice_ = pairedDevice;1377 this.handedness_ = fakeInputSourceInit.handedness;1378 this.target_ray_mode_ = fakeInputSourceInit.targetRayMode;1379 if (fakeInputSourceInit.pointerOrigin == null) {1380 throw new TypeError("FakeXRInputSourceInit.pointerOrigin is required.");1381 }1382 this.setPointerOrigin(fakeInputSourceInit.pointerOrigin);1383 this.setProfiles(fakeInputSourceInit.profiles);1384 this.primary_input_pressed_ = false;1385 if (fakeInputSourceInit.selectionStarted != null) {1386 this.primary_input_pressed_ = fakeInputSourceInit.selectionStarted;1387 }1388 this.primary_input_clicked_ = false;1389 if (fakeInputSourceInit.selectionClicked != null) {1390 this.primary_input_clicked_ = fakeInputSourceInit.selectionClicked;1391 }1392 this.primary_squeeze_pressed_ = false;1393 this.primary_squeeze_clicked_ = false;1394 this.mojo_from_input_ = null;1395 if (fakeInputSourceInit.gripOrigin != null) {1396 this.setGripOrigin(fakeInputSourceInit.gripOrigin);1397 }1398 // This properly handles if supportedButtons were not specified.1399 this.setSupportedButtons(fakeInputSourceInit.supportedButtons);1400 this.emulated_position_ = false;1401 this.desc_dirty_ = true;1402 }1403 // WebXR Test API1404 setHandedness(handedness) {1405 if (this.handedness_ != handedness) {1406 this.desc_dirty_ = true;1407 this.handedness_ = handedness;1408 }1409 }1410 setTargetRayMode(targetRayMode) {1411 if (this.target_ray_mode_ != targetRayMode) {1412 this.desc_dirty_ = true;1413 this.target_ray_mode_ = targetRayMode;1414 }1415 }1416 setProfiles(profiles) {1417 this.desc_dirty_ = true;1418 this.profiles_ = profiles;1419 }1420 setGripOrigin(transform, emulatedPosition = false) {1421 // grip_origin was renamed to mojo_from_input in mojo1422 this.mojo_from_input_ = composeGFXTransform(transform);1423 this.emulated_position_ = emulatedPosition;1424 // Technically, setting the grip shouldn't make the description dirty, but1425 // the webxr-test-api sets our pointer as mojoFromPointer; however, we only1426 // support it across mojom as inputFromPointer, so we need to recalculate it1427 // whenever the grip moves.1428 this.desc_dirty_ = true;1429 }1430 clearGripOrigin() {1431 // grip_origin was renamed to mojo_from_input in mojo1432 if (this.mojo_from_input_ != null) {1433 this.mojo_from_input_ = null;1434 this.emulated_position_ = false;1435 this.desc_dirty_ = true;1436 }1437 }1438 setPointerOrigin(transform, emulatedPosition = false) {1439 // pointer_origin is mojo_from_pointer.1440 this.desc_dirty_ = true;1441 this.mojo_from_pointer_ = composeGFXTransform(transform);1442 this.emulated_position_ = emulatedPosition;1443 }1444 disconnect() {1445 this.pairedDevice_._removeInputSource(this);1446 }1447 reconnect() {1448 this.pairedDevice_._addInputSource(this);1449 }1450 startSelection() {1451 this.primary_input_pressed_ = true;1452 if (this.gamepad_) {1453 this.gamepad_.buttons[0].pressed = true;1454 this.gamepad_.buttons[0].touched = true;1455 }1456 }1457 endSelection() {1458 if (!this.primary_input_pressed_) {1459 throw new Error("Attempted to end selection which was not started");1460 }1461 this.primary_input_pressed_ = false;1462 this.primary_input_clicked_ = true;1463 if (this.gamepad_) {1464 this.gamepad_.buttons[0].pressed = false;1465 this.gamepad_.buttons[0].touched = false;1466 }1467 }1468 simulateSelect() {1469 this.primary_input_clicked_ = true;1470 }1471 setSupportedButtons(supportedButtons) {1472 this.gamepad_ = null;1473 this.supported_buttons_ = [];1474 // If there are no supported buttons, we can stop now.1475 if (supportedButtons == null || supportedButtons.length < 1) {1476 return;1477 }1478 const supported_button_map = {};1479 this.gamepad_ = this._getEmptyGamepad();1480 for (let i = 0; i < supportedButtons.length; i++) {1481 const buttonType = supportedButtons[i].buttonType;1482 this.supported_buttons_.push(buttonType);1483 supported_button_map[buttonType] = supportedButtons[i];1484 }1485 // Let's start by building the button state in order of priority:1486 // Primary button is index 0.1487 this.gamepad_.buttons.push({1488 pressed: this.primary_input_pressed_,1489 touched: this.primary_input_pressed_,1490 value: this.primary_input_pressed_ ? 1.0 : 0.01491 });1492 // Now add the rest of our buttons1493 this._addGamepadButton(supported_button_map['grip']);1494 this._addGamepadButton(supported_button_map['touchpad']);1495 this._addGamepadButton(supported_button_map['thumbstick']);1496 this._addGamepadButton(supported_button_map['optional-button']);1497 this._addGamepadButton(supported_button_map['optional-thumbstick']);1498 // Finally, back-fill placeholder buttons/axes1499 for (let i = 0; i < this.gamepad_.buttons.length; i++) {1500 if (this.gamepad_.buttons[i] == null) {1501 this.gamepad_.buttons[i] = {1502 pressed: false,1503 touched: false,1504 value: 01505 };1506 }1507 }1508 for (let i=0; i < this.gamepad_.axes.length; i++) {1509 if (this.gamepad_.axes[i] == null) {1510 this.gamepad_.axes[i] = 0;1511 }1512 }1513 }1514 updateButtonState(buttonState) {1515 if (this.supported_buttons_.indexOf(buttonState.buttonType) == -1) {1516 throw new Error("Tried to update state on an unsupported button");1517 }1518 const buttonIndex = this._getButtonIndex(buttonState.buttonType);1519 const axesStartIndex = this._getAxesStartIndex(buttonState.buttonType);1520 if (buttonIndex == -1) {1521 throw new Error("Unknown Button Type!");1522 }1523 // is this a 'squeeze' button?1524 if (buttonIndex === this._getButtonIndex('grip')) {1525 // squeeze1526 if (buttonState.pressed) {1527 this.primary_squeeze_pressed_ = true;1528 } else if (this.gamepad_.buttons[buttonIndex].pressed) {1529 this.primary_squeeze_clicked_ = true;1530 this.primary_squeeze_pressed_ = false;1531 } else {1532 this.primary_squeeze_clicked_ = false;1533 this.primary_squeeze_pressed_ = false;1534 }1535 }1536 this.gamepad_.buttons[buttonIndex].pressed = buttonState.pressed;1537 this.gamepad_.buttons[buttonIndex].touched = buttonState.touched;1538 this.gamepad_.buttons[buttonIndex].value = buttonState.pressedValue;1539 if (axesStartIndex != -1) {1540 this.gamepad_.axes[axesStartIndex] = buttonState.xValue == null ? 0.0 : buttonState.xValue;1541 this.gamepad_.axes[axesStartIndex + 1] = buttonState.yValue == null ? 0.0 : buttonState.yValue;1542 }1543 }1544 // DOM Overlay Extensions1545 setOverlayPointerPosition(x, y) {1546 this.overlay_pointer_position_ = {x: x, y: y};1547 }1548 // Helpers for Mojom1549 _getInputSourceState() {1550 const input_state = {};1551 input_state.sourceId = this.source_id_;1552 input_state.isAuxiliary = false;1553 input_state.primaryInputPressed = this.primary_input_pressed_;1554 input_state.primaryInputClicked = this.primary_input_clicked_;1555 input_state.primarySqueezePressed = this.primary_squeeze_pressed_;1556 input_state.primarySqueezeClicked = this.primary_squeeze_clicked_;1557 // Setting the input source's "clicked" state should generate one "select"1558 // event. Reset the input value to prevent it from continuously generating1559 // events.1560 this.primary_input_clicked_ = false;1561 // Setting the input source's "clicked" state should generate one "squeeze"1562 // event. Reset the input value to prevent it from continuously generating1563 // events....
Using AI Code Generation
1var wptools = require('./wptools.js');2var state = wptools._getInputSourceState();3console.log(state);4var _getInputSourceState = function() {5 var state = {};6 return state;7};8module.exports = {9};
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var result = wptoolkit._getInputSourceState();3console.log(result);4var wptoolkit = require('wptoolkit');5var result = wptoolkit._getInputSourceState();6console.log(result);7var wptoolkit = require('wptoolkit');8var result = wptoolkit._getInputSourceState();9console.log(result);10var wptoolkit = require('wptoolkit');11var result = wptoolkit._getInputSourceState();12console.log(result);13var wptoolkit = require('wptoolkit');14var result = wptoolkit._getInputSourceState();15console.log(result);16var wptoolkit = require('wptoolkit');17var result = wptoolkit._getInputSourceState();18console.log(result);19var wptoolkit = require('wptoolkit');20var result = wptoolkit._getInputSourceState();21console.log(result);22var wptoolkit = require('wptoolkit');23var result = wptoolkit._getInputSourceState();24console.log(result);25var wptoolkit = require('wptoolkit');26var result = wptoolkit._getInputSourceState();27console.log(result);28var wptoolkit = require('wptoolkit');29var result = wptoolkit._getInputSourceState();30console.log(result);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var test = wptoolkit._getInputSourceState();3console.log(test);4var wptoolkit = require('wptoolkit');5var test = wptoolkit._getInputSourceState();6console.log(test);7var wptoolkit = require('wptoolkit');8var test = wptoolkit._getInputSourceState();9console.log(test);10var wptoolkit = require('wptoolkit');11var test = wptoolkit._getInputSourceState();12console.log(test);13var wptoolkit = require('wptoolkit');14var test = wptoolkit._getInputSourceState();15console.log(test);16var wptoolkit = require('wptoolkit');17var test = wptoolkit._getInputSourceState();18console.log(test);19var wptoolkit = require('wptoolkit');20var test = wptoolkit._getInputSourceState();21console.log(test);22var wptoolkit = require('wptoolkit');23var test = wptoolkit._getInputSourceState();24console.log(test);25var wptoolkit = require('wptoolkit');26var test = wptoolkit._getInputSourceState();27console.log(test);28var wptoolkit = require('wptoolkit');
Using AI Code Generation
1var wpt = require('wptoolkit');2var input = wpt._getInputSourceState();3console.log(input);4var wpt = require('wptoolkit');5var input = wpt._getInputSourceState();6console.log(input);7console.log(input);8console.log(input);9var wpt = require('wptoolkit');10var input = wpt._getInputSourceState();11console.log(input);12console.log(input);13console.log(input);14console.log(input);15var wpt = require('wptoolkit');16var input = wpt._getInputSourceState();17console.log(input);18console.log(input);19console.log(input);20console.log(input);21console.log(input);22var wpt = require('wptoolkit');23var input = wpt._getInputSourceState();24console.log(input);25console.log(input);26console.log(input);27console.log(input);28console.log(input);29console.log(input);30var wpt = require('wptoolkit');31var input = wpt._getInputSourceState();32console.log(input);33console.log(input);34console.log(input);35console.log(input);36console.log(input);37console.log(input);38console.log(input);39var wpt = require('wptoolkit');40var input = wpt._getInputSourceState();41console.log(input);42console.log(input);43console.log(input);44console.log(input);45console.log(input);46console.log(input);47console.log(input);48console.log(input);49var wpt = require('
Using AI Code Generation
1var wptools = require('wptools');2wptools._getInputSourceState('test', function(err, data) {3 console.log(data);4});5var wptools = require('wptools');6wptools._getInputSources('test', function(err, data) {7 console.log(data);8});9var wptools = require('wptools');10wptools._getInputSources('test', function(err, data) {11 console.log(data);12});13var wptools = require('wptools');14wptools._getInputSources('test', function(err, data) {15 console.log(data);16});17var wptools = require('wptools');18wptools._getInputSources('test', function(err, data) {19 console.log(data);20});21var wptools = require('wptools');22wptools._getInputSources('test', function(err, data) {23 console.log(data);24});25var wptools = require('wptools');26wptools._getInputSources('test', function(err, data) {27 console.log(data);28});29var wptools = require('wptools');30wptools._getInputSources('test', function(err, data) {31 console.log(data);32});33var wptools = require('wptools');34wptools._getInputSources('test', function(err, data) {35 console.log(data);36});37var wptools = require('wptools');38wptools._getInputSources('
Using AI Code Generation
1var wpt = require('wpt.js');2var inputSourceState = wpt._getInputSourceState();3console.log(inputSourceState);4var wpt = require('wpt.js');5var inputSourceState = wpt._setInputSourceState({6 "touch": {7 },8 "mouse": {9 }10});11console.log(inputSourceState);12var wpt = require('wpt.js');13var eventListeners = wpt._getEventListeners();14console.log(eventListeners);
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!!