~barry/autopilot/lp1488175

« back to all changes in this revision

Viewing changes to autopilot/tests/functional/test_input_stack.py

  • Committer: CI Train Bot
  • Author(s): Christopher Lee
  • Date: 2015-02-26 22:47:31 UTC
  • mfrom: (504.1.1 release-changelog-conflicts)
  • Revision ID: ci-train-bot@canonical.com-20150226224731-mth02f80vss1h5ev
Autopilot Release February 20th 2015.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import os
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 (
 
27
    IsInstance,
 
28
    Equals,
 
29
    raises,
 
30
    GreaterThan
 
31
)
 
32
from testscenarios import TestWithScenarios
27
33
from textwrap import dedent
28
34
from time import sleep
 
35
import timeit
29
36
from unittest import SkipTest
30
37
from unittest.mock import patch
31
38
 
43
50
from autopilot.utilities import on_test_started
44
51
 
45
52
 
 
53
class ElapsedTimeCounter(object):
 
54
 
 
55
    """A simple utility to count the amount of real time that passes."""
 
56
 
 
57
    def __enter__(self):
 
58
        self._start_time = timeit.default_timer()
 
59
        return self
 
60
 
 
61
    def __exit__(self, *args):
 
62
        pass
 
63
 
 
64
    @property
 
65
    def elapsed_time(self):
 
66
        return timeit.default_timer() - self._start_time
 
67
 
 
68
 
46
69
class InputStackKeyboardBase(AutopilotTestCase):
47
70
 
48
71
    scenarios = [
328
351
        )
329
352
 
330
353
 
 
354
class MockAppMouseTestBase(AutopilotTestCase):
 
355
 
 
356
    def start_mock_app(self):
 
357
        window_spec_file = mktemp(suffix='.json')
 
358
        window_spec = {"Contents": "MouseTest"}
 
359
        json.dump(
 
360
            window_spec,
 
361
            open(window_spec_file, 'w')
 
362
        )
 
363
        self.addCleanup(os.remove, window_spec_file)
 
364
 
 
365
        return self.launch_test_application(
 
366
            'window-mocker', window_spec_file, app_type='qt')
 
367
 
 
368
 
331
369
class MouseTestCase(AutopilotTestCase, tests.LogHandlerTestCase):
332
370
 
 
371
    def setUp(self):
 
372
        super(MouseTestCase, self).setUp()
 
373
        self.device = Mouse.create()
 
374
 
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.
338
380
 
339
381
        """
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)))
346
387
 
347
388
    @patch('autopilot.platform.model', new=lambda *args: "Not Desktop", )
348
389
    def test_mouse_creation_on_device_raises_useful_error(self):
368
409
 
369
410
 
370
411
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (WinMocker)")
371
 
class TouchTests(AutopilotTestCase):
 
412
class TouchTests(MockAppMouseTestBase):
372
413
 
373
414
    def setUp(self):
374
415
        super(TouchTests, self).setUp()
379
420
        self.button_status = self.app.select_single(
380
421
            'QLabel', objectName='button_status')
381
422
 
382
 
    def start_mock_app(self):
383
 
        window_spec_file = mktemp(suffix='.json')
384
 
        window_spec = {"Contents": "MouseTest"}
385
 
        json.dump(
386
 
            window_spec,
387
 
            open(window_spec_file, 'w')
388
 
        )
389
 
        self.addCleanup(os.remove, window_spec_file)
390
 
 
391
 
        return self.launch_test_application(
392
 
            'window-mocker', window_spec_file, app_type='qt')
393
 
 
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))
478
507
 
479
508
 
 
509
@skipIf(platform.model() != "Desktop", "Window mocker only available on X11")
 
510
class InputEventDelayTests(MockAppMouseTestBase, TestWithScenarios):
 
511
 
 
512
    scenarios = [
 
513
        ('Touch', dict(input_class=Touch)),
 
514
        ('Mouse', dict(input_class=Mouse)),
 
515
    ]
 
516
 
 
517
    def setUp(self):
 
518
        super(InputEventDelayTests, self).setUp()
 
519
        self.device = Pointer(self.input_class.create())
 
520
        self.widget = self.get_mock_app_main_widget()
 
521
 
 
522
    def get_mock_app_main_widget(self):
 
523
        self.app = self.start_mock_app()
 
524
        return self.app.select_single('MouseTestWidget')
 
525
 
 
526
    def test_subsequent_events_delay(self):
 
527
        with ElapsedTimeCounter() as time_counter:
 
528
            for i in range(3):
 
529
                self.device.click_object(self.widget, time_between_events=0.6)
 
530
 
 
531
        self.assertThat(time_counter.elapsed_time, GreaterThan(1.0))
 
532
 
 
533
    def test_subsequent_events_default_delay(self):
 
534
        with ElapsedTimeCounter() as time_counter:
 
535
            for i in range(10):
 
536
                self.device.click_object(self.widget)
 
537
 
 
538
        self.assertThat(time_counter.elapsed_time, GreaterThan(0.9))
 
539
 
 
540
 
480
541
class InputStackCleanupTests(TestCase):
481
542
 
482
543
    def test_cleanup_called(self):