~dhillon-v10/qa-regression-testing/mago-packages-checking

« back to all changes in this revision

Viewing changes to mago/application/gnome.py

  • Committer: Vikram Dhillon
  • Date: 2010-01-12 02:42:18 UTC
  • Revision ID: dhillonv10@gmail.com-20100112024218-7ntl2wrpbxqjb3kx
Initial commit: getting in the data from mago

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- coding:utf-8 -*-
 
2
"""
 
3
This is the "gnome" module.
 
4
 
 
5
The gnome module provides wrappers for LDTP to make the write of Gnome tests easier. 
 
6
"""
 
7
import ooldtp
 
8
import ldtp
 
9
from .main import Application
 
10
from ..gconfwrapper import GConf
 
11
import time
 
12
 
 
13
class Calculator(Application):
 
14
    """
 
15
    The Calculator class contains methods for exercising the gcalctool application
 
16
    """
 
17
    LAUNCHER            = "gcalctool"
 
18
    WINDOW              = "frmCalculator"
 
19
    WINDOW_ADVANCED     = "frmCalculator-Advanced"
 
20
    WINDOW_FINANCIAL    = "frmCalculator-Financial"
 
21
    WINDOW_SCIENTIFIC   = "frmCalculator-Scientific"
 
22
    WINDOW_PROGRAMMING  = "frmCalculator-Programming"
 
23
    MNU_BASIC           = "mnuBasic"
 
24
    MNU_ADVANCED        = "mnuAdvanced"
 
25
    MNU_FINANCIAL       = "mnuFinancial"
 
26
    MNU_SCIENTIFIC      = "mnuScientific"
 
27
    MNU_PROGRAMMING     = "mnuProgramming"
 
28
    VIEWS = {"BASIC"         : {"WINDOW" : WINDOW,
 
29
                                "MENU" : MNU_BASIC},
 
30
             "ADVANCED"      : {"WINDOW" : WINDOW_ADVANCED,
 
31
                                "MENU" : MNU_ADVANCED},
 
32
             "FINANCIAL"     : {"WINDOW" : WINDOW_FINANCIAL,
 
33
                                "MENU" : MNU_FINANCIAL},
 
34
             "SCIENTIFIC"    : {"WINDOW" : WINDOW_SCIENTIFIC,
 
35
                                "MENU" : MNU_SCIENTIFIC},
 
36
             "PROGRAMMING"   : {"WINDOW" : WINDOW_PROGRAMMING,
 
37
                                "MENU" : MNU_PROGRAMMING}}
 
38
    BUTTONS = {"0" : "0",
 
39
               "1" : "1",
 
40
               "2" : "2",
 
41
               "3" : "3",
 
42
               "4" : "4",
 
43
               "5" : "5",
 
44
               "6" : "6",
 
45
               "7" : "7",
 
46
               "8" : "8",
 
47
               "9" : "9",
 
48
               "=" : "=",
 
49
               "+" : "+",
 
50
               "-" : "−",
 
51
               "*" : "×",
 
52
               "/" : "÷",
 
53
               "C" : "Clr",
 
54
               "." : ".",
 
55
               "s" : "√"}
 
56
    EDITBAR_ROLE        = "edit bar"
 
57
    EDITBAR_INDEX       = 0
 
58
    SLEEP_DELAY         = 2
 
59
    GCONF_MODE_KEY      = "/apps/gcalctool/button_layout"
 
60
    GCONF_MODE_VAL      = "BASIC"
 
61
    
 
62
    def __init__(self):
 
63
        Application.__init__(self)
 
64
    
 
65
    def open(self):
 
66
        """
 
67
        Opens the application
 
68
        """
 
69
        try:
 
70
            GConf.set_item(self.GCONF_MODE_KEY, self.GCONF_MODE_VAL)
 
71
        except GConf.GConfError:
 
72
            raise ldtp.LdtpExecutionError, "Could not set default view in gconf"
 
73
        Application.open(self)
 
74
        
 
75
    def showing(self, button):
 
76
        """
 
77
        Test whether the specified button is showing
 
78
        
 
79
        @param button: Specify the button to test
 
80
        @type button: string
 
81
        @return: Value indicating whether the button is showing in the view
 
82
        @rtype: boolean
 
83
        
 
84
        >>> c = Calculator()
 
85
        >>> c.open() # Calculator opens in BASIC view
 
86
        >>> c.showing("Reciprocal")
 
87
        False
 
88
        >>> c.set_view("ADVANCED")
 
89
        >>> c.showing("Reciprocal")
 
90
        True
 
91
        """
 
92
        calculator = ooldtp.context(self.name)
 
93
        try:
 
94
            children = calculator.getchild(button,'push button')
 
95
            for child in children:
 
96
                childName=child.getName()
 
97
                if calculator.hasstate(childName, ldtp.state.SHOWING):
 
98
                    return True
 
99
        except ldtp.LdtpExecutionError:
 
100
            raise ldtp.LdtpExecutionError, "Error locating button %s" % button
 
101
        return False
 
102
    
 
103
    def push(self, buttons):
 
104
        """
 
105
        Push the specified buttons in the calculator window.
 
106
        
 
107
        >>> c = Calculator()
 
108
        >>> c.open()
 
109
        >>> c.push('2+2=')
 
110
        
 
111
        @param buttons: Specify the buttons to push.
 
112
        @type buttons: string
 
113
        """
 
114
        calculator = ooldtp.context(self.name)
 
115
        for button in buttons:
 
116
            try:
 
117
                # The calculator has some buttons with the same names that are
 
118
                # hidden depending on the view.
 
119
                children = calculator.getchild(self.BUTTONS[button],'push button')
 
120
                for child in children:
 
121
                    childName=child.getName()
 
122
                    if calculator.hasstate(childName, ldtp.state.SHOWING):
 
123
                        calculator.click(childName)
 
124
                        break
 
125
                else:
 
126
                    raise LookupError, "Could not find button %s in current view" % button
 
127
            except ldtp.LdtpExecutionError:
 
128
                raise ldtp.LdtpExecutionError, "Error clicking button %s" % button
 
129
    
 
130
    def get_view(self):
 
