1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
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.
8
"""Tests for the Button Qml component."""
10
from autopilot.matchers import Eventually
11
from textwrap import dedent
12
from testtools.matchers import Is, Not, Equals
13
from testtools import skip
15
from tavastia.tests import TavastiaTestCase
17
class EnabledButtonTests(TavastiaTestCase):
19
"""Tests for an enabled Button component."""
21
test_qml = dedent("""\
23
import Ubuntu.Components 0.1
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)))
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()')
42
self.mouse.move_to_object(btn)
45
self.assertThat(signal.was_emitted, Equals(True))
46
self.assertThat(signal.num_emissions, Equals(1))
48
def test_entered_signal_emitted(self):
49
"""The entered() signal must be emitted when the mouse hovers the button.
51
Note: this test fails. It seems the entered() signal is never triggered.
55
btn = self.app.select_single('Button')
56
mouse_area = btn.get_children_by_type('QDeclarativeMouseArea')[0]
57
signal = mouse_area.watch_signal('entered()')
59
self.mouse.move_to_object(btn)
61
self.assertThat(signal.was_emitted, Equals(True))
62
self.assertThat(signal.num_emissions, Equals(1))
64
def test_exited_signal_emitted(self):
65
"""The exited() signal must be emitted when the mouse hovers the button.
67
Note: This test fails - it seems the exited() signal is never fired. This
68
seems like a but to me...
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()')
76
# assuming the WM will never put the window over 0,0:
79
self.assertThat(signal.was_emitted, Equals(True))
80
self.assertThat(signal.num_emissions, Equals(1))
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')
86
self.mouse.move_to_object(btn)
88
self.addCleanup(self.mouse.release)
90
self.assertThat(btn.pressed, Eventually(Equals(True)))
93
class DisabledButtonTests(TavastiaTestCase):
95
"""Tests for a disabled button component."""
97
test_qml = dedent("""\
99
import Ubuntu.Components 0.1
103
text: "Disabled button"
108
def test_button_is_disabled(self):
109
"""Test that the button really is disabled.
111
Note: this test fails. This seems like a legitimate bug to me...
113
btn = self.app.select_single('Button')
115
self.assertThat(btn.enabled, Equals(False))
117
def test_clicked_signal_not_emitted(self):
118
"""Clicking a disabled button must not emit the clicked() signal.
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?
124
btn = self.app.select_single('Button')
125
signal = btn.watch_signal('clicked()')
127
self.mouse.move_to_object(btn)
130
self.assertThat(signal.was_emitted, Equals(False))
131
self.assertThat(signal.num_emissions, Equals(0))
134
class ButtonColorTests(TavastiaTestCase):
136
"""Tests for the button color properties."""
138
test_qml = dedent("""\
140
import Ubuntu.Components 0.1
144
text: "Coloured Button"
146
pressedColor: "#00FFFF"
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]))
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]))
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')
165
self.mouse.move_to_object(btn)
167
self.addCleanup(self.mouse.release)
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])
173
self.assertThat(btnbase, Not(Is(None)))
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()