<!DOCTYPE html>
<html>
<head>
    <title>Geometry Interfaces: DOMRect and DOMRectReadOnly interface tests</title>
    <link rel="author" title="Hwanseung Lee" href="mailto:hs1217.lee@samsung.com" />
    <link rel="help" href="https://drafts.fxtf.org/geometry-1/#DOMRect">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <div id="log"></div>
    <script>
        testConstructor("DOMRect");
        testConstructor("DOMRectReadOnly");
        test(function() {
            var r = new DOMRect();
            r.top = 5;
            assert_equals(r.top, 0, "top");
            r.right = 5;
            assert_equals(r.right, 0, "right");
            r.bottom = 5;
            assert_equals(r.bottom, 0, "bottom");
            r.left = 5;
            assert_equals(r.left, 0, "left");
        }, 'DOMRect: set top/right/bottom/left');

        test(function() {
            var r = new DOMRect();
            r.x = 5;
            assert_equals(r.x, 5, "Expected value for x is 5");
            assert_equals(r.left, 5, "Expected value for left is 5");
            assert_equals(r.right, 5, "Expected value for right is 5");
            r.y = 5;
            assert_equals(r.y, 5, "Expected value for y is 5");
            assert_equals(r.top, 5, "Expected value for top is 5");
            assert_equals(r.bottom, 5, "Expected value for bottom is 5");
            r.width = 5;
            assert_equals(r.width, 5, "Expected value for width is 5");
            assert_equals(r.left, 5, "Expected value for left is 5");
            assert_equals(r.right, 10, "Expected value for right is 10");
            r.height = 5;
            assert_equals(r.height, 5, "Expected value for height is 5");
            assert_equals(r.top, 5, "Expected value for top is 5");
            assert_equals(r.bottom, 10, "Expected value for bottom is 10");
        }, 'DOMRect: set x/y/width/height');

        test(function() {
            var r = new DOMRectReadOnly();
            r.top = 5;
            assert_equals(r.top, 0, "top after setting top");
            assert_equals(r.y, 0, "y after setting top");
            assert_equals(r.bottom, 0, "bottom after setting top");
            r.right = 5;
            assert_equals(r.right, 0, "right after setting right");
            assert_equals(r.x, 0, "x after setting right");
            assert_equals(r.left, 0, "left after setting right");
            r.bottom = 5;
            assert_equals(r.bottom, 0, "bottom after setting bottom");
            assert_equals(r.y, 0, "y after setting bottom");
            assert_equals(r.top, 0, "top after setting bottom");
            r.left = 5;
            assert_equals(r.left, 0, "left after setting left");
            assert_equals(r.x, 0, "x after setting left");
            assert_equals(r.right, 0, "right after setting left");
        }, 'DOMRectReadOnly: set top/right/bottom/left');

        test(function() {
            var r = new DOMRectReadOnly();
            r.x = 5;
            assert_equals(r.x, 0, "x after setting x");
            assert_equals(r.left, 0, "left after setting x");
            assert_equals(r.right, 0, "right after setting x");
            r.y = 5;
            assert_equals(r.y, 0, "y after setting y");
            assert_equals(r.top, 0, "top after setting y");
            assert_equals(r.bottom, 0, "bottom after setting y");
            r.width = 5;
            assert_equals(r.width, 0, "width after setting width");
            assert_equals(r.x, 0, "x after setting width");
            assert_equals(r.right, 0, "right after setting width");
            r.height = 5;
            assert_equals(r.height, 0, "height after setting height");
            assert_equals(r.y, 0, "y after setting height");
            assert_equals(r.bottom, 0, "bottom after setting height");
        }, 'DOMRectReadOnly: set x/y/width/height');

        test(function() {
            var actual = DOMRect.fromRect({x: 1, y: 2, width: 3, height: 4});
            var expected = new DOMRect(1, 2, 3, 4);
            checkDOMRect(actual, expected);
            assert_true(actual instanceof DOMRectReadOnly, "actual instanceof DOMRectReadOnly");
            assert_true(actual instanceof DOMRect, "actual instanceof DOMRect");
        }, 'DOMRect.fromRect');

        test(function() {
            var actual = DOMRectReadOnly.fromRect({x: 1, y: 2, width: 3, height: 4});
            var expected = new DOMRectReadOnly(1, 2, 3, 4);
            checkDOMRect(actual, expected);
            assert_true(actual instanceof DOMRectReadOnly, "actual instanceof DOMRectReadOnly");
            assert_false(actual instanceof DOMRect, "actual instanceof DOMRect");
        }, 'DOMRectReadOnly.fromRect');

        function testConstructor(constructorString) {
            var constructor = self[constructorString];
            test(function() {
                checkDOMRect(new constructor(),
                    { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 });
            }, constructorString + ' constructor without parameter');
            test(function() {
                checkDOMRect(new constructor(1),
                    { x: 1, y: 0, width: 0, height: 0, top: 0, right: 1, bottom: 0, left: 1 });
            }, constructorString + ' constructor with one parameter');
            test(function() {
                checkDOMRect(new constructor(1, 2),
                    { x: 1, y: 2, width: 0, height: 0, top: 2, right: 1, bottom: 2, left: 1 });
            }, constructorString + ' constructor with two parameters');
            test(function() {
                checkDOMRect(new constructor(1, 2, 3),
                    { x: 1, y: 2, width: 3, height: 0, top: 2, right: 4, bottom: 2, left: 1 });
            }, constructorString + ' constructor with three parameters');
            test(function() {
                checkDOMRect(new constructor(1, 2, 3, 4),
                    { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 });
            }, constructorString + ' constructor with four parameters');
            test(function() {
                checkDOMRect(new constructor(1, 2, 3, 4, 5),
                    { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 });
            }, constructorString + ' constructor with five parameters');
            test(function() {
                checkDOMRect(new constructor(2, 2, -4, 4),
                    { x: 2, y: 2, width: -4, height: 4, top: 2, right: 2, bottom: 6, left: -2 });
            }, constructorString + ' constructor with negative width');
            test(function() {
                checkDOMRect(new constructor(2, 2, 4, -4),
                    { x: 2, y: 2, width: 4, height: -4, top: -2, right: 6, bottom: 2, left: 2 });
            }, constructorString + ' constructor with negative height');
            test(function() {
                checkDOMRect(new constructor(2, 2, -4, -4),
                    { x: 2, y: 2, width: -4, height: -4, top: -2, right: 2, bottom: 2, left: -2 });
            }, constructorString + ' constructor with negative width and height');
            test(function() {
                checkDOMRect(new constructor(0, 0, undefined, 4),
                    { x: 0, y: 0, width: 0, height: 4, top: 0, right: 0, bottom: 4, left: 0 });
            }, constructorString + ' constructor with undefined');
            test(function() {
                checkDOMRect(new constructor(NaN, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, null),
                    { x: NaN, y: -Infinity, width: Infinity, height: 0, top: -Infinity, right: NaN, bottom: -Infinity, left: NaN });
            }, constructorString + ' constructor with NaN and infinity and null');
            test(function() {
                checkDOMRect(new constructor("1", "2", "3", "4"),
                    { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 });
            }, constructorString + ' constructor with number string');
            test(function() {
                checkDOMRect(new constructor("a", "b", "c", "d"),
                    { x: NaN, y: NaN, width: NaN, height: NaN, top: NaN, right: NaN, bottom: NaN, left: NaN });
            }, constructorString + ' constructor with character string');
        }

        function checkDOMRect(r, exp) {
            assert_equals(r.x, exp.x, "x");
            assert_equals(r.y, exp.y, "y");
            assert_equals(r.width, exp.width, "width");
            assert_equals(r.height, exp.height, "height");
            assert_equals(r.top, exp.top, "top");
            assert_equals(r.left, exp.left, "left");
            assert_equals(r.bottom, exp.bottom, "bottom");
            assert_equals(r.right, exp.right, "right");
        }
    </script>
</body>
</html>