131
        """
 
132
        Get the current view of the calculator.
 
133
        
 
134
        >>> c = Calculator()
 
135
        >>> c.open()
 
136
        >>> c.get_view()
 
137
        'BASIC'
 
138
        
 
139
        @return: The current view: BASIC, ADVANCED, FINANCIAL, SCIENTIFIC,
 
140
                 or PROGRAMMING
 
141
        @rtype: string
 
142
        """
 
143
        windowname = self.name
 
144
        for view in self.VIEWS:
 
145
            if self.VIEWS[view]['WINDOW'] == windowname:
 
146
                return view
 
147
        raise LookupError, "Could not identify the current calculator view"
 
148
 
 
149
    def set_view(self, new_view):
 
150
        """
 
151
        Change to the specified view in the calculator
 
152
        
 
153
        >>> c = Calculator()
 
154
        >>> c.open()
 
155
        >>> c.set_view('ADVANCED')
 
156
        
 
157
        @param new_view: Specify the new view. Valid values are:
 
158
                         BASIC, ADVANCED, FINANCIAL, SCIENTIFIC, PROGRAMMING
 
159
        @type new_view: string
 
160
        """
 
161
        new_window_name = self.VIEWS[new_view]["WINDOW"]
 
162
        new_view_menu_name = self.VIEWS[new_view]["MENU"]
 
163
        calculator = ooldtp.context(self.name)
 
164
        try:
 
165
            mnu_new_view = calculator.getchild(new_view_menu_name)
 
166
        except ldtp.LdtpExecutionError:
 
167
            raise ldtp.LdtpExecutionError, "The menu item for the requested view was not found."
 
168
 
 
169
        try:
 
170
            mnu_new_view.selectmenuitem() 
 
171
        except ldtp.LdtpExecutionError:
 
172
            raise ldtp.LdtpExecutionError, "There was a problem changing views."
 
173
        self.set_name(new_window_name)
 
174
        time.sleep(self.SLEEP_DELAY) # need to give mago some time to catch up to calc ui
 
175
    
 
176
    def get_value(self):
 
177
        """
 
178
        Read the value from the calculator screen
 
179
        
 
180
        >>> c = Calculator()
 
181
        >>> c.open()
 
182
        >>> c.push('2+2=')
 
183
        >>> c.get_value()
 
184
        u'4'
 
185
        
 
186
        @return: The contents of the calculator's screen
 
187
        @rtype: string
 
188
        """
 
189
        calculator = ooldtp.context(self.name)
 
190
        try:
 
191
            editBar = calculator.getchild('txtResultRegion') # txtResultRegion
 
192
            value = editBar.gettextvalue()
 
193
        except ldtp.LdtpExecutionError:
 
194
            raise ldtp.LdtpExecutionError, "There was a problem reading the value."
 
195
        return value
 
196
 
 
197
class GnomeScreenshot(Application):
 
198
    """
 
199
    GnomeScreenshot manages the gnome-screenshot application in interactive
 
200
    mode.
 
201
    """
 
202
    LAUNCHER = 'gnome-screenshot'
 
203
    LAUNCHER_ARGS = ['--interactive']
 
204
    CLOSE_TYPE = 'button'
 
205
    CLOSE_NAME = 'btnCancel'
 
206
 
 
207
    WINDOW = 'dlgTakeScreenshot'
 
208
    
 
209
    GRAB_AFTER_DELAY_FIELD = 'sbtnGrabafteradelayof'
 
210
    GRAB_THE_WHOLE_DESKTOP_RADIO = 'rbtnGrabthewholedesktop'
 
211
    GRAB_THE_CURRENT_WINDOW_RADIO = 'rbtnGrabthecurrentwindow'
 
212
    GRAB_A_SELECTED_AREA = 'rbtnSelectareatograb'
 
213
 
 
214
    TAKE_SCREENSHOT_BUTTON = 'btnTakeScreenshot'
 
215
 
 
216
    SAVE_FILE_WINDOW = 'dlgSaveScreenshot'
 
217
    SAVE_FILE_BUTTON = 'btnSave'
 
218
    SAVE_FILE_NAME_TEXT = 'txtName'
 
219
    SAVE_FILE_FOLDER_DROPDOWN = 'cbSaveFile'
 
220
 
 
221
    def __init__(self):
 
222
        Application.__init__(self)
 
223
 
 
224
    def take_whole_desktop_screenshot(self, delay):
 
225
        """
 
226
        Complete the "Take Screenshot" dialog box by choosing whole desktop and
 
227
        clicking Take Screenshot.
 
228
 
 
229
        You must have started the application already.
 
230
 
 
231
        @type delay: int
 
232
        @param delay: The number of seconds to delay before taking the 
 
233
                      screenshot.
 
234
        """
 
235
        try:
 
236
            gnome_screenshot = ooldtp.context(self.name)
 
237
        except ldtp.LdtpExecutionError:
 
238
            raise ldtp.LdtpExecutionError, "Error finding " + name
 
239
 
 
240
        try:
 
241
            radio_button = gnome_screenshot.getchild(self.GRAB_THE_WHOLE_DESKTOP_RADIO)
 
242
        except ldtp.LdtpExecutionError:
 
243
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_THE_WHOLE_DESKTOP_RADIO
 
244
 
 
245
        try:
 
246
            radio_button.click()
 
247
        except ldtp.LdtpExecutionError:
 
248
            raise ldtp.LdtpExecutionError, "Error clicking on radio button."
 
249
 
 
250
        try:
 
251
            delay_field = gnome_screenshot.getchild(self.GRAB_AFTER_DELAY_FIELD)
 
252
        except ldtp.LdtpExecutionError:
 
253
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_AFTER_DELAY_FIELD
 
254
 
 
255
        try:        
 
256
            delay_field.setvalue(delay)
 
257
        except ldtp.LdtpExecutionError:
 
258
            raise ldtp.LdtpExecutionError, "Error setting value to " + self.GRAB_AFTER_DELAY_FIELD
 
259
 
 
260
        try:
 
261
            save_button = gnome_screenshot.getchild(self.TAKE_SCREENSHOT_BUTTON)
 
262
        except ldtp.LdtpExecutionError:
 
263
            raise ldtp.LdtpExecutionError, "Error finding " + self.TAKE_SCREENSHOT_BUTTON
 
264
 
 
265
        try:
 
