aboutsummaryrefslogblamecommitdiffstats
path: root/test/data/common_steps/helpers.py
blob: e1cfd4b12cc961f30826fee44eebb52c9f99f97c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591














































































































































































































































































































































































































































































































































































































                                                                               
#!/usr/bin/python
"""
Library for basic acceptance tests
Author: Martin Simon <msimon@redhat.com>
Version: 2 (2012-04-13)
"""
import sys
import re
import os
import logging
from dogtail.tree import root, SearchError
from dogtail.rawinput import keyCombo, click, typeText, absoluteMotion
from dogtail.predicate import GenericPredicate
from subprocess import Popen, PIPE
from iniparse import ConfigParser
from time import sleep, time

# Create a dummy unittest class to have nice assertions
import unittest


class dummy(unittest.TestCase):
    def runTest(self):
        assert True


def timeout(func, args=(), expected=True, equals=True,
            timeout=30, period=0.25):
    """This function waits until specified function returns required result"""
    mustend = int(time()) + timeout
    while int(time()) < mustend:
        res = func.__call__(args)
        if equals:
            if res == expected:
                return True
        else:
            if res != expected:
                return True
        sleep(period)
    return False


def check_for_errors(context):
    """Check that no error is displayed on Evolution UI"""
    # Don't try to check for errors on dead app
    if not context.app.instance or context.app.instance.dead:
        return
    alerts = context.app.instance.findChildren(
        GenericPredicate(roleName='alert'))
    if not alerts:
        # alerts can also return None
        return
    alerts = filter(lambda x: x.showing, alerts)
    if len(alerts) > 0:
        labels = alerts[0].findChildren(GenericPredicate(roleName='label'))
        messages = [x.name for x in labels]

        if alerts[0].name != 'Error' and alerts[0].showing:
            # Erase the configuration and start all over again
            #return Popen("pkill -u `whoami` -9 " + self.appCommand,
            os.system("pkill -u `whoami` /*%s* &> /dev/null" %
                      context.app.appCommand)
            sleep(3)
            os.system("pkill -u `whoami` -9 /*%s* &> /dev/null" %
                      context.app.appCommand)

            # Remove previous data
            for folder in context.app.clean_dirs:
                os.system("rm -rf %s > /dev/null" % folder)

            raise RuntimeError("Error occurred: %s" % messages)
        else:
            print("Error occurred: %s" % messages)


def getMiniaturesPosition(name):
    """Get a position of miniature on Overview"""
    miniatures = []

    over = root.application('gnome-shell').child(name='Overview')
    mini = over.parent.children[-1]
    if mini == over:
        #print "Overview is not active"
        return miniatures
    widgets = mini.findChildren(GenericPredicate(name=name, roleName='label'))

    for widget in widgets:
        (x, y) = widget.position
        (a, b) = widget.size
        miniatures.append((x + a / 2, y + b / 2 - 100))
    return miniatures


def getDashIconPosition(name):
    """Get a position of icon on Overview dash"""
    over = root.application('gnome-shell').child(name='Overview')
    button = over[2].child(name=name)
    (x, y) = button.position
    (a, b) = button.size
    return (x + a / 2, y + b / 2)


