~aacid/ubuntu-ui-toolkit/save_stat_loading_already_loaded_image

« back to all changes in this revision

Viewing changes to tests/autopilot/tavastia/tests/button/test_button.py

  • Committer: Juhapekka Piiroinen
  • Date: 2012-09-21 09:42:51 UTC
  • mto: (74.2.2 autopilot-tests-2)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: juhapekka.piiroinen@canonical.com-20120921094251-poyhyytiijfke43m
Added autopilot test template

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2012 Canonical
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License version 3, as published
 
6
# by the Free Software Foundation.
 
7
 
 
8
"""Tests for the Button Qml component."""
 
9
 
 
10
from autopilot.matchers import Eventually
 
11
from textwrap import dedent
 
12
from testtools.matchers import Is, Not, Equals
 
13
from testtools import skip
 
14
 
 
15
from tavastia.tests import TavastiaTestCase
 
16
 
 
17
class EnabledButtonTests(TavastiaTestCase):
 
18
 
 
19
    """Tests for an enabled Button component."""
 
20
 
 
21
    test_qml = dedent("""\
 
22
    import QtQuick 1.1
 
23
    import Ubuntu.Components 0.1
 
24
 
 
25
    Button {
 
26
       id: button
 
27
       text: "Hello World"
 
28
    }
 
29
    """)
 
30
 
 
31
 
 
32
    def test_can_select_button(self):
 
33
        """Must be able to select the Qml button component."""
 
34
        btn = self.app.select_single('Button')
 
35
        self.assertThat(btn, Not(Is(None)))
 
36
 
 
37
    def test_clicked_signal_emitted(self):
 
38
        """Clicking the button component must emit the clicked() signal."""
 
39
        btn = self.app.select_single('Button')
 
40
        signal = btn.watch_signal('clicked()')
 
41
 
 
42
        self.mouse.move_to_object(btn)
 
43
        self.mouse.click()
 
44
 
 
45
        self.assertThat(signal.was_emitted, Equals(True))
 
46
        self.assertThat(signal.num_emissions, Equals(1))
 
47
 
 
48
    def test_entered_signal_emitted(self):
 
49
        """The entered() signal must be emitted when the mouse hovers the button.
 
50
 
 
51
        Note: this test fails. It seems the entered() signal is never triggered.
 
52
        A real bug?
 
53
 
 
54
        """
 
55
        btn = self.app.select_single('Button')
 
56
        mouse_area = btn.get_children_by_type('QDeclarativeMouseArea')[0]
 
57
        signal = mouse_area.watch_signal('entered()')
 
58
 
 
59
        self.mouse.move_to_object(btn)
 
60
 
 
61
        self.assertThat(signal.was_emitted, Equals(True))
 
62
        self.assertThat(signal.num_emissions, Equals(1))
 
63
 
 
64
    def test_exited_signal_emitted(self):
 
65
        """The exited() signal must be emitted when the mouse hovers the button.
 
66
 
 
67
        Note: This test fails - it seems the exited() signal is never fired. This
 
68
        seems like a but to me...
 
69
 
 
70
        """
 
71
        btn = self.app.select_single('Button')
 
72
        self.mouse.move_to_object(btn)
 
73
        mouse_area = btn.get_children_by_type('QDeclarativeMouseArea')[0]
 
74
        signal = mouse_area.watch_signal('exited()')
 
75
 
 
76
        # assuming the WM will never put the window over 0,0:
 
77
        self.mouse.move(0,0)
 
78
 
 
79
        self.assertThat(signal.was_emitted, Equals(True))
 
80
        self.assertThat(signal.num_emissions, Equals(1))
 
81
 
 
82
    def test_can_press_button(self):
 
83
        """Test that when we click and hold the button down it's pressed property is set."""
 
84
        btn = self.app.select_single('Button')
 
85
 
 
86
        self.mouse.move_to_object(btn)
 
87
        self.mouse.press()
 
88
        self.addCleanup(self.mouse.release)
 
89
 
 
90
        self.assertThat(btn.pressed, Eventually(Equals(True)))
 
91
 
 
92
 
 
93
class DisabledButtonTests(TavastiaTestCase):
 
94
 
 
95
    """Tests for a disabled button component."""
 
96
 
 
97
    test_qml = dedent("""\
 
98
    import QtQuick 1.1
 
99
    import Ubuntu.Components 0.1
 
100
 
 
101
    Button {
 
102
       id: button
 
103
       text: "Disabled button"
 
104
       enabled: False
 
105
    }
 
106
    """)
 
107
 
 
108
    def test_button_is_disabled(self):
 
109
        """Test that the button really is disabled.
 
110
 
 
111
        Note: this test fails. This seems like a legitimate bug to me...
 
112
        """
 
113
        btn = self.app.select_single('Button')
 
114
 
 
115
        self.assertThat(btn.enabled, Equals(False))
 
116
 
 
117
    def test_clicked_signal_not_emitted(self):
 
118
        """Clicking a disabled button must not emit the clicked() signal.
 
119
 
 
120
        This test fails. I'm not sure if it's a symptom of the enabled property
 
121
        not working, or perhaps this is desired behavior?
 
122
 
 
123
        """
 
124
        btn = self.app.select_single('Button')
 
125
        signal = btn.watch_signal('clicked()')
 
126
 
 
127
        self.mouse.move_to_object(btn)
 
128
        self.mouse.click()
 
129
 
 
130
        self.assertThat(signal.was_emitted, Equals(False))
 
131
        self.assertThat(signal.num_emissions, Equals(0))
 
132
 
 
133
 
 
134
class ButtonColorTests(TavastiaTestCase):
 
135
 
 
136
    """Tests for the button color properties."""
 
137
 
 
138
    test_qml = dedent("""\
 
139
    import QtQuick 1.1
 
140
    import Ubuntu.Components 0.1
 
141
 
 
142
    Button {
 
143
       id: button
 
144
       text: "Coloured Button"
 
145
       color: "#FFFF00"
 
146
       pressedColor: "#00FFFF"
 
147
    }
 
148
    """)
 
149
 
 
150
 
 
151
    def test_button_has_correct_color(self):
 
152
        """Button component must have correct color."""
 
153
        btn = self.app.select_single('Button')
 
154
        self.assertThat(btn.color, Equals([255,255,0,255]))
 
155
 
 
156
    def test_button_has_correct_pressed_color(self):
 
157
        """Button component must have correct pressed color."""
 
158
        btn = self.app.select_single('Button')
 
159
        self.assertThat(btn.pressedColor, Equals([0,255,255,255]))
 
160
 
 
161
    def test_button_color_changes_on_mouse_press(self):
 
162
        """Button color must change to pressedColor when pressed with mouse."""
 
163
        btn = self.app.select_single('Button')
 
164
 
 
165
        self.mouse.move_to_object(btn)
 
166
        self.mouse.press()
 
167
        self.addCleanup(self.mouse.release)
 
168
 
 
169
        # this is hacky because the base rectangle in the button has no name. If
 
170
        # the component were named this would be a much more readable test...
 
171
        btnbase = self.app.select_single('QDeclarativeRectangle', color=[0,255,255,255])
 
172
 
 
173
        self.assertThat(btnbase, Not(Is(None)))
 
174
 
 
175
# This is a little hack to allow you to launch the test_qml in the viewer so it
 
176
# shows up in the autopilot vis tool. Not really needed at all...
 
177
if __name__ == '__main__':
 
178
    ButtonColorTests('test_button_has_correct_color').launch_test_qml()