266
            ldtp.click(self.WINDOW, self.TAKE_SCREENSHOT_BUTTON)
 
267
        except ldtp.LdtpExecutionError:
 
268
            raise ldtp.LdtpExecutionError, "Error clicking on save button"
 
269
 
 
270
        try:
 
271
            ldtp.waittillguinotexist(self.WINDOW)
 
272
        except ldtp.LdtpExecutionError:
 
273
            raise ldtp.LdtpExecutionError, "Window " + self.WINDOW + " did not go away."
 
274
 
 
275
 
 
276
    def take_current_window_screenshot(self, delay):
 
277
        """
 
278
        Complete the "Take Screenshot" dialog box by choosing current window and
 
279
        clicking Take Screenshot.
 
280
 
 
281
        You must have started the application already.
 
282
 
 
283
        @type delay: int
 
284
        @param delay: The number of seconds to delay before taking the 
 
285
                      screenshot.
 
286
        """
 
287
        try:
 
288
            gnome_screenshot = ooldtp.context(self.name)
 
289
        except ldtp.LdtpExecutionError:
 
290
            raise ldtp.LdtpExecutionError, "Error finding " + name
 
291
 
 
292
        try:
 
293
            radio_button = gnome_screenshot.getchild(self.GRAB_THE_CURRENT_WINDOW_RADIO)
 
294
        except ldtp.LdtpExecutionError:
 
295
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_THE_CURRENT_WINDOW_RADIO
 
296
 
 
297
        try:
 
298
            radio_button.click()
 
299
        except ldtp.LdtpExecutionError:
 
300
            raise ldtp.LdtpExecutionError, "Error clicking on radio button"
 
301
    
 
302
        try:
 
303
            delay_field = gnome_screenshot.getchild(self.GRAB_AFTER_DELAY_FIELD)
 
304
        except ldtp.LdtpExecutionError:
 
305
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_AFTER_DELAY_FIELD
 
306
 
 
307
        try:
 
308
            delay_field.setvalue(delay)
 
309
        except ldtp.LdtpExecutionError:
 
310
            raise ldtp.LdtpExecutionError, "Error setting delay to " + delay
 
311
 
 
312
        try:
 
313
            save_button = gnome_screenshot.getchild(self.TAKE_SCREENSHOT_BUTTON)
 
314
        except ldtp.LdtpExecutionError:
 
315
            raise ldtp.LdtpExecutionError, "Error finding " + self.TAKE_SCREENSHOT_BUTTON
 
316
 
 
317
        try:
 
318
            save_button.click()
 
319
        except ldtp.LdtpExecutionError:
 
320
            raise ldtp.LdtpExecutionError, "Error clicking on save button"
 
321
 
 
322
        try:
 
323
            ldtp.waittillguinotexist(self.WINDOW)
 
324
        except ldtp.LdtpExecutionError:
 
325
            raise ldtp.LdtpExecutionError, "Window " + self.WINDOW + " did not go away"
 
326
 
 
327
    def grab_selected_area(self, delay):
 
328
        """
 
329
        Complete the "Take Screenshot" dialog box by choosing selected area and
 
330
        clicking Take Screenshot.
 
331
 
 
332
        You must have started the application already.
 
333
 
 
334
        @type delay: int
 
335
        @param delay: The number of seconds to delay before taking the 
 
336
                      screenshot.
 
337
        """
 
338
        try:
 
339
            gnome_screenshot = ooldtp.context(self.name)
 
340
        except ldtp.LdtpExecutionError:
 
341
            raise ldtp.LdtpExecutionError, "Error finding " + name
 
342
 
 
343
        try:
 
344
            radio_button = gnome_screenshot.getchild(self.GRAB_A_SELECTED_AREA)
 
345
        except ldtp.LdtpExecutionError:
 
346
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_A_SELECTED_AREA
 
347
 
 
348
        try:
 
349
            radio_button.click()
 
350
        except ldtp.LdtpExecutionError:
 
351
            raise ldtp.LdtpExecutionError, "Error clicking on radio button"
 
352
 
 
353
        try:
 
354
            delay_field = gnome_screenshot.getchild(self.GRAB_AFTER_DELAY_FIELD)
 
355
        except ldtp.LdtpExecutionError:
 
356
            raise ldtp.LdtpExecutionError, "Error finding " + self.GRAB_AFTER_DELAY_FIELD
 
357
        
 
358
        try:
 
359
            delay_field.setvalue(delay)
 
360
        except ldtp.LdtpExecutionError:
 
361
            raise ldtp.LdtpExecutionError, "Error setting delay to " + delay
 
362
 
 
363
        try:
 
364
            save_button = gnome_screenshot.getchild(self.TAKE_SCREENSHOT_BUTTON)
 
365
        except ldtp.LdtpExecutionError:
 
366
            raise ldtp.LdtpExecutionError, "Error finding " + self.TAKE_SCREENSHOT_BUTTON
 
367
 
 
368
        try:
 
369
            save_button.click()
 
370
        except ldtp.LdtpExecutionError:
 
371
            raise ldtp.LdtpExecutionError, "Error clicking on save button"
 
372
 
 
373
        try:
 
374
            ldtp.waittillguinotexist(self.WINDOW)
 
375
        except ldtp.LdtpExecutionError:
 
376
            raise ldtp.LdtpExecutionError, "Window " + self.WINDOW + " did not go away"
 
377
 
 
378
        # Select the area of the screen for the screenshot.  As long as your screen is
 
379
        # larger than 200x200px this should work fine.
 
380
        try:
 
381
            ldtp.generatemouseevent(100,100,'b1p') # button 1 press
 
382
            ldtp.generatemouseevent(200,200,'abs') # move mouse to 200,200
 
383
            ldtp.generatemouseevent(200,200,'b1r') # button 1 release
 
384
        except ldtp.LdtpExecutionError:
 
385
            raise ldtp.LdtpExecutionError, "Error selecting screen area"
 
386
    
 
387
    def save_to_file(self, filename):
 
388
        """
 
389
        Complete the save to file dialog.  You must have completed a grab_* already
 
390
 
 
391
        @type filename: string
 
392
        @param filename: The name of the file to save.  This file will be saved 
 
393
                         into the current users home folder.  You cannot
 
394
                         specify a directory.
 
395
        """
 
396
        try:
 
