chromium/third_party/blink/web_tests/fast/dom/geometry-interfaces-dom-matrix.html

<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../resources/sab-polyfill.js"></script>
<script src="./resources/geometry-interfaces-test-helpers.js"></script>
<script>
test(() => {
  var matrix = new DOMMatrix();
  assert_identity_2d_matrix(matrix);
}, "DOMMatrix() constructor");

test(() => {
  var float32Array = new Float32Array([1, 2, 3, 4, 5, 6]);
  var matrix2d = DOMMatrix.fromFloat32Array(float32Array);
  assert_2d_matrix_equals(matrix2d, [1, 2, 3, 4, 5, 6]);
}, "DOMMatrix fromFloat32Array - 2D matrix");

test(() => {
  // 3.1 is not representable as a 32-bit float
  var float64Array = new Float64Array([1, 2, 3, 3.1, 4, 5]);
  var matrix2d = DOMMatrix.fromFloat64Array(float64Array);
  assert_2d_matrix_equals(matrix2d, [1, 2, 3, 3.1, 4, 5]);
}, "DOMMatrix fromFloat64Array - 2D matrix");

test(() => {
  var float32Array = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  var matrix3d = DOMMatrix.fromFloat32Array(float32Array);
  assert_3d_matrix_equals(matrix3d, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
}, "DOMMatrix fromFloat32Array - 3D matrix");

test(() => {
  // 10.1 and 16.6 are not representable as a 32-bit float
  var float64Array = new Float64Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]);
  var matrix3d = DOMMatrix.fromFloat64Array(float64Array);
  assert_3d_matrix_equals(matrix3d, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]);
}, "DOMMatrix fromFloat64Array - 3D matrix");

test(() => {
  var matrix = new DOMMatrix("");
  assert_identity_2d_matrix(matrix);
}, "DOMMatrix(transformList) - emptyString");

test(() => {
  var matrix = new DOMMatrix("matrix(1.0, 2.0, 3.0, 4.0, 5.0, 6.0) translate(44px, 55px) skewX(30deg)");
  var expectedMatrix = new DOMMatrix();
  expectedMatrix.multiplySelf(new DOMMatrix([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]))
  expectedMatrix.translateSelf(44, 55)
  expectedMatrix.skewXSelf(30);
  assert_matrix_almost_equals(matrix, expectedMatrix);
}, "DOMMatrix(transformList) - transformList");

test(() => {
  var matrix = new DOMMatrix([1, 2, 3, 4, 5, 6]);
  assert_2d_matrix_equals(matrix, [1, 2, 3, 4, 5, 6]);
}, "DOMMatrix(numberSequence) constructor");

test(() => {
  var matrix = new DOMMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  assert_3d_matrix_equals(matrix, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
}, "DOMMatrix(numberSequence) constructor");

test(() => {
  var matrix = new DOMMatrix();
  matrix.a = 10;
  matrix.b = 20;
  matrix.m24 = 2;
  matrix.m33 = 3;
  matrix.m42 = 3;
  matrix.m44 = 9;
  assert_3d_matrix_equals(matrix, [10, 20, 0, 0, 0, 1, 0, 2, 0, 0, 3, 0, 0, 3, 0, 9]);
}, "DOMMatrix attributes");

test(() => {
  var matrix = new DOMMatrix();
  assert_true(matrix.is2D);
  assert_true(matrix.isIdentity);
  matrix.m31 = 1;
  matrix.m33 = 0;
  assert_false(matrix.is2D);
  assert_false(matrix.isIdentity);
  matrix.m31 = 0;
  matrix.m33 = 1;
  assert_false(matrix.is2D);
  assert_true(matrix.isIdentity);
}, "DOMMatrix.is2D can never be set to 'true' when it was set to 'false' before calling setMatrixValue()");

test(() => {
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([1, 2, 3, 4, 5])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([1, 2, 3, 4, 5])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([1, 2, 3, 4, 5, 6 ,7])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([1, 2, 3, 4, 5, 6 ,7])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
}, "DOMMatrix fromFloat*Array - invalid array size of nearby 6");

test(() => {
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12, 13, 14, 15])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12, 13, 14, 15])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
}, "DOMMatrix fromFloat*Array - invalid array size of nearby 16");

test(() => {
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array([1])); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array([1])); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array(65536)); },
    "fromFloat32Array function only accepts 1 Float32Array with 6 or 16 elements");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array(65536)); },
    "fromFloat64Array function only accepts 1 Float64Array with 6 or 16 elements");
}, "DOMMatrix fromFloat*Array - invalid array size");

test(() => {
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat32Array(new Float32Array(new SharedArrayBuffer(24))); },
    "");
  assert_throws_js(TypeError, () => { DOMMatrix.fromFloat64Array(new Float64Array(new SharedArrayBuffer(32))); },
    "");
}, "DOMMatrix fromFloat*Array - can't use SharedArrayBuffer view");

test(() => {
  assert_identity_2d_matrix(DOMMatrix.fromMatrix());
}, "DOMMatrix.fromMatrix() with no parameter");

test(() => {
  assert_identity_2d_matrix(DOMMatrix.fromMatrix(null));
}, "DOMMatrix.fromMatrix() with null");

