~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/application/gnome.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2010-04-14 14:05:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100414140534-grv0bwv9wv97khir
Tags: 0.2-0ubuntu1
* Mago tests updated for Lucid
  + Fixes arguments handling (LP: #562965)
  + Fixes seahorse tests (LP: #552618)
  + Fixes ubuntu-menu tests (LP: #551492)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- coding:utf-8 -*-
1
2
"""
2
3
This is the "gnome" module.
3
4
 
4
5
The gnome module provides wrappers for LDTP to make the write of Gnome tests easier. 
5
6
"""
6
7
import ooldtp
7
 
import ldtp 
 
8
import ldtp
8
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(role=self.EDITBAR_ROLE)
 
192
            value = editBar[0].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"
9
441
 
10
442
class Seahorse(Application):
11
443
    """
17
449
    NEWKEY_DLG   = "Create New ..."
18
450
    BTN_CONTINUE = "btnContinue"
19
451
    TYPE_PGP            = "PGP Key"
20
 
    NEWPGP_DLG          = "dlgCreateaPGPKey"
 
452
    NEWPGP_DLG          = "dlgNewPGPKey"
21
453
    DLG_NEWPGP_FULLNAME = "txtFullName"
22
454
    DLG_NEWPGP_EMAIL    = "txtEmailAddress"
23
455
    DLG_NEWPGP_COMMENT  = "txtComment"
346
778
 
347
779
        self.go_to_tab(tab_name)
348
780
        seahorse = ooldtp.context(self.name)
349
 
        return bool(seahorse.doesrowexist(self.TBL_PERSONAL_KEYS, name))
 
781
 
 
782
        selected_tab = seahorse.getchild(tab_name)
 
783
        table = selected_tab.getchild(role="table")
 
784
        
 
785
        return bool(table[0].doesrowexist(name))
350
786
 
351
787
    def remove_key(self, key):
352
788
        seahorse = ooldtp.context(self.name)