397
            ldtp.waittillguiexist(self.SAVE_FILE_WINDOW)
 
398
        except ldtp.LdtpExecutionError:
 
399
            raise ldtp.LdtpExecutionError, "Window " + self.SAVE_FILE_WINDOW + " did not appear"
 
400
 
 
401
        try:
 
402
            save_file_window = ooldtp.context(self.SAVE_FILE_WINDOW)
 
403
        except ldtp.LdtpExecutionError:
 
404
            raise ldtp.LdtpExecutionError, "Error finding window " + self.SAVE_FILE_WINDOW
 
405
 
 
406
        try:
 
407
            filename_field = save_file_window.getchild(self.SAVE_FILE_NAME_TEXT)
 
408
        except ldtp.LdtpExecutionError:
 
409
            raise ldtp.LdtpExecutionError, "Error finding " + self.SAVE_FILE_NAME_TEXT
 
410
 
 
411
        try:
 
412
            filename_field.settextvalue(filename)
 
413
        except ldtp.LdtpExecutionError:
 
414
            raise ldtp.LdtpExecutionError, "Error setting filename to " + delay
 
415
 
 
416
        # There is a bug in gnome-screenshot that prevents the combobox 
 
417
        # name from being used.  There is only one combo box so its easy to 
 
418
        # select it by index.
 
419
        #
 
420
        # Also for some reason ooldtp didn't seem to be able select the combo
 
421
        # box properly.
 
422
        try:
 
423
            ldtp.comboselectindex(self.SAVE_FILE_WINDOW, "cbo#0", 0)
 
424
        except ldtp.LdtpExecutionError:
 
425
            raise ldtp.LdtpExecutionError, "Error setting folder to home folder"
 
426
 
 
427
        try:
 
428
            save_file_button = save_file_window.getchild(self.SAVE_FILE_BUTTON)
 
429
        except ldtp.LdtpExecutionError:
 
430
            raise ldtp.LdtpExecutionError, "Error finding " + self.SAVE_FILE_BUTTON
 
431
 
 
432
        try:
 
433
            save_file_button.click()
 
434
        except ldtp.LdtpExecutionError:
 
435
            raise ldtp.LdtpExecutionError, "Error clicking on save button"
 
436
 
 
437
        try:
 
438
            ldtp.waittillguinotexist(self.SAVE_FILE_WINDOW)
 
439
        except ldtp.LdtpExecutionError:
 
440
            raise ldtp.LdtpExecutionError, "Window " + self.SAVE_FILE_WINDOW + " did not go away"
 
441
 
 
442
class Seahorse(Application):
 
443
    """
 
444
    Seahorse manages the Seahorse application.
 
445
    """
 
446
    WINDOW       = "frmPasswordsandEncryptionKeys"
 
447
    LAUNCHER     = "seahorse"
 
448
    MNU_NEWKEY   = "mnuNew"
 
449
    NEWKEY_DLG   = "Create New ..."
 
450
    BTN_CONTINUE = "btnContinue"
 
451
    TYPE_PGP            = "PGP Key"
 
452
    NEWPGP_DLG          = "dlgCreateaPGPKey"
 
453
    DLG_NEWPGP_FULLNAME = "txtFullName"
 
454
    DLG_NEWPGP_EMAIL    = "txtEmailAddress"
 
455
    DLG_NEWPGP_COMMENT  = "txtComment"
 
456
    BTN_NEWPGP_CREATE   = "btnCreate"
 
457
    DLG_NEWKEY_PASS     = "dlgPassphrasefor*"
 
458
    BTN_PASS_OK         = "btnOK"
 
459
    DLG_GENERATING_KEY  = "dlgGeneratingkey"
 
460
    DLG_CREATING_SSH    = "dlgCreatingSecureShellKey"
 
461
    TYPE_SSH            = "Secure Shell Key"
 
462
    NEWSSH_DLG          = "New Secure Shell Key" 
 
463
    DLG_NEWSSH_DESC     = "txtKeyDescription"
 
464
    BTN_NEWSSH_CREATE_AND_SETUP = "Create and Set Up"
 
465
    DLG_SET_UP          = "Set Up Computer for SSH Connection"
 
466
    TXT_SET_UP_COMPUTER = "txtThehostnameoraddressoftheserver."
 
467
    TXT_SET_UP_LOGIN    = "txtLoginName"
 
468
    BTN_SET_UP          = "btnSetUp"
 
469
    BTN_NEWSSH_CREATE   = "Just Create Key"
 
470
    TAB_PERSONAL_KEYS   = "My Personal Keys"
 
471
    TAB_LIST            = "ptl0"
 
472
    TBL_PERSONAL_KEYS   = "tbl0"
 
473
    LBL_DELETE_KEY_Q   = "lblOneormoreofthedeletedkeysareprivatekeys.Areyousureyouwanttoproceed?"
 
474
 
 
475
    def __init__(self):
 
476
        Application.__init__(self)
 
477
        self.generated_keys = []
 
478
 
 
479
    def new_key(self, key_type):
 
480
        """
 
481
        It opens up the list of available new keys, and select the one to create.
 
482
        
 
483
        @type key_type: string
 
484
        @param key_type: The type of key to create. 
 
485
        """
 
486
        
 
487
        seahorse = ooldtp.context(self.name)
 
488
        
 
489
        try:
 
490
            mnu_new_key = seahorse.getchild(self.MNU_NEWKEY)
 
491
        except ldtp.LdtpExecutionError:
 
492
            raise ldtp.LdtpExecutionError, "The new key menu was not found."
 
493
 
 
494
        try:
 
495
            mnu_new_key.selectmenuitem() 
 
496
        except ldtp.LdtpExecutionError:
 
497
            raise ldtp.LdtpExecutionError, "There was a problem when selecting new key menu item."
 
498
 
 
499
        try:
 
500
            ldtp.waittillguiexist(self.NEWKEY_DLG)
 
501
            dlg_new_key = ooldtp.context(self.NEWKEY_DLG)
 
502
        except ldtp.LdtpExecutionError:
 
503
            raise ldtp.LdtpExecutionError, "The new key dialog was not found."
 
504
 
 
505
        try:
 
506
            table  = dlg_new_key.getchild(role = 'table')
 
507
            types_table = table[0]
 
