DOMRect-002.html (8869B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Geometry Interfaces: DOMRect and DOMRectReadOnly interface tests</title> 5 <link rel="author" title="Hwanseung Lee" href="mailto:hs1217.lee@samsung.com" /> 6 <link rel="help" href="https://drafts.fxtf.org/geometry-1/#DOMRect"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 </head> 10 <body> 11 <div id="log"></div> 12 <script> 13 testConstructor("DOMRect"); 14 testConstructor("DOMRectReadOnly"); 15 test(function() { 16 var r = new DOMRect(); 17 r.top = 5; 18 assert_equals(r.top, 0, "top"); 19 r.right = 5; 20 assert_equals(r.right, 0, "right"); 21 r.bottom = 5; 22 assert_equals(r.bottom, 0, "bottom"); 23 r.left = 5; 24 assert_equals(r.left, 0, "left"); 25 }, 'DOMRect: set top/right/bottom/left'); 26 27 test(function() { 28 var r = new DOMRect(); 29 r.x = 5; 30 assert_equals(r.x, 5, "Expected value for x is 5"); 31 assert_equals(r.left, 5, "Expected value for left is 5"); 32 assert_equals(r.right, 5, "Expected value for right is 5"); 33 r.y = 5; 34 assert_equals(r.y, 5, "Expected value for y is 5"); 35 assert_equals(r.top, 5, "Expected value for top is 5"); 36 assert_equals(r.bottom, 5, "Expected value for bottom is 5"); 37 r.width = 5; 38 assert_equals(r.width, 5, "Expected value for width is 5"); 39 assert_equals(r.left, 5, "Expected value for left is 5"); 40 assert_equals(r.right, 10, "Expected value for right is 10"); 41 r.height = 5; 42 assert_equals(r.height, 5, "Expected value for height is 5"); 43 assert_equals(r.top, 5, "Expected value for top is 5"); 44 assert_equals(r.bottom, 10, "Expected value for bottom is 10"); 45 }, 'DOMRect: set x/y/width/height'); 46 47 test(function() { 48 var r = new DOMRectReadOnly(); 49 r.top = 5; 50 assert_equals(r.top, 0, "top after setting top"); 51 assert_equals(r.y, 0, "y after setting top"); 52 assert_equals(r.bottom, 0, "bottom after setting top"); 53 r.right = 5; 54 assert_equals(r.right, 0, "right after setting right"); 55 assert_equals(r.x, 0, "x after setting right"); 56 assert_equals(r.left, 0, "left after setting right"); 57 r.bottom = 5; 58 assert_equals(r.bottom, 0, "bottom after setting bottom"); 59 assert_equals(r.y, 0, "y after setting bottom"); 60 assert_equals(r.top, 0, "top after setting bottom"); 61 r.left = 5; 62 assert_equals(r.left, 0, "left after setting left"); 63 assert_equals(r.x, 0, "x after setting left"); 64 assert_equals(r.right, 0, "right after setting left"); 65 }, 'DOMRectReadOnly: set top/right/bottom/left'); 66 67 test(function() { 68 var r = new DOMRectReadOnly(); 69 r.x = 5; 70 assert_equals(r.x, 0, "x after setting x"); 71 assert_equals(r.left, 0, "left after setting x"); 72 assert_equals(r.right, 0, "right after setting x"); 73 r.y = 5; 74 assert_equals(r.y, 0, "y after setting y"); 75 assert_equals(r.top, 0, "top after setting y"); 76 assert_equals(r.bottom, 0, "bottom after setting y"); 77 r.width = 5; 78 assert_equals(r.width, 0, "width after setting width"); 79 assert_equals(r.x, 0, "x after setting width"); 80 assert_equals(r.right, 0, "right after setting width"); 81 r.height = 5; 82 assert_equals(r.height, 0, "height after setting height"); 83 assert_equals(r.y, 0, "y after setting height"); 84 assert_equals(r.bottom, 0, "bottom after setting height"); 85 }, 'DOMRectReadOnly: set x/y/width/height'); 86 87 test(function() { 88 var actual = DOMRect.fromRect({x: 1, y: 2, width: 3, height: 4}); 89 var expected = new DOMRect(1, 2, 3, 4); 90 checkDOMRect(actual, expected); 91 assert_true(actual instanceof DOMRectReadOnly, "actual instanceof DOMRectReadOnly"); 92 assert_true(actual instanceof DOMRect, "actual instanceof DOMRect"); 93 }, 'DOMRect.fromRect'); 94 95 test(function() { 96 var actual = DOMRectReadOnly.fromRect({x: 1, y: 2, width: 3, height: 4}); 97 var expected = new DOMRectReadOnly(1, 2, 3, 4); 98 checkDOMRect(actual, expected); 99 assert_true(actual instanceof DOMRectReadOnly, "actual instanceof DOMRectReadOnly"); 100 assert_false(actual instanceof DOMRect, "actual instanceof DOMRect"); 101 }, 'DOMRectReadOnly.fromRect'); 102 103 function testConstructor(constructorString) { 104 var constructor = self[constructorString]; 105 test(function() { 106 checkDOMRect(new constructor(), 107 { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 }); 108 }, constructorString + ' constructor without parameter'); 109 test(function() { 110 checkDOMRect(new constructor(1), 111 { x: 1, y: 0, width: 0, height: 0, top: 0, right: 1, bottom: 0, left: 1 }); 112 }, constructorString + ' constructor with one parameter'); 113 test(function() { 114 checkDOMRect(new constructor(1, 2), 115 { x: 1, y: 2, width: 0, height: 0, top: 2, right: 1, bottom: 2, left: 1 }); 116 }, constructorString + ' constructor with two parameters'); 117 test(function() { 118 checkDOMRect(new constructor(1, 2, 3), 119 { x: 1, y: 2, width: 3, height: 0, top: 2, right: 4, bottom: 2, left: 1 }); 120 }, constructorString + ' constructor with three parameters'); 121 test(function() { 122 checkDOMRect(new constructor(1, 2, 3, 4), 123 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 124 }, constructorString + ' constructor with four parameters'); 125 test(function() { 126 checkDOMRect(new constructor(1, 2, 3, 4, 5), 127 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 128 }, constructorString + ' constructor with five parameters'); 129 test(function() { 130 checkDOMRect(new constructor(2, 2, -4, 4), 131 { x: 2, y: 2, width: -4, height: 4, top: 2, right: 2, bottom: 6, left: -2 }); 132 }, constructorString + ' constructor with negative width'); 133 test(function() { 134 checkDOMRect(new constructor(2, 2, 4, -4), 135 { x: 2, y: 2, width: 4, height: -4, top: -2, right: 6, bottom: 2, left: 2 }); 136 }, constructorString + ' constructor with negative height'); 137 test(function() { 138 checkDOMRect(new constructor(2, 2, -4, -4), 139 { x: 2, y: 2, width: -4, height: -4, top: -2, right: 2, bottom: 2, left: -2 }); 140 }, constructorString + ' constructor with negative width and height'); 141 test(function() { 142 checkDOMRect(new constructor(0, 0, undefined, 4), 143 { x: 0, y: 0, width: 0, height: 4, top: 0, right: 0, bottom: 4, left: 0 }); 144 }, constructorString + ' constructor with undefined'); 145 test(function() { 146 checkDOMRect(new constructor(NaN, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, null), 147 { x: NaN, y: -Infinity, width: Infinity, height: 0, top: -Infinity, right: NaN, bottom: -Infinity, left: NaN }); 148 }, constructorString + ' constructor with NaN and infinity and null'); 149 test(function() { 150 checkDOMRect(new constructor("1", "2", "3", "4"), 151 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 152 }, constructorString + ' constructor with number string'); 153 test(function() { 154 checkDOMRect(new constructor("a", "b", "c", "d"), 155 { x: NaN, y: NaN, width: NaN, height: NaN, top: NaN, right: NaN, bottom: NaN, left: NaN }); 156 }, constructorString + ' constructor with character string'); 157 } 158 159 function checkDOMRect(r, exp) { 160 assert_equals(r.x, exp.x, "x"); 161 assert_equals(r.y, exp.y, "y"); 162 assert_equals(r.width, exp.width, "width"); 163 assert_equals(r.height, exp.height, "height"); 164 assert_equals(r.top, exp.top, "top"); 165 assert_equals(r.left, exp.left, "left"); 166 assert_equals(r.bottom, exp.bottom, "bottom"); 167 assert_equals(r.right, exp.right, "right"); 168 } 169 </script> 170 </body> 171 </html>