~ubuntu-branches/ubuntu/lucid/ldtp/lucid

« back to all changes in this revision

Viewing changes to ldtpd/core.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2010-01-27 17:57:45 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100127175745-4g7my2y5c17zhkop
Tags: 2.0.2-0ubuntu1
* New upstream release (Fixes LP: #516248):
  + LDTPv2 is a complete rewrite of LDTPv1 in Python
  + LTFX is completely removed in LDTP v2 in favor of wnck

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
LDTP v2 Core.
 
3
 
 
4
@author: Eitan Isaacson <eitan@ascender.com>
 
5
@author: Nagappan Alagappan <nagappan@gmail.com>
 
6
@copyright: Copyright (c) 2009 Eitan Isaacson
 
7
@copyright: Copyright (c) 2009 Nagappan Alagappan
 
8
@license: LGPL
 
9
 
 
10
http://ldtp.freedesktop.org
 
11
 
 
12
This file may be distributed and/or modified under the terms of the GNU General
 
13
Public License version 2 as published by the Free Software Foundation. This file
 
14
is distributed without any warranty; without even the implied warranty of 
 
15
merchantability or fitness for a particular purpose.
 
16
 
 
17
See "COPYING" in the source distribution for more information.
 
18
 
 
19
Headers in this file shall remain intact.
 
20
'''
 
21
 
 
22
from pyatspi import findDescendant, Registry
 
23
import locale
 
24
import subprocess
 
25
from utils import Utils
 
26
from constants import abbreviated_roles
 
27
from waiters import ObjectExistsWaiter, GuiExistsWaiter, \
 
28
    GuiNotExistsWaiter, ObjectNotExistsWaiter, NullWaiter, \
 
29
    MaximizeWindow, MinimizeWindow, UnmaximizeWindow, UnminimizeWindow, \
 
30
    ActivateWindow, CloseWindow
 
31
from server_exception import LdtpServerException
 
32
import os
 
33
import re
 
34
import time
 
35
import pyatspi
 
36
import traceback
 
37
from fnmatch import translate as glob_trans
 
38
 
 
39
from menu import Menu
 
40
from text import Text
 
41
from mouse import Mouse
 
42
from table import Table
 
43
from value import Value
 
44
from generic import Generic
 
45
from combo_box import ComboBox
 
46
from page_tab_list import PageTabList
 
47
 
 
48
class Ldtpd(Utils, ComboBox, Table, Menu, PageTabList,
 
49
            Text, Mouse, Generic, Value):
 
50
    '''
 
51
    Core LDTP class.
 
52
    '''
 
53
    def __init__(self):
 
54
        Utils.__init__(self)
 
55
        self._states = {}
 
56
        self._get_all_state_names()
 
57
        # Window up time and onwindowcreate events
 
58
        self._events = ["window:create", "window:destroy"]
 
59
        # User registered events
 
60
        self._registered_events = []
 
61
        pyatspi.Registry.registerEventListener(self._event_cb, *self._events)
 
62
 
 
63
    def __del__(self):
 
64
        pyatspi.Registry.deregisterEventListener(self._event_cb, *self._events)
 
65
        pyatspi.Registry.deregisterEventListener(self._registered_event_cb,
 
66
                                                 *self._registered_events)
 
67
 
 
68
    def _registered_event_cb(self, event):
 
69
        if event and event.source and event.type:
 
70
            abbrev_role, abbrev_name = self._ldtpize_accessible(event.source)
 
71
            window_name = u'%s%s' % (abbrev_role, abbrev_name)
 
72
            self._callback_event.append(u"%s-%s" % (event.type, window_name))
 
73
 
 
74
    def _event_cb(self, event):
 
75
        if event and event.type == "window:create":
 
76
            for window in self._callback:
 
77
                if event and event.source and window and \
 
78
                        self._match_name_to_acc(window, event.source):
 
79
                    self._callback_event.append(u"onwindowcreate-%s" % window)
 
80
            abbrev_role, abbrev_name = self._ldtpize_accessible(event.source)
 
81
            win_name = u'%s%s' % (abbrev_role, abbrev_name)
 
82
            self._window_uptime[win_name] = [event.source_name,
 
83
                                             time.strftime("%Y %m %d %H %M %S")]
 
84
        elif event and event.type == "window:destroy":
 
85
            abbrev_role, abbrev_name = self._ldtpize_accessible(event.source)
 
86
            win_name = u'%s%s' % (abbrev_role, abbrev_name)
 
87
            if win_name in self._window_uptime:
 
88
                self._window_uptime[win_name].append( \
 
89
                    time.strftime("%Y %m %d %H %M %S"))
 
90
 
 
91
    def getapplist(self):
 
92
        '''
 
93
        Get all accessibility application name that are currently running
 
94
        
 
95
        @return: list of appliction name of string type on success.
 
96
        @rtype: list
 
97
        '''
 
98
        app_list = []
 
99
        for app in self._list_apps():
 
100
            if app.name != '<unknown>':
 
101
                app_list.append(app.name)
 
102
        return app_list
 
103
 
 
104
    def getwindowlist(self):
 
105
        '''
 
106
        Get all accessibility window that are currently open
 
107
        
 
108
        @return: list of window names in LDTP format of string type on success.
 
109
        @rtype: list
 
110
        '''
 
111
        window_list = []
 
112
        window_type = {}
 
113
        for gui in self._list_guis():
 
114
            window_name = self._ldtpize_accessible(gui)
 
115
            if window_name[1] == '':
 
116
                if window_name[0] in window_type:
 
117
                    window_type[window_name[0]] += 1
 
118
                else:
 
119
                    window_type[window_name[0]] = 0
 
120
                tmp_name = '%d' % window_type[window_name[0]]
 
121
            else:
 
122
                tmp_name = window_name[1]
 
123
            w_name = window_name = '%s%s' % (window_name[0], tmp_name)
 
124
            index = 1
 
125
            while window_name in window_list:
 
126
                window_name = '%s%d' % (w_name, index)
 
127
                index += 1
 
128
            window_list.append(window_name)
 
129
        return window_list
 
130
 
 
131
    def isalive(self):
 
132
        return True
 
133
 
 
134
    def _get_all_state_names(self):
 
135
        """
 
136
        This is used by client internally to populate all states
 
137
        Create a dictionary
 
138
        """
 
139
        for state in pyatspi.STATE_VALUE_TO_NAME.keys():
 
140
            self._states[state.__repr__()] = state
 
141
        return self._states
 
142
 
 
143
    def launchapp(self, cmd, args=[], delay = 5, env = 1):
 
144
        '''
 
145
        Launch application.
 
146
 
 
147
        @param cmdline: Command line string to execute.
 
148
        @type cmdline: string
 
149
        @param args: Arguments to the application
 
150
        @type args: list
 
151
        @param delay: Delay after the application is launched
 
152
        @type delay: int
 
153
        @param env: GNOME accessibility environment to be set or not
 
154
        @type env: int
 
155
 
 
156
        @return: PID of new process
 
157
        @rtype: integer
 
158
 
 
159
        @raise LdtpServerException: When command fails
 
160
        '''
 
161
        os.environ['NO_GAIL'] = '0'
 
162
        os.environ['NO_AT_BRIDGE'] = '0'
 
163
        if env:
 
164
            os.environ['GTK_MODULES'] = 'gail:atk-bridge'
 
165
            os.environ['GNOME_ACCESSIBILITY'] = '1'
 
166
        try:
 
167
            process = subprocess.Popen([cmd]+args, close_fds = True)
 
168
            # Let us wait so that the application launches
 
169
            try:
 
170
                time.sleep(int(delay))
 
171
            except ValueError:
 
172
                time.sleep(5)
 
173
        except Exception, e:
 
174
            raise LdtpServerException(str(e))
 
175
        os.environ['NO_GAIL'] = '1'
 
176
        os.environ['NO_AT_BRIDGE'] = '1'
 
177
        return process.pid
 
178
 
 
179
    def poll_events(self):
 
180
        '''
 
181
        Poll for any registered events or window create events
 
182
 
 
183
        @return: window name
 
184
        @rtype: string
 
185
        '''
 
186
 
 
187
        if not self._callback_event:
 
188
            return ''
 
189
 
 
190
        return self._callback_event.pop()
 
191
 
 
192
    def windowuptime(self, window_name):
 
193
        '''
 
194
        Get window uptime
 
195
 
 
196
        @param window_name: Window name to look for, either full name,
 
197
        LDTP's name convention, or a Unix glob.
 
198
        @type window_name: string
 
199
 
 
200
        @return: "starttime - endtime" in string format
 
201
        ex: '2010 01 12 14 21 13 - 2010 01 12 14 23 05'
 
202
        @rtype: string
 
203
        '''
 
204
 
 
205
        if window_name in self._window_uptime and \
 
206
                len(self._window_uptime[window_name]) == 3:
 
207
            return '%s-%s' % (self._window_uptime[window_name][1],
 
208
                                self._window_uptime[window_name][2])
 
209
        for window in self._window_uptime:
 
210
            if re.match(glob_trans(window_name), window,
 
211
                        re.M | re.U | re.L) or \
 
212
                        re.match(glob_trans(window_name),
 
213
                                 self._window_uptime[window][0],
 
214
                                 re.M | re.U | re.L):
 
215
                        return '%s-%s' % (self._window_uptime[window][1],
 
216
                                          self._window_uptime[window][2])
 
217
        return ''
 
218
 
 
219
    def onwindowcreate(self, window_name):
 
220
        '''
 
221
        Raise event on window create
 
222
 
 
223
        @param window_name: Window name to look for, either full name,
 
224
        LDTP's name convention, or a Unix glob.
 
225
        @type window_name: string
 
226
 
 
227
        @return: 1 if registration was successful, 0 if not.
 
228
        @rtype: integer
 
229
        '''
 
230
 
 
231
        self._callback[window_name] = window_name
 
232
 
 
233
        return 1
 
234
 
 
235
    def removecallback(self, window_name):
 
236
        '''
 
237
        Remove callback of window create
 
238
 
 
239
        @param window_name: Window name to look for, either full name,
 
240
        LDTP's name convention, or a Unix glob.
 
241
        @type window_name: string
 
242
 
 
243
        @return: 1 if remove was successful, 0 if not.
 
244
        @rtype: integer
 
245
        '''
 
246
 
 
247
        if window_name in self._callback:
 
248
            del self._callback[window_name]
 
249
 
 
250
        return 1
 
251
 
 
252
    def registerevent(self, event_name):
 
253
        '''
 
254
        Register at-spi event
 
255
 
 
256
        @param event_name: Event name in at-spi format.
 
257
        @type event_name: string
 
258
 
 
259
        @return: 1 if registration was successful, 0 if not.
 
260
        @rtype: integer
 
261
        '''
 
262
 
 
263
        pyatspi.Registry.deregisterEventListener( \
 
264
            self._registered_event_cb, *self._registered_events)
 
265
        self._registered_events.append(event_name)
 
266
        pyatspi.Registry.registerEventListener(self._registered_event_cb,
 
267
                                               *self._registered_events)
 
268
 
 
269
        return 1
 
270
 
 
271
    def removeevent(self, event_name):
 
272
        '''
 
273
        Remove callback of registered event
 
274
 
 
275
        @param event_name: Event name in at-spi format.
 
276
        @type event_name: string
 
277
 
 
278
        @return: 1 if remove was successful, 0 if not.
 
279
        @rtype: integer
 
280
        '''
 
281
 
 
282
        for event in self._registered_events:
 
283
            if event_name == event:
 
284
                pyatspi.Registry.deregisterEventListener( \
 
285
                    self._registered_event_cb, *self._registered_events)
 
286
                self._registered_events.remove(event)
 
287
                pyatspi.Registry.registerEventListener( \
 
288
                    self._registered_event_cb, *self._registered_events)
 
289
                break
 
290
 
 
291
        return 1
 
292
 
 
293
    def objectexist(self, window_name, object_name):
 
294
        '''
 
295
        Checks whether a window or component exists.
 
296
        
 
297
        @param window_name: Window name to look for, either full name,
 
298
        LDTP's name convention, or a Unix glob.
 
299
        @type window_name: string
 
300
        @param object_name: Object name to look for, either full name,
 
301
        LDTP's name convention, or a Unix glob.
 
302
        @type object_name: string
 
303
 
 
304
        @return: 1 if GUI was found, 0 if not.
 
305
        @rtype: integer
 
306
        '''
 
307
        return self.guiexist(window_name, object_name)
 
308
 
 
309
    def maximizewindow(self, window_name = None):
 
310
        '''
 
311
        Maximize a window using wnck
 
312
        
 
313
        @param window_name: Window name to look for, either full name,
 
314
        LDTP's name convention, or a Unix glob.
 
315
        @type window_name: string
 
316
 
 
317
        @return: 1 if window maximized, 0 if not.
 
318
        @rtype: integer
 
319
        '''
 
320
        waiter = MaximizeWindow(window_name)
 
321
 
 
322
        return int(waiter.run())
 
323
 
 
324
    def minimizewindow(self, window_name = None):
 
325
        '''
 
326
        Minimize a window using wnck
 
327
        
 
328
        @param window_name: Window name to look for, either full name,
 
329
        LDTP's name convention, or a Unix glob.
 
330
        @type window_name: string
 
331
 
 
332
        @return: 1 if window minimized, 0 if not.
 
333
        @rtype: integer
 
334
        '''
 
335
        waiter = MinimizeWindow(window_name)
 
336
 
 
337
        return int(waiter.run())
 
338
 
 
339
    def unmaximizewindow(self, window_name = None):
 
340
        '''
 
341
        Unmaximize a window using wnck
 
342
        
 
343
        @param window_name: Window name to look for, either full name,
 
344
        LDTP's name convention, or a Unix glob.
 
345
        @type window_name: string
 
346
 
 
347
        @return: 1 if window unmaximized, 0 if not.
 
348
        @rtype: integer
 
349
        '''
 
350
        waiter = UnmaximizeWindow(window_name)
 
351
 
 
352
        return int(waiter.run())
 
353
 
 
354
    def unminimizewindow(self, window_name = None):
 
355
        '''
 
356
        Unminimize a window using wnck
 
357
        
 
358
        @param window_name: Window name to look for, either full name,
 
359
        LDTP's name convention, or a Unix glob.
 
360
        @type window_name: string
 
361
 
 
362
        @return: 1 if window unminimized, 0 if not.
 
363
        @rtype: integer
 
364
        '''
 
365
        waiter = UnminimizeWindow(window_name)
 
366
 
 
367
        return int(waiter.run())
 
368
 
 
369
    def activatewindow(self, window_name):
 
370
        '''
 
371
        Activate a window using wnck
 
372
        
 
373
        @param window_name: Window name to look for, either full name,
 
374
        LDTP's name convention, or a Unix glob.
 
375
        @type window_name: string
 
376
 
 
377
        @return: 1 if window unminimized, 0 if not.
 
378
        @rtype: integer
 
379
        '''
 
380
        waiter = ActivateWindow(window_name)
 
381
 
 
382
        return int(waiter.run())
 
383
 
 
384
    def closewindow(self, window_name = None):
 
385
        '''
 
386
        Close a window using wnck
 
387
        
 
388
        @param window_name: Window name to look for, either full name,
 
389
        LDTP's name convention, or a Unix glob.
 
390
        @type window_name: string
 
391
 
 
392
        @return: 1 if window unminimized, 0 if not.
 
393
        @rtype: integer
 
394
        '''
 
395
        waiter = CloseWindow(window_name)
 
396
 
 
397
        return int(waiter.run())
 
398
 
 
399
    def guiexist(self, window_name, object_name=''):
 
400
        '''
 
401
        Checks whether a window or component exists.
 
402
        
 
403
        @param window_name: Window name to look for, either full name,
 
404
        LDTP's name convention, or a Unix glob.
 
405
        @type window_name: string
 
406
        @param object_name: Object name to look for, either full name,
 
407
        LDTP's name convention, or a Unix glob.
 
408
        @type object_name: string
 
409
 
 
410
        @return: 1 if GUI was found, 0 if not.
 
411
        @rtype: integer
 
412
        '''
 
413
        if object_name:
 
414
            waiter = ObjectExistsWaiter(window_name, object_name, 0)
 
415
        else:
 
416
            waiter = GuiExistsWaiter(window_name, 0)
 
417
 
 
418
        return int(waiter.run())
 
419
 
 
420
    def waittillguiexist(self, window_name, object_name='', guiTimeOut=30):
 
421
        '''
 
422
        Wait till a window or component exists.
 
423
        
 
424
        @param window_name: Window name to look for, either full name,
 
425
        LDTP's name convention, or a Unix glob.
 
426
        @type window_name: string
 
427
        @param object_name: Object name to look for, either full name,
 
428
        LDTP's name convention, or a Unix glob.
 
429
        @type object_name: string
 
430
        @param guiTimeOut: Wait timeout in seconds
 
431
        @type guiTimeOut: integer
 
432
 
 
433
        @return: 1 if GUI was found, 0 if not.
 
434
        @rtype: integer
 
435
        '''
 
436
        if object_name:
 
437
            waiter = ObjectExistsWaiter(window_name, object_name, guiTimeOut)
 
438
        else:
 
439
            waiter = GuiExistsWaiter(window_name, guiTimeOut)
 
440
 
 
441
        return int(waiter.run())
 
442
 
 
443
    def waittillguinotexist(self, window_name, object_name='', guiTimeOut=30):
 
444
        '''
 
445
        Wait till a window does not exist.
 
446
        
 
447
        @param window_name: Window name to look for, either full name,
 
448
        LDTP's name convention, or a Unix glob.
 
449
        @type window_name: string
 
450
        @param object_name: Object name to look for, either full name,
 
451
        LDTP's name convention, or a Unix glob.
 
452
        @type object_name: string
 
453
        @param guiTimeOut: Wait timeout in seconds
 
454
        @type guiTimeOut: integer
 
455
 
 
456
        @return: 1 if GUI has gone away, 0 if not.
 
457
        @rtype: integer
 
458
        '''
 
459
        if object_name:
 
460
            waiter = \
 
461
                ObjectNotExistsWaiter(window_name, object_name, guiTimeOut)
 
462
        else:
 
463
            waiter = GuiNotExistsWaiter(window_name, guiTimeOut)
 
464
 
 
465
        return int(waiter.run())
 
466
 
 
467
    def getobjectsize(self, window_name, object_name):
 
468
        '''
 
469
        Get object size
 
470
        
 
471
        @param window_name: Window name to look for, either full name,
 
472
        LDTP's name convention, or a Unix glob.
 
473
        @type window_name: string
 
474
        @param object_name: Object name to look for, either full name,
 
475
        LDTP's name convention, or a Unix glob. Or menu heirarchy
 
476
        @type object_name: string
 
477
 
 
478
        @return: x, y, width, height on success.
 
479
        @rtype: list
 
480
        '''
 
481
        obj = self._get_object(window_name, object_name)
 
482
 
 
483
        _coordinates = self._get_size(obj)
 
484
        return [_coordinates.x, _coordinates.y, \
 
485
                    _coordinates.width, _coordinates.height]
 
486
 
 
487
    def getallstates(self, window_name, object_name):
 
488
        '''
 
489
        Get all states of given object
 
490
        
 
491
        @param window_name: Window name to look for, either full name,
 
492
        LDTP's name convention, or a Unix glob.
 
493
        @type window_name: string
 
494
        @param object_name: Object name to look for, either full name,
 
495
        LDTP's name convention, or a Unix glob. 
 
496
        @type object_name: string
 
497
 
 
498
        @return: list of integers on success.
 
499
        @rtype: list
 
500
        '''
 
501
        if re.search(';', object_name):
 
502
            obj = self._get_menu_hierarchy(window_name, object_name)
 
503
        else:
 
504
            obj = self._get_object(window_name, object_name)
 
505
 
 
506
        _state = obj.getState()
 
507
        _current_state = _state.getStates()
 
508
        _obj_states = []
 
509
        for state in _current_state:
 
510
            _obj_states.append(state.real)
 
511
        _state.unref()
 
512
        return _obj_states
 
513
 
 
514
    def hasstate(self, window_name, object_name, state):
 
515
        '''
 
516
        has state
 
517
        
 
518
        @param window_name: Window name to look for, either full name,
 
519
        LDTP's name convention, or a Unix glob.
 
520
        @type window_name: string
 
521
        @param object_name: Object name to look for, either full name,
 
522
        LDTP's name convention, or a Unix glob. 
 
523
        @type object_name: string
 
524
 
 
525
        @return: 1 on success.
 
526
        @rtype: integer
 
527
        '''
 
528
        try:
 
529
            if re.search(';', object_name):
 
530
                obj = self._get_menu_hierarchy(window_name, object_name)
 
531
            else:
 
532
                obj = self._get_object(window_name, object_name)
 
533
 
 
534
            _state = obj.getState()
 
535
            _obj_state = _state.getStates()
 
536
            state = 'STATE_%s' % state.upper()
 
537
            if state in self._states and \
 
538
                    self._states[state] in _obj_state:
 
539
                return 1
 
540
        except:
 
541
            pass
 
542
        return 0
 
543
 
 
544
    def grabfocus(self, window_name, object_name):
 
545
        '''
 
546
        Grab focus.
 
547
        
 
548
        @param window_name: Window name to look for, either full name,
 
549
        LDTP's name convention, or a Unix glob.
 
550
        @type window_name: string
 
551
        @param object_name: Object name to look for, either full name,
 
552
        LDTP's name convention, or a Unix glob. 
 
553
        @type object_name: string
 
554
 
 
555
        @return: 1 on success.
 
556
        @rtype: integer
 
557
        '''
 
558
        obj = self._get_object(window_name, object_name)
 
559
        self._grab_focus(obj)
 
560
 
 
561
        return 1
 
562
 
 
563
    def click(self, window_name, object_name):
 
564
        '''
 
565
        Click item.
 
566
        
 
567
        @param window_name: Window name to look for, either full name,
 
568
        LDTP's name convention, or a Unix glob.
 
569
        @type window_name: string
 
570
        @param object_name: Object name to look for, either full name,
 
571
        LDTP's name convention, or a Unix glob. 
 
572
        @type object_name: string
 
573
 
 
574
        @return: 1 on success.
 
575
        @rtype: integer
 
576
        '''
 
577
        obj = self._get_object(window_name, object_name)
 
578
        self._grab_focus(obj)
 
579
 
 
580
        if obj.getRole() == pyatspi.ROLE_TOGGLE_BUTTON:
 
581
            self._click_object(obj, '(click|activate)')
 
582
        elif obj.getRole() == pyatspi.ROLE_COMBO_BOX:
 
583
            self._click_object(obj, '(click|press)')
 
584
        else:
 
585
            self._click_object(obj)
 
586
 
 
587
        return 1
 
588
    
 
589
    def press(self, window_name, object_name):
 
590
        '''
 
591
        Press item.
 
592
        
 
593
        @param window_name: Window name to look for, either full name,
 
594
        LDTP's name convention, or a Unix glob.
 
595
        @type window_name: string
 
596
        @param object_name: Object name to look for, either full name,
 
597
        LDTP's name convention, or a Unix glob. 
 
598
        @type object_name: string
 
599
 
 
600
        @return: 1 on success.
 
601
        @rtype: integer
 
602
        '''
 
603
        obj = self._get_object(window_name, object_name)
 
604
        self._grab_focus(obj)
 
605
 
 
606
        self._click_object(obj, 'press')
 
607
 
 
608
        return 1
 
609
    
 
610
    def check(self, window_name, object_name):
 
611
        '''
 
612
        Check item.
 
613
        
 
614
        @param window_name: Window name to look for, either full name,
 
615
        LDTP's name convention, or a Unix glob.
 
616
        @type window_name: string
 
617
        @param object_name: Object name to look for, either full name,
 
618
        LDTP's name convention, or a Unix glob. 
 
619
        @type object_name: string
 
620
 
 
621
        @return: 1 on success.
 
622
        @rtype: integer
 
623
        '''
 
624
        obj = self._get_object(window_name, object_name)
 
625
        self._grab_focus(obj)
 
626
 
 
627
        if self._check_state(obj, pyatspi.STATE_CHECKED) == False:
 
628
            self._click_object(obj)
 
629
 
 
630
        return 1
 
631
 
 
632
    def uncheck(self, window_name, object_name):
 
633
        '''
 
634
        Uncheck item.
 
635
        
 
636
        @param window_name: Window name to look for, either full name,
 
637
        LDTP's name convention, or a Unix glob.
 
638
        @type window_name: string
 
639
        @param object_name: Object name to look for, either full name,
 
640
        LDTP's name convention, or a Unix glob. 
 
641
        @type object_name: string
 
642
 
 
643
        @return: 1 on success.
 
644
        @rtype: integer
 
645
        '''
 
646
        obj = self._get_object(window_name, object_name)
 
647
        self._grab_focus(obj)
 
648
 
 
649
        if self._check_state(obj, pyatspi.STATE_CHECKED):
 
650
            self._click_object(obj)
 
651
 
 
652
        return 1
 
653
    
 
654
    def verifytoggled(self, window_name, object_name):
 
655
        '''
 
656
        Verify toggle item toggled.
 
657
        
 
658
        @param window_name: Window name to look for, either full name,
 
659
        LDTP's name convention, or a Unix glob.
 
660
        @type window_name: string
 
661
        @param object_name: Object name to look for, either full name,
 
662
        LDTP's name convention, or a Unix glob. 
 
663
        @type object_name: string
 
664
 
 
665
        @return: 1 on success 0 on failure.
 
666
        @rtype: integer
 
667
        '''
 
668
        return self.verifycheck(window_name, object_name)
 
669
 
 
670
    def verifycheck(self, window_name, object_name):
 
671
        '''
 
672
        Verify check item.
 
673
        
 
674
        @param window_name: Window name to look for, either full name,
 
675
        LDTP's name convention, or a Unix glob.
 
676
        @type window_name: string
 
677
        @param object_name: Object name to look for, either full name,
 
678
        LDTP's name convention, or a Unix glob. 
 
679
        @type object_name: string
 
680
 
 
681
        @return: 1 on success 0 on failure.
 
682
        @rtype: integer
 
683
        '''
 
684
        try:
 
685
            obj = self._get_object(window_name, object_name)
 
686
 
 
687
            return int(self._check_state(obj, pyatspi.STATE_CHECKED))
 
688
        except:
 
689
            return 0
 
690
 
 
691
    def verifyuncheck(self, window_name, object_name):
 
692
        '''
 
693
        Verify uncheck item.
 
694
        
 
695
        @param window_name: Window name to look for, either full name,
 
696
        LDTP's name convention, or a Unix glob.
 
697
        @type window_name: string
 
698
        @param object_name: Object name to look for, either full name,
 
699
        LDTP's name convention, or a Unix glob. 
 
700
        @type object_name: string
 
701
 
 
702
        @return: 1 on success 0 on failure.
 
703
        @rtype: integer
 
704
        '''
 
705
        try:
 
706
            obj = self._get_object(window_name, object_name)
 
707
 
 
708
            return int(not self._check_state(obj, pyatspi.STATE_CHECKED))
 
709
        except:
 
710
            return 0
 
711
 
 
712
    def stateenabled(self, window_name, object_name):
 
713
        '''
 
714
        Check whether an object state is enabled or not
 
715
        
 
716
        @param window_name: Window name to look for, either full name,
 
717
        LDTP's name convention, or a Unix glob.
 
718
        @type window_name: string
 
719
        @param object_name: Object name to look for, either full name,
 
720
        LDTP's name convention, or a Unix glob. 
 
721
        @type object_name: string
 
722
 
 
723
        @return: 1 on success 0 on failure.
 
724
        @rtype: integer
 
725
        '''
 
726
        try:
 
727
            obj = self._get_object(window_name, object_name)
 
728
 
 
729
            return int(self._check_state(obj, pyatspi.STATE_ENABLED))
 
730
        except:
 
731
            return 0
 
732
 
 
733
    def getobjectlist(self, window_name):
 
734
        '''
 
735
        Get list of items in given GUI.
 
736
        
 
737
        @param window_name: Window name to look for, either full name,
 
738
        LDTP's name convention, or a Unix glob.
 
739
        @type window_name: string
 
740
 
 
741
        @return: list of items in LDTP naming convention.
 
742
        @rtype: list
 
743
        '''
 
744
        obj_list = []
 
745
        gui = self._get_window_handle(window_name)
 
746
        if not gui:
 
747
            raise LdtpServerException('Unable to find window "%s"' % \
 
748
                                          window_name)
 
749
        for name, obj, obj_index in self._appmap_pairs(gui):
 
750
            obj_list.append(name)
 
751
        return obj_list
 
752
 
 
753
    def getobjectinfo(self, window_name, object_name):
 
754
        '''
 
755
        Get object properties.
 
756
        
 
757
        @param window_name: Window name to look for, either full name,
 
758
        LDTP's name convention, or a Unix glob.
 
759
        @type window_name: string
 
760
        @param object_name: Object name to look for, either full name,
 
761
        LDTP's name convention, or a Unix glob. 
 
762
        @type object_name: string
 
763
 
 
764
        @return: list of properties
 
765
        @rtype: list
 
766
        '''
 
767
        obj = self._get_object(window_name, object_name)
 
768
 
 
769
        props = \
 
770
            ['child_index', 'key', 'obj_index', 'parent', 'class', \
 
771
                 'children', 'label', 'label_by']
 
772
 
 
773
        return props
 
774
 
 
775
    def getobjectproperty(self, window_name, object_name, prop):
 
776
        '''
 
777
        Get object property value.
 
778
        
 
779
        @param window_name: Window name to look for, either full name,
 
780
        LDTP's name convention, or a Unix glob.
 
781
        @type window_name: string
 
782
        @param object_name: Object name to look for, either full name,
 
783
        LDTP's name convention, or a Unix glob. 
 
784
        @type object_name: string
 
785
        @param prop: property name.
 
786
        @type prop: string
 
787
 
 
788
        @return: list of properties
 
789
        @rtype: list
 
790
        '''
 
791
        if prop == 'child_index':
 
792
            obj = self._get_object(window_name, object_name)
 
793
            return obj.getIndexInParent()
 
794
        elif prop == 'key':
 
795
            obj = self._get_object(window_name, object_name) # A sanity check.
 
796
            return object_name # For now, we only match exact names anyway.
 
797
        elif prop == 'label':
 
798
            obj = self._get_object(window_name, object_name) # A sanity check.
 
799
            return obj.name
 
800
        elif prop == 'obj_index':
 
801
            role_count = {}
 
802
            for gui in self._list_guis():
 
803
                if self._match_name_to_acc(window_name, gui):
 
804
                    for name, obj, obj_index in self._appmap_pairs(gui):
 
805
                        role = obj.getRole()
 
806
                        role_count[role] = role_count.get(role, 0) + 1
 
807
                        if name == object_name:
 
808
                            return obj_index
 
809
 
 
810
            raise LdtpServerException(
 
811
                'Unable to find object name in application map')
 
812
        elif prop == 'parent':
 
813
            cached_list = []
 
814
            for gui in self._list_guis():
 
815
                if self._match_name_to_acc(window_name, gui):
 
816
                    for name, obj, obj_index in self._appmap_pairs(gui):
 
817
                        if name == object_name:
 
818
                            for pname, pobj in cached_list:
 
819
                                if obj in pobj: # avoid double link issues
 
820
                                    return pname
 
821
                            _parent = self._ldtpize_accessible(obj.parent)
 
822
                            return '%s%s' % (_parent[0], _parent[1])
 
823
                        cached_list.insert(0, (name, obj))
 
824
 
 
825
            raise LdtpServerException(
 
826
                'Unable to find object name in application map')
 
827
        elif prop == 'class':
 
828
            obj = self._get_object(window_name, object_name)
 
829
            return obj.getRoleName().replace(' ', '_')
 
830
        elif prop == 'children':
 
831
            children = []
 
832
            obj = self._get_object(window_name, object_name)
 
833
            for gui in self._list_guis():
 
834
                if self._match_name_to_acc(window_name, gui):
 
835
                    for name, child, obj_index in self._appmap_pairs(gui):
 
836
                        if child in obj:
 
837
                            children.append(name)
 
838
                    break
 
839
            return ' '.join(children)
 
840
 
 
841
        raise LdtpServerException('Unknown property "%s" in %s' % \
 
842
                                      (prop, object_name))
 
843
 
 
844
    def getchild(self, window_name, child_name='', role='', first=False):
 
845
        '''
 
846
        Gets the list of object available in the window, which matches 
 
847
        component name or role name or both.
 
848
        
 
849
        @param window_name: Window name to look for, either full name,
 
850
        LDTP's name convention, or a Unix glob.
 
851
        @type window_name: string
 
852
        @param child_name: Child name to search for.
 
853
        @type child_name: string
 
854
        @param role: role name to search for, or an empty string for wildcard.
 
855
        @type role: string
 
856
 
 
857
        @return: list of matched children names
 
858
        @rtype: list
 
859
        '''
 
860
        matches = []
 
861
        for gui in self._list_guis():
 
862
            if self._match_name_to_acc(window_name, gui):
 
863
                for name, obj, obj_index in self._appmap_pairs(gui):
 
864
                    if child_name and role:
 
865
                        if obj.getRoleName() == role and \
 
866
                                (child_name == name or \
 
867
                                     self._match_name_to_acc(child_name, obj)):
 
868
                            matches.append(name)
 
869
                    elif role:
 
870
                        if obj.getRoleName() == role:
 
871
                            matches.append(name)
 
872
                    elif child_name:
 
873
                        if child_name == name or \
 
874
                                self._match_name_to_acc(child_name, obj):
 
875
                            matches.append(name)
 
876
 
 
877
                    #print matches, first
 
878
                    if matches and first:
 
879
                        # Return once we have a match
 
880
                        return matches
 
881
                
 
882
        if not matches:
 
883
            raise LdtpServerException('Could not find a child.')
 
884
 
 
885
        return matches
 
886
 
 
887
    def remap(self, window_name):
 
888
        '''
 
889
        For backwards compatability, does not do anything since we are entirely
 
890
        dynamic.
 
891
 
 
892
        @param window_name: Window name to look for, either full name,
 
893
        LDTP's name convention, or a Unix glob.
 
894
        @type window_name: string
 
895
 
 
896
        @return: 1
 
897
        @rtype: integer
 
898
        '''
 
899
        return 1
 
900
 
 
901
    def wait(self, timeout=5):
 
902
        '''
 
903
        Wait a given amount of seconds.
 
904
 
 
905
        @param timeout: Wait timeout in seconds
 
906
        @type timeout: integer
 
907
 
 
908
        @return: 1
 
909
        @rtype: integer
 
910
        '''
 
911
        waiter = NullWaiter(1, timeout)
 
912
 
 
913
        return waiter.run()
 
914
 
 
915
    def getstatusbartext(self, window_name, object_name):
 
916
        '''
 
917
        Get text value
 
918
        
 
919
        @param window_name: Window name to type in, either full name,
 
920
        LDTP's name convention, or a Unix glob.
 
921
        @type window_name: string
 
922
        @param object_name: Object name to type in, either full name,
 
923
        LDTP's name convention, or a Unix glob. 
 
924
        @type object_name: string
 
925
 
 
926
        @return: text on success.
 
927
        @rtype: string
 
928
        '''
 
929
        return self.gettextvalue(window_name, object_name)
 
930
 
 
931
    def setlocale(self, locale_str):
 
932
        '''
 
933
        Set the locale to the given value.
 
934
 
 
935
        @param locale_str: locale to set to.
 
936
        @type locale_str: string
 
937
 
 
938
        @return: 1
 
939
        @rtype: integer
 
940
        '''
 
941
        locale.setlocale(locale.LC_ALL, locale_str)
 
942
        return 1
 
943
 
 
944
    def getwindowsize (self, window_name):
 
945
        '''
 
946
        Get window size.
 
947
        
 
948
        @param window_name: Window name to get size of.
 
949
        @type window_name: string
 
950
 
 
951
        @return: list of dimensions [x, y, w, h]
 
952
        @rtype: list
 
953
        '''
 
954
        obj_list = []
 
955
        for gui in self._list_guis():
 
956
            if self._match_name_to_acc(window_name, gui):
 
957
                size = self._get_size(gui)
 
958
                return [size.x, size.y, size.width, size.height]
 
959
 
 
960
        raise LdtpServerException('Window does not exist')