summaryrefslogtreecommitdiff
path: root/ldtpd/combo_box.py
blob: ca4dc688e41bed798348fde7bfec296999614e3a (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
"""
LDTP v2 Core Combo box.

@author: Eitan Isaacson <eitan@ascender.com>
@author: Nagappan Alagappan <nagappan@gmail.com>
@copyright: Copyright (c) 2009 Eitan Isaacson
@copyright: Copyright (c) 2009-12 Nagappan Alagappan
@license: LGPL

http://ldtp.freedesktop.org

This file may be distributed and/or modified under the terms of the GNU Lesser General
Public License version 2 as published by the Free Software Foundation. This file
is distributed without any warranty; without even the implied warranty of 
merchantability or fitness for a particular purpose.

See 'COPYING' in the source distribution for more information.

Headers in this file shall remain intact.
"""

import pyatspi 
from utils import Utils
from server_exception import LdtpServerException

class LayeredPane(Utils):
    def _lp_selectitem(self, obj, item_name):
        """
        Select layered pane item

        @param obj: Layered pane object
        @type window_name: instance
        @param item_name: Item name to select
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        index = 0
        for child in self._list_objects(obj):
            if child == obj:
                # As the _list_objects gives the current object as well
                # ignore it
                continue
            try:
                texti = child.queryText()
                text = texti.getText(0, texti.characterCount)
            except NotImplementedError:
                text = child.name

            if self._glob_match(item_name, text):
                selectioni = obj.querySelection()
                selectioni.selectChild(index)
                try:
                    try:
                        # If click action is available, then do it
                        self._click_object(child)
                    except:
                        # Incase of exception, just ignore it
                        pass
                finally:
                    return 1
            index += 1
        raise LdtpServerException('Unable to select item')

    def _lp_selectindex(self, obj, item_index):
        """
        Select layered pane item based on index

        @param obj: Layered pane object
        @type window_name: instance
        @param item_index: Item index to select
        @type object_name: integer

        @return: 1 on success.
        @rtype: integer
        """
        selectioni = obj.querySelection()
        try:
            selectioni.selectChild(item_index)
            return 1
        except:
            raise LdtpServerException('Unable to select index')

    def unselectitem(self, window_name, object_name, item_name):
        """
        Select layered pane item

        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @param item_name: Item name to select
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        index = 0
        for child in self._list_objects(obj):
            if child == obj:
                # As the _list_objects gives the current object as well
                # ignore it
                continue
            try:
                texti = child.queryText()
                text = texti.getText(0, texti.characterCount)
            except NotImplementedError:
                text = child.name

            if self._glob_match(item_name, text):
                selectioni = obj.querySelection()
                selectioni.deselectChild(index)
                try:
                    try:
                        # If click action is available, then do it
                        self._click_object(child)
                    except:
                        # Incase of exception, just ignore it
                        pass
                finally:
                    return 1
            index += 1
        raise LdtpServerException('Unable to unselect item')

    def unselectindex(self, window_name, object_name, item_index):
        """
        Select layered pane item based on index

        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string
        @param item_index: Item index to select
        @type object_name: integer

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        selectioni = obj.querySelection()
        try:
            selectioni.deselectChild(item_index)
            return 1
        except:
            raise LdtpServerException('Unable to unselect index')

    def ischildselected(self, window_name, object_name, item_name):
        """
        Is layered pane item selected

        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @param item_name: Item name to select
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name, False)
        self._grab_focus(obj)

        index = 0
        for child in self._list_objects(obj):
            if child == obj:
                # As the _list_objects gives the current object as well
                # ignore it
                continue
            try:
                texti = child.queryText()
                text = texti.getText(0, texti.characterCount)
            except NotImplementedError:
                text = child.name

            if self._glob_match(item_name, text):
                selectioni = obj.querySelection()
                return int(selectioni.isChildSelected(index))
            index += 1
        return 0

    def ischildindexselected(self, window_name, object_name, item_index):
        """
        Is layered pane item selected in the given index

        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string
        @param item_index: Item index to select
        @type object_name: integer

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name, False)
        self._grab_focus(obj)

        selectioni = obj.querySelection()
        try:
            return int(selectioni.isChildSelected(item_index))
        except:
            pass
        return 0

    def selecteditemcount(self, window_name, object_name):
        """
        Selected item count in layered pane
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        selectioni = obj.querySelection()
        return selectioni.nSelectedChildren

    def selectall(self, window_name, object_name):
        """
        Select all item in layered pane
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        selectioni = obj.querySelection()
        try:
            selectioni.selectAll()
            return 1
        except:
            raise LdtpServerException('Unable to select all item')

    def unselectall(self, window_name, object_name):
        """
        Unselect all item in layered pane
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        selectioni = obj.querySelection()
        try:
            selectioni.clearSelection()
            return 1
        except:
            raise LdtpServerException('Unable to select all item')

