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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import dogtail.config
import pyatspi
from dogtail.rawinput import absoluteMotion, relativeMotion, doubleClick, press, drag, dragWithTrajectory, \
pressKey, absoluteMotionWithTrajectory, release, checkCoordinates, click, keyCombo, typeText
from dogtail.tree import SearchError
from gtkdemotest import GtkDemoTest
dogtail.config.config.logDebugToFile = False
dogtail.config.config.logDebugToStdOut = True
"""
Unit tests for the dogtail.rawinput package
"""
class TestRawinput(GtkDemoTest):
def test_motion(self):
absoluteMotion(100, 100)
absoluteMotion(100, 100, mouseDelay=1)
absoluteMotion(-100, -100, check=False)
absoluteMotion(100, 100, mouseDelay=1, check=False)
relativeMotion(-10, -10)
absoluteMotion(100, 100)
relativeMotion(-10, -10, mouseDelay=1)
def test_motion_with_trajectory(self):
absoluteMotionWithTrajectory(100, 100, 120, 120)
absoluteMotionWithTrajectory(120, 120, 120, 120)
absoluteMotionWithTrajectory(120, 120, 130, 120)
absoluteMotionWithTrajectory(130, 120, 130, 130)
absoluteMotionWithTrajectory(130, 130, 150, 100, mouseDelay=0.1)
def test_check_coordinates_direct(self):
checkCoordinates(0, 0)
checkCoordinates(-0, -0)
checkCoordinates(0, 10)
checkCoordinates(10, 0)
with self.assertRaises(ValueError):
checkCoordinates(-1, 100)
with self.assertRaises(ValueError):
checkCoordinates(100, -1)
with self.assertRaises(ValueError):
checkCoordinates(-1, -1)
def test_check_coordinates_builtin(self):
with self.assertRaises(ValueError):
absoluteMotion(-5, 5)
with self.assertRaises(ValueError):
absoluteMotion(5, -5)
with self.assertRaises(ValueError):
absoluteMotionWithTrajectory(10, 10, -5, 5)
with self.assertRaises(ValueError):
absoluteMotionWithTrajectory(10, 10, 5, -5)
with self.assertRaises(ValueError):
absoluteMotionWithTrajectory(-5, 5, 10, 10)
with self.assertRaises(ValueError):
absoluteMotionWithTrajectory(5, -5, 10, 10)
with self.assertRaises(ValueError):
click(-5, 5)
with self.assertRaises(ValueError):
click(5, -5)
with self.assertRaises(ValueError):
doubleClick(-5, 5)
with self.assertRaises(ValueError):
doubleClick(5, -5)
with self.assertRaises(ValueError):
press(-5, 5)
with self.assertRaises(ValueError):
press(5, -5)
with self.assertRaises(ValueError):
release(-5, 5)
with self.assertRaises(ValueError):
release(5, -5)
def test_doubleClick(self):
btn = self.app.child('Builder')
doubleClick(btn.position[0], btn.position[1])
self.assertEqual(len([x for x in self.app.children if x.name in ['GtkBuilder demo', 'Builder']]), 2)
def test_click(self):
btn = self.app.child('Builder')
self.assertFalse(btn.focused)
click(btn.position[0], btn.position[1])
self.assertTrue(btn.focused)
def test_press_release(self):
self.runDemo('Builder')
try:
wnd = self.app.child('GtkBuilder demo', roleName='frame', recursive=False, retry=False)
except dogtail.tree.SearchError:
wnd = self.app.child('Builder', roleName='frame', recursive=False, retry=False)
btn = wnd.button('New')
press(btn.position[0] + btn.size[0] / 2, btn.position[1] + btn.size[1] / 2, check=False)
self.assertIn(pyatspi.STATE_ARMED, btn.getState().getStates())
release(btn.position[0] + btn.size[0] / 2, btn.position[1] + btn.size[1] / 2, check=False)
self.assertNotIn(pyatspi.STATE_ARMED, btn.getState().getStates())
def test_drag(self):
self.runDemo('Tool Palette')
win = self.app.window('Tool Palette')
src = win.findChildren(lambda x: x.roleName == 'button' and x.showing)[0]
dst = win.child(roleName='viewport')
drag((src.position[0] + src.size[0] / 2, src.position[1] + src.size[1] / 2),
(dst.position[0] + dst.size[0] / 2, dst.position[1] + dst.size[1] / 2))
def test_drag_with_trajectory(self):
self.runDemo('Tool Palette')
win = self.app.window('Tool Palette')
src = win.findChildren(lambda x: x.roleName == 'button' and x.showing)[0]
dst = win.child(roleName='viewport')
dragWithTrajectory((src.position[0] + src.size[0] / 2, src.position[1] + src.size[1] / 2),
(dst.position[0] + dst.size[0] / 2, dst.position[1] + dst.size[1] / 2))
def test_pressKey_no_such_key(self):
with self.assertRaises(KeyError):
pressKey("no such key")
def test_keyCombo_simple(self):
keyCombo('<End>')
self.assertTrue(self.app.child('Tree View').showing)
def test_keyCombo_multi(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)
textfield = wnd.child(roleName='text')
textfield.text = 'something'
keyCombo('<Control>a')
keyCombo('<Control>c')
keyCombo('<Control>v')
keyCombo('<Control>v')
self.assertEqual(textfield.text, 'somethingsomething')
def test_keyCombo_wrong_key(self):
with self.assertRaises(ValueError):
keyCombo('<WORK_FASTER_THAN_LIGHTSPEED>')
def test_typeText(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)
textfield = wnd.child(roleName='text')
typeText('something')
self.assertEqual(textfield.text, 'something')
|