summaryrefslogtreecommitdiff
path: root/src/test/WordsTest.py
blob: a5e856e57c9d7f7f66e257c258ef8a6f71068cf1 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#!/usr/bin/python
# -*- coding: utf-8 -*-
# libavg - Media Playback Engine.
# Copyright (C) 2003-2014 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Current versions can be found at www.libavg.de
#

import platform

from libavg import avg, player
from testcase import *

class WordsTestCase(AVGTestCase):
    def __init__(self, testFuncName):
        AVGTestCase.__init__(self, testFuncName)
   
    def testSimpleWords(self):
        def checkFont():
            self.assertEqual(node.variant, "bold")
    
        def checkUnicodeText():
            node.text = u"föa"
            avg.WordsNode(text=u"öäü", font="Bitstream Vera Sans")
       
        fontList = avg.WordsNode.getFontFamilies()
        try:
            fontList.index("Bitstream Vera Sans")
        except ValueError:
            self.fail("Font not found")
        variantList = avg.WordsNode.getFontVariants("Bitstream Vera Sans")
        self.assert_(len(variantList) >= 4)
        root = self.loadEmptyScene()
        avg.WordsNode (pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                text="Bitstream Vera Sans", variant="roman", parent=root)
        node = avg.WordsNode(pos=(1,16), fontsize=12, font="Bitstream Vera Sans",
                text="Bold", variant="bold", parent=root)
        self.assertNotEqual(node.size, (0,0))
        node.getGlyphPos(0)
        self.start(True, 
                (lambda: self.compareImage("testSimpleWords"),
                 checkFont,
                 checkUnicodeText,
                ))

    def testRedrawOnDemand(self):

        def changeText(newText):
            size = node.size
            node.text = newText 
            self.assertNotEqual(node.size, size)

        def changeFont():
            size = node.size
            node.fontsize = 18
            self.assertNotEqual(node.size, size)

        root = self.loadEmptyScene()
        node = avg.WordsNode(font="Bitstream Vera Sans", fontsize=12, text="foo", 
                parent=root)
        changeText("foobar")
        self.start(True, 
                (lambda: changeText("bar"),
                 changeFont,
                ))

    def testFontStyle(self):

        def setStyle(node, style):
            node.fontstyle = style
            self.assert_(node.fontsize == 15)

        fontStyle = avg.FontStyle(font="Bitstream Vera Sans", variant="Roman",
                fontsize=12)
        self.assert_(fontStyle.font == "Bitstream Vera Sans")
        root = self.loadEmptyScene()
        words = avg.WordsNode(pos=(1,1), fontstyle=fontStyle, text="Bitstream Vera Sans", 
                parent=root)
        avg.WordsNode (pos=(1,16), fontstyle=fontStyle, variant="bold", text="Bold",
                parent=root)
        otherFontStyle = fontStyle
        otherFontStyle.fontsize = 15
        self.start(True, 
                (lambda: self.compareImage("testFontStyle1"),
                 lambda: setStyle(words, otherFontStyle),
                 lambda: self.compareImage("testFontStyle2"),
                ))

    def testBaseStyle(self):
        attrs = {"font": "Bitstream Vera Sans",
                 "variant": "Bold",
                 "color": "FF0000",
                 "aagamma": 0.5,
                 "fontsize": 20,
                 "indent": 1,
                 "linespacing": 2,
                 "alignment": "right",
                 "wrapmode": "char",
                 "justify": True,
                 "letterspacing": 3,
                 "hint": False}
        defaultStyle = avg.FontStyle()
        fontStyle1 = avg.FontStyle(basestyle=defaultStyle, **attrs)
        for attrName in attrs.iterkeys():
            self.assert_(getattr(fontStyle1, attrName) != getattr(defaultStyle, attrName))
            self.assert_(getattr(fontStyle1, attrName) == attrs[attrName])
        fontStyle2 = avg.FontStyle(basestyle=fontStyle1)
        for attrName in attrs.iterkeys():
            self.assert_(getattr(fontStyle2, attrName) == getattr(fontStyle1, attrName))

    def testGlyphPos(self):
        def posAlmostEqual(pos1, pos2):
            return math.fabs(pos1[0]-pos2[0]) <= 2 and math.fabs(pos1[1]-pos2[1]) <= 2
        
        node = avg.WordsNode(text="Bold", font="Bitstream Vera Sans")
        self.assertEqual(node.getGlyphPos(0), (0,0))
        size = node.getGlyphSize(0)
        self.assert_(posAlmostEqual(size, (10, 18)))
        self.assert_(posAlmostEqual(node.getGlyphPos(3), (22,0)))
        size = node.getGlyphSize(3)
        self.assert_(posAlmostEqual(size, (8, 18)))
        self.assertException(lambda: node.getGlyphPos(4))
        node.text=u"föa"
        self.assert_(posAlmostEqual(node.getGlyphPos(1), (4,0)))
