~zsombi/ubuntu-ui-toolkit/32-listitemactions-attached

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Gerry Boland, Juhapekka Piiroinen
  • Date: 2012-09-25 08:59:11 UTC
  • mfrom: (74.2.4 autopilot-tests-2)
  • Revision ID: tarmac-20120925085911-xer62zdjd2isce1c
[added] autopilot test for button. Approved by Gerry Boland, jenkins.

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
    """Tests for an enabled Button component."""
 
19
 
 
20
    test_qml = dedent("""\
 
21
    import QtQuick 1.1
 
22
    import Ubuntu.Components 0.1
 
23
 
 
24
    Button {
 
25
       id: button
 
26
       text: "Hello World"
 
27
    }
 
28
    """)
 
29
 
 
30
 
 
31
    def test_can_select_button(self):
 
32
        """Must be able to select the Qml button component."""
 
33
 
 
34
        btn = self.app.select_single('Button')
 
35
        self.assertThat(btn, Not(Is(None)))
 
36
 
 
37
 
 
38
    def test_clicked_signal_emitted(self):
 
39
        """Clicking the button component must emit the clicked() signal."""
 
40
 
 
41
        btn = self.app.select_single('Button')
 
42
        signal = btn.watch_signal('clicked()')
 
43
 
 
44
        self.mouse.move_to_object(btn)
 
45
        self.mouse.click()
 
46
 
 
47
        self.assertThat(signal.was_emitted, Equals(True))
 
48
        self.assertThat(signal.num_emissions, Equals(1))
 
49
 
 
50
 
 
51
    def test_entered_signal_emitted(self):
 
52
        """The hoveredChanged() signal must be emitted when the mouse hovers over
 
53
        the button. Then the hovered property should be true"""
 
54
 
 
55
        btn = self.app.select_single('Button')
 
56
        signal = btn.watch_signal('hoveredChanged()')
 
57
 
 
58
        self.mouse.move_to_object(btn)
 
59
 
 
60
        self.assertThat(signal.was_emitted, Equals(True))
 
61
        self.assertThat(signal.num_emissions, Equals(1))
 
62
        self.assertThat(btn.hovered, Eventually(Equals(True)))
 
63
 
 
64
 
 
65
    def test_exited_signal_emitted(self):
 
66
        """The hoveredChanged() signal must be emitted when the mouse is hovering
 
67
        over hovers the button, and is moved out. Then the hovered property should be 
 
68
        false"""
 
69
 
 
70
        btn = self.app.select_single('Button')
 
71
 
 
72
        # position mouse over button before starting to watch for signals
 
73
        self.mouse.move_to_object(btn)
 
74
        signal = btn.watch_signal('hoveredChanged()')
 
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
        self.assertThat(btn.hovered, Equals(False))
 
82
 
 
83
 
 
84
    def test_can_press_button(self):
 
85
        """Test that when we click and hold the button down it's pressed property is set."""
 
86
 
 
87
        btn = self.app.select_single('Button')
 
88
 
 
89
        self.mouse.move_to_object(btn)
 
90
        self.mouse.press()
 
91
        self.addCleanup(self.mouse.release)
 
92
 
 
93
        self.assertThat(btn.pressed, Eventually(Equals(True)))
 
94
 
 
95
 
 
96
class DisabledButtonTests(TavastiaTestCase):
 
97
    """Tests for a disabled button component."""
 
98
 
 
99
    test_qml = dedent("""\
 
100
    import QtQuick 1.1
 
101
    import Ubuntu.Components 0.1
 
102
 
 
103
    Button {
 
104
       id: button
 
105
       text: "Disabled button"
 
106
       enabled: false
 
107
    }
 
108
    """)
 
109
 
 
110
    def test_button_is_disabled(self):
 
111
        """Test that the button really is disabled."""
 
112
 
 
113
        btn = self.app.select_single('Button')
 
114
 
 
115
        self.assertThat(btn.enabled, Equals(False))
 
116
 
 
117
 
 
118
    def test_clicked_signal_not_emitted(self):
 
119
        """Clicking a disabled button must not emit the clicked() signal."""
 
120
 
 
121
        btn = self.app.select_single('Button')
 
122
        signal = btn.watch_signal('clicked()')
 
123
 
 
124
        self.mouse.move_to_object(btn)
 
125
        self.mouse.click()
 
126
 
 
127
        self.assertThat(signal.was_emitted, Equals(False))
 
128
        self.assertThat(signal.num_emissions, Equals(0))
 
129
 
 
130
 
 
131
class ButtonColorTests(TavastiaTestCase):
 
132
    """Tests for the button color properties."""
 
133
 
 
134
    test_qml = dedent("""\
 
135
    import QtQuick 1.1
 
136
    import Ubuntu.Components 0.1
 
137
 
 
138
    Button {
 
139
       id: button
 
140
       text: "Coloured Button"
 
141
       color: "#FFFF00"
 
142
       pressedColor: "#00FFFF"
 
143
    }
 
144
    """)
 
145
 
 
146
 
 
147
    def test_button_has_correct_color(self):
 
148
        """Button component must have correct color."""
 
149
 
 
150
        btn = self.app.select_single('Button')
 
151
        self.assertThat(btn.color, Equals([255,255,0,255]))
 
152
 
 
153
 
 
154
    def test_button_has_correct_pressed_color(self):
 
155
        """Button component must have correct pressed color."""
 
156
 
 
157
        btn = self.app.select_single('Button')
 
158
        self.assertThat(btn.pressedColor, Equals([0,255,255,255]))
 
159
 
 
160
 
 
161
    def test_button_color_changes_on_mouse_press(self):
 
162
        """Button color must change to pressedColor when pressed with mouse."""
 
163
 
 
164
        btn = self.app.select_single('Button')
 
165
 
 
166
        self.mouse.move_to_object(btn)
 
167
        self.mouse.press()
 
168
        self.addCleanup(self.mouse.release)
 
169
 
 
170
        # this is hacky because the base rectangle in the button has no name. If
 
171
        # the component were named this would be a much more readable test...
 
172
        btnbase = self.app.select_single('QDeclarativeRectangle', color=[0,255,255,255])
 
173
 
 
174
        self.assertThat(btnbase, Not(Is(None)))
 
175
 
 
176
 
 
177
# This is a little hack to allow you to launch the test_qml in the viewer so it
 
178
# shows up in the autopilot vis tool. Not really needed at all...
 
179
if __name__ == '__main__':
 
180
    ButtonColorTests('test_button_has_correct_color').launch_test_qml()