File: test_tree.py

package info (click to toggle)
dogtail 1.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,488 kB
  • sloc: python: 5,970; makefile: 60; sh: 7
file content (929 lines) | stat: -rw-r--r-- 35,168 bytes parent folder | download | duplicates (2)
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import dogtail.config
import dogtail.predicate
import dogtail.tree
import dogtail.rawinput
import pyatspi
import time
import unittest
import os

from gtkdemotest import GtkDemoTest, trap_stdout


"""
Unit tests for the dogtail.Node class

Notes on pyunit (the "unittest" module):

Test classes are written as subclass of unittest.TestCase.
A test is a method of such a class, beginning with the string "test"

unittest.main() will run all such methods.  Use "-v" to get feedback on which tests are being run.
Tests are run in alphabetical order; all failure reports are gathered at the end.

setUp and tearDown are "magic" methods, called before and after each such
test method is run.
"""
__author__ = "Dave Malcolm <dmalcolm@redhat.com>"

dogtail.config.config.logDebugToFile = False


class TestNode(GtkDemoTest):

    """
    Unit tests for the the various synthesized attributes of a Node
    """

    def test_get_bogus(self):
        """
        Getting a non-existant attribute should raise an attribute error
        """
        self.assertRaises(AttributeError, getattr, self.app, "thisIsNotAnAttribute")

    # FIXME: should setattr for a non-existant attr be allowed?

    # 'name' (read-only string):
    def test_get_name(self):
        """
        Node.name of the gtk-demo app should be "gtk-demo"
        """
        print(self.app.name)
        self.assertEqual(self.app.name, 'gtk3-demo')

        self.assertEqual(dogtail.tree.root.name, 'main')

    def test_set_name(self):
        """
        Node.name should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "name", "hello world")

    # 'debugName' (string)
    def test_get_debugName(self):
        self.assertEqual(self.app.debugName, "'gtk3-demo' application")
        self.assertEqual(dogtail.tree.root.debugName, 'root')

    def test_set_debugName(self):
        self.app.debugName = "my application"
        self.assertEqual(self.app.debugName, 'my application')
        dogtail.tree.root.debugName = "my root"
        self.assertEqual(dogtail.tree.root.debugName, 'my root')

    # 'roleName' (read-only string):
    def test_get_roleName(self):
        """
        roleName of the gtk3-demo app should be "application"
        """
        self.assertEqual(self.app.roleName, 'application')

    def test_set_roleName(self):
        """
        Node.roleName should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "roleName", "hello world")

    # 'role' (read-only atspi role enum):
    def test_get_role(self):
        """
        Node.role for a gtk-demo app should be SPI_ROLE_APPLICATION
        """
        self.assertEqual(self.app.role, dogtail.tree.pyatspi.ROLE_APPLICATION)

    def test_set_role(self):
        """
        Node.role should be read-only
        """
        # FIXME should be AttributeError?
        self.assertRaises(RuntimeError, self.app.__setattr__, "role", pyatspi.Atspi.Role(1))

    # 'description' (read-only string):
    def test_get_description(self):
        # FIXME: can we get a more interesting test case here?
        self.assertEqual(self.app.description, "")

    def test_set_description(self):
        """
        Node.description should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "description", "hello world")

    # 'parent' (read-only Node instance):
    def test_get_parent(self):
        # the app has a parent if gnome-shell is used, so parent.parent is a
        # safe choice
        if [x for x in self.app.applications() if x.name == 'gnome-shell']:
            self.assertEqual(self.app.parent.parent, None)
        self.assertEqual(self.app.children[0].parent, self.app)

    def test_set_parent(self):
        """
        Node.parent should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "parent", None)

    # 'children' (read-only list of Node instances):
    def test_get_children(self):
        """
        A fresh gtk-demo app should have a single child: the window.
        """
        kids = self.app.children
        self.assertEqual(len(kids), 1)
        self.assertEqual(kids[0].name, "Application Class")
        self.assertEqual(kids[0].roleName, "frame")

    def test_set_children(self):
        """
        Node.children should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "children", [])

    def test_get_children_with_limit(self):
        haveWarnedAboutChildrenLimit = False
        dogtail.config.config.childrenLimit = 1
        widget = self.app.child(roleName='tree table')
        self.assertEqual(len(widget.children), 1)

    #  combovalue (string):
    def test_get_combo_value(self):
        self.runDemo('Combo Boxes')
        try:
            wnd = self.app.child('Combo boxes', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            wnd = self.app.child('Combo Boxes', roleName='frame', retry=False)
        combo = wnd.child(roleName='combo box')
        self.assertEqual(combo.combovalue, 'dialog-warning')

    def test_get_URI_not_implemented(self):
        with self.assertRaises(NotImplementedError):
            self.app.URI

    # 'text' (string):
    def test_set_text(self):
        """
        Use gtk-demo's text entry example to check that reading and writing
        Node.text works as expected
        """
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        wnd.button('Interactive Dialog').click()
        dlg = self.app.dialog('Interactive Dialog')
        entry1 = dlg.child(label='Entry 1')
        entry2 = dlg.child(roleName='text')

        # Try reading the entries:
        self.assertEqual(entry1.text, "")
        self.assertEqual(entry2.text, "")

        # Set them...
        entry1.text = "hello"
        entry2.text = "world"

        # Ensure that they got set:
        self.assertEqual(entry1.text, "hello")
        self.assertEqual(entry2.text, "world")

        # and try again, searching for them again, to ensure it actually
        # affected the UI:
        self.assertEqual(dlg.child(label='Entry 1').text, "hello")
        self.assertEqual(dlg.child(label='Entry 2').text, "world")

        # Ensure app.text is None
        self.assertEqual(self.app.text, None)

        # Ensure a label's text is read-only as expected:
        # FIXME: this doesn't work; the label has no 'text'; it has a name.  we wan't a readonly text entry
        # label = dlg.child('Entry 1')
        # self.assertRaises(dogtail.tree.ReadOnlyError, label.text.__setattr__, "text", "hello world")

        # FIXME: should we assert that things are logged and delays are added?
        # FIXME: should have a test case involving the complex GtkTextView
        # widget

    def test_text_set_error(self):
        with self.assertRaises(AttributeError):
            self.app.text = 'something'

    def test_caretOffset(self):
        """
        Make sure the caret offset works as expected
        """
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        entry1 = wnd.child(label='Entry 1')
        entry2 = wnd.child(roleName='text')

        # Try reading the entries:
        self.assertEqual(entry1.text, '')
        self.assertEqual(entry2.text, '')

        # Set them...
        s1 = "I just need a sentence"
        s2 = "And maybe a second one to be sure"
        entry1.text = s1
        entry2.text = s2

        # Make sure the caret offset is zero
        self.assertEqual(entry1.caretOffset, 0)
        self.assertEqual(entry2.caretOffset, 0)

        # Set the caret offset to something ridiculous
        entry1.caretOffset = len(s1 * 3)
        entry2.caretOffset = len(s2 * 3)

        # Make sure the caret offset only goes as far as the end of the string
        self.assertEqual(entry1.caretOffset, len(s1))
        self.assertEqual(entry2.caretOffset, len(s2))

        def splitByOffsets(node, string):
            # Verify the equality of node.text and string, word by word.
            # I realize this doesn't really test dogtail itself, but that could
            #   change in the future and I don't want to throw the code away.
            textIface = node.queryText()
            endOffset = -1  # We only set this now so the loop looks nicer
            startOffset = 0
            while startOffset != len(string):
                (text, startOffset, endOffset) = textIface.getTextAtOffset(
                    startOffset, pyatspi.TEXT_BOUNDARY_WORD_START)
                self.assertEqual(startOffset, string.find(text, startOffset, endOffset))
                startOffset = endOffset

        splitByOffsets(entry1, s1)
        splitByOffsets(entry2, s2)

    # 'combovalue' (read/write string):
    def test_comboValue(self):
        self.runDemo('Combo Boxes')
        try:
            wnd = self.app.child('Combo boxes', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            wnd = self.app.child('Combo Boxes', roleName='frame', retry=False)
        combo1 = wnd.child('Items with icons').child(roleName='combo box')
        combo1.combovalue = 'Clear'
        self.assertEqual(combo1.combovalue, 'edit-clear')

    # 'stateSet' (read-only StateSet instance):
    def test_getStateSet(self):
        """
        Node.sensitive should be False for the gtk-demo app node
        """
        self.assertFalse(self.app.sensitive)

    def test_setStateSet(self):
        """
        Node.stateSet should be read-only
        """
        # FIXME should be AttributeError?
        self.assertRaises(RuntimeError, self.app.__setattr__, "states", pyatspi.StateSet())

    # 'relations' (read-only list of atspi.Relation instances):
    def test_get_relations(self):
        # FIXME once relations are used for something other than labels
        pass

    # 'labelee' (read-only list of Node instances):
    def test_get_labelee(self):
        """
        Entry1/2's labelee should be a text widget
        """
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        label = wnd.child(roleName='label', name='Entry 1')
        self.assertEqual(label.labelee.roleName, 'text')
        self.assertEqual(label.labellee.roleName, 'text')

    def test_set_labelee(self):
        """
        Node.labelee should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "labellee", None)

    def test_get_labeler(self):
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        text = wnd.findChildren(dogtail.predicate.GenericPredicate(roleName='text'))[1]
        self.assertEqual(text.labeler.name, 'Entry 1')
        self.assertEqual(text.labeller.name, 'Entry 1')

    def test_set_labeller(self):
        """
        Node.labeller should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "labeller", None)

    # 'sensitive' (read-only boolean):
    def test_get_sensitive(self):
        """
        Node.sensitive should not be set for the gtk-demo app.
        It should be set for the window within the app.
        """
        self.assertFalse(self.app.sensitive)
        self.assertTrue(self.app.children[0].sensitive)

    def test_set_sensitive(self):
        """
        Node.sensitive should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "sensitive", True)

    # 'showing' (read-only boolean):
    def test_get_showing(self):
        """
        Node.showing should not be set for the gtk-demo.  It should be set for the window within the app
        """
        self.assertFalse(self.app.showing)
        self.assertTrue(self.app.children[0].showing)

    def test_set_showing(self):
        """
        Node.showing should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "showing", True)

    # 'visible' (read-only boolean):
    def test_get_visible(self):
        """
        Node.visible reflects a node property if is should be render or not
        """
        self.assertTrue(self.app.child(roleName='frame').visible)
        self.assertFalse(self.app.child(roleName='table column header').visible)

    def test_set_visible(self):
        """
        Node.visible should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "visible", False)

    # 'actions' (read-only list of Action instances):
    def test_get_actions(self):
        """
        Node.actions should be an empty list for the app node
        """
        self.assertEqual(len(self.app.actions), 0)

    def test_set_actions(self):
        """
        Node.actions should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "actions", {})

    # 'extents' (readonly tuple):
    def test_get_extents(self):
        """
        Node.extents should be a 4-tuple for a window, with non-zero size
        """
        return
        (x, y, w, h) = self.app.children[0].extents
        self.assertTrue(w > 0)
        self.assertTrue(h > 0)

    def test_get_extens_wrong(self):
        """
        Some accessible do not specify size nor position
        """
        self.assertIsNone(self.app.extents)

    def test_set_extents(self):
        """
        Node.extents should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "extents", (0, 0, 640, 480))

    # 'position' (readonly tuple):
    def test_get_position(self):
        """
        Node.position should be a 2-tuple for a window
        """
        return
        (x, y) = self.app.children[0].position
        self.assertTrue(isinstance(x, int))
        self.assertTrue(isinstance(y, int))

    def test_get_position_not_implemented(self):
        """
        Some accessible do not specify position (nor size)
        """
        with self.assertRaises(NotImplementedError):
            self.app.position

    def test_set_position(self):
        """
        Node.position should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "position", (0, 0))

    # 'size' (readonly tuple):
    def test_get_size(self):
        """
        Node.size should be a 2-tuple for a window, with non-zero values
        """
        (w, h) = self.app.children[0].size
        self.assertTrue(w > 0)
        self.assertTrue(h > 0)

    def test_get_size_not_implemented(self):
        """
        Some accessible do not specify size (nor position)
        """
        with self.assertRaises(NotImplementedError):
            self.app.size

    def test_set_size(self):
        """
        Node.size should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "size", (640, 480))

    # 'toolkitName' (readonly string):
    def test_get_toolkit(self):
        """
        Node.toolkitName is a read-only string
        """
        self.assertEqual(self.app.toolkitName, "gtk")

    def test_set_toolkit(self):
        """
        Node.toolkit should be read-only
        """
        self.assertRaises(AttributeError, self.app.__setattr__, "toolkitName", "gtk")

    # 'ID'
    def test_get_ID(self):
        """
        Node.id should be numeric
        """
        self.assertEqual(type(self.app.id), type(42))

    def test_set_ID(self):
        """
        Node.id should be read-only
        """
        self.assertRaises(AttributeError, setattr, self.app, "id", 42)

    def test_checked(self):
        self.runDemo("Application Class")
        wnd = dogtail.tree.root.application('gtk3-demo-application')
        wnd.menu("Preferences").select()
        from time import sleep
        sleep(3)
        checkbox = wnd.menu("Preferences").menuItem("Bold")
        status = checkbox.checked
        checkbox.click()
        self.assertEqual(checkbox.checked, not status)
        self.assertEqual(checkbox.isChecked, not status)

    def test_dead(self):
        self.assertFalse(self.app.dead)
        import os
        import signal
        os.kill(self.pid, signal.SIGKILL)
        dogtail.utils.doDelay(5)
        self.assertTrue(self.app.dead)

    def test_dead_empty(self):
        node = dogtail.tree.Node()
        self.assertTrue(node.dead)

    # https://bugzilla.gnome.org/show_bug.cgi?id=710730
    # GError: Method "Contains" with signature "iin" on interface "org.a11y.atspi.Component" doesn't exist
    def test_contains(self):
        return
        child = self.app.children[0]
        position = child.position
        self.assertTrue(child.contains(position[0]+1, position[1]+1))

    def test_childAtPoint(self):
        top = self.app.children[0]
        bottom = top.children[1].children[1]
        position = bottom.position
        actual_acc = top.getChildAtPoint(position[0], position[1])
        self.assertEqual(actual_acc, bottom)

    def test_click(self):
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        wnd.button('Interactive Dialog').click()
        self.assertTrue(self.app.dialog("Interactive Dialog").showing)

    def test_doubleClick(self):
        builder = self.app.child("Builder")
        self.assertEqual(len([x for x in self.app.children if x.name in ['GtkBuilder demo', 'Builder']]), 0)
        builder.doubleClick()
        self.assertEqual(len([x for x in self.app.children if x.name in ['GtkBuilder demo', 'Builder']]), 2)

    def test_point(self):
        self.runDemo('Application Class')
        wnd = dogtail.tree.root.application('gtk3-demo-application')
        wnd.menu("Help").click() # workaround for wayland, first app focus
        wnd.menu("Preferences").point()
        color = wnd.menu("Preferences").menu("Color")
        red = wnd.menu("Preferences").menu("Color").menuItem("Red")
        self.assertFalse(red.showing)
        color.point()
        self.assertTrue(red.showing)

    def test_typeText_nonfucable(self):
        """
        Node.typeText on non-focusable node which is not input-friendly should end in an error
        """
        with self.assertRaises(NotImplementedError):
            self.app.typeText('Something')