class ComboBox(Utils, LayeredPane):
    def selectitem(self, window_name, object_name, item_name):
        """
        Select combo box / layered pane item
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string
        @param item_name: Item name to select
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        if obj.getRole() == pyatspi.ROLE_LAYERED_PANE:
            return self._lp_selectitem(obj, item_name)
        elif obj.getRole() == pyatspi.ROLE_LIST:
            # Firefox Preference has ROLE_LIST item as top level object
            child_obj = obj
        else:
            child_obj = self._get_combo_child_object_type(obj)
            if not child_obj:
                raise LdtpServerException('Unable to get combo box children')
        if child_obj.getRole() == pyatspi.ROLE_LIST:
            index = 0
            for child in self._list_objects(child_obj):
                if child == child_obj:
                    # As the _list_objects gives the current object as well
                    # ignore it
                    continue
                try:
                    texti = child.queryText()
                    text = texti.getText(0, texti.characterCount)
                except NotImplementedError:
                    text = child.name

                if self._glob_match(item_name, text):
                    selectioni = child_obj.querySelection()
                    selectioni.selectChild(index)
                    try:
                        try:
                            # In Firefox Preferences: Action to select
                            # list item has empty action
                            # If click action is available, then do it
                            self._click_object(child, action = 'click|')
                        except:
                            # Incase of exception, just ignore it
                            pass
                    finally:
                        return 1
                index += 1
        elif child_obj.getRole() == pyatspi.ROLE_MENU:
            for child in self._list_objects(child_obj):
                if child == child_obj:
                    # As the _list_objects gives the current object as well
                    # ignore it
                    continue
                if self._glob_match(item_name, child.name):
                    self._click_object(child)
                    return 1
                # Get LDTP format accessibile name
                _ldtpize_accessible_name = self._ldtpize_accessible(child)
                # Concat object type and object name
                # ex: 'frmUnsavedDocument1-gedit' for Gedit application
                # frm - Frame, Window title - 'Unsaved Document 1 - gedit'
                _object_name = '%s%s' % (_ldtpize_accessible_name[0],
                                         _ldtpize_accessible_name[1])
                if self._glob_match(item_name, _object_name):
                    self._click_object(child)
                    return 1
        raise LdtpServerException('Unable to select item')

    # Since selectitem and comboselect implementation are same,
    # for backward compatibility let us assign selectitem to comboselect
    comboselect = selectitem

    def selectindex(self, window_name, object_name, item_index):
        """
        Select combo box item / layered pane based on index
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string
        @param item_index: Item index to select
        @type object_name: integer

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        if obj.getRole() == pyatspi.ROLE_LAYERED_PANE:
            self._lp_selectindex(obj, item_index)

        child_obj = self._get_combo_child_object_type(obj)
        if not child_obj:
            raise LdtpServerException('Unable to get combo box children')
        if child_obj.getRole() == pyatspi.ROLE_LIST:
            selectioni = child_obj.querySelection()
            selectioni.selectChild(item_index)
            return 1
        elif child_obj.getRole() == pyatspi.ROLE_MENU:
            index = 0
            for child in self._list_objects(child_obj):
                if child == child_obj:
                    # As the _list_objects gives the current object as well
                    # ignore it
                    continue
                if index == item_index:
                    self._click_object(child)
                    return 1
                index += 1
        raise LdtpServerException('Unable to select item index')

    # Since selectindex and comboselectindex implementation are same,
    # for backward compatibility let us assign selectindex to comboselectindex
    comboselectindex = selectindex

    def getallitem(self, window_name, object_name):
        """
        Get all combo box item

        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: list of string on success.
        @rtype: list
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        child_obj = self._get_combo_child_object_type(obj)
        if not child_obj:
            raise LdtpServerException('Unable to get combo box children')
        item_list = []
        if child_obj.getRole() == pyatspi.ROLE_LIST:
            for child in self._list_objects(child_obj):
                if child == child_obj:
                    # As the _list_objects gives the current object as well
                    # ignore it
                    continue
                try:
                    texti = child.queryText()
                    text = texti.getText(0, texti.characterCount)
                except NotImplementedError:
                    text = child.name

                item_list.append(text)
            return item_list
        elif child_obj.getRole() == pyatspi.ROLE_MENU:
            for child in self._list_objects(child_obj):
                if child == child_obj:
                    # As the _list_objects gives the current object as well
                    # ignore it
                    continue
                if child.name and child.name != '':
                    item_list.append(child.name)
            return item_list
        raise LdtpServerException('Unable to select item')

    def showlist(self, window_name, object_name):
        """
        Show combo box list / menu
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        child_obj = self._get_combo_child_object_type(obj)
        if not child_obj:
            raise LdtpServerException('Unable to get combo box children')

        if not self._check_state(child_obj, pyatspi.STATE_VISIBLE):
            self._click_object(obj, 'press')

        return 1

    def hidelist(self, window_name, object_name):
        """
        Hide combo box list / menu
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        obj = self._get_object(window_name, object_name)
        self._grab_focus(obj)

        child_obj = self._get_combo_child_object_type(obj)
        if not child_obj:
            raise LdtpServerException('Unable to get combo box children')

        if self._check_state(child_obj, pyatspi.STATE_VISIBLE):
            self._click_object(obj, 'press')

        return 1

    def verifydropdown(self, window_name, object_name):
        """
        Verify drop down list / menu poped up
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success 0 on failure.
        @rtype: integer
        """
        try:
            obj = self._get_object(window_name, object_name, False)
            self._grab_focus(obj)

            child_obj = self._get_combo_child_object_type(obj)
            if not child_obj:
                return 0

            if child_obj.getRole() == pyatspi.ROLE_LIST and \
                    self._check_state(obj, pyatspi.STATE_FOCUSABLE):
                return 1
            elif child_obj.getRole() == pyatspi.ROLE_MENU:
                if self._check_state(child_obj, pyatspi.STATE_VISIBLE):
                    return 1
        except:
            pass
        return 0

    def verifyshowlist(self, window_name, object_name):
        """
        Verify drop down list / menu poped up
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success 0 on failure.
        @rtype: integer
        """
        return self.verifydropdown(window_name, object_name)

    def verifyhidelist(self, window_name, object_name):
        """
        Verify list / menu is hidden in combo box
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: 1 on success 0 on failure.
        @rtype: integer
        """
        try:
            obj = self._get_object(window_name, object_name, False)
            self._grab_focus(obj)

            child_obj = self._get_combo_child_object_type(obj)
            if not child_obj:
                return 0

            if child_obj.getRole() == pyatspi.ROLE_LIST and \
                    not self._check_state(obj, pyatspi.STATE_FOCUSABLE):
                return 1
            elif child_obj.getRole() == pyatspi.ROLE_MENU:
                if not self._check_state(obj, pyatspi.STATE_VISIBLE) and \
                        not self._check_state(obj, pyatspi.STATE_SHOWING):
                    return 1
        except:
            pass
        return 0

    def verifyselect(self, window_name, object_name, item_name):
        """
        Verify the item selected in combo box
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string
        @param item_name: Item name to select
        @type object_name: string

        @return: 1 on success.
        @rtype: integer
        """
        try:
            obj = self._get_object(window_name, object_name, False)
            self._grab_focus(obj)

            child_obj = self._get_combo_child_object_type(obj)
            if not child_obj:
                return 0
            if child_obj.getRole() == pyatspi.ROLE_LIST:
                for child in self._list_objects(child_obj):
                    if child == child_obj:
                        # As the _list_objects gives the current object as well
                        # ignore it
                        continue
                    try:
                        texti = child.queryText()
                        text = texti.getText(0, texti.characterCount)
                    except NotImplementedError:
                        text = child.name

                    if self._glob_match(item_name, text):
                        return 1
            elif child_obj.getRole() == pyatspi.ROLE_MENU:
                if self._glob_match(item_name, obj.name):
                    return 1
                # Get LDTP format accessibile name
                _ldtpize_accessible_name = self._ldtpize_accessible(obj)
                # Concat object type and object name
                # ex: 'frmUnsavedDocument1-gedit' for Gedit application
                # frm - Frame, Window title - 'Unsaved Document 1 - gedit'
                _object_name = '%s%s' % (_ldtpize_accessible_name[0],
                                         _ldtpize_accessible_name[1])
                if self._glob_match(item_name, _object_name):
                    return 1
        except:
            pass
        return 0

    def getcombovalue(self, window_name, object_name):
        """
        Get current selected combobox value
        
        @param window_name: Window name to type in, either full name,
        LDTP's name convention, or a Unix glob.
        @type window_name: string
        @param object_name: Object name to type in, either full name,
        LDTP's name convention, or a Unix glob. 
        @type object_name: string

        @return: selected item on success, else LdtpExecutionError on failure.
        @rtype: string
        """
        obj = self._get_object(window_name, object_name,
                               obj_type = ["combo_box"])
        self._grab_focus(obj)

        child_obj = self._get_child_object_type(obj, pyatspi.ROLE_TEXT)
        if child_obj:
            # Combo box object which has children type text
            # If yes, then that's the one selected, just return it
            try:
                texti = child_obj.queryText()
                text = texti.getText(0, texti.characterCount)
            except NotImplementedError:
                text = child_obj.name
            return text
        _ldtpize_accessible_name = self._ldtpize_accessible(obj)
        if not _ldtpize_accessible_name[1] and not _ldtpize_accessible_name[2]:
            raise LdtpServerException("Unable to get currently selected item")
        # Return label by value, which is actually selected one
        # Preference to label_by rather than label
        text=_ldtpize_accessible_name[2] or _ldtpize_accessible_name[1]
        try:
            return unicode(text)
        except UnicodeDecodeError:
            return text