Update with current status
[gnash.git] / testsuite / actionscript.all / TextFormat.as
blob90c2235b1c39791da6ac2aa0603cef9a93fec03c
1 //
2 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 // Test case for TextFormat ActionScript class
20 // compile this test case with Ming makeswf, and then
21 // execute it like this gnash -1 -r 0 -v out.swf
23 rcsid="TextFormat.as";
25 #include "check.as"
27 Object.prototype.hasOwnProperty = ASnative(101, 5);
29 check_equals(typeof(TextFormat), 'function');
30 check_equals(typeof(TextFormat.prototype), 'object');
31 tfObj = new TextFormat();
32 check_equals(typeof(tfObj), 'object');
33 check(tfObj instanceof TextFormat);
35 // The members below would not exist before
36 // the construction of first TextFormat object
37 check(TextFormat.prototype.hasOwnProperty('display'));
38 check(TextFormat.prototype.hasOwnProperty('bullet'));
39 check(TextFormat.prototype.hasOwnProperty('tabStops'));
40 check(TextFormat.prototype.hasOwnProperty('blockIndent'));
41 check(TextFormat.prototype.hasOwnProperty('leading'));
42 check(TextFormat.prototype.hasOwnProperty('indent'));
43 check(TextFormat.prototype.hasOwnProperty('rightMargin'));
44 check(TextFormat.prototype.hasOwnProperty('leftMargin'));
45 check(TextFormat.prototype.hasOwnProperty('align'));
46 check(TextFormat.prototype.hasOwnProperty('underline'));
47 check(TextFormat.prototype.hasOwnProperty('italic'));
48 check(TextFormat.prototype.hasOwnProperty('bold'));
49 check(TextFormat.prototype.hasOwnProperty('target'));
50 check(TextFormat.prototype.hasOwnProperty('url'));
51 check(TextFormat.prototype.hasOwnProperty('color'));
52 check(TextFormat.prototype.hasOwnProperty('size'));
53 check(TextFormat.prototype.hasOwnProperty('font'));
54 check(!TextFormat.prototype.hasOwnProperty('getTextExtent'));
55 check(tfObj.hasOwnProperty('getTextExtent'));
58 // When you construct a TextFormat w/out args all members
59 // are of the 'null' type. In general, uninitialized members
60 // are all of the 'null' type.
61 check_equals(typeof(tfObj.display), 'string');
62 check_equals(tfObj.display, 'block');
63 check_equals(typeof(tfObj.bullet), 'null');
64 check_equals(typeof(tfObj.tabStops), 'null');
65 check_equals(typeof(tfObj.blockIndent), 'null');
66 check_equals(typeof(tfObj.leading), 'null');
67 check_equals(typeof(tfObj.indent), 'null');
68 check_equals(typeof(tfObj.rightMargin), 'null');
69 check_equals(typeof(tfObj.leftMargin), 'null');
70 check_equals(typeof(tfObj.align), 'null');
71 check_equals(typeof(tfObj.underline), 'null');
72 check_equals(typeof(tfObj.italic), 'null');
73 check_equals(typeof(tfObj.bold), 'null');
74 check_equals(typeof(tfObj.target), 'null');
75 check_equals(typeof(tfObj.url), 'null');
76 check_equals(typeof(tfObj.color), 'null');
77 check_equals(typeof(tfObj.size), 'null');
78 check_equals(typeof(tfObj.font), 'null');
79 check_equals(typeof(tfObj.getTextExtent), 'function');
81 // new TextFormat([font, [size, [color, [bold, [italic, [underline, [url, [target, [align,[leftMargin, [rightMargin, [indent, [leading]]]]]]]]]]]]])
82 tfObj = new TextFormat("fname", 2, 30, true, false, true, 'http', 'tgt', 'cEnter', '23', '32', 12, 4);
83 check_equals(typeof(tfObj.display), 'string');
84 check_equals(tfObj.display, 'block');
85 check_equals(typeof(tfObj.bullet), 'null');
86 check_equals(typeof(tfObj.tabStops), 'null');
87 check_equals(typeof(tfObj.blockIndent), 'null');
88 check_equals(tfObj.leading, 4);
89 check_equals(tfObj.indent, 12);
90 check_equals(typeof(tfObj.rightMargin), 'number'); // even if we passed a string to it
91 check_equals(tfObj.rightMargin, 32);
92 check_equals(typeof(tfObj.leftMargin), 'number'); // even if we passed a string to it
93 check_equals(tfObj.leftMargin, 23);
94 check_equals(tfObj.align, 'center');
95 check_equals(tfObj.target, 'tgt');
96 check_equals(tfObj.url, 'http');
97 check_equals(tfObj.underline, true);
98 check_equals(typeof(tfObj.italic), 'boolean');
99 check_equals(tfObj.italic, false);
100 check_equals(tfObj.bold, true);
101 check_equals(tfObj.color, 30);
102 check_equals(tfObj.size, 2);
103 check_equals(tfObj.font, 'fname');
106 /// Bold
108 // The boolean conversion of a string is version dependent.
109 stringbool = "string" ? true : false;
111 tf = new TextFormat();
112 check_equals(tf.bold, null);
113 tf.bold = true;
114 check_equals(tf.bold, true);
115 tf.bold = false;
116 check_equals(tf.bold, false);
117 tf.bold = "string";
118 check_equals(tf.bold, stringbool);
119 tf.bold = null;
120 check_equals(tf.bold, null);
121 tf.bold = "string";
122 check_equals(tf.bold, stringbool);
123 tf.bold = undefined;
124 check_equals(tf.bold, null);
127 // rightMargin
128 tf = new TextFormat();
129 check_equals(tf.rightMargin, null);
130 tf.rightMargin = 10;
131 check_equals(tf.rightMargin, 10);
132 tf.rightMargin = -10;
133 check_equals(tf.rightMargin, 0);
134 tf.rightMargin = "string";
135 check_equals(tf.rightMargin, 0);
136 tf.rightMargin = null;
137 check_equals(tf.rightMargin, null);
138 tf.rightMargin = "string";
139 check_equals(tf.rightMargin, 0);
140 tf.rightMargin = undefined;
141 check_equals(tf.rightMargin, null);
143 // leftMargin
144 tf = new TextFormat();
145 check_equals(tf.leftMargin, null);
146 tf.leftMargin = 10;
147 check_equals(tf.leftMargin, 10);
148 tf.leftMargin = -10;
149 check_equals(tf.leftMargin, 0);
150 tf.leftMargin = "string";
151 check_equals(tf.leftMargin, 0);
152 tf.leftMargin = null;
153 check_equals(tf.leftMargin, null);
154 tf.leftMargin = "string";
155 check_equals(tf.leftMargin, 0);
156 tf.leftMargin = undefined;
157 check_equals(tf.leftMargin, null);
159 // blockIndent
160 tf = new TextFormat();
161 check_equals(tf.blockIndent, null);
162 tf.blockIndent = 10;
163 check_equals(tf.blockIndent, 10);
164 tf.blockIndent = -10;
166 #if OUTPUT_VERSION < 8
167 check_equals(tf.blockIndent, 0);
168 #else
169 xcheck_equals(tf.blockIndent, -10);
170 #endif
172 tf.blockIndent = "string";
173 #if OUTPUT_VERSION < 8
174 check_equals(tf.blockIndent, 0);
175 #else
176 xcheck_equals(tf.blockIndent, -2147483648);
177 #endif
179 tf.blockIndent = null;
180 check_equals(tf.blockIndent, null);
182 tf.blockIndent = "string";
183 #if OUTPUT_VERSION < 8
184 check_equals(tf.blockIndent, 0);
185 #else
186 xcheck_equals(tf.blockIndent, -2147483648);
187 #endif
188 tf.blockIndent = undefined;
189 check_equals(tf.blockIndent, null);
191 // leading
192 tf = new TextFormat();
193 check_equals(tf.leading, null);
194 tf.leading = 10;
195 check_equals(tf.leading, 10);
196 tf.leading = -10;
198 #if OUTPUT_VERSION < 8
199 check_equals(tf.leading, 0);
200 #else
201 xcheck_equals(tf.leading, -10);
202 #endif
204 tf.leading = "string";
205 #if OUTPUT_VERSION < 8
206 check_equals(tf.leading, 0);
207 #else
208 xcheck_equals(tf.leading, -2147483648);
209 #endif
211 tf.leading = null;
212 check_equals(tf.leading, null);
214 tf.leading = "string";
215 #if OUTPUT_VERSION < 8
216 check_equals(tf.leading, 0);
217 #else
218 xcheck_equals(tf.leading, -2147483648);
219 #endif
220 tf.leading = undefined;
221 check_equals(tf.leading, null);
223 // indent
224 tf = new TextFormat();
225 check_equals(tf.indent, null);
226 tf.indent = 10;
227 check_equals(tf.indent, 10);
228 tf.indent = -10;
230 #if OUTPUT_VERSION < 8
231 check_equals(tf.indent, 0);
232 #else
233 xcheck_equals(tf.indent, -10);
234 #endif
236 tf.indent = "string";
237 #if OUTPUT_VERSION < 8
238 check_equals(tf.indent, 0);
239 #else
240 xcheck_equals(tf.indent, -2147483648);
241 #endif
243 tf.indent = null;
244 check_equals(tf.indent, null);
246 tf.indent = "string";
247 #if OUTPUT_VERSION < 8
248 check_equals(tf.indent, 0);
249 #else
250 xcheck_equals(tf.indent, -2147483648);
251 #endif
252 tf.indent = undefined;
253 check_equals(tf.indent, null);
255 // size
256 tf = new TextFormat();
257 check_equals(tf.size, null);
258 tf.size = 10;
259 check_equals(tf.size, 10);
261 tf.size = -10;
262 xcheck_equals(tf.size, -10);
264 tf.size = "string";
265 #if OUTPUT_VERSION < 8
266 check_equals(tf.size, 0);
267 #else
268 xcheck_equals(tf.size, -2147483648);
269 #endif
271 tf.size = null;
272 check_equals(tf.size, null);
274 tf.size = "string";
275 #if OUTPUT_VERSION < 8
276 check_equals(tf.size, 0);
277 #else
278 xcheck_equals(tf.size, -2147483648);
279 #endif
280 tf.size = undefined;
281 check_equals(tf.size, null);
283 // align
284 tf.align = "hi";
285 check_equals(tf.align, null);
286 tf.align = "Left";
287 check_equals(tf.align, "left");
288 tf.align = "o";
289 check_equals(tf.align, "left");
290 tf.align = "righto";
291 check_equals(tf.align, "left");
292 tf.align = "center";
293 check_equals(tf.align, "center");
294 tf.align = "right";
295 check_equals(tf.align, "right");
296 tf.align = undefined;
297 check_equals(tf.align, "right");
298 tf.align = null;
299 check_equals(tf.align, "right");
301 // Check tabStops property.
302 // The passed array is processed before assignment, not simply stored.
303 tf = new TextFormat();
305 o = {};
306 o.valueOf = function() { return 6; };
307 o.toString = function() { return "string"; };
309 a = [ o ];
311 tf.tabStops = a;
312 check_equals(a.toString(), "string");
313 xcheck_equals(tf.tabStops.toString(), "6");
315 tf2 = new TextFormat("dejafont", 12);
317 // getTextExtent has different behaviour for SWF6.
318 #if OUTPUT_VERSION > 6
320 // I don't know how to test this properly, as we can only test device fonts
321 // here, and the pp uses a different font from Gnash.
323 te = tf2.getTextExtent("Hello");
325 // The object is a bare object
326 te.hasOwnProperty = Object.prototype.hasOwnProperty;
328 check(te.hasOwnProperty("ascent"));
329 check(te.hasOwnProperty("descent"));
330 check(te.hasOwnProperty("textFieldWidth"));
331 check(te.hasOwnProperty("textFieldHeight"));
332 check(te.hasOwnProperty("width"));
333 check(te.hasOwnProperty("height"));
335 check_equals(te.textFieldWidth, 37);
336 check_equals(te.width, 33);
337 #if OUTPUT_VERSION > 7
338 check_equals(te.ascent, 11.1);
339 #else
340 check_equals(te.ascent, 11);
341 #endif
343 #if OUTPUT_VERSION > 7
344 check_equals(te.textFieldHeight, 17.9);
345 check_equals(te.descent, 2.8);
346 #else
347 check_equals(te.textFieldHeight, 17);
348 check_equals(te.descent, 2);
349 #endif
351 te = tf2.getTextExtent("a");
352 check_equals(te.width, 8);
353 te = tf2.getTextExtent("aa");
354 check_equals(te.width, 16);
355 te = tf2.getTextExtent("aaa");
356 check_equals(te.width, 24);
358 te = tf2.getTextExtent("Hello", 10);
359 #if OUTPUT_VERSION > 7
360 check_equals(te.textFieldHeight, 73.5);
361 #else
362 check_equals(te.textFieldHeight, 17);
363 #endif
365 #if OUTPUT_VERSION == 7
366 var wrappingPoint = 0;
367 for (var i:Number = 10; i < 30; i++) {
368 if (tf2.getTextExtent("Hello", i).height != 13) {
369 wrappingPoint = i;
370 break;
374 // Wrapping is enabled only if the wrapping value >= tfw of the first two
375 // letters. Doh.
376 check_equals(wrappingPoint, tf2.getTextExtent("He").textFieldWidth );
377 check_equals(tf2.getTextExtent("Hello", wrappingPoint).height, 27);
378 #endif
380 check_equals(te.textFieldWidth, 10);
382 #if OUTPUT_VERSION > 7
383 check_equals(te.width, 9);
384 #else
385 check_equals(te.width, 33);
386 #endif
389 te = tf2.getTextExtent("Hello", 5);
390 #if OUTPUT_VERSION < 8
391 check_equals(te.textFieldHeight, 17);
392 check_equals(te.descent, 2);
393 #else
394 check_equals(te.textFieldHeight, 73.5);
395 check_equals(te.descent, 2.8);
396 #endif
397 check_equals(te.textFieldWidth, 5);
398 #if OUTPUT_VERSION > 7
399 check_equals(te.ascent, 11.1);
400 #else
401 check_equals(te.ascent, 11);
402 #endif
404 #if OUTPUT_VERSION > 7
405 // Text is wrapped in v8; 'advance' of largest character.
406 check_equals(te.width, 9);
407 #else
408 check_equals(te.width, 33);
409 #endif
412 te = tf2.getTextExtent("Longer sentence with more words.", 30);
413 check_equals(te.textFieldWidth, 30);
414 check_equals(te.width, 25);
415 #if OUTPUT_VERSION > 7
416 xcheck_equals(te.height, 152.9);
417 #else
418 xcheck_equals(te.height, 152);
419 #endif
421 te = tf2.getTextExtent("Longersentencewithoneword", 30);
422 check_equals(te.textFieldWidth, 30);
423 #if OUTPUT_VERSION > 7
424 check_equals(te.height, 125.1);
425 check_equals(Math.round(te.width), 25);
426 #else
427 check_equals(te.width, 26);
428 check_equals(te.height, 111);
429 #endif
431 te = tf2.getTextExtent("o");
432 check_equals(te.textFieldWidth, 12);
433 #if OUTPUT_VERSION > 7
434 check_equals(te.ascent, 11.1);
435 #else
436 check_equals(te.ascent, 11);
437 #endif
438 #if OUTPUT_VERSION < 8
439 check_equals(te.textFieldHeight, 17);
440 check_equals(te.descent, 2);
441 #else
442 check_equals(te.textFieldHeight, 17.9);
443 check_equals(te.descent, 2.8);
444 #endif
446 te = tf2.getTextExtent("oo");
447 check_equals(Math.round(te.textFieldWidth), 20);
448 check_equals(Math.round(te.ascent), 11);
449 #if OUTPUT_VERSION < 8
450 check_equals(te.textFieldHeight, 17);
451 check_equals(te.descent, 2);
452 #else
453 check_equals(te.textFieldHeight, 17.9);
454 check_equals(te.descent, 2.8);
455 #endif
457 te = tf2.getTextExtent("ool");
458 check_equals(te.textFieldWidth, 24);
459 #if OUTPUT_VERSION > 7
460 check_equals(te.ascent, 11.1);
461 #else
462 check_equals(te.ascent, 11);
463 #endif
464 #if OUTPUT_VERSION < 8
465 check_equals(te.textFieldHeight, 17);
466 check_equals(te.descent, 2);
467 #else
468 check_equals(te.textFieldHeight, 17.9);
469 check_equals(te.descent, 2.8);
470 #endif
472 tf2.size = 20;
473 te = tf2.getTextExtent("ool");
474 check_equals(Math.round(te.textFieldHeight), 27);
475 check_equals(Math.round(te.textFieldWidth), 36);
476 #if OUTPUT_VERSION < 8
477 check_equals(te.ascent, 18);
478 check_equals(te.descent, 4);
479 #else
480 check_equals(te.ascent, 18.55);
481 check_equals(te.descent, 4.7);
482 #endif
484 #endif
486 #if OUTPUT_VERSION < 7
487 check_totals(122);
488 #elif OUTPUT_VERSION == 7
489 check_totals(168);
490 #else
491 check_totals(166);
492 #endif