24
24
from tempfile import mktemp
25
25
from testtools import TestCase, skipIf
26
from testtools.matchers import IsInstance, Equals, raises
26
from testtools.matchers import (
32
from testscenarios import TestWithScenarios
27
33
from textwrap import dedent
28
34
from time import sleep
29
36
from unittest import SkipTest
30
37
from unittest.mock import patch
43
50
from autopilot.utilities import on_test_started
53
class ElapsedTimeCounter(object):
55
"""A simple utility to count the amount of real time that passes."""
58
self._start_time = timeit.default_timer()
61
def __exit__(self, *args):
65
def elapsed_time(self):
66
return timeit.default_timer() - self._start_time
46
69
class InputStackKeyboardBase(AutopilotTestCase):
354
class MockAppMouseTestBase(AutopilotTestCase):
356
def start_mock_app(self):
357
window_spec_file = mktemp(suffix='.json')
358
window_spec = {"Contents": "MouseTest"}
361
open(window_spec_file, 'w')
363
self.addCleanup(os.remove, window_spec_file)
365
return self.launch_test_application(
366
'window-mocker', window_spec_file, app_type='qt')
331
369
class MouseTestCase(AutopilotTestCase, tests.LogHandlerTestCase):
372
super(MouseTestCase, self).setUp()
373
self.device = Mouse.create()
333
375
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (Mouse)")
334
376
def test_move_to_nonint_point(self):
335
377
"""Test mouse does not get stuck when we move to a non-integer point.
340
382
screen_geometry = Display.create().get_screen_geometry(0)
341
device = Mouse.create()
342
383
target_x = screen_geometry[0] + 10
343
384
target_y = screen_geometry[1] + 10.6
344
device.move(target_x, target_y)
345
self.assertEqual(device.position(), (target_x, int(target_y)))
385
self.device.move(target_x, target_y)
386
self.assertEqual(self.device.position(), (target_x, int(target_y)))
347
388
@patch('autopilot.platform.model', new=lambda *args: "Not Desktop", )
348
389
def test_mouse_creation_on_device_raises_useful_error(self):
370
411
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (WinMocker)")
371
class TouchTests(AutopilotTestCase):
412
class TouchTests(MockAppMouseTestBase):
374
415
super(TouchTests, self).setUp()
379
420
self.button_status = self.app.select_single(
380
421
'QLabel', objectName='button_status')
382
def start_mock_app(self):
383
window_spec_file = mktemp(suffix='.json')
384
window_spec = {"Contents": "MouseTest"}
387
open(window_spec_file, 'w')
389
self.addCleanup(os.remove, window_spec_file)
391
return self.launch_test_application(
392
'window-mocker', window_spec_file, app_type='qt')
394
423
def test_tap(self):
395
424
x, y = get_center_point(self.widget)
396
425
self.device.tap(x, y)
477
506
self.assertThat(p.y, Equals(123))
509
@skipIf(platform.model() != "Desktop", "Window mocker only available on X11")
510
class InputEventDelayTests(MockAppMouseTestBase, TestWithScenarios):
513
('Touch', dict(input_class=Touch)),
514
('Mouse', dict(input_class=Mouse)),
518
super(InputEventDelayTests, self).setUp()
519
self.device = Pointer(self.input_class.create())
520
self.widget = self.get_mock_app_main_widget()
522
def get_mock_app_main_widget(self):
523
self.app = self.start_mock_app()
524
return self.app.select_single('MouseTestWidget')
526
def test_subsequent_events_delay(self):
527
with ElapsedTimeCounter() as time_counter:
529
self.device.click_object(self.widget, time_between_events=0.6)
531
self.assertThat(time_counter.elapsed_time, GreaterThan(1.0))
533
def test_subsequent_events_default_delay(self):
534
with ElapsedTimeCounter() as time_counter:
536
self.device.click_object(self.widget)
538
self.assertThat(time_counter.elapsed_time, GreaterThan(0.9))
480
541
class InputStackCleanupTests(TestCase):
482
543
def test_cleanup_called(self):