| OLD | NEW |
| (Empty) |
| 1 function isDebugEnabled() | |
| 2 { | |
| 3 // Add #debug at the end of the url to visually debug the test case. | |
| 4 return window.location.hash == "#debug"; | |
| 5 } | |
| 6 | |
| 7 function getFlowByName(name) | |
| 8 { | |
| 9 var namedFlows = document.webkitGetNamedFlows(); | |
| 10 return namedFlows[name] ? namedFlows[name] : null; | |
| 11 } | |
| 12 | |
| 13 function rectToArray(rect) | |
| 14 { | |
| 15 return [rect.top, rect.left, rect.bottom, rect.right, rect.width, rect.heigh
t]; | |
| 16 } | |
| 17 | |
| 18 function areEqualNumbers(actual, expected, tolerance) | |
| 19 { | |
| 20 var diff = Math.abs(actual - expected); | |
| 21 return diff <= tolerance; | |
| 22 } | |
| 23 | |
| 24 function areEqualRects(r1, r2, tolerance) | |
| 25 { | |
| 26 if (r1.length != r2.length) | |
| 27 return false; | |
| 28 | |
| 29 for (var i = 0; i < r1.length; ++i) | |
| 30 if (!areEqualNumbers(r1[i], r2[i], tolerance)) | |
| 31 return false; | |
| 32 | |
| 33 return true; | |
| 34 } | |
| 35 | |
| 36 function assertTopLeftMatch (r1, r2, tolerance) | |
| 37 { | |
| 38 if (sameTopLeft(r1, r2, tolerance)) | |
| 39 return "PASS"; | |
| 40 return "FAIL. Expected top left points to match, but got ("+ r1.top + "," +
r1.left + ") and ("+ r2.top + "," + r2.left + ")"; | |
| 41 } | |
| 42 | |
| 43 function sameTopLeft(r1, r2, tolerance) | |
| 44 { | |
| 45 if (tolerance === undefined) | |
| 46 tolerance = 0; | |
| 47 if ( areEqualNumbers(r1.top, r2.top, tolerance) && areEqualNumbers(r1.left,
r2.left, tolerance) ) | |
| 48 return true; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 function assertEqualRects(results, name, actualRect, expectedRect, tolerance) | |
| 53 { | |
| 54 if (areEqualRects(actualRect, expectedRect, tolerance)) | |
| 55 return; | |
| 56 | |
| 57 results.push("FAIL(" + name + ") bounding rect was: [" + actualRect.join(",
") + "], expected: [" + expectedRect.join(", ") + "]"); | |
| 58 } | |
| 59 | |
| 60 function testBoundingRects(expectedBoundingRects, tolerance) | |
| 61 { | |
| 62 if (tolerance === undefined) | |
| 63 tolerance = 0; | |
| 64 | |
| 65 var results = []; | |
| 66 | |
| 67 for (var name in expectedBoundingRects) { | |
| 68 if (!expectedBoundingRects.hasOwnProperty(name)) | |
| 69 continue; | |
| 70 var rect = document.getElementById(name).getBoundingClientRect(); | |
| 71 assertEqualRects(results, name, rectToArray(rect), expectedBoundingRects
[name], tolerance); | |
| 72 } | |
| 73 | |
| 74 document.write("<p>" + (results.length ? results.join("<br />") : "PASS") +
"</p>"); | |
| 75 | |
| 76 return !results.length; | |
| 77 } | |
| 78 | |
| 79 function assertRectContains(results, name, containerRect, insideRect, tolerance) | |
| 80 { | |
| 81 // make the container rect bigger with tolerance | |
| 82 var left = containerRect.left - tolerance; | |
| 83 var right = containerRect.right + tolerance; | |
| 84 var top = containerRect.top - tolerance; | |
| 85 var bottom = containerRect.bottom + tolerance; | |
| 86 var pass = left <= insideRect.left && insideRect.right <= right | |
| 87 && top <= insideRect.top && insideRect.bottom <= bottom; | |
| 88 if (!pass) | |
| 89 results.push("FAIL(" + name + ") outside bounding rect was: [" + rectToA
rray(containerRect).join(", ") + "], and inside was: [" + rectToArray(insideRect
).join(", ") + "]"); | |
| 90 return pass; | |
| 91 } | |
| 92 | |
| 93 function addPageLevelDebugBox(rect, color) | |
| 94 { | |
| 95 var el = document.createElement("div"); | |
| 96 el.style.position = "absolute"; | |
| 97 el.style.left = rect.left + "px"; | |
| 98 el.style.top = rect.top + "px"; | |
| 99 el.style.width = rect.width + "px"; | |
| 100 el.style.height = rect.height + "px"; | |
| 101 el.style.backgroundColor = color; | |
| 102 document.body.appendChild(el); | |
| 103 } | |
| 104 | |
| 105 function testContentToRegionsMapping(tolerance) | |
| 106 { | |
| 107 if (tolerance === undefined) | |
| 108 tolerance = 0; | |
| 109 | |
| 110 var debug = isDebugEnabled(); | |
| 111 var drawnRegions = {}; | |
| 112 | |
| 113 var elements = document.getElementsByClassName("check"); | |
| 114 var results = []; | |
| 115 for (var i = 0; i < elements.length; ++i) { | |
| 116 var el = elements[i]; | |
| 117 var regionId = el.className.split(" ")[1]; | |
| 118 var region = document.getElementById(regionId); | |
| 119 var regionRect = region.getBoundingClientRect(); | |
| 120 var contentRect = el.getClientRects(); | |
| 121 if (debug && !drawnRegions[regionId]) { | |
| 122 addPageLevelDebugBox(regionRect, "rgba(255,0,0,0.3)"); | |
| 123 drawnRegions[regionId] = true; | |
| 124 } | |
| 125 for (var j = 0; j < contentRect.length; ++j) { | |
| 126 var passed = assertRectContains(results, el.className, regionRect, c
ontentRect[j], tolerance); | |
| 127 if (debug) | |
| 128 addPageLevelDebugBox(contentRect[j], passed ? "rgba(0,255,0,0.3)
" : "rgba(255,0,255,0.5)"); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 document.write("<p>" + (results.length ? results.join("<br />") : "PASS") +
"</p>"); | |
| 133 | |
| 134 return !results.length && !debug; | |
| 135 } | |
| 136 | |
| 137 function logMessage(message, success) | |
| 138 { | |
| 139 var pElem = document.createElement("p"); | |
| 140 | |
| 141 var spanElement = document.createElement("span"); | |
| 142 spanElement.className = success ? "pass" : "fail"; | |
| 143 var spanTextNode = success ? document.createTextNode("PASS") : document.crea
teTextNode("FAIL"); | |
| 144 spanElement.appendChild(spanTextNode); | |
| 145 pElem.appendChild(spanElement); | |
| 146 | |
| 147 var textNode = document.createTextNode(message); | |
| 148 pElem.appendChild(textNode); | |
| 149 document.getElementById("console").appendChild(pElem); | |
| 150 } | |
| 151 | |
| 152 function logPassMessage(message) | |
| 153 { | |
| 154 logMessage(message, true); | |
| 155 } | |
| 156 | |
| 157 function logFailMessage(message) | |
| 158 { | |
| 159 var logMsg = "" + message; | |
| 160 if (logMsg.length > 0) | |
| 161 logMsg = " : " + logMsg; | |
| 162 logMessage(logMsg, false); | |
| 163 } | |
| 164 | |
| 165 function assert(expression, failMessage) | |
| 166 { | |
| 167 expression ? logPassMessage("") : logFailMessage(failMessage); | |
| 168 } | |
| 169 | |
| 170 // used by getRegionFlowRanges tests | |
| 171 function getName(node) { | |
| 172 if (!node) return "undefined"; | |
| 173 if (node.nodeType == 3) // Text node | |
| 174 return "#text"; | |
| 175 // all the others should have an id | |
| 176 return node.id; | |
| 177 } | |
| 178 | |
| 179 function getRangeAt(arrRange, index) { | |
| 180 if (index < arrRange.length) | |
| 181 return [getName(arrRange[index].startContainer), arrRange[index].startOffset
, getName(arrRange[index].endContainer), arrRange[index].endOffset]; | |
| 182 return null; | |
| 183 } | |
| 184 | |
| 185 function compareArrays(current, expected) { | |
| 186 try { | |
| 187 if (current == null) { | |
| 188 testFailed("Null object. Expected [" + expected.toString() + "] was
null"); | |
| 189 return; | |
| 190 } | |
| 191 if (current.length !== expected.length) { | |
| 192 testFailed("Array length differs. Expected [" + expected.toString()
+ "] was [" + current.toString() + "]"); | |
| 193 return; | |
| 194 } | |
| 195 for (var i = 0; i < current.length; i++) | |
| 196 if (current[i] !== expected[i]) { | |
| 197 testFailed("Expected [" + expected.toString() + "]. Was [" + cu
rrent.toString() + "]"); | |
| 198 return; | |
| 199 } | |
| 200 } catch (ex) { | |
| 201 testFailed(current + " threw exception " + ex); | |
| 202 } | |
| 203 testPassed("Array [" + expected.toString() + "] is equal to [" + current.to
String() + "]"); | |
| 204 } | |
| 205 | |
| 206 function selectContentByRange(fromX, fromY, toX, toY) { | |
| 207 if (!window.testRunner) | |
| 208 return; | |
| 209 | |
| 210 eventSender.mouseMoveTo(fromX, fromY); | |
| 211 eventSender.mouseDown(); | |
| 212 | |
| 213 eventSender.mouseMoveTo(toX, toY); | |
| 214 eventSender.mouseUp(); | |
| 215 } | |
| 216 | |
| 217 function selectContentByIds(fromId, toId) { | |
| 218 var fromRect = document.getElementById(fromId).getBoundingClientRect(); | |
| 219 var toRect = document.getElementById(toId).getBoundingClientRect(); | |
| 220 | |
| 221 var fromRectVerticalCenter = fromRect.top + fromRect.height / 2; | |
| 222 var toRectVerticalCenter = toRect.top + toRect.height / 2; | |
| 223 | |
| 224 selectContentByRange(fromRect.left, fromRectVerticalCenter, toRect.right, to
RectVerticalCenter); | |
| 225 } | |
| 226 | |
| 227 function selectContentByIdsVertical(fromId, toId) { | |
| 228 var fromRect = document.getElementById(fromId).getBoundingClientRect(); | |
| 229 var toRect = document.getElementById(toId).getBoundingClientRect(); | |
| 230 | |
| 231 var fromRectHorizontalCenter = fromRect.left + fromRect.width / 2; | |
| 232 var toRectHorizontalCenter = toRect.left + toRect.width / 2; | |
| 233 | |
| 234 selectContentByRange(fromRectHorizontalCenter, fromRect.top, toRectHorizonta
lCenter, toRect.bottom); | |
| 235 } | |
| 236 | |
| 237 function selectBaseAndExtent(fromId, fromOffset, toId, toOffset) { | |
| 238 var from = document.getElementById(fromId); | |
| 239 var to = document.getElementById(toId); | |
| 240 | |
| 241 var selection = window.getSelection(); | |
| 242 selection.setBaseAndExtent(from, fromOffset, to, toOffset); | |
| 243 } | |
| 244 | |
| 245 function mouseClick(positionX, positionY) { | |
| 246 if (!window.testRunner) | |
| 247 return; | |
| 248 | |
| 249 eventSender.mouseMoveTo(positionX, positionY); | |
| 250 eventSender.mouseDown(); | |
| 251 eventSender.mouseUp(); | |
| 252 } | |
| 253 | |
| 254 function onMouseUpLogSelection(elementId) { | |
| 255 document.onmouseup = function() { | |
| 256 var selectedContent = document.getElementById(elementId); | |
| 257 selectedContent.innerHTML = window.getSelection().getRangeAt(0); | |
| 258 } | |
| 259 } | |
| OLD | NEW |