File: test_utils.py

package info (click to toggle)
dogtail 1.0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: python: 6,124; makefile: 56; sh: 7
file content (180 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import dogtail.predicate
import dogtail.tree
import dogtail.i18n
import os.path
import dogtail.config
import dogtail.version
import dogtail.utils
from gtkdemotest import GtkDemoTest, trap_stdout
import unittest
import time

"""
Unit tests for the dogtail.procedural API
"""

__author__ = "Zack Cerza <zcerza@redhat.com>"

dogtail.config.config.logDebugToFile = False
dogtail.config.config.logDebugToStdOut = True


class TestScreenshot(GtkDemoTest):
    def make_expected_and_compare(self, actual_path, jpg_tolerance=None):
        extension = actual_path.split('.')[-1]
        expected_path = actual_path.replace(extension, "expected." + extension)

        import os
        os.system("import -window root %s" % expected_path)

        command = ["compare", "-metric", "MAE",
                   actual_path, expected_path, "output"]
        import subprocess
        p = subprocess.Popen(command, stderr=subprocess.PIPE)
        output, error = p.communicate()

        import re
        m = re.search(r"\((.*)\)", str(error))
        self.assertTrue(0.1 >= float(m.group(1)))


    def test_screenshot_incorrect_timestamp(self):
        self.assertRaises(TypeError, dogtail.utils.screenshot, "timeStamp", None)


    def test_screenshot_default(self):
        actual_path = dogtail.utils.screenshot()
        self.make_expected_and_compare(actual_path)


    def test_screenshot_basename(self):
        actual_path = dogtail.utils.screenshot("basename")
        self.make_expected_and_compare(actual_path)


    def test_screenshot_no_time_stamp(self):
        actual_path = dogtail.utils.screenshot(timeStamp=False)
        self.make_expected_and_compare(actual_path)


    def test_screenshot_jpeg(self):
        actual_path = dogtail.utils.screenshot("basename.jpg")
        self.make_expected_and_compare(actual_path, jpg_tolerance=True)


    def test_screenshot_unknown_format(self):
        self.assertRaises(ValueError, dogtail.utils.screenshot, "basename.dat")


class TestRun(unittest.TestCase):
    def setUp(self):
        self.pid = None


    def tearDown(self):
        import os
        import signal
        if self.pid:
            os.kill(self.pid, signal.SIGKILL)
        os.system('killall gtk3-demo-application > /dev/null 2>&1')
        os.system('killall gtk3-demo-appli > /dev/null 2>&1')
        # Sleep just enough to let the app actually die.
        # AT-SPI doesn't like being hammered too fast.
        time.sleep(0.5)


    def test_run(self):
        self.pid = dogtail.utils.run('gtk3-demo')
        dogtail.tree.root.application('gtk3-demo')


    def test_run_wrong(self):
        self.pid = None
        with self.assertRaises(OSError):
            self.pid = dogtail.utils.run('gtk3-virtual-nonexisting-demo')


    def test_run_dumb(self):
        self.pid = dogtail.utils.run('gtk3-demo', dumb=True)
        dogtail.tree.root.application('gtk3-demo')


class TestDelay(unittest.TestCase):
    def test_doDelay_implicit(self):
        dogtail.utils.config.defaultDelay = 2.0
        start = time.time()
        dogtail.utils.doDelay()
        self.assertTrue(time.time() - start >= 2.0)


    def test_doDelay_explicit(self):
        dogtail.utils.config.defaultDelay = 1.0
        start = time.time()
        dogtail.utils.doDelay(2.0)
        self.assertTrue(time.time() - start >= 2.0)


    def test_doDelay_logger(self):
        dogtail.utils.config.defaultDelay = 2.0
        dogtail.utils.config.debugSleep = True
        output = trap_stdout(dogtail.utils.doDelay).split()
        self.assertEqual(len(output), 5)
        self.assertAlmostEqual(float(output[-1]), 2.0)
        dogtail.utils.config.debugSleep = False


class TestA11Y(unittest.TestCase):
    def test_bail_when_a11y_disabled(self):
        self.assertRaises(SystemExit, dogtail.utils.bailBecauseA11yIsDisabled)


    def test_enable_a11y(self):
        dogtail.utils.enableA11y()


class TestLock(unittest.TestCase):
    def tearDown(self):
        os.system("rm -rf /tmp/dogtail-test.lock*")


    def test_set_unrandomized_lock(self):
        test_lock = dogtail.utils.Lock(lockname='dogtail-test.lock', randomize=False)
        self.assertEqual(test_lock.lockdir, "/tmp/dogtail-test.lock")
        self.assertFalse(os.path.isdir(test_lock.lockdir))
        test_lock.lock()
        self.assertTrue(os.path.isdir(test_lock.lockdir))
        test_lock.unlock()
        self.assertFalse(os.path.isdir(test_lock.lockdir))


    def test_double_lock(self):
        test_lock = dogtail.utils.Lock(lockname='dogtail-test.lock', randomize=False, unlockOnExit=True)
        test_lock.lock()
        with self.assertRaises(OSError):
            test_lock.lock()


    def test_double_unlock(self):
        test_lock = dogtail.utils.Lock(lockname='dogtail-test.lock', randomize=False)
        test_lock.lock()
        test_lock.unlock()
        with self.assertRaises(OSError):
            test_lock.unlock()


    def test_randomize(self):
        test_lock = dogtail.utils.Lock(lockname='dogtail-test.lock', randomize=True)
        self.assertIn("/tmp/dogtail-test.lock", test_lock.lockdir)
        self.assertFalse(os.path.isdir(test_lock.lockdir))
        test_lock.lock()
        self.assertTrue(os.path.isdir(test_lock.lockdir))
        test_lock.unlock()
        self.assertFalse(os.path.isdir(test_lock.lockdir))


class TestI18N(unittest.TestCase):
    def test_load_all_translations_for_language(self):
        dogtail.i18n.loadAllTranslationsForLanguage('en_US')