class TestSelection(GtkDemoTest):

    def test_tabs(self):
        """
        Tabs in the gtk-demo should be selectable, and be queryable for
        "isSelected", and the results should change as they are selected.
        """
        # Use the Info/Source tabs of gtk-demo:
        info = self.app.child('Info')
        source = self.app.child('Source')

        # Check initial state:
        self.assertTrue(info.isSelected)
        self.assertFalse(source.isSelected)

        # Select other tab:
        source.select()

        # Check new state:
        self.assertFalse(info.isSelected)
        self.assertTrue(source.isSelected)

    def test_iconView(self):
        self.app.child(roleName='tree table').typeText("Icon View")
        self.app.child(roleName='tree table').child("Icon View").click()
        self.app.child(roleName='tree table').child("Icon View").typeText("+")
        self.runDemo('Icon View Basics')
        try:
            wnd = self.app.child('GtkIconView demo', roleName='frame', recursive=False, retry=False)
        except dogtail.tree.SearchError:
            wnd = self.app.child('Icon View Basics', roleName='frame', recursive=False, retry=False)

        pane = wnd.child(roleName="layered pane")
        icons = pane.children

        pane.selectAll()
        self.assertNotIn(False, [x.selected for x in icons])
        pane.select_all()
        self.assertNotIn(False, [x.selected for x in icons])
        pane.select_child(0)
        self.assertTrue(icons[0].selected)
        self.assertNotIn(False, [x.selected for x in icons])