test(() => {
  assert_identity_2d_matrix(DOMMatrix.fromMatrix(undefined));
}, "DOMMatrix.fromMatrix() with undefined");

test(() => {
  assert_identity_2d_matrix(DOMMatrix.fromMatrix({}));
}, "DOMMatrix.fromMatrix() with empty object");

test(() => {
  var matrix = DOMMatrix.fromMatrix({a: 1, b: 2, c: 3, d: 4, e: 5, f: 6});
  assert_2d_matrix_equals(matrix, [1, 2, 3, 4, 5, 6]);
}, "DOMMatrix.fromMatrix({a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}) should create a 2D DOMMatrix");

test(() => {
  var matrix = DOMMatrix.fromMatrix({m11: 1, m22: 2, m33: 3, m44: 4, m23: 5, m43: 6});
  assert_3d_matrix_equals(matrix, [1, 0, 0, 0, 0, 2, 5, 0, 0, 0, 3, 0, 0, 0, 6, 4]);
}, "DOMMatrix.fromMatrix({m11: 1, m22: 2, m33: 3, m44: 4, m23: 5, m43: 6}) should create a 3D DOMMatrix");

test(() => {
  var matrix = DOMMatrix.fromMatrix({a: 7, c: 9});
  assert_2d_matrix_equals(matrix, [7, 0, 9, 1, 0, 0]);
}, "If 2d related properties don't be set, should set to fallback");

test(() => {
  var matrix = DOMMatrix.fromMatrix({
    m11: NaN, m12: NaN, m13: NaN, m14: NaN,
    m21: NaN, m22: NaN, m23: NaN, m24: NaN,
    m31: NaN, m32: NaN, m33: NaN, m34: NaN,
    m41: NaN, m42: NaN, m43: NaN, m44: NaN,
    is2D: false
  });
  assert_3d_matrix_equals(matrix, [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]);
}, "DOMMatrix.fromMatrix(): NaN test");

test(() => {
  var identity_matrix = DOMMatrix.fromMatrix();
  assert_true(identity_matrix.is2D);
  assert_object_equals(identity_matrix.toJSON(),
    { a : 1, b : 0, c : 0, d : 1, e : 0, f : 0,
      is2D : true, isIdentity : true,
      m11 : 1, m12 : 0, m13 : 0, m14 : 0,
      m21 : 0, m22 : 1, m23 : 0, m24 : 0,
      m31 : 0, m32 : 0, m33 : 1, m34 : 0,
      m41 : 0, m42 : 0, m43 : 0, m44 : 1});
}, "DOMMatrix toJSON() - identity matrix");

test(() => {
  var matrix2d = new DOMMatrix([1, 2, 3, 3.1, 2, 1]);
  assert_true(matrix2d.is2D);
  assert_object_equals(matrix2d.toJSON(),
    { a : 1, b : 2, c : 3, d : 3.1, e : 2, f : 1,
      is2D : true, isIdentity : false,
      m11 : 1, m12 : 2, m13 : 0, m14 : 0,
      m21 : 3, m22 : 3.1, m23 : 0, m24 : 0,
      m31 : 0, m32 : 0, m33 : 1, m34 : 0,
      m41 : 2, m42 : 1, m43 : 0, m44 : 1});
}, "DOMMatrix toJSON() - 2D matrix");

test(() => {
  var matrix3d = new DOMMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]);
  assert_false(matrix3d.is2D);
  assert_object_equals(matrix3d.toJSON(),
    { a : 1, b : 2, c : 5, d : 6, e : 13, f : 14,
      is2D : false, isIdentity : false,
      m11 : 1, m12 : 2, m13 : 3, m14 : 4,
      m21 : 5, m22 : 6, m23 : 7, m24 : 8,
      m31 : 9, m32 : 10.1, m33 : 11, m34 : 12,
      m41 : 13, m42 : 14, m43 : 15, m44 : 16.6});
}, "DOMMatrix toJSON() - 3D matrix");

test(() => {
  assert_throws_dom("SyntaxError", () => { new DOMMatrix(1, 2, 3, 4, 5, 6); },
    "DOMMatrix(transformList) can't parse unknown keyword - DOMMatrixReadOnly(1, 2, 3, 4, 5, 6) is same with DOMMatrixReadOnly('1')");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("myString"); },
    "DOMMatrix(transformList) can't parse unknown keyword");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("initial");},
    "CSS-wide keywords are disallowed");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("notExistFunction()"); },
    "can't parse not exist function");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateY(50%)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(1.2em)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10ex)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10ch)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10rem)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10vw)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10vh)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10vmin)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(10vmax)"); },
    "using relative units should throw a SyntaxError");
  assert_throws_dom("SyntaxError", () => { new DOMMatrix("translateX(calc(10px + 1em))"); },
    "using relative units should throw a SyntaxError");
  assert_throws_js(TypeError, () => {
    var matrix = DOMMatrix.fromMatrix({a: 1, b: 2, m33: 3, m44: 4, is2D: true});
  }, "The 'is2D' property is set to true but the input matrix is 3d matrix");
  assert_throws_js(TypeError, () => {
    var matrix = DOMMatrix.fromMatrix({a: 1, b: 2, m11: 3});
  }, "The 'a' property should equal the 'm11' property");
}, "DOMMatrix.fromMatrix(): Exception test");

</script>