DOMRect-001.html (8811B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Geometry Interfaces: DOMRect and DOMRectReadOnly interface tests</title> 5 <link rel="author" title="Dirk Schulze" href="mailto:dschulze@adobe.com" /> 6 <link rel="help" href="https://www.w3.org/TR/geometry-1/#DOMRect"> 7 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-x"> 8 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-y"> 9 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-width"> 10 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-height"> 11 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-top"> 12 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-left"> 13 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-right"> 14 <link rel="help" href="http://www.w3.org/TR/geometry-1/#dom-domrectreadonly-domrect-bottom"> 15 <script src="/resources/testharness.js"></script> 16 <script src="/resources/testharnessreport.js"></script> 17 </head> 18 <body> 19 <p>Test DOMRect and DOMRectReadOnly interfaces</p> 20 <div id="log"></div> 21 <script> 22 testConstructor(DOMRect); 23 testConstructor(DOMRectReadOnly); 24 test(function() { 25 var r = new DOMRect(); 26 r.top = 5; 27 assert_equals(r.top, 0, "Expected value for top is 0"); 28 r.right = 5; 29 assert_equals(r.right, 0, "Expected value for right is 0"); 30 r.bottom = 5; 31 assert_equals(r.bottom, 0, "Expected value for bottom is 0"); 32 r.left = 5; 33 assert_equals(r.left, 0, "Expected value for left is 0"); 34 },'testSetReadOnlyAttributes'); 35 test(function() { 36 var r = new DOMRect(); 37 r.x = 5; 38 assert_equals(r.x, 5, "Expected value for x is 5"); 39 assert_equals(r.left, 5, "Expected value for left is 5"); 40 assert_equals(r.right, 5, "Expected value for right is 5"); 41 r.y = 5; 42 assert_equals(r.y, 5, "Expected value for y is 5"); 43 assert_equals(r.top, 5, "Expected value for top is 5"); 44 assert_equals(r.bottom, 5, "Expected value for bottom is 5"); 45 r.width = 5; 46 assert_equals(r.width, 5, "Expected value for width is 5"); 47 assert_equals(r.left, 5, "Expected value for left is 5"); 48 assert_equals(r.right, 10, "Expected value for right is 10"); 49 r.height = 5; 50 assert_equals(r.height, 5, "Expected value for height is 5"); 51 assert_equals(r.top, 5, "Expected value for top is 5"); 52 assert_equals(r.bottom, 10, "Expected value for bottom is 10"); 53 },'testSetAttributes'); 54 test(function() { 55 var r = new DOMRectReadOnly(); 56 r.top = 5; 57 assert_equals(r.top, 0, "Expected value for top is 0"); 58 assert_equals(r.y, 0, "Expected value for y is 0"); 59 assert_equals(r.bottom, 0, "Expected value for bottom is 0"); 60 r.right = 5; 61 assert_equals(r.right, 0, "Expected value for right is 0"); 62 assert_equals(r.x, 0, "Expected value for x is 0"); 63 assert_equals(r.left, 0, "Expected value for left is 0"); 64 r.bottom = 5; 65 assert_equals(r.bottom, 0, "Expected value for bottom is 0"); 66 assert_equals(r.y, 0, "Expected value for y is 0"); 67 assert_equals(r.top, 0, "Expected value for top is 0"); 68 r.left = 5; 69 assert_equals(r.left, 0, "Expected value for left is 0"); 70 assert_equals(r.x, 0, "Expected value for x is 0"); 71 assert_equals(r.right, 0, "Expected value for right is 0"); 72 },'testReadOnlySetReadOnlyAttributes'); 73 test(function() { 74 var r = new DOMRectReadOnly(); 75 r.x = 0; 76 assert_equals(r.x, 0, "Expected value for x is 0"); 77 assert_equals(r.left, 0, "Expected value for left is 0"); 78 assert_equals(r.right, 0, "Expected value for right is 0"); 79 r.y = 0; 80 assert_equals(r.y, 0, "Expected value for y is 0"); 81 assert_equals(r.top, 0, "Expected value for top is 0"); 82 assert_equals(r.bottom, 0, "Expected value for bottom is 0"); 83 r.width = 0; 84 assert_equals(r.width, 0, "Expected value for width is 0"); 85 assert_equals(r.x, 0, "Expected value for x is 0"); 86 assert_equals(r.right, 0, "Expected value for right is 0"); 87 r.height = 0; 88 assert_equals(r.height, 0, "Expected value for height is 0"); 89 assert_equals(r.y, 0, "Expected value for y is 0"); 90 assert_equals(r.bottom, 0, "Expected value for bottom is 0"); 91 },'testReadOnlySetAttributes'); 92 93 function testConstructor(constructor) { 94 var prefix = constructor === DOMRect ? 'testConstructor' : 'testReadOnlyConstructor'; 95 test(function() { 96 checkDOMRect(new constructor(), 97 { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 }); 98 }, prefix + '0'); 99 test(function() { 100 checkDOMRect(new constructor(1), 101 { x: 1, y: 0, width: 0, height: 0, top: 0, right: 1, bottom: 0, left: 1 }); 102 }, prefix + '1'); 103 test(function() { 104 checkDOMRect(new constructor(1, 2), 105 { x: 1, y: 2, width: 0, height: 0, top: 2, right: 1, bottom: 2, left: 1 }); 106 }, prefix + '2'); 107 test(function() { 108 checkDOMRect(new constructor(1, 2, 3), 109 { x: 1, y: 2, width: 3, height: 0, top: 2, right: 4, bottom: 2, left: 1 }); 110 }, prefix + '3'); 111 test(function() { 112 checkDOMRect(new constructor(1, 2, 3, 4), 113 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 114 }, prefix + '4'); 115 test(function() { 116 checkDOMRect(new constructor(1, 2, 3, 4, 5), 117 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 118 }, prefix + '5'); 119 test(function() { 120 checkDOMRect(new constructor(2, 2, -4, 4), 121 { x: 2, y: 2, width: -4, height: 4, top: 2, right: 2, bottom: 6, left: -2 }); 122 }, prefix + 'NegativeWidth'); 123 test(function() { 124 checkDOMRect(new constructor(2, 2, 4, -4), 125 { x: 2, y: 2, width: 4, height: -4, top: -2, right: 6, bottom: 2, left: 2 }); 126 }, prefix + 'NegativeHeight'); 127 test(function() { 128 checkDOMRect(new constructor(2, 2, -4, -4), 129 { x: 2, y: 2, width: -4, height: -4, top: -2, right: 2, bottom: 2, left: -2 }); 130 }, prefix + 'NegativeWidthHeight'); 131 test(function() { 132 checkDOMRect(new constructor(0, 0, undefined, 4), 133 { x: 0, y: 0, width: 0, height: 4, top: 0, right: 0, bottom: 4, left: 0 }); 134 }, prefix + 'Undefined1'); 135 test(function() { 136 checkDOMRect(new constructor(NaN, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, null), 137 { x: NaN, y: -Infinity, width: Infinity, height: 0, top: -Infinity, right: NaN, bottom: -Infinity, left: NaN }); 138 }, prefix + 'Undefined2'); 139 test(function() { 140 checkDOMRect(new constructor("1", "2", "3", "4"), 141 { x: 1, y: 2, width: 3, height: 4, top: 2, right: 4, bottom: 6, left: 1 }); 142 }, prefix + 'String1'); 143 test(function() { 144 checkDOMRect(new constructor("a", "b", "c", "d"), 145 { x: NaN, y: NaN, width: NaN, height: NaN, top: NaN, right: NaN, bottom: NaN, left: NaN }); 146 }, prefix + 'String2'); 147 } 148 149 function checkDOMRect(r, exp) { 150 assert_equals(r.x, exp.x, "Expected value for x is " + exp.x); 151 assert_equals(r.y, exp.y, "Expected value for y is " + exp.y); 152 assert_equals(r.width, exp.width, "Expected value for width is " + exp.width); 153 assert_equals(r.height, exp.height, "Expected value for height is " + exp.height); 154 assert_equals(r.top, exp.top, "Expected value for top is " + exp.top); 155 assert_equals(r.left, exp.left, "Expected value for left is " + exp.left); 156 assert_equals(r.bottom, exp.bottom, "Expected value for bottom is " + exp.bottom); 157 assert_equals(r.right, exp.right, "Expected value for right is " + exp.right); 158 } 159 </script> 160 </body> 161 </html>