summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/svg/svglength.html
blob: 9e6fadf442473842df0981dd3103198fc6c982d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!doctype html> 
<title>SVGlength tests</title>
<script src=../../resources/testharness.js></script>
<script src=../../resources/testharnessreport.js></script>
<div id="testcontainer">
  <svg width="1" height="1" visibility="hidden"> </svg>
</div>
<script>

var svg = document.querySelector("svg");

var EPSILON = Math.pow(2, -8);
var lengths = [10, 0, 360, 500, 90, 180, 45, 25.9];
var validUnits = { 
    "" : 1, 
    "%": 2, 
    "em": 3, 
    "ex": 4,
    "px": 5,
    "cm": 6,
    "mm": 7,
    "in": 8,
    "pt": 9,
    "pc": 10,
};

function createLength(valuestr) {
    var length = svg.createSVGLength();
    length.valueAsString = valuestr;
    return length;
}

function convertTo(value, unit, outunit) {
    var userUnits;
    var cssPixelsPerInch = 96;
    var cssPixelsPerCentimeter = cssPixelsPerInch / 2.54; //2.54 cm/in
    var cssPixelsPerMillimeter = cssPixelsPerCentimeter / 10;
    var cssPixelsPerPoint = cssPixelsPerInch / 72;
    var cssPixelsPerPica = cssPixelsPerInch / 6;

    switch(unit) {
        case "":
        case "px":
            userUnits = value;
            break;
        case "%":
        case "em":
        case "ex":
        case "rem":
        case "ch":
            return value;
        case "cm":
            userUnits = value * cssPixelsPerCentimeter;
            break;
        case "mm":
            userUnits = value * cssPixelsPerMillimeter;
            break;
        case "in":
            userUnits = value * cssPixelsPerInch;
            break;
        case "pt":
            userUnits = value * cssPixelsPerPoint;
            break;
        case "pc":
            userUnits = value * cssPixelsPerPica;
            break;
    }

    switch(outunit) {
        case "":
        case "px":
            return userUnits;
        case "%":
        case "em":
        case "ex":
        case "rem":
        case "ch":
            return value;
        case "cm":
            return userUnits / cssPixelsPerCentimeter;
        case "mm":
            return userUnits / cssPixelsPerMillimeter;
        case "in":
            return userUnits / cssPixelsPerInch;
        case "pt":
            return userUnits / cssPixelsPerPoint;
        case "pc":
            return userUnits / cssPixelsPerPica;
    }
}

test(function() {
  for (var unit in validUnits) {
    var length = createLength(10 + unit);
    assert_equals(length.unitType, validUnits[unit]);
  }
}, "Test valid unit types are accepted in valueAsString");

test(function() {
  var invalidUnits = {
    "rem": 1,
    "ch": 2
  };
  for (var unit in invalidUnits) {
    assert_throws(null, function() { createLength(10 + unit) });
  }
}, "Test invalid unit types are not accepted in valueAsString");

test(function() {
  var unitConstants = {
    "UNKNOWN" : 0,
    "NUMBER": 1,
    "PERCENTAGE": 2,
    "EMS": 3,
    "EXS": 4,
    "PX": 5,
    "CM": 6,
    "MM": 7,
    "IN": 8,
    "PT": 9,
    "PC": 10,
  };
  for (var constant in unitConstants) {
    var str = "SVG_LENGTHTYPE_" + constant;
    assert_exists(SVGLength, str, str + " should exist in SVGlength");
  }
}, "Test that unit constants that are supposed to be exposed are available");

test(function() {
  var nonexposedUnitConstants = {
    "REMS": 11,
    "CHS":12
  };
  for (var constant in nonexposedUnitConstants) {
    var str = "SVG_LENGTHTYPE_" + constant;
    assert_not_exists(SVGLength, str, str + " should not be exposed in SVGlength");
  }
}, "Test that unit constants that are not supposed to be exposed are not available");

test(function() {
  for (var i = 0; i < validUnits.length; ++i) {
    var unit = validUnits[i];
    for (var j = 0; j < lengths.length; ++j) {
      var length = lengths[i];
      var value = createLength(length + unit);
      assert_equals(length, value.valueInSpecifiedUnits);
    }
  }
}, "Test result from valueInSpecifiedUnits");

test(function() {
  var nonRelativeUnits = ["px", "cm", "mm", "in", "pt", "pc"];

  for (var i = 0; i < lengths.length; ++i) {
    var length = lengths[i];
    for (var j = 0; j < nonRelativeUnits.length; ++j) {
      var unit = nonRelativeUnits[j];
      var lengthStr = length + unit;
      for (var k = 0; k < nonRelativeUnits.length; ++k) {
        var otherUnit = nonRelativeUnits[k];
        var svgLength = createLength(lengthStr);
        svgLength.convertToSpecifiedUnits(validUnits[otherUnit]);
        assert_approx_equals(svgLength.valueInSpecifiedUnits, convertTo(length, unit, otherUnit), EPSILON);
      }
    }
  }
}, "Test converting unit types for non-relative units");

test(function() {
  for (var i = 0; i < lengths.length; ++i) {
    var length = lengths[i];
    for (var j = 0; j < validUnits.length; ++j) {
      var unit = validUnits[j];
      var ref = createLength(length + unit);

      for (var k = 0; k < validUnits.length; ++k) {
        var otherUnit = validUnits[k];

        var value = createLength(47 + otherUnit);
        value.newValueSpecifiedUnits(unit, length);

        assert_equals(value.valueAsString, ref.valueAsString);
      }
    }
  }
}, "Test newValueSpecifiedUnits for each unit");

</script>