Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / svg / text / resources / SelectionTestCase.js
blob5869ecdbdb6fbf664c636587b96855855fe658a7
1 if (!window.eventSender || !window.testRunner) {
2 alert('This test needs to be run in DRT, to get results!');
5 var svgRoot = 0;
7 // Map 'point' into absolute coordinates, usable for eventSender
8 function transformPoint(point, matrix) {
9 return point.matrixTransform(matrix);
12 function transformRect(rect, matrix) {
13 var topLeft = svgRoot.createSVGPoint();
14 topLeft.x = rect.x;
15 topLeft.y = rect.y;
16 topLeft = transformPoint(topLeft, matrix);
18 var bottomRight = svgRoot.createSVGPoint();
19 bottomRight.x = rect.x + rect.width;
20 bottomRight.y = rect.y + rect.height;
21 bottomRight = transformPoint(bottomRight, matrix);
23 var newRect = svgRoot.createSVGRect();
24 newRect.x = topLeft.x;
25 newRect.y = topLeft.y;
26 newRect.width = bottomRight.x - topLeft.x;
27 newRect.height = bottomRight.y - topLeft.y;
28 return newRect;
31 function toAbsoluteCoordinates(point, element) {
32 // getScreenCTM() returns the transformation matrix from current user units (i.e., after application of the ‘transform’ property)
33 // to the parent user agent's notice of a "pixel".
34 return transformPoint(point, element.getScreenCTM());
37 // Select a range of characters in text element 'id', from the start position of the 'start' character to the end position of the 'end' character
38 function selectRange(id, start, end, expectedText) {
39 var element = document.getElementById(id);
40 var startExtent = element.getExtentOfChar(start);
41 var endExtent = element.getExtentOfChar(end);
43 var startPos = element.getStartPositionOfChar(start);
44 var endPos = element.getEndPositionOfChar(end);
46 // Handle lengthAdjust+textLength on our own.
47 var scale = element.textLength.baseVal.value / element.getComputedTextLength();
48 if (element.lengthAdjust.baseVal == SVGTextContentElement.LENGTHADJUST_SPACINGANDGLYPHS && scale != 1) {
49 svgRoot = element.ownerSVGElement;
51 var firstCharPosition = element.getStartPositionOfChar(0);
52 var scaleMatrix = svgRoot.createSVGMatrix()
53 .translate(firstCharPosition.x, firstCharPosition.y)
54 .scaleNonUniform(scale, 1)
55 .translate(-firstCharPosition.x, -firstCharPosition.y);
57 startPos = transformPoint(startPos, scaleMatrix);
58 endPos = transformPoint(endPos, scaleMatrix);
60 startExtent = transformRect(startExtent, scaleMatrix);
61 endExtent = transformRect(endExtent, scaleMatrix);
64 if (window.eventSender) {
65 // Trigger 'partial glyph selection' code, by adjusting the end x position by half glyph width
66 var xOldStart = startPos.x;
67 var xOldEnd = endPos.x;
68 endPos.x -= endExtent.width / 2 - 1;
70 // Round the points "inwards" to avoid being affected by the truncation
71 // taking place in eventSender.mouseMoveTo(...).
72 startPos.x = Math.ceil(startPos.x);
74 var absStartPos = toAbsoluteCoordinates(startPos, element);
75 var absEndPos = toAbsoluteCoordinates(endPos, element);
77 // Move to selection origin and hold down mouse
78 eventSender.mouseMoveTo(absStartPos.x, absStartPos.y);
79 eventSender.mouseDown();
81 // Move again to start selection
82 eventSender.mouseMoveTo(absStartPos.x, absStartPos.y);
84 // Move to end location and release mouse
85 eventSender.mouseMoveTo(absEndPos.x, absEndPos.y);
86 eventSender.mouseUp();
88 startPos.x = xOldStart;
89 endPos.x = xOldEnd;
92 // Mark start position using a green line
93 var startLineElement = document.createElementNS("http://www.w3.org/2000/svg", "svg:line");
94 startLineElement.setAttribute("x1", startPos.x);
95 startLineElement.setAttribute("y1", startExtent.y);
96 startLineElement.setAttribute("x2", startPos.x);
97 startLineElement.setAttribute("y2", startExtent.height + 1);
98 startLineElement.setAttribute("stroke", "green");
99 document.getElementById("container").appendChild(startLineElement);
101 // Mark end position using a green line
102 var endLineElement = document.createElementNS("http://www.w3.org/2000/svg", "svg:line");
103 endLineElement.setAttribute("x1", endPos.x);
104 endLineElement.setAttribute("y1", endExtent.y);
105 endLineElement.setAttribute("x2", endPos.x);
106 endLineElement.setAttribute("y2", endExtent.height + 1);
107 endLineElement.setAttribute("stroke", "green");
108 document.getElementById("container").appendChild(endLineElement);
110 // Highlight rect that we've selected using the extent information
111 var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "svg:rect");
112 rectElement.setAttribute("x", startExtent.x);
113 rectElement.setAttribute("y", endExtent.y);
114 rectElement.setAttribute("width", endExtent.x + endExtent.width - startExtent.x);
115 rectElement.setAttribute("height", endExtent.height);
116 rectElement.setAttribute("fill-opacity", "0.4");
117 rectElement.setAttribute("fill", "red");
118 document.getElementById("container").appendChild(rectElement);
120 // Check selection worked properly, otherwhise report error
121 var actualText = window.getSelection().toString();
122 if (actualText != expectedText) {
123 var textElement = document.createElementNS("http://www.w3.org/2000/svg", "svg:text");
124 textElement.setAttribute("x", "0");
125 textElement.setAttribute("y", "35");
126 textElement.setAttribute("fill", "red");
127 textElement.setAttribute("transform", "scale(0.5)");
128 textElement.setAttribute("font-size", "8");
129 textElement.textContent = "Expected '" + expectedText + "' to be selected, got: '" + actualText + "'";
130 document.getElementById("container").appendChild(textElement);