508
 
 
509
            for i in range(0, types_table.getrowcount(), 1):
 
510
                text = types_table.getcellvalue(i, 1)
 
511
                candidate = text.split('\n')[0]
 
512
                if candidate == key_type:
 
513
                    types_table.selectrowindex(i)
 
514
                    break
 
515
                ldtp.wait(1)
 
516
        except ldtp.LdtpExecutionError:
 
517
            raise ldtp.LdtpExecutionError, "Error getting the key types table."
 
518
 
 
519
        try:
 
520
            btn_continue = dlg_new_key.getchild(self.BTN_CONTINUE)
 
521
        except ldtp.LdtpExecutionError:
 
522
            raise ldtp.LdtpExecutionError, "The continue button at the new key dialog was not found."
 
523
 
 
524
        try:
 
525
            btn_continue.click() 
 
526
        except ldtp.LdtpExecutionError:
 
527
            raise ldtp.LdtpExecutionError, "There was a problem when clicking the continue button."
 
528
        
 
529
    def new_pgp_key(self, full_name, email, comment, passphrase):
 
530
        """
 
531
        It creates a new PGP key with the default settings.
 
532
 
 
533
        TODO: Allow advanced options
 
534
        TODO: Check the list afterwards for the newly created key
 
535
 
 
536
        @type full_name: string 
 
537
        @param full_name: Full name to type for the PGP key
 
538
 
 
539
        @type email: string 
 
540
        @param email: Email to type for the PGP key
 
541
 
 
542
        @type comment: string 
 
543
        @param comment: Comment to type for the PGP key
 
544
 
 
545
        @type passphrase: string 
 
546
        @param passphrase: Passphrase to type for the PGP key
 
547
        """
 
548
        
 
549
        self.new_key(self.TYPE_PGP)
 
550
 
 
551
        try:
 
552
            ldtp.waittillguiexist(self.NEWPGP_DLG)
 
553
            dlg_new_pgp = ooldtp.context(self.NEWPGP_DLG)
 
554
        except ldtp.LdtpExecutionError:
 
555
            raise ldtp.LdtpExecutionError, "The new key dialog was not found."
 
556
 
 
557
        try:
 
558
            txt_field = dlg_new_pgp.getchild(self.DLG_NEWPGP_FULLNAME)
 
559
        except ldtp.LdtpExecutionError:
 
560
            raise ldtp.LdtpExecutionError, "The " + txt_field + " text field was not found."
 
561
        try:
 
562
            txt_field.settextvalue(full_name)
 
563
        except ldtp.LdtpExecutionError:
 
564
            raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
565
 
 
566
        try:
 
567
            txt_field = dlg_new_pgp.getchild(self.DLG_NEWPGP_EMAIL)
 
568
        except ldtp.LdtpExecutionError:
 
569
            raise ldtp.LdtpExecutionError, "The " + txt_field + " text field was not found."
 
570
        try:
 
571
            txt_field.settextvalue(email)
 
572
        except ldtp.LdtpExecutionError:
 
573
            raise ldtp.LdtpExecutionError, "There was a problem when writing the text."
 
574
   
 
575
        try:
 
576
            txt_field = dlg_new_pgp.getchild(self.DLG_NEWPGP_COMMENT)
 
577
        except ldtp.LdtpExecutionError:
 
578
            raise ldtp.LdtpExecutionError, "The " + txt_field + " text field was not found."
 
579
        try:
 
580
            txt_field.settextvalue(comment)
 
581
        except ldtp.LdtpExecutionError:
 
582
            raise ldtp.LdtpExecutionError, "There was a problem when writing the text."
 
583
 
 
584
        try:
 
585
            btn_create = dlg_new_pgp.getchild(self.BTN_NEWPGP_CREATE)
 
586
        except ldtp.LdtpExecutionError:
 
587
            raise ldtp.LdtpExecutionError, "The create button at the new PGP key dialog was not found."
 
588
 
 
589
        try:
 
590
            btn_create.click() 
 
591
        except ldtp.LdtpExecutionError:
 
592
            raise ldtp.LdtpExecutionError, "There was a problem when clicking the create button."
 
593
       
 
594
        try:
 
595
            ldtp.waittillguiexist(self.DLG_NEWKEY_PASS)
 
596
            dlg_new_pgp_pass = ooldtp.context(self.DLG_NEWKEY_PASS)
 
597
        except ldtp.LdtpExecutionError:
 
598
            raise ldtp.LdtpExecutionError, "The new pgp key passphrase dialog was not found."
 
599
 
 
600
        try:
 
601
            ldtp.enterstring(passphrase)
 
602
            ldtp.enterstring("<tab>")
 
603
            ldtp.enterstring(passphrase)
 
604
        except ldtp.LdtpExecutionError:
 
605
            raise ldtp.LdtpExecutionError, "Error entering passphrase."
 
606
 
 
607
        try:
 
608
            btn_pass_ok = dlg_new_pgp_pass.getchild(self.BTN_PASS_OK)
 
609
        except ldtp.LdtpExecutionError:
 
610
            raise ldtp.LdtpExecutionError, "The OK button at the new PGP key passphrase dialog was not found."
 
611
 
 
612
        try:
 
613
            btn_pass_ok.click() 
 
614
        except ldtp.LdtpExecutionError:
 
615
            raise ldtp.LdtpExecutionError, "There was a problem when clicking the OK button."
 
616
 
 
617
        try:
 
618
            ldtp.waittillguiexist(self.DLG_GENERATING_KEY)
 
619
            while ldtp.guiexist(self.DLG_GENERATING_KEY) == 1:
 
620
                ldtp.wait(1)
 
621
        except ldtp.LdtpExecutionError:
 
622
            raise ldtp.LdtpExecutionError, "The new pgp generating key dialog was not found."
 
623
 
 
624
        # Add key name to generated key list, so we know to delete it later.
 
625
        self.generated_keys.append('  '.join([full_name, email, "'"+comment+"'"]))
 
626
 
 
627
    def new_ssh_key(self, description, passphrase, set_up = False, computer = '', login = ''):
 
