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
|
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
from dogtail.procedural import focus, keyCombo, deselect, activate, select, click, tree, FocusError, run, config, type
from dogtail.tree import SearchError, ActionNotSupported, NotSensitiveError
from gtkdemotest import GtkDemoTest, trap_stdout
from time import sleep
import pyatspi
"""
Unit tests for the dogtail.procedural API
"""
__author__ = "Zack Cerza <zcerza@redhat.com>"
config.logDebugToFile = False
config.logDebugToStdOut = True
class GtkDemoTest(GtkDemoTest):
def setUp(self):
self.pid = run('gtk3-demo')
# Turn off activities overview
keyCombo('Esc')
self.app = focus.application.node
def tearDown(self):
import signal, os
os.kill(self.pid, signal.SIGKILL)
os.system('killall gedit > /dev/null 2>&1')
class TestFocusApplication(GtkDemoTest):
def test_throw_exception_on_focusing_bogus_name(self):
config.fatalErrors = True
self.assertRaises(FocusError, focus.application, "should not be found")
def test_focusing_basic(self):
"""
Ensure that focus.application() sets focus.application.node properly
"""
focus.application.node = None
focus.application("gtk3-demo")
self.assertEqual(focus.application.node, self.app)
def test_throw_exception_on_get_no_such_attribute(self):
with self.assertRaises(AttributeError):
focus.no_such_attribute
def test_throw_exception_on_get_no_such_attribute_when_node_doesnt_exist(self):
focus.application.node = None
with self.assertRaises(AttributeError):
focus.no_such_attribute
def test_throw_exception_on_set_no_such_attribute(self):
with self.assertRaises(AttributeError):
focus.no_such_attribute = 0
class TestFocusWindow(GtkDemoTest):
def test_focusing_bogus_name_without_a_fatal_error(self):
config.fatalErrors = False
output = trap_stdout(focus.window, "should not be found")
self.assertIsNone(focus.window.node)
def test_throw_exception_on_focusing_bogus_name(self):
config.fatalErrors = True
self.assertRaises(FocusError, focus.window, "should not be found")
class TestFocusDialog(GtkDemoTest):
def test_focusing_bogus_name_without_a_fatal_error(self):
config.fatalErrors = False
output = trap_stdout(focus.dialog, "should not be found")
self.assertIsNone(focus.dialog.node)
def test_throw_exception_on_focusing_bogus_name(self):
config.fatalErrors = True
self.assertRaises(FocusError, focus.dialog, "should not be found")
class TestFocusWidget(GtkDemoTest):
def test_focusing_empty_name(self):
self.assertRaises(TypeError, focus.widget)
def test_focusing_bogus_name_without_a_fatal_error(self):
config.fatalErrors = False
output = trap_stdout(focus.widget, "should not be found")
self.assertIsNone(focus.widget.node)
def test_throw_exception_on_focusing_bogus_name(self):
config.fatalErrors = True
self.assertRaises(FocusError, focus.widget, "should not be found")
def test_focusing_basic(self):
"""
Ensure that focus.widget('foo') finds a node with name 'foo'
"""
focus.widget("Application Class")
self.assertEqual(focus.widget.name, "Application Class")
class TestFocus(GtkDemoTest):
def test_initial_state(self):
"""
Ensure that focus.widget, focus.dialog and focus.window are None initially.
"""
self.assertIsNone(focus.widget.node)
self.assertIsNone(focus.dialog.node)
self.assertIsNone(focus.window.node)
def test_focusing_app(self):
"""
Ensure that focus.app() works
"""
focus.app.node = None
focus.app('gtk3-demo')
self.assertEqual(focus.app.node, self.app)
def test_focusing_app_via_application(self):
"""
Ensure that focus.application() works
"""
focus.app.node = None
focus.application('gtk3-demo')
self.assertEqual(focus.app.node, self.app)
def test_focus_getting_bogus_attribute(self):
self.assertRaises(AttributeError, getattr, focus, 'nosuchtype')
def test_focus_setting_bogus_attribute(self):
self.assertRaises(AttributeError, setattr, focus, 'nosuchtype', 'nothing')
def test_focusing_roleName(self):
"""
Ensure that focus.widget(roleName=...) works.
"""
focus.widget(roleName='page tab')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_PAGE_TAB)
def test_focus_menu(self):
self.runDemo('Builder')
focus.menu('File')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_MENU)
def test_focus_menuItem(self):
return
self.runDemo('Builder')
click.menu('File')
focus.menuItem('New')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_MENU_ITEM)
def test_focus_button(self):
self.runDemo('Builder')
focus.button('Open')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_PUSH_BUTTON)
def test_focus_table(self):
self.runDemo('Builder')
focus.table('')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_TABLE)
def test_focus_tableCell(self):
self.runDemo('Builder')
focus.tableCell('')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_TABLE_CELL)
def test_focus_text(self):
self.runDemo('Assistant')
focus.window('Page 1')
focus.text('')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_TEXT)
def test_focus_icon(self):
self.runDemo('Clipboard')
try:
wnd = self.app.child('Clipboard demo', roleName='frame', retry=False, recursive=False)
except SearchError:
wnd = self.app.child('Clipboard', roleName='frame', retry=False, recursive=False)
focus.window(wnd.name)
focus.icon('Warning')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_ICON)
class TestKeyCombo(GtkDemoTest):
def test_keyCombo(self):
self.runDemo('Builder')
keyCombo('<F7>')
res = False
try:
res = focus.dialog('About Builder demo')
except:
try:
res = focus.dialog('About GtkBuilder demo')
except:
pass
self.assertTrue(res)
def test_keyCombo_on_widget(self):
self.runDemo('Builder')
focus.button('Copy')
keyCombo('<F7>')
try:
res = focus.dialog('About Builder demo')
except:
try:
res = focus.dialog('About GtkBuilder demo')
except:
pass
self.assertTrue(res)
class TestActions(GtkDemoTest):
def test_click(self):
click('Source')
self.assertTrue(focus.widget.isSelected)
def test_click_on_invisible_element(self):
with self.assertRaises(ValueError):
click("Spinner")
def test_click_with_raw(self):
click('Source', raw=True)
self.assertTrue(focus.widget.isSelected)
def test_select(self):
select('Source')
self.assertTrue(focus.widget.isSelected)
def test_deselect(self):
type('Icon View')
click('Icon View')
type('+')
sleep(0.5)
self.runDemo('Icon View Basics')
try:
wnd = self.app.child('GtkIconView demo', roleName='frame', recursive=False, retry=False)
except SearchError:
wnd = self.app.child('Icon View Basics', roleName='frame', recursive=False, retry=False)
focus.window(wnd.name)
focus.widget(roleName='icon')
select()
deselect()
self.assertFalse(focus.widget.isSelected)
def test_typing_on_widget(self):
try:
self.runDemo('Dialog and Message Boxes', retry=False)
focus.window('Dialogs')
except SearchError:
self.runDemo('Dialogs and Message Boxes', retry=False)
focus.window('Dialogs and Message Boxes')
focus.widget(roleName='text')
type("hello world")
from time import sleep
sleep(0.1)
self.assertEqual(focus.widget.node.text, 'hello world')
def test_custom_actions(self):
activate("Combo Boxes")
self.assertEqual(focus.widget.node.text, 'Combo Boxes')
def test_blink_on_actions(self):
config.blinkOnActions = True
activate("Combo Boxes")
self.assertEqual(focus.widget.node.text, 'Combo Boxes')
def test_custom_actions_button(self):
try:
self.runDemo('Dialog and Message Boxes', retry=False)
focus.window('Dialogs')
except SearchError:
self.runDemo('Dialogs and Message Boxes', retry=False)
focus.window('Dialogs and Message Boxes')
click.button('Interactive Dialog')
self.assertTrue(focus.dialog("Interactive Dialog"))
def test_custom_actions_menu(self):
return
self.runDemo('Builder')
try:
wnd = self.app.child('GtkBuilder demo', roleName='frame', recursive=False, retry=False)
except SearchError:
wnd = self.app.child('Builder', roleName='frame', recursive=False, retry=False)
focus.window(wnd.name)
click.menu('File')
click.menuItem('New')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_MENU_ITEM)
def test_custom_actions_text(self):
self.runDemo('Builder')
try:
wnd = self.app.child('GtkBuilder demo', roleName='frame', recursive=False, retry=False)
except SearchError:
wnd = self.app.child('Builder', roleName='frame', recursive=False, retry=False)
focus.window(wnd.name)
click.text('')
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_TEXT)
def test_custom_actions_table_cell(self):
activate.tableCell("Combo Boxes")
self.assertTrue(isinstance(focus.widget.node, tree.Node))
self.assertEqual(focus.widget.node.role, pyatspi.ROLE_TABLE_CELL)
def test_throws_action_not_supported(self):
self.runDemo('Builder')
try:
wnd = self.app.child('GtkBuilder demo', roleName='frame', recursive=False, retry=False)
except SearchError:
wnd = self.app.child('Builder', roleName='frame', recursive=False, retry=False)
focus.window(wnd.name)
with self.assertRaises(ActionNotSupported) as cm:
activate.text('')
self.assertEqual(str(cm.exception), "Cannot do 'activate' action on [text | ]")
def test_action_on_insensitive(self):
self.runDemo("Assistant")
wnd = self.app.child("Page 1", roleName='frame')
child = wnd.child("Next")
config.ensureSensitivity = True
with self.assertRaises(NotSensitiveError):
output1 = trap_stdout(child.actions['click'].do())
config.ensureSensitivity = False
output2 = trap_stdout(child.actions['click'].do)
#self.assertEqual(output2.strip("\n"), "")
self.assertNotEqual(output2.strip("\n"), "") # we want the log
|