tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

xrRigidTransform_constructor.https.html (5687B)


      1 <!DOCTYPE html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="resources/webxr_util.js"></script>
      5 <script src="resources/webxr_test_constants.js"></script>
      6 <script>
      7 
      8 let testName = "XRRigidTransform constructor works";
      9 let fakeDeviceInitParams = TRACKED_IMMERSIVE_DEVICE;
     10 
     11 let testFunction =
     12  (session, fakeDeviceController, t) => new Promise((resolve, reject) => {
     13  let coordDict = function(coords) {
     14    let tempDict = {
     15        x : coords[0],
     16        y : coords[1],
     17        z : coords[2]
     18    }
     19 
     20    if (coords.length >= 4) {
     21        tempDict["w"] = coords[3];
     22    }
     23 
     24    return tempDict;
     25  };
     26 
     27  let createDOMPoint = function(coords) {
     28    return DOMPoint.fromPoint(coordDict(coords));
     29  };
     30 
     31  let createDOMPointReadOnly = function(coords) {
     32    return DOMPointReadOnly.fromPoint(coordDict(coords));
     33  };
     34 
     35  let quaternionLength = function(point) {
     36    return Math.sqrt(
     37        (point.x * point.x) +
     38        (point.y * point.y) +
     39        (point.z * point.z) +
     40        (point.w * point.w));
     41  };
     42 
     43  let checkDOMPoint = function(point, x, y, z, w, desc) {
     44    t.step(() => {
     45      assert_approx_equals(point.x, x, FLOAT_EPSILON, `${desc}: x value`);
     46      assert_approx_equals(point.y, y, FLOAT_EPSILON, `${desc}: y value`);
     47      assert_approx_equals(point.z, z, FLOAT_EPSILON, `${desc}: z value`);
     48      assert_approx_equals(point.w, w, FLOAT_EPSILON, `${desc}: w value`);
     49    });
     50  };
     51 
     52  let checkTransform = function(transformObj, desc) {
     53    t.step(() => {
     54      assert_not_equals(transformObj, null, `${desc}: exists`);
     55      assert_not_equals(transformObj.position, null, `${desc}: position exists`);
     56      assert_not_equals(transformObj.orientation, null, `${desc}: orientation exists`);
     57      assert_not_equals(transformObj.matrix, null, `${desc}: matrix exists`);
     58      assert_equals(transformObj.matrix.length, 16, `${desc}: matrix of correct length`);
     59    });
     60  };
     61 
     62  // test creating transform with specified position and orientation
     63  // make sure that orientation was normalized to have length = 1.0
     64  let transform = new XRRigidTransform(
     65      createDOMPoint([1.0, 2.0, 3.0]),
     66      createDOMPoint([1.1, 2.1, 3.1, 1.0]));
     67  checkTransform(transform, "Arbitrary transform");
     68  checkDOMPoint(transform.position, 1.0, 2.0, 3.0, 1.0, "Arbitrary transform position");
     69  assert_approx_equals(quaternionLength(transform.orientation), 1.0, FLOAT_EPSILON,
     70                       "Arbitrary transform is normalized");
     71 
     72  // test creating identity transform
     73  let identity = new XRRigidTransform();
     74  checkTransform(identity, "Identity transform");
     75  checkDOMPoint(identity.position, 0.0, 0.0, 0.0, 1.0, "Identity transform position");
     76  checkDOMPoint(identity.orientation, 0.0, 0.0, 0.0, 1.0, "Identity transform orientation");
     77 
     78  // create transform with only position specified
     79  transform = new XRRigidTransform(createDOMPoint([1.0, 2.0, 3.0]));
     80  checkTransform(transform, "Position-only");
     81 
     82  // create transform with only orientation specified
     83  transform = new XRRigidTransform(undefined, createDOMPoint([1.1, 2.1, 3.1, 1.0]));
     84  checkTransform(transform, "orientation-only");
     85 
     86  // create transform with DOMPointReadOnly
     87  transform = new XRRigidTransform(
     88      createDOMPointReadOnly([1.0, 2.0, 3.0]),
     89      createDOMPointReadOnly([1.1, 2.1, 3.1, 1.0]));
     90  checkTransform(transform, "Created with DOMPointReadOnly");
     91 
     92  // create transform with dictionary
     93  transform = new XRRigidTransform(
     94      coordDict([1.0, 2.0, 3.0]),
     95      coordDict([1.1, 2.1, 3.1, 1.0]));
     96  checkTransform(transform, "Created with dict");
     97 
     98  assert_throws_js(TypeError, () => new XRRigidTransform(
     99      coordDict([1.0, 2.0, 3.0, 0.5]),
    100      coordDict([1.1, 2.1, 3.1, 1.0])
    101  ), "Constructor should throw TypeError for non-1 position w values");
    102 
    103 assert_throws_js(TypeError, () => new XRRigidTransform(
    104    coordDict([NaN, 2.0, 3.0, 1.0]),
    105    coordDict([1.1, 2.1, 3.1, 1.0])
    106 ), "Constructor should throw TypeError if position values contain NaN");
    107 
    108 assert_throws_js(TypeError, () => new XRRigidTransform(
    109    coordDict([1.0, Infinity, 3.0, 1.0]),
    110    coordDict([1.1, 2.1, 3.1, 1.0])
    111 ), "Constructor should throw TypeError if position values contain Infinity");
    112 
    113 assert_throws_js(TypeError, () => new XRRigidTransform(
    114    coordDict([1.0, 2.0, -Infinity, 1.0]),
    115    coordDict([1.1, 2.1, 3.1, 1.0])
    116 ), "Constructor should throw TypeError if position values contain -Infinity");
    117 
    118 assert_throws_js(TypeError, () => new XRRigidTransform(
    119    coordDict([1.0, 2.0, 3.0, 1.0]),
    120    coordDict([NaN, 2.1, 3.1, 1.0])
    121 ), "Constructor should throw TypeError if orientation values contain NaN");
    122 
    123 assert_throws_js(TypeError, () => new XRRigidTransform(
    124    coordDict([1.0, 2.0, 3.0, 1.0]),
    125    coordDict([1.1, Infinity, 3.1, 1.0])
    126 ), "Constructor should throw TypeError if orientation values contain Infinity");
    127 
    128 assert_throws_js(TypeError, () => new XRRigidTransform(
    129    coordDict([1.0, 2.0, 3.0, 1.0]),
    130    coordDict([1.1, 2.1, -Infinity, 1.0])
    131 ), "Constructor should throw TypeError if orientation values contain -Infinity");
    132 
    133  assert_throws_dom("InvalidStateError", () => new XRRigidTransform(
    134      coordDict([1.0, 2.0, 3.0, 1.0]),
    135      coordDict([0, 0, 0, 0])
    136  ), "Constructor should throw InvalidStateError for non-normalizeable orientation values");
    137 
    138 assert_throws_dom("InvalidStateError", () => new XRRigidTransform(
    139    coordDict([1.0, 2.0, 3.0, 1.0]),
    140    coordDict([-1.7976931348623157e+308, 0, 0, 0])
    141 ), "Constructor should throw InvalidStateError for non-normalizeable orientation values");
    142  resolve();
    143 });
    144 
    145 xr_session_promise_test(testName, testFunction, fakeDeviceInitParams,
    146    'immersive-vr');
    147 
    148 </script>