628
        """
 
629
        It creates a new SSH key with the default settings.
 
630
 
 
631
        TODO: Setting up the key is not working yet
 
632
 
 
633
        @type description: string 
 
634
        @param description: Description to type in the SSH key
 
635
 
 
636
        @type passphrase: string 
 
637
        @param passphrase: Passphrase to type for the SSH key
 
638
 
 
639
        @type set_up: boolean 
 
640
        @param passphrase: True, to set up the SSH key
 
641
 
 
642
        @type computer: string 
 
643
        @param computer: URL or IP of the computer to set up the key
 
644
        
 
645
        @type login: string
 
646
        @param login: Login to use in the remote computer
 
647
        """
 
648
        
 
649
        self.new_key(self.TYPE_SSH)
 
650
 
 
651
        try:
 
652
            ldtp.waittillguiexist(self.NEWSSH_DLG)
 
653
            dlg_new_ssh = ooldtp.context(self.NEWSSH_DLG)
 
654
        except ldtp.LdtpExecutionError:
 
655
            raise ldtp.LdtpExecutionError, "The new key dialog was not found."
 
656
 
 
657
        try:
 
658
            txt_field = dlg_new_ssh.getchild(self.DLG_NEWSSH_DESC)
 
659
        except ldtp.LdtpExecutionError:
 
660
            raise ldtp.LdtpExecutionError, "The " + self.DLG_NEWSSH_DESC + " text field was not found."
 
661
        try:
 
662
            txt_field.settextvalue(description)
 
663
        except ldtp.LdtpExecutionError:
 
664
            raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
665
 
 
666
        if set_up == True:
 
667
            try:
 
668
                btn_create = dlg_new_ssh.getchild(self.BTN_NEWSSH_CREATE_AND_SETUP)
 
669
            except ldtp.LdtpExecutionError:
 
670
                raise ldtp.LdtpExecutionError, "The create button at the new PGP key dialog was not found."
 
671
 
 
672
        else:
 
673
            try:
 
674
                btn_create = dlg_new_ssh.getchild(self.BTN_NEWSSH_CREATE)
 
675
            except ldtp.LdtpExecutionError:
 
676
                raise ldtp.LdtpExecutionError, "The create button at the new PGP key dialog was not found."
 
677
 
 
678
        try:
 
679
            btn_create.click() 
 
680
        except ldtp.LdtpExecutionError:
 
681
            raise ldtp.LdtpExecutionError, "There was a problem when clicking the create button."
 
682
      
 
683
 
 
684
        try:
 
685
            ldtp.waittillguiexist(self.DLG_NEWKEY_PASS)
 
686
            dlg_new_key_pass = ooldtp.context(self.DLG_NEWKEY_PASS)
 
687
        except ldtp.LdtpExecutionError:
 
688
            raise ldtp.LdtpExecutionError, "The new key passphrase dialog was not found."
 
689
 
 
690
        try:
 
691
            ldtp.enterstring(passphrase)
 
692
            ldtp.enterstring("<tab>")
 
693
            ldtp.enterstring(passphrase)
 
694
        except ldtp.LdtpExecutionError:
 
695
            raise ldtp.LdtpExecutionError, "Error entering passphrase."
 
696
 
 
697
        try:
 
698
            btn_pass_ok = dlg_new_key_pass.getchild(self.BTN_PASS_OK)
 
699
        except ldtp.LdtpExecutionError:
 
700
            raise ldtp.LdtpExecutionError, "The OK button at the new key passphrase dialog was not found."
 
701
 
 
702
        try:
 
703
            btn_pass_ok.click() 
 
704
        except ldtp.LdtpExecutionError:
 
705
            raise ldtp.LdtpExecutionError, "There was a problem when clicking the OK button."
 
706
 
 
707
        if set_up == True and login is not None:
 
708
 
 
709
            try:
 
710
                ldtp.waittillguiexist(self.DLG_SET_UP)
 
711
                dlg_set_up_computer = ooldtp.context(self.DLG_SET_UP)
 
712
            except ldtp.LdtpExecutionError:
 
713
                raise ldtp.LdtpExecutionError, "The set up computer dialog was not found."
 
714
 
 
715
            try:
 
716
                txt_field = dlg_set_up_computer.getchild(self.TXT_SET_UP_LOGIN)
 
717
            except ldtp.LdtpExecutionError:
 
718
                raise ldtp.LdtpExecutionError, "The " + self.TXT_SET_UP_LOGIN + " text field was not found."
 
719
            try:
 
720
                txt_field.settextvalue(login)
 
721
            except ldtp.LdtpExecutionError:
 
722
                raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
723
 
 
724
        if set_up == True:
 
725
            try:
 
726
                txt_field = dlg_set_up_computer.getchild(self.TXT_SET_UP_COMPUTER)
 
727
            except ldtp.LdtpExecutionError:
 
728
                raise ldtp.LdtpExecutionError, "The " + self.TXT_SET_UP_COMPUTER + " text field was not found."
 
729
            try:
 
730
                txt_field.settextvalue(computer)
 
731
            except ldtp.LdtpExecutionError:
 
732
                raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
733
 
 
734
            try:
 
735
                btn_set_up = dlg_set_up_computer.getchild(self.BTN_SET_UP)
 
736
            except ldtp.LdtpExecutionError:
 
737
                raise ldtp.LdtpExecutionError, "The set up button was not found."
 
738
 
 
739
            try:
 
740
                btn_set_up.click() 
 
741
            except ldtp.LdtpExecutionError:
 
742
                raise ldtp.LdtpExecutionError, "There was a problem when clicking the set up button."
 
743
            
 
744
        try:
 
745
            while ldtp.guiexist(self.DLG_CREATING_SSH) == 1:
 
746
                ldtp.wait(1)
 
747
            
 
748
        except ldtp.LdtpExecutionError:
 
749
            raise ldtp.LdtpExecutionError, "The creating key dialog was not found."
 
750
        # Add key name to generated key list, so we know to delete it later.
 
751
        self.generated_keys.append(description)
 
752
 
 
753
        # It is too fast to grab the main window afterwards
 
754
        ldtp.wait(3)
 
755
 
 
756
    def go_to_tab(self, tab_name):
 
757
        """
 
758
        Go to the specified tab.
 
759
        """
 
760
        seahorse = ooldtp.context(self.name)
 
761
        page_list = seahorse.getchild(self.TAB_LIST)
 