class App():
    """
    Does all basic events with app
    """
    def __init__(self, appName, critical=None, shortcut='<Control><Q>',
                 quitButton=None, timeout=10, forceKill=True,
                 parameters='', clean_dirs=None, polkit=False):
        """
        Initialize object App
        appName     command to run the app
        critical    what's the function we check? {start,quit}
        shortcut    default quit shortcut
        timeout     timeout for starting and shuting down the app
        forceKill   is the app supposed to be kill before/after test?
        parameters  has the app any params needed to start?
                    (only for startViaCommand)
        """
        self.appCommand = appName.lower()
        self.shortcut = shortcut
        self.timeout = timeout
        self.forceKill = forceKill
        self.critical = critical
        self.quitButton = quitButton
        # the result remains false until the correct result is verified
        self.result = False
        self.updateCorePattern()
        self.parameters = parameters
        self.internCommand = self.appCommand.lower()
        self.polkit = polkit
        self.polkitPass = 'redhat'
        self.clean_dirs = clean_dirs or []

        """
            Getting all necessary data from *.desktop file of the app
        """
        cmd = "rpm -qlf $(which %s)" % appName
        #cmd2 = "grep %s.desktop" % self.appCommand
        #is it enough to search for .desktop?; vhumpa: no
        cmd += '|grep "^/usr/share/applications/.*\%s.desktop$"' % appName
        logging.debug("cmd = %s", cmd)
        proc = Popen(cmd, shell=True, stdout=PIPE)
        #have to check if the command and its desktop file exist
        #raise Exception, "*.desktop file of the app not found"
        #print("*.desktop file of the app not found")
        output = proc.communicate()[0].rstrip()
        logging.debug("output = %s", output)
        self.desktopConfig = ConfigParser()
        self.desktopConfig.read(output)

    def end(self):
        """
        Ends the test with correct return value
        """
        if self.result:
            #print "PASS"
            sys.exit(0)
        else:
            sys.exit("FAIL")

    def updateResult(self, result):
        self.result = result

    def getName(self):
        return self.desktopConfig.get('Desktop Entry', 'name')

    def getCategories(self):
        return self.desktopConfig.get('Desktop Entry', 'categories')

    def getMenuGroups(self):
        """
        Convert group list to the one correct menu group
        """
        groupsList = self.getCategories().split(';')
        groupsList.reverse()
        groupConversionDict = {
            'Accessibility': 'Universal Access',
            'System': 'System Tools',
            'Development': 'Programming',
            'Network': 'Internet',
            'Office': 'Office',
            'Graphics': 'Graphics',
            'Game': 'Games',
            'Education': 'Education',
            'Utility': 'Accessories',
            'AudioVideo': 'Sound &amp; Video'
        }

        for i in groupsList:
            if i in groupConversionDict:
                return groupConversionDict[i]

    def isRunning(self):
        """
        Is the app running?
        """
        #print "*** Checking if '%s' is running" % self.internCommand
        app = root

        apps = root.applications()
        for i in apps:
            if i.name.lower() == self.internCommand:
                app = i
                break

        if app.isChild(roleName='frame', recursive=False):
            #print "*** The app '%s' is running" % self.internCommand
            return True
        else:
            #print "*** The app '%s' is not running" % self.internCommand
            return False

    def kill(self):
        """
        Kill the app via 'killall'
        """
        #print "*** Killing all '%s' instances" % self.appCommand
        #return Popen("killall " + self.appCommand, shell = True).wait()
        return Popen("pkill -u `whoami` -9 " + self.appCommand,
                     shell=True).wait()

    def updateCorePattern(self):
        """
        Update string in /proc/sys/kernel/core_pattern to catch
        possible return code
        """
        #Popen("sudo rm -rf /tmp/cores", shell = True).wait()
        #Popen("mkdir /tmp/cores", shell = True).wait()
        #Popen("chmod a+rwx /tmp/cores", shell = True).wait()
        #Popen("echo \"/tmp/cores/core.%e.%s.%p\" \
        #       | sudo tee /proc/sys/kernel/core_pattern", shell = True).wait()
        pass

    def existsCoreDump(self):
        """
        Check if there is core dump created
        """
        #dirPath = "/tmp/cores/"
        #files = os.listdir(dirPath)
        #regexp = "core\.%s\.[0-9]{1,3}\.[0-9]*" % self.appCommand
        #for f in files:
        #    if re.match(regexp, f):
        #        return int(f.split(".")[2])
        return 0

    def startViaMenu(self):
        """
        Start the app via Gnome Shell menu
        """
        internCritical = (self.critical == 'start')

        #check if the app is running
        if self.forceKill and self.isRunning():
            self.kill()
            sleep(2)
            if self.isRunning():
                if internCritical:
                    self.updateResult(False)
                #print "!!! The app is running but it shouldn't be"
                return False
            else:
                #print "*** The app has been killed succesfully"
                pass

        try:
            #panel button Activities
            gnomeShell = root.application('gnome-shell')
            activities = gnomeShell.child(name='Activities', roleName='label')
            activities.click()
            sleep(6)  # time for overview to appear

            #menu Applications
            x, y = getDashIconPosition('Show Applications')
            absoluteMotion(x, y)
            sleep(1)
            click(x, y)
            sleep(4)  # time for all the oversized app icons to appear

            #submenu that contains the app
            submenu = gnomeShell.child(name=self.getMenuGroups(),
                                       roleName='list item')
            submenu.click()
            sleep(4)

            #the app itself
            app = gnomeShell.child(name=self.getName(), roleName='label')
            app.click()

            #if there is a polkit
            if self.polkit:
                sleep(3)
                typeText(self.polkitPass)
                keyCombo('<Enter>')

            sleep(self.timeout)

            if self.isRunning():
                #print "*** The app started successfully"
                if internCritical:
                    self.updateResult(True)
                return True
            else:
                #print "!!! The app is not running but it should be"
                if internCritical:
                    self.updateResult(False)
                return False
        except SearchError:
            #print "!!! Lookup error while passing the path"
            if internCritical:
                self.updateResult(False)
            return False

    def startViaCommand(self):
        """
        Start the app via command
        """
        internCritical = (self.critical == 'start')
        if self.forceKill and self.isRunning():
            self.kill()
            sleep(2)
            if self.isRunning():
                if internCritical:
                    self.updateResult(False)
                #print "!!! The app is running but it shouldn't be"
                return False
            else:
                pass
                #print "*** The app has been killed succesfully"

        returnValue = 0
        command = "%s %s &" % (self.appCommand, self.parameters)
        import os
        os.system(command)
        returnValue = 1
        #returnValue = utilsRun(command, timeout = 1, dumb = True)

        #if there is a polkit
        if self.polkit:
            sleep(3)
            typeText(self.polkitPass)
            keyCombo('<Enter>')

        start_time = 0
        while start_time < self.timeout:
            if self.isRunning():
                break
            sleep(0.5)
            start_time += 0.5

        #check the returned values
        if returnValue is None:
            if internCritical:
                self.updateResult(False)
            #print "!!! The app command could not be found"
            return False
        else:
            if self.isRunning():
                if internCritical:
                    self.updateResult(True)
                #print "*** The app started successfully"
                return True
            else:
                if internCritical:
                    self.updateResult(False)
                    #print "!!! The app did not started despite " \
                    #        + "the fact that the command was found"
                return False

    def closeViaShortcut(self):
        """
        Close the app via shortcut
        """
        internCritical = (self.critical == 'quit')

        if not self.isRunning():
            if internCritical:
                self.updateResult(False)
            #print "!!! The app does not seem to be running"
            return False

        keyCombo(self.shortcut)
        sleep(self.timeout)

        if self.isRunning():
            if self.forceKill:
                self.kill()
            if internCritical:
                self.updateResult(False)
            #print "!!! The app is running but it shouldn't be"
            return False
        else:
            if self.existsCoreDump() != 0:
                if internCritical:
                    self.updateResult(False)
                # print "!!! The app closed with core dump created. Signal %d"\
                #        % self.existsCoreDump()
                return False
            if internCritical:
                self.updateResult(True)
            #print "*** The app was successfully closed"
            return True

    def closeViaMenu(self):
        """
        Close app via menu button
        """
        internCritical = (self.critical == 'quit')

        if not self.isRunning():
            if internCritical:
                self.updateResult(False)
            #print "!!! The app does not seem to be running"
            return False

        #bind to the right app
        app = root
        apps = root.applications()
        for i in apps:
            if i.name.lower() == self.internCommand:
                app = i
                break
        app = app  # variable app is not used FIXME

        #try to bind the menu and the button
        try:
            firstSubmenu = self.getMenuNth(0)
            firstSubmenu.click()
            length = len(firstSubmenu.children)
            closeButton = firstSubmenu.children[length - 1]
            if self.quitButton is None:
                while re.search('(Close|Quit|Exit)', closeButton.name) is None:
                    length = length - 1
                    closeButton = firstSubmenu.children[length]
                    if length < 0:
                        if internCritical:
                            self.update(False)
                        #print "!!! The app quit button coldn't be found"
                        return False
            else:
                closeButton = firstSubmenu.child(self.quitButton)
        except SearchError:
            if internCritical:
                self.updateResult(False)
            #print "!!! The app menu bar or the quit button could'n be found"
            if self.forceKill:
                self.kill()
            return False

        sleep(2)  # timeout until menu appear
        #print "*** Trying to click to '%s'" % closeButton
        closeButton.click()
        sleep(self.timeout)

        if self.isRunning():
            if self.forceKill:
                self.kill()
            if internCritical:
                self.updateResult(False)
            #print "!!! The app is running but it shouldn't be"
            return False
        else:
            if self.existsCoreDump() != 0:
                if internCritical:
                    self.updateResult(False)
                # print "!! The app closed with core dump created. Signal %d" \
                # % self.existsCoreDump()
                return False
            if internCritical:
                self.updateResult(True)
            #print "*** The app was successfully closed"
            return True

    def getMenuNamed(self, menuName):
        """
        Return submenu with name specified with 'menuName'
        """
        #bind to the right app
        app = root
        apps = root.applications()
        for i in apps:
            if i.name.lower() == self.internCommand:
                app = i
                break

        #try to bind the menu and the button
        try:
            appMenu = app.child(roleName='menu bar')
            return appMenu.child(name=menuName)
        except:
            return None

    def getMenuNth(self, nth):
        """
        Return nth submenu
        """
        #bind to the right app
        app = root
        apps = root.applications()
        for i in apps:
            if i.name.lower() == self.internCommand:
                app = i
                break

        #try to bind the menu and the button
        try:
            appMenu = app.child(roleName='menu bar')
            return appMenu.children[nth]
        except:
            return None

    def getGnomePanelMenu(self):
        """
        Return object of gnome-panel menu of the app
        """
        try:
            app = root.application('gnome-shell')
            menu = app.child(roleName='menu')
            return menu.child(self.getName(), roleName='label')
        except:
            raise Exception("Gnome-panel menu of the app could not be found!")

    def openGnomePanelMenu(self):
        """
        Click to the gnome-panel menu
        """
        menu = self.getGnomePanelMenu()
        if menu is not None:
            menu.click()
        else:
            raise Exception("Gnome-panel menu of the app could not be found!")

    def getGnomePanelMenuItems(self):
        """
        Return a list of objects in gnome-panel app menu
        """
        quitButton = self.getGnomePanelQuit()
        if quitButton is None:
            return []
        else:
            return quitButton.get_parent().children

    def getGnomePanelQuit(self):
        """
        Return object of Quit button in gnome-panel menu
        It's the only way how to find out the gnome-panel menu...
        """
        try:
            return root.application('gnome-shell').child('Quit')
        except:
            raise Exception("Quit menu item at gnome-panel menu" +
                            " could not be found!")

    def closeViaGnomePanel(self):
        """
        Close the app via menu at gnome-panel
        """
        internCritical = (self.critical == 'quit')

        if not self.isRunning():
            if internCritical:
                self.updateResult(False)
            #print "!!! The app does not seem to be running"
            return False

        self.openGnomePanelMenu()
        closeButton = self.getGnomePanelQuit()

        sleep(2)  # timeout until menu appear
        #print "*** Trying to click to '%s'" % closeButton
        closeButton.click()
        sleep(self.timeout)

        if self.isRunning():
            if self.forceKill:
                self.kill()
            if internCritical:
                self.updateResult(False)
            #print "!!! The app is running but it shouldn't be"
            return False
        else:
            if self.existsCoreDump() != 0:
                if internCritical:
                    self.updateResult(False)
                # print "!!! The app closed with core dumps. Signal %d" % \
                #        self.existsCoreDump()
                return False
            if internCritical:
                self.updateResult(True)
            #print "*** The app was successfully closed"
            return True