<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<script>
var EPSILON = 1e-6; // float epsilon
var values = [
  {input: new CSSScale(0, 0), x: 0, y: 0, z: 1, is2D: true,
    cssText: "scale(0, 0)"},
  {input: new CSSScale(1, 2), x: 1, y: 2, z: 1, is2D: true,
    cssText: "scale(1, 2)"},
  {input: new CSSScale(-2, -4), x: -2, y: -4, z: 1, is2D: true,
    cssText: "scale(-2, -4)"},
  {input: new CSSScale(3.4, 2.7), x: 3.4, y: 2.7, z: 1, is2D: true,
    cssText: "scale(3.4, 2.7)"},
  {input: new CSSScale(0, 0, 0), x: 0, y: 0, z: 0, is2D: false,
    cssText: "scale3d(0, 0, 0)"},
  {input: new CSSScale(1, 2, 3), x: 1, y: 2, z: 3, is2D: false,
    cssText: "scale3d(1, 2, 3)"},
  {input: new CSSScale(3.5, -2.7, -2), x: 3.5, y: -2.7, z: -2, is2D: false,
    cssText: "scale3d(3.5, -2.7, -2)"}
];

test(function() {
  for (var i = 0; i < values.length; ++i) {
    assert_equals(values[i].input.x, values[i].x);
    assert_equals(values[i].input.y, values[i].y);
    assert_equals(values[i].input.z, values[i].z);
  }
}, "Test that the (x, y, z) values for CSSScale are correct.");

test(function() {
  for (var i = 0; i < values.length; ++i) {
    assert_equals(values[i].input.is2D(), values[i].is2D);
  }
}, "Test that the is2D values for CSSScale is correct.");

test(function() {
  for (var i = 0; i < values.length; ++i) {
    assert_equals(values[i].input.cssText, values[i].cssText);
  }
}, "Test that the cssText for CSSScale is correct.");

test(function() {
  assert_throws(null, () => { new CSSScale(); });
  assert_throws(null, () => { new CSSScale(1); });
}, "Test that invalid number of arguments for CSSScale throws an exception.");

test(function() {
  assert_throws(null, () => { new CSSScale(NaN, 0); });
  assert_throws(null, () => { new CSSScale(0, NaN); });
  assert_throws(null, () => { new CSSScale(NaN, NaN); });
  assert_throws(null, () => { new CSSScale(Infinity, 0); });
  assert_throws(null, () => { new CSSScale(-Infinity, 0); });
  assert_throws(null, () => { new CSSScale("hello", 0); });
  assert_throws(null, () => { new CSSScale(0, "world"); });
  assert_throws(null, () => { new CSSScale(undefined, 0); });
  assert_throws(null, () => { new CSSScale({}, {}); });

  assert_throws(null, () => { new CSSScale("hello", 0, 0); });
  assert_throws(null, () => { new CSSScale(0, NaN, 0); });
  assert_throws(null, () => { new CSSScale(0, Infinity, 0); });
  assert_throws(null, () => { new CSSScale(0, 0, NaN); });
  assert_throws(null, () => { new CSSScale(0, 0, Infinity); });
  assert_throws(null, () => { new CSSScale(0, 0, -Infinity); });
  assert_throws(null, () => { new CSSScale(0, 0, undefined); });
  assert_throws(null, () => { new CSSScale(undefined, undefined, 0); });
  assert_throws(null, () => { new CSSScale(NaN, undefined, 0); });
  assert_throws(null, () => { new CSSScale(NaN, 0, NaN); });
  assert_throws(null, () => { new CSSScale(0, "hello", "world"); });
  assert_throws(null, () => { new CSSScale(0, {}, {}); });
  assert_throws(null, () => { new CSSScale({}, {}, {}); });
  assert_throws(null, () => { new CSSScale(NaN, NaN, NaN); });
}, "Test that invalid input throws an exception.");

test(function() {
  for (var i = 0; i < values.length; ++i) {
    var input = values[i].input;
    var inputAsMatrix = input.asMatrix();
    assert_equals(inputAsMatrix.is2D(), input.is2D());

    var expectedMatrix = input.is2D() ? new CSSMatrixTransformComponent(input.x, 0, 0, input.y, 0, 0) :
        new CSSMatrixTransformComponent(input.x, 0, 0, 0, 0, input.y, 0, 0, 0, 0, input.z, 0, 0, 0, 0, 1);
    for (var attribute in expectedMatrix) {
      if (typeof expectedMatrix[attribute] === "number") {
        assert_approx_equals(inputAsMatrix[attribute], expectedMatrix[attribute], EPSILON);
      } else {
        assert_equals(inputAsMatrix[attribute], expectedMatrix[attribute]);
      }
    }
  }
}, "Test that asMatrix is constructed correctly for CSSScale.");

</script>