762
        page_list.selecttab(self.TAB_PERSONAL_KEYS)
 
763
        
 
764
 
 
765
    def assert_exists_key(self, name, tab_name = None):
 
766
        """
 
767
        It checks that the KEY with description 'description' is
 
768
        part of the keys of the current user
 
769
 
 
770
        @type name: string
 
771
        @param name: The name of the key to search
 
772
        
 
773
        @type tab_name: string
 
774
        @param tab_name: The tab name to search for the key.
 
775
        """
 
776
        if not tab_name:
 
777
            tab_name = self.TAB_PERSONAL_KEYS
 
778
 
 
779
        self.go_to_tab(tab_name)
 
780
        seahorse = ooldtp.context(self.name)
 
781
        return bool(seahorse.doesrowexist(self.TBL_PERSONAL_KEYS, name))
 
782
 
 
783
    def remove_key(self, key):
 
784
        seahorse = ooldtp.context(self.name)
 
785
 
 
786
        tbl_personal_keys = seahorse.getchild(self.TBL_PERSONAL_KEYS)
 
787
        mnu_delete = seahorse.getchild('mnuDelete')
 
788
        dlg_question = ooldtp.context('dlgQuestion')
 
789
        
 
790
        try:
 
791
            tbl_personal_keys.selectrow(key)
 
792
        except ldtp.LdtpExecutionError:
 
793
            # Doesn't exist, return False.
 
794
            return False
 
795
 
 
796
        mnu_delete.selectmenuitem()
 
797
        # General 'private key' verification.
 
798
        dlg_question.waittillguiexist(self.LBL_DELETE_KEY_Q)
 
799
        dlg_question.click('btnDelete')
 
800
        dlg_question.waittillguinotexist(self.LBL_DELETE_KEY_Q)
 
801
 
 
802
        # Specific key type verification.
 
803
        dlg_question.waittillguiexist()
 
804
        dlg_question.click('btnDelete')
 
805
        dlg_question.waittillguinotexist()
 
806
        
 
807
        return True
 
808
 
 
809
    def remove_keys(self, keys=None, tab_name=None):
 
810
        self.go_to_tab(tab_name or self.TAB_PERSONAL_KEYS)
 
811
 
 
812
        keys = keys or self.generated_keys
 
813
 
 
814
        for key in keys:
 
815
            self.remove_key(key)
 
816
        
 
817
class GEdit(Application):
 
818
    """
 
819
    GEdit manages the Gedit application.
 
820
    """
 
821
    WINDOW     = "frm*gedit"
 
822
    TXT_FIELD  = "txt1"
 
823
    LAUNCHER   = "gedit"
 
824
    SAVE_DLG   = "dlgSave*"
 
825
    SAVE_DLG_TXT_NAME = "txtName"
 
826
    SAVE_DLG_BTN_SAVE = "btnSave"
 
827
    QUESTION_DLG = "dlgQuestion"
 
828
    QUESTION_DLG_BTN_SAVE = "btnSave"
 
829
    QUESTION_DLG_BTN_SAVE_AS = "btnSaveAs"
 
830
    QUESTION_DLG_BTN_CLOSE = "btnClosewithoutSaving"
 
831
    MNU_QUIT = "mnuQuit"
 
832
    MNU_CLOSE = "mnuClose"
 
833
    MNU_NEW = "mnuNew"
 
834
 
 
835
    def __init__(self):
 
836
        Application.__init__(self)
 
837
 
 
838
 
 
839
    def write_text(self, text):
 
840
        """
 
841
        It writes text to the current buffer of the Gedit window.
 
842
 
 
843
        @type text: string
 
844
        @param text: The text string to be written to the current buffer.
 
845
        """
 
846
        Application.write_text(self, text, self.TXT_FIELD)
 
847
 
 
848
    def save(self, filename):
 
849
        """
 
850
        It tries to save the current opened buffer to the filename passed as parameter.
 
851
 
 
852
        TODO: It does not manage the overwrite dialog yet.
 
853
 
 
854
        @type filename: string
 
855
        @param filename: The name of the file to save the buffer to.
 
856
        """
 
857
        Application.save(self)
 
858
        ooldtp.context(self.name)
 
859
 
 
860
        try:
 
861
            ldtp.waittillguiexist(self.SAVE_DLG)
 
862
            save_dialog = ooldtp.context(self.SAVE_DLG)
 
863
        except ldtp.LdtpExecutionError:
 
864
            raise ldtp.LdtpExecutionError, "The Gedit save dialog was not found."
 
865
        try:
 
866
            save_dlg_txt_filename = save_dialog.getchild(self.SAVE_DLG_TXT_NAME)
 
867
        except ldtp.LdtpExecutionError:
 
868
            raise ldtp.LdtpExecutionError, "The filename txt field in Gedit save dialog was not found."
 
869
        try:
 
870
            ldtp.wait(2)
 
871
            save_dlg_txt_filename.settextvalue(filename)
 
872
        except ldtp.LdtpExecutionError:
 
873
           raise ldtp.LdtpExecutionError, "We couldn't write text."
 
874
 
 
875
        try:
 
876
            save_dlg_btn_save = save_dialog.getchild(self.SAVE_DLG_BTN_SAVE)
 
877
        except ldtp.LdtpExecutionError:
 
878
            raise ldtp.LdtpExecutionError, "The button Save in Gedit save dialog was not found."
 
879
        
 
880
        try:
 
881
            save_dlg_btn_save.click()
 
882
        except ldtp.LdtpExecutionError:
 
883
            raise ldtp.LdtpExecutionError, "There was an error when pushing the Save button."
 
884
 
 
885
        ldtp.waittillguinotexist(self.SAVE_DLG)
 
886
        ldtp.wait(1)
 
887
 
 
888
    def close(self, save=False, filename=''):
 
889
        """
 
890
        Given a gedit window, it tries to close the application.
 
891
        By default, it closes without saving. This behaviour can be changed to save (or save as) on close.
 
892
         
 
893
        @type save: boolean
 
894
        @param save: If True, the edited file will be saved on close.
 
895
 
 
896
        @type filename: string
 
897
        @param filename: The file name to save the buffer to 
 
898
        """
 
899
 
 
900
        # Exit using the Quit menu 
 
901
        try:
 