#        print [ node.getGlyphPos(i) for i in range(3)]
        self.assert_(posAlmostEqual(node.getGlyphPos(2), (12,0)))
        self.assertException(lambda: node.getGlyphPos(3))

    def testParaWords(self):
        root = self.loadEmptyScene()
        avg.LineNode(pos1=(0.5, 0), pos2=(0.5, 50), color="FF0000", parent=root)
        avg.LineNode(pos1=(119.5, 0.5), pos2=(119.5, 50), color="FF0000", parent=root)
        avg.LineNode(pos1=(74.5,60), pos2=(74.5, 110), color="FF0000", parent=root)
        avg.WordsNode(id="para", pos=(1,1), fontsize=12, width=70,
                font="Bitstream Vera Sans", text="Left-justified paragraph.", 
                parent=root)
        avg.WordsNode(id="paracenter", pos=(120,1), fontsize=12, width=70, 
                font="Bitstream Vera Sans", text="Centered paragraph", 
                alignment="center", parent=root)
        avg.WordsNode(id="pararight", pos=(75,60), fontsize=12, width=70,
                font="Bitstream Vera Sans", alignment="right",
                text="Right-justified paragraph.<i>l</i>",
                parent=root)
        avg.WordsNode(id="paralinespacing", pos=(80,60), fontsize=12, width=70,
                font="Bitstream Vera Sans", linespacing=-4,
                text="Paragraph with custom line spacing.",
                parent=root)
        self.start(True, [lambda: self.compareImage("testParaWords")])

    def testJustify(self):
        root = self.loadEmptyScene()
        avg.WordsNode(pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", justify=True, width=100,
                text="Justified paragraph more than one line long.", parent=root)
        self.start(True, [lambda: self.compareImage("testJustify")])

    def testWrapMode(self):
        def setCharMode():
            node.wrapmode = 'char'
        
        def setWordMode():
            node.wrapmode = 'word'
        
        def setWordCharMode():
            node.wrapmode = 'wordchar'
            
        root = self.loadEmptyScene()
        node = avg.WordsNode(pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", width=100, 
                text="""Wrapped paragraph more than one line long.
                        Withaverylongpackedlinewithnobreaks""",
                parent=root)
        self.start(True, 
                (lambda: self.compareImage("testWrapMode1"),
                 setCharMode,
                 lambda: self.compareImage("testWrapMode2"),
                 setWordMode,
                 lambda: self.compareImage("testWrapMode3"),
                 setWordCharMode,
                 lambda: self.compareImage("testWrapMode4"),
                ))

    def testWordsMask(self):
        def setMask():
            try:
                node.maskhref = "mask1.png"
            except RuntimeError:
                self.skip("no shader support")
                player.stop()
           
        def setColor():
            node.color = "FFFF00"

        def setOpacity():
            node.opacity = 0.5

        def setSize():
            rect = avg.RectNode(pos=(39.5, 30.5), size=(80, 60))
            root.appendChild(rect)
            node.masksize = (160, 120)
            node.opacity = 1 

        def setPos():
            node.pos = (40, 20)
            node.maskpos = (-40, -20)

        def setDefaultSize():
            node.masksize = (0,0)

        def setCentered():
            node.alignment = "center"
            node.masksize = (160, 120)
            node.pos = (80,20)
            node.maskpos = (0, -20)

        root = self.loadEmptyScene()
        node = avg.WordsNode(fontsize=8, linespacing=-4, font="Bitstream Vera Sans",
                variant="roman", width=160,
                text="Ich bin nur ein kleiner Blindtext. Wenn ich gross bin, will ich \
                    Ulysses von James Joyce werden. Aber jetzt lohnt es sich noch nicht, \
                    mich weiterzulesen. Denn vorerst bin ich nur ein kleiner Blindtext. \
                    Ich bin nur ein kleiner Blindtext. Wenn ich gross bin, will ich \
                    Ulysses von James Joyce werden. Aber jetzt lohnt es sich noch nicht, \
                    mich weiterzulesen. Denn vorerst bin ich nur ein kleiner Blindtext. \
                    Ich bin nur ein kleiner Blindtext. Wenn ich gross bin, will ich \
                    Ulysses von James Joyce werden. Aber jetzt lohnt es sich noch nicht, \
                    mich weiterzulesen. Denn vorerst bin ich nur ein kleiner Blindtext.",
                parent=root)
        self.start(True,
                (setMask,
                 lambda: self.compareImage("testWordsMask1"),
                 setColor,
                 lambda: self.compareImage("testWordsMask2"),
                 setOpacity,
                 lambda: self.compareImage("testWordsMask3"),
                 setSize,
                 lambda: self.compareImage("testWordsMask4"),
                 setPos,
                 lambda: self.compareImage("testWordsMask5"),
                 setDefaultSize,
                 lambda: self.compareImage("testWordsMask6"),
                 setCentered,
                 lambda: self.compareImage("testWordsMask7"),
                ))

    def testHinting(self):
        def checkPositions():