class TestValue(GtkDemoTest):

    def test_get_value(self):
        """
        The scrollbar starts out at position zero.
        """
        sb = self.app.child(roleName='scroll bar')
        self.assertEqual(sb.value, 0)

    def test_set_value(self):
        """
        Ensure that we can set the value of the scrollbar.
        """
        sb = self.app.findChildren(dogtail.predicate.GenericPredicate(roleName='scroll bar'))[1]
        sb.value = 100
        self.assertEqual(sb.value, 100)

    def test_min_value(self):
        """
        Ensure that the minimum value for the scrollbar is correct.
        """
        sb = self.app.findChildren(dogtail.predicate.GenericPredicate(roleName='scroll bar'))[1]
        self.assertEqual(sb.minValue, 0)

    def test_max_value(self):
        sb = self.app.findChildren(dogtail.predicate.GenericPredicate(roleName='scroll bar'))[1]
        self.assertTrue(sb.maxValue > 250)

    def test_min_value_increment(self):
        """
        Ensure that the minimum value increment of the scrollbar is an int.
        """
        sb = self.app.findChildren(dogtail.predicate.GenericPredicate(roleName='scroll bar'))[1]
        self.assertEqual(sb.minValueIncrement, sb.minValueIncrement)


class TestSearching(GtkDemoTest):

    def test_findChildren(self):
        """
        Ensure that there are the correct number of table cells in the list
        of demos.
        """
        pred = dogtail.predicate.GenericPredicate(roleName='table cell')
        tableCells = self.app.findChildren(pred)

        def get_table_cells_recursively(node):
            counter = 0
            for child in node.children:
                if child.roleName == 'table cell':
                    counter += 1
                counter += get_table_cells_recursively(child)
            return counter

        counter = get_table_cells_recursively(self.app)
        self.assertEqual(len(tableCells), counter)

    def test_findChild_lambda(self):
        """
        Ensure that the lambda usage works as expected with Node.findChild
        """
        try:
            self.app.findChild(lambda x: x.roleName == 'page tab list')
        except dogtail.tree.SearchError:
            self.fail("Got a SearchError trying to find the page tab list")

    def test_findChildren2(self):
        """
        Ensure that there are two tabs in the second page tab list.
        """
        pred = dogtail.predicate.GenericPredicate(roleName='page tab list')
        pageTabList = self.app.findChildren(pred)
        pred = dogtail.predicate.GenericPredicate(roleName='page tab')
        # only one page tab list present since gtk3-demo 3.14
        pageTabs = pageTabList[0].findChildren(pred)
        self.assertEqual(len(pageTabs), 5)

    def test_findChildren2_lambda(self):
        """
        Ensure that there ic correct number of tabs in the first page tab list, use lambdas
        """
        pageTabList = self.app.findChildren(lambda x: x.roleName == 'page tab list')
        pageTabs = pageTabList[0].findChildren(lambda x: x.roleName == 'page tab')
        self.assertEqual(len(pageTabs), 5)

    def test_findChildren_lambdas(self):
        """
        Ensure that the lambda usage works as expected in Node.findChildren
        """
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        texts = wnd.findChildren(lambda x: x.roleName == 'text', isLambda=True)
        self.assertEqual(len(texts), 2)
        self.assertEqual(texts[0].roleName, 'text')
        self.assertEqual(texts[1].roleName, 'text')
        texts1 = wnd.findChildren(lambda x: x.roleName == 'text' and x.labeler.name == 'Entry 1', isLambda=True)
        self.assertEqual(len(texts1), 1)
        self.assertEqual(texts1[0].roleName, 'text')
        self.assertEqual(texts1[0].labeler.name, 'Entry 1')
        texts2 = wnd.findChildren(lambda x: x.roleName == 'text' and x.showing, isLambda=True)
        self.assertEqual(len(texts2), 2)
        self.assertEqual(texts2[0].roleName, 'text')
        self.assertTrue(texts2[0].showing)
        self.assertEqual(texts2[1].roleName, 'text')
        self.assertTrue(texts2[1].showing)

    def test_findAncestor(self):
        pred = dogtail.predicate.GenericPredicate(roleName='table cell')
        child = self.app.child("Builder")
        parent = child.findAncestor(pred)
        self.assertIn(child, parent.children)
        pred = dogtail.predicate.GenericPredicate(roleName='frame')
        parent = child.findAncestor(pred)
        self.assertIn(parent, self.app.children)
        # No ancestor found
        self.assertIsNone(parent.findAncestor(pred))

    def test_isChild(self):
        parent = self.app.child(roleName='tree table')
        self.assertTrue(parent.isChild("Builder"))

    def test_getUserVisibleStrings(self):
        child = self.app.child("Builder")
        self.assertEqual(child.getUserVisibleStrings(), ['Builder'])

    def test_satisfies(self):
        pred = dogtail.predicate.GenericPredicate(roleName='table cell')
        builder = self.app.child("Builder")
        self.assertTrue(builder.satisfies(pred))

    def test_absoluteSearchPath(self):
        self.assertEqual(
            str(self.app.getAbsoluteSearchPath()),
            "{/('gtk3-demo' application,False)}")
        builder = self.app.child("Builder")
        self.assertEqual(
            str(builder.getAbsoluteSearchPath()),
            "{/('gtk3-demo' application,False)/('Application Class' window,False)/(child with name='Builder' "
            "roleName='table cell',True)}")

    def test_compare_equal_search_paths(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        self.assertTrue(builder_sp == builder_sp)

    def test_compare_unequal_search_paths_different_length(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        app_sp = self.app.getAbsoluteSearchPath()
        self.assertFalse(builder_sp == app_sp)

    def test_compare_unequal_search_paths_same_length(self):
        builder = self.app.child("Builder")
        assistant = self.app.child("Assistant")
        builder_sp = builder.getAbsoluteSearchPath()
        assistant_sp = assistant.getAbsoluteSearchPath()
        self.assertFalse(builder_sp == assistant_sp)

    def test_get_search_path_length(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        self.assertEqual(builder_sp.length(), 3)

    def test_iterate_search_path(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        self.assertEqual(
            [x[0].makeScriptVariableName() for x in builder_sp],
            ['gtk3DemoApp', 'applicationClassWin', 'builderNode'])

    def test_make_script_method_call_from_search_path(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        self.assertEqual(
            builder_sp.makeScriptMethodCall(),
            ".application('gtk3-demo').window('Application Class').child( name='Builder' roleName='table cell')")

    def test_get_relative_search_path_for_path(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        frame_sp = self.app.window("Application Class").getAbsoluteSearchPath()
        # FIXME: Should be "child( name="Widget (double click for demo)" roleName=\'page tab\').child( name="Builder""
        #                  " roleName=\'table cell\')"
        self.assertIsNone(builder_sp.getRelativePath(frame_sp))

    def test_get_prefix_for_search_path(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        self.assertEqual(
            str(builder_sp.getPrefix(1)),
            "{/('gtk3-demo' application,False)}")

    def test_get_predicate(self):
        builder = self.app.child("Builder")
        builder_sp = builder.getAbsoluteSearchPath()
        pred = builder_sp.getPredicate(0)
        self.assertEqual(type(pred), dogtail.predicate.IsAnApplicationNamed)
        self.assertEqual(str(pred.appName), "'gtk3-demo'")

    def test_getRelativeSearch_app(self):
        relpath = self.app.getRelativeSearch()
        self.assertEqual(str(relpath[0]), '[desktop frame | main]')
        self.assertEqual(relpath[1].name.untranslatedString, 'gtk3-demo')
        self.assertFalse(relpath[2])

    def test_getRelativeSearch_widget(self):
        builder = self.app.child("Builder")
        relpath = builder.getRelativeSearch()
        self.assertEqual(str(relpath[0]), '[frame | Application Class]')
        self.assertEqual(relpath[1].describeSearchResult(), "child with name='Builder' roleName='table cell'")
        self.assertTrue(relpath[2])

    def test_findChildren_non_recursive(self):
        dogtail.rawinput.keyCombo("<End>")
        self.app.child(roleName='tree table').child("Tree View").click()
        self.app.child(roleName='tree table').child("Tree View").typeText("+")
        self.runDemo('Tree Store')
        try:
            wnd = self.app.child('Card planning sheet', roleName='frame', retry=False, recursive=False)
        except dogtail.tree.SearchError:
            wnd = self.app.child('Tree Store', roleName='frame', retry=False, recursive=False)
        table = wnd.child(roleName='tree table')
        pred = dogtail.predicate.GenericPredicate(roleName='table cell')
        dogtail.config.config.childrenLimit = 10000
        cells = table.findChildren(pred, recursive=False)
        direct_cells = [cell for cell in table.children if cell.roleName == 'table cell']
        self.assertEqual(len(cells), len(direct_cells))

    def test_find_by_shortcut(self):
        self.runDemo('Builder')
        try:
            wnd = self.app.child('GtkBuilder demo', roleName='frame', recursive=False, retry=False)
        except dogtail.tree.SearchError:
            wnd = self.app.child('Builder', roleName='frame', recursive=False, retry=False)

        self.assertIsNotNone(wnd.menu("File"))
        self.assertIsNotNone(wnd.menu("File").menuItem("New"))
        self.assertIsNotNone(wnd.button("Save"))
        self.assertIsNotNone(wnd.childNamed("File"))
        self.assertIsNotNone(self.app.tab("Info"))

    def test_find_by_shortcut2(self):
        try:
            self.runDemo('Dialog and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs', roleName='frame', retry=False)
        except dogtail.tree.SearchError:
            self.runDemo('Dialogs and Message Boxes', retry=False)
            wnd = self.app.child('Dialogs and Message Boxes', roleName='frame', retry=False)
        self.assertIsNotNone(wnd.childLabelled("Entry 1"))
        self.assertIsNotNone(wnd.button("Message Dialog"))


# A painful point of collision between strings in python2 and python3!
@unittest.skipIf(os.system('ls /usr/bin/gedit') != 0, "Skipping, need gedit")
class TestUnicodeNames(unittest.TestCase):
    def setUp(self):
        import dogtail.config
        dogtail.config.config.logDebugToStdOut = True
        dogtail.config.config.logDebugToFile = True
        dogtail.config.config.logDebugToStdOut = True
        dogtail.config.config.debugSearching = False
        dogtail.config.config.searchCutoffCount = 3
        import dogtail.utils
        self.pid = dogtail.utils.run('gedit')
        self.ver = os.popen('gedit -V').read().split()[-1]
        try:
            self.app = dogtail.tree.root.application('org.gnome.gedit')
        except Exception:
            self.app = dogtail.tree.root.application('gedit')

    def test_unicode_char_in_name(self):
        self.app.child('Menu', roleName='toggle button').click()
        unicode_button = None
        unicode_button = self.app.child(name=u'Find and Replace…', roleName='button')
        assert unicode_button is not None

    def test_unicode_char_in_name_click(self):
        self.app.child('Menu', roleName='toggle button').click()
        unicode_button = self.app.child(name=u'Find and Replace…', roleName='button')
        unicode_button.click()
        dialog = None
        t_ver = tuple(map(int, (self.ver.split("."))))
        chooser_name = u'Find and Replace'
        try:
            dialog = self.app.child(name=chooser_name, roleName='dialog')
        except dogtail.tree.SearchError:
            self.fail()
        assert dialog is not None

    def test_unicode_logging_nocrash(self):
        try:
            self.app.child(name='…Other stuff…', roleName='button')
            self.fail()
        except dogtail.tree.SearchError:
            pass

    def tearDown(self):
        import signal
        os.kill(self.pid, signal.SIGKILL)
        os.system('killall gedit > /dev/null 2>&1')
        # Sleep just enough to let the app actually die.
        # AT-SPI doesn't like being hammered too fast.
        time.sleep(0.5)

class TestDump(GtkDemoTest):

    def test_dump_to_stdout(self):
        child = self.app.child('Source')
        output = trap_stdout(child.dump)
        self.assertEqual(
            output,
            """[page tab | Source]
 [scroll pane | ]
  [text | ]
  [scroll bar | ]
  [scroll bar | ]""")

    def test_dump_with_actions(self):
        child = self.app.child('Builder', roleName='table cell')
        output = trap_stdout(child.dump)
        self.assertEqual(
            output,
            """[table cell | Builder]
 [action | activate |  ]
 [action | edit |  ]
 [action | expand or contract |  ]""")