902
            gedit = ooldtp.context(self.name)
 
903
            try:
 
904
                quit_menu = gedit.getchild(self.MNU_QUIT)
 
905
            except ldtp.LdtpExecutionError:
 
906
                raise ldtp.LdtpExecutionError, "The quit menu was not found."
 
907
            quit_menu.selectmenuitem()
 
908
        except ldtp.LdtpExecutionError:
 
909
            raise ldtp.LdtpExecutionError, "Mmm, something went wrong when closing the application."
 
910
 
 
911
        question_dialog = None
 
912
        count = 0
 
913
        while not gedit.waittillguinotexist(guiTimeOut=1) and \
 
914
                count < 10:
 
915
            try:
 
916
                question_dialog = ooldtp.context(self.QUESTION_DLG)
 
917
            except:
 
918
                count += 1
 
919
            else:
 
920
                break
 
921
 
 
922
        # If the text has changed, the save dialog will appear
 
923
        if question_dialog:
 
924
            # Test if the file needs to be saved
 
925
            if save:
 
926
                try:
 
927
                    question_dlg_btn_save = question_dialog.getchild(self.QUESTION_DLG_BTN_SAVE)
 
928
                    question_dlg_btn_save.click()
 
929
                except ldtp.LdtpExecutionError:
 
930
                    # If the Save button was not found, we will try to find the Save As
 
931
                    try:
 
932
                        question_dlg_btn_save = question_dialog.getchild(self.QUESTION_DLG_BTN_SAVE_AS)
 
933
                        question_dlg_btn_save.click()
 
934
                    except ldtp.LdtpExecutionError:
 
935
                        raise ldtp.LdtpExecutionError, "The save or save as buttons in Gedit question dialog were not found."
 
936
 
 
937
                    try:
 
938
                        ldtp.waittillguiexist(self.SAVE_DLG)
 
939
                        save_dialog = ooldtp.context(self.SAVE_DLG)
 
940
                    except ldtp.LdtpExecutionError:
 
941
                        raise ldtp.LdtpExecutionError, "The Gedit save dialog was not found."
 
942
                    try:
 
943
                        save_dlg_txt_filename = save_dialog.getchild(self.SAVE_DLG_TXT_NAME)
 
944
                    except ldtp.LdtpExecutionError:
 
945
                        raise ldtp.LdtpExecutionError, "The filename txt field in Gedit save dialog was not found."
 
946
                    try:
 
947
                        ldtp.wait(2)
 
948
                        save_dlg_txt_filename.settextvalue(filename)
 
949
                    except ldtp.LdtpExecutionError:
 
950
                        raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
951
 
 
952
                    try:
 
953
                        save_dlg_btn_save = save_dialog.getchild(self.SAVE_DLG_BTN_SAVE)
 
954
                    except ldtp.LdtpExecutionError:
 
955
                        raise ldtp.LdtpExecutionError, "The save button in Gedit save dialog was not found."
 
956
        
 
957
                    try:
 
958
                        save_dlg_btn_save.click()
 
959
                    except ldtp.LdtpExecutionError:
 
960
                        raise ldtp.LdtpExecutionError, "There was an error when pushing the Save button."
 
961
 
 
962
                    ldtp.waittillguinotexist(self.SAVE_DLG)
 
963
            
 
964
            else:
 
965
                try:
 
966
                    question_dlg_btn_close = question_dialog.getchild(self.QUESTION_DLG_BTN_CLOSE)
 
967
                    question_dlg_btn_close.click()
 
968
                except ldtp.LdtpExecutionError:
 
969
                    raise ldtp.LdtpExecutionError, "It was not possible to click the close button."
 
970
 
 
971
            gedit.waittillguinotexist(guiTimeOut=20)
 
972
 
 
973
class PolicyKit(Application):
 
974
    """
 
975
    PolicyKit class manages the GNOME pop up that ask for password for admin activities.
 
976
    """
 
977
    WINDOW     = "dlg0"
 
978
    TXT_PASS   = "txtPassword"
 
979
    BTN_OK     = "btnOK"
 
980
    BTN_CANCEL = "btnCancel"
 
981
 
 
982
 
 
983
    def __init__(self, password):
 
984
        """
 
985
        UpdateManager class main constructor
 
986
        
 
987
        @type password: string
 
988
        @param password: User's password for administrative tasks.
 
989
 
 
990
        """
 
991
        Application.__init__(self)
 
992
        self.password = password
 
993
    
 
994
    def wait(self):
 
995
        """
 
996
        Wait for the pop up window asking for the password to appear.
 
997
 
 
998
        @return 1, if the gksu window exists, 0 otherwise.
 
999
        """
 
1000
        return ldtp.waittillguiexist(self.name)
 
1001
        
 
1002
    def set_password(self):
 
1003
        """
 
1004
        It enters the password in the text field and clicks enter. 
 
1005
        """
 
1006
        
 
1007
        ooldtp.context(self.name)
 
1008
        
 
1009
        try:
 
1010
            ldtp.enterstring (self.password)
 
1011
            ldtp.enterstring ("<enter>")
 
1012
            
 
1013
        except ldtp.LdtpExecutionError:
 
1014
            raise ldtp.LdtpExecutionError, "The PolicyKit txt field for the password was not found."
 
1015
   
 
1016
# TODO: Change this to use ooldtp
 
1017
#        try:
 
1018
#            btnOK = polKit.getchild(self.BTN_OK)
 
1019
#        except ldtp.LdtpExecutionError, msg:
 
1020
#            raise ldtp.LdtpExecutionError, "The GtkSudo OK button was not found."
 
1021
#          
 
1022
#        btnOK.click()
 
1023
    
 
1024
        #This also have problems because of the lack of accesibiliy information
 
1025
        #ldtp.waittillguinotexist (self.name)
 
1026
        
 
1027
    def cancel(self):
 
1028
        polKit = ooldtp.context(self.name)
 
1029
 
 
1030
        try:
 
1031
            cancelButton = polKit.getchild(self.BTN_CANCEL)
 
1032
        except ldtp.LdtpExecutionError:
 
1033
            raise ldtp.LdtpExecutionError, "The PolicyKit cancel button was not found."
 
1034
          
 
1035
        cancelButton.click()
 
1036
        ldtp.waittillguinotexist (self.name)