#            node0 = root.getChild(0)
#            for i in range(len(node0.text)):
#                print node0.getGlyphPos(i)
            noHint = root.getChild(0)
            hint = root.getChild(1)
            posNoHint = noHint.getGlyphPos(6)
            posHint = hint.getGlyphPos(6)
            self.assertNotEqual(posNoHint, posHint)
            noHint.hint = True
            hint.hint = False
            self.assertEqual(posNoHint, hint.getGlyphPos(6))
            self.assertEqual(posHint, noHint.getGlyphPos(6))

        if platform.system() == "Linux":
            self.skip("Linux support requires modified font config")
        else:
            root = self.loadEmptyScene()
            avg.WordsNode(pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                    variant="roman", hint=False, text="Lorem ipsum dolor (no hinting)",
                    parent=root)
            avg.WordsNode(pos=(1,15), fontsize=12, font="Bitstream Vera Sans",
                    variant="roman", hint=True, text="Lorem ipsum dolor (hinting)",
                    parent=root)
            self.start(True, [checkPositions])


    def testSpanWords(self):
        def setTextAttrib():
            self.baselineBmp = player.screenshot()
            player.getElementByID("words").text = self.text
        
        def checkSameImage():
            bmp = player.screenshot()
            self.assert_(self.areSimilarBmps(bmp, self.baselineBmp, 0, 0))
        
        def createUsingDict():
            player.getElementByID("words").unlink()
            node = avg.WordsNode(id="words", pos=(1,1), fontsize=12, width=120,
                    font="Bitstream Vera Sans", variant="roman", text=self.text)
            root.appendChild(node)
        
        self.text = """
              Markup: 
              <span size='14000' rise='5000' foreground='red'>span</span>, 
              <i>italics</i>, <b>bold</b>
        """
        root = self.loadEmptyScene()
        node = player.createNode("""
            <words id="words" x="1" y="1" fontsize="12" width="120" 
                font="Bitstream Vera Sans" variant="roman">
        """
        +self.text+
        """
            </words>
        """)
        root.appendChild(node)
        self.start(True, 
                [lambda: self.compareImage("testSpanWords"),
                 setTextAttrib,
                 lambda: self.compareImage("testSpanWords"),
                 checkSameImage,
                 createUsingDict,
                 lambda: self.compareImage("testSpanWords"),
                 checkSameImage,
                ])
    
    def testDynamicWords(self):
        def changeText():
            oldwidth = words.width
            words.text = "blue" 
            self.assertNotEqual(words.width, oldwidth)
            words.color = "404080"
            words.x += 10
        
        def changeHeight():
            words.height = 28
        
        def activateText():
            words.active = True
        
        def deactivateText():
            words.active = False
        
        def changeFont():
            words.font = "Bitstream Vera Sans"
            words.height = 0
            words.fontsize = 30
        
        def changeFont2():
            words.fontsize = 18
        
        def changeTextWithInvalidTag():
            try:
                words.text = "This <invalid_tag/>bombs"
            except:
                words.text = "except"
            self.assertEqual(words.text, "except")
       
        root = self.loadEmptyScene()
        words = avg.WordsNode(pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                text="foo", parent=root)
        self.start(True, 
                (lambda: self.compareImage("testDynamicWords1"),
                 changeText,
                 changeHeight,
                 changeFont,
                 lambda: self.compareImage("testDynamicWords2"),
                 deactivateText,
                 lambda: self.compareImage("testDynamicWords3"),
                 activateText,
                 changeFont2,
                 lambda: self.compareImage("testDynamicWords4"),
                 changeTextWithInvalidTag
                ))

    def testI18NWords(self):
        def changeUnicodeText():
            words.text = "Arabic nonsense: ﯿﭗ"
        
        def setNBSP():
            words.width=100
            words.text=(u"blindtext1\u00A0blindtext2\u00Ablindtext3 "+
                    u"blindtext4\u00A0blindtext\u00A0blindtext\u00A0")

        root = self.loadEmptyScene()
        avg.WordsNode(pos=(1,1), fontsize=14, font="Bitstream Vera Sans",
                text="一二三四五六七八九", parent=root)
        words = avg.WordsNode(pos=(1,24), fontsize=12, font="Bitstream Vera Sans",
                text="foo", parent=root)
        root.appendChild(
                player.createNode("""
                    <words x="1" y="48" fontsize="12" font="Bitstream Vera Sans">
                            &amp;
                    </words>
                """))
        avg.WordsNode(pos=(12,48), fontsize=12, font="Bitstream Vera Sans", text="&amp;",
                rawtextmode=True, parent=root)

        self.start(True, 
                (lambda: self.compareImage("testI18NWords1"),
                 changeUnicodeText,
                 lambda: self.compareImage("testI18NWords2"),
                 setNBSP,
                 lambda: self.compareImage("testI18NWords3"),
                ))

    def testRawText(self):
        def createDynNodes():
            self.dictdnode = avg.WordsNode(text='&lt;test dyndict&amp;', 
                    rawtextmode=True, pos=(1,65), font='Bitstream Vera Sans', 
                    variant='roman', fontsize=12)
            root.appendChild(self.dictdnode)

            self.xmldnode = player.createNode("""
                <words text="&lt;test dynattr&amp;" fontsize="12" 
                        font="Bitstream Vera Sans" variant="roman" rawtextmode="true"
                        x="1" y="85"/>""")
            root.appendChild(self.xmldnode)
        
        def switchRawMode():
            self.dictdnode.rawtextmode = False
            valNode.rawtextmode = True
            attribNode.rawtextmode = True
        
        def bombIt():
            def cantRun():
                self.xmldnode.rawtextmode = False
                self.assert_(0)
            self.assertException(cantRun)
        
        def assignNewTexts():
            text = u'&ùùààxx>'
            self.dictdnode.rawtextmode = True
            self.dictdnode.text = text
            self.xmldnode.text = text
            valNode.text = text
            attribNode.text = text

        root = self.loadEmptyScene()
        attribNode = avg.WordsNode(text="ùnicòdé <b>bold</b>",
                fontsize=12, pos=(1,5), font="Bitstream Vera Sans", parent=root)
        valNode = player.createNode("""
            <words id="nodeval" fontsize="10" x="1" y="25" font="Bitstream Vera Sans"><b>bold</b> ùnicòdé  &lt;</words>""")
        root.appendChild(valNode)
        root.appendChild(
                player.createNode("""
                        <words x="1" y="45" fontsize="15" font="Bitstream Vera Sans">
                            &amp;
                        </words>"""))

        self.start(True, 
                (lambda: self.compareImage("testRawText1"),
                 createDynNodes,
                 lambda: self.compareImage("testRawText2"),
                 switchRawMode,
                 lambda: self.compareImage("testRawText3"),
                 bombIt,
                 assignNewTexts,
                 lambda: self.compareImage("testRawText4"),
                ))

    def testWordsBR(self):
        root = self.loadEmptyScene()
        avg.WordsNode(pos=(1,1), fontsize=12, font="Bitstream Vera Sans", variant="roman",
               text="paragraph 1<br/>paragraph 2", parent=root)
        self.start(True, 
                [lambda: self.compareImage("testWordsBR")])

    def testLetterSpacing(self):
        def setSpacing():
            player.getElementByID("words1").letterspacing=-2
            player.getElementByID("words2").letterspacing=-2
        
        root = self.loadEmptyScene()
        avg.WordsNode(id="words1", pos=(1,1), fontsize=12, font="Bitstream Vera Sans",
                variant="roman",
                text="""normal
                   <span letter_spacing="-2048"> packed</span>
                   <span letter_spacing="2048"> spaced</span>""",
                parent=root)
        avg.WordsNode(id="words2", pos=(1,20), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", letterspacing=2, text="spaced", parent=root)
        self.start(True, 
                (lambda: self.compareImage("testLetterSpacing1"),
                 setSpacing,
                 lambda: self.compareImage("testLetterSpacing2")
                ))
       
    def testPositioning(self):
        def click(pos):
            self.fakeClick(int(pos[0]), int(pos[1]))
           
        def testInside(bInside):
            ok = bInside == self.clicked
            self.clicked = False
            return ok

        def onMouse(event):
            self.clicked = True

        root = self.loadEmptyScene()
        avg.LineNode(pos1=(4, 20.5), pos2=(157, 20.5), color="FF0000", parent=root)
        avg.LineNode(pos1=(4.5, 20.5), pos2=(4.5, 110), color="FF0000", parent=root)
        avg.LineNode(pos1=(156.5, 20.5), pos2=(156.5, 110), color="FF0000", parent=root)
        avg.LineNode(pos1=(80.5, 20.5), pos2=(80.5, 110), color="FF0000", parent=root)
        avg.WordsNode(id="left", pos=(4,20), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", text="Norm", parent=root)
        avg.WordsNode(pos=(45,20), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", text="orm", parent=root)
        avg.WordsNode(pos=(75,20), fontsize=12, font="Bitstream Vera Sans",
                variant="roman", text="ÖÄÜ", parent=root)
        avg.WordsNode(pos=(4,40), fontsize=12, font="Bitstream Vera Sans",
                variant="oblique", text="Jtalic", parent=root)
        avg.WordsNode(id="right", pos=(156,60), fontsize=12, alignment="right",
                font="Bitstream Vera Sans", variant="roman", text="Right-aligned",
                parent=root)
        avg.WordsNode(id="center", pos=(80,80), fontsize=12, alignment="center",
                font="Bitstream Vera Sans", variant="roman", text="Centered",
                parent=root)
        for id in ["left", "center", "right"]:
            player.getElementByID(id).subscribe(avg.Node.CURSOR_DOWN, onMouse)
        self.clicked = False
        leftWidth = player.getElementByID("left").getMediaSize()[0]
        centerWidth = player.getElementByID("center").getMediaSize()[0]
        rightWidth = player.getElementByID("right").getMediaSize()[0]

        self.start(True, 
                (lambda: self.compareImage("testPositioning"),
                 lambda: click((4,20)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((3,20)),
                 lambda: self.assert_(testInside(False)),
                 lambda: click((3+leftWidth,20)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((4+leftWidth,20)),
                 lambda: self.assert_(testInside(False)),
                
                 lambda: click((81-centerWidth/2,80)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((80-centerWidth/2,80)),
                 lambda: self.assert_(testInside(False)),
                 lambda: click((80+centerWidth/2,80)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((81+centerWidth/2,80)),
                 lambda: self.assert_(testInside(False)),

                 lambda: click((156-rightWidth,60)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((155-rightWidth,60)),
                 lambda: self.assert_(testInside(False)),
                 lambda: click((155,60)),
                 lambda: self.assert_(testInside(True)),
                 lambda: click((156,60)),
                 lambda: self.assert_(testInside(False)),
                ))

    def testInvalidColor(self):
        def testColor(col):
            avg.WordsNode(color=col)
        
        def assignValidColor():
            testColor('123456')
        
        def assignInvalidColor1():
            testColor('1234567')
        
        def assignInvalidColor2():
            testColor('xxx')
        
        def assignInvalidColor3():
            testColor('xxxxxx')

        self.loadEmptyScene()
        self.start(True, 
                (self.assertException(assignInvalidColor1),
                 self.assertException(assignInvalidColor2),
                 self.assertException(assignInvalidColor3),
                ))

    def testFontDir(self):
        avg.WordsNode.addFontDir('extrafonts')
        root = self.loadEmptyScene()
        avg.WordsNode(font="testaddfontdir", fontsize=50, text="ABAAA", parent=root)
        self.start(True, 
                (lambda: self.compareImage("testFontDir"),))
    
    def testGetNumLines(self):
        textNode = avg.WordsNode(text="paragraph 1<br/>paragraph 2<br/>paragraph 3")
        self.assertEqual(textNode.getNumLines(), 3)
        textNode.text = ""
        self.assertEqual(textNode.getNumLines(), 0)
    
    def testGetLineExtents(self):        
        textNode = avg.WordsNode(fontsize = 100,
                                font = "Bitstream Vera Sans",
                                text = "bla <br/> blabli <br/> blabliblabla")
        self.assertEqual(textNode.getLineExtents(0), (184,117))
        self.assertEqual(textNode.getLineExtents(1), (303,117))
        
    def testGetCharIndexFromPos(self):
        textNode = avg.WordsNode(fontsize=30,
                      font = "Bitstream Vera Sans",
                      text = "A B C D E F G H Ä Ö Ü ? Ì Á Í Å Ø ∏ ~ ç Ç Å",
                      width = 300)

        for k in (1,2,3,23,42):
            pos = textNode.getGlyphPos(k)
            char = textNode.getCharIndexFromPos(pos)
            self.assertEqual(char, k)
        
    def testGetTextAsDisplayed(self):
        orgText = "A<br/>B C <b>D</b> E F G H <i>Ä</i> Ö Ü ? Ì Á<br/>Í Å Ø ∏ ~ ç Ç Å"
        orgTextWithout = "A\nB C D E F G H Ä Ö Ü ? Ì Á\nÍ Å Ø ∏ ~ ç Ç Å"
        textNode = avg.WordsNode(fontsize=30,
                      font = "Bitstream Vera Sans",
                      text = orgText,
                      width = 300)
        self.assertEqual(orgTextWithout, textNode.getTextAsDisplayed())

    def testSetWidth(self):
        root = self.loadEmptyScene()
        text = "42 " * 42
        textNode = avg.WordsNode(
                parent=root,
                fontsize = 10,
                font = "Bitstream Vera Sans",
                text = text)
        
        def testSize(p1, p2):
            self.assert_(abs(p1.x - p2.x) < 5)
            self.assert_(abs(p1.y - p2.y) < 50)
        
        testSize(textNode.size, avg.Point2D(630,13))
        testSize(textNode.getMediaSize(), avg.Point2D(630,13))
        mediaSize = textNode.getMediaSize()

        def changeSize():
            textNode.width = 50
            testSize(textNode.size, avg.Point2D(50,182))
            testSize(textNode.getMediaSize(), avg.Point2D(45,182))
            self.assertNotEqual(mediaSize, textNode.getMediaSize())

        self.start(True, 
                [lambda: changeSize()])
        
    def testTooWide(self):
        root = self.loadEmptyScene()
        text = "42 " * 42 * 20 
        avg.WordsNode(parent=root, text=text)
        self.assertException(
                lambda: self.start((None, None))
        )

    def testWordsGamma(self):
        
        def setGamma():
            node.aagamma = 4

        root = self.loadEmptyScene()
        for i, gamma in enumerate((2, 1.5, 1)):
            node = avg.WordsNode(pos=(1,i*20), fontsize=12, font="Bitstream Vera Sans", 
                    variant="roman", aagamma=gamma, text="lorem ipsum dolor", 
                    parent=root)
        self.start(True, 
                (lambda: self.compareImage("testWordsGamma1"),
                 setGamma,
                 lambda: self.compareImage("testWordsGamma2"),
                ))


def wordsTestSuite(tests):
    availableTests = (
            "testSimpleWords",
            "testRedrawOnDemand",
            "testFontStyle",
            "testBaseStyle",
            "testGlyphPos",
            "testParaWords",
            "testJustify",
            "testWrapMode",
            "testWordsMask",
            "testHinting",
            "testSpanWords",
            "testDynamicWords",
            "testI18NWords",
            "testRawText",
            "testWordsBR",
            "testLetterSpacing",
            "testPositioning",
            "testInvalidColor",
            "testFontDir",
            "testGetNumLines",
            "testGetLineExtents",
            "testGetCharIndexFromPos",
            "testGetTextAsDisplayed",
            "testSetWidth",
            "testTooWide",
            "testWordsGamma",
            )
    return createAVGTestSuite(availableTests, WordsTestCase, tests)