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):
18
"""Tests for an enabled Button component."""
20
test_qml = dedent("""\
22
import Ubuntu.Components 0.1
31
def test_can_select_button(self):
32
"""Must be able to select the Qml button component."""
34
btn = self.app.select_single('Button')
35
self.assertThat(btn, Not(Is(None)))
38
def test_clicked_signal_emitted(self):
39
"""Clicking the button component must emit the clicked() signal."""
41
btn = self.app.select_single('Button')
42
signal = btn.watch_signal('clicked()')
44
self.mouse.move_to_object(btn)
47
self.assertThat(signal.was_emitted, Equals(True))
48
self.assertThat(signal.num_emissions, Equals(1))
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"""
55
btn = self.app.select_single('Button')
56
signal = btn.watch_signal('hoveredChanged()')
58
self.mouse.move_to_object(btn)
60
self.assertThat(signal.was_emitted, Equals(True))
61
self.assertThat(signal.num_emissions, Equals(1))
62
self.assertThat(btn.hovered, Eventually(Equals(True)))
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
70
btn = self.app.select_single('Button')
72
# position mouse over button before starting to watch for signals
73
self.mouse.move_to_object(btn)
74
signal = btn.watch_signal('hoveredChanged()')
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))
81
self.assertThat(btn.hovered, Equals(False))
84
def test_can_press_button(self):
85
"""Test that when we click and hold the button down it's pressed property is set."""
87
btn = self.app.select_single('Button')
89
self.mouse.move_to_object(btn)
91
self.addCleanup(self.mouse.release)
93
self.assertThat(btn.pressed, Eventually(Equals(True)))
96
class DisabledButtonTests(TavastiaTestCase):
97
"""Tests for a disabled button component."""
99
test_qml = dedent("""\
101
import Ubuntu.Components 0.1
105
text: "Disabled button"
110
def test_button_is_disabled(self):
111
"""Test that the button really is disabled."""
113
btn = self.app.select_single('Button')
115
self.assertThat(btn.enabled, Equals(False))
118
def test_clicked_signal_not_emitted(self):
119
"""Clicking a disabled button must not emit the clicked() signal."""
121
btn = self.app.select_single('Button')
122
signal = btn.watch_signal('clicked()')
124
self.mouse.move_to_object(btn)
127
self.assertThat(signal.was_emitted, Equals(False))
128
self.assertThat(signal.num_emissions, Equals(0))
131
class ButtonColorTests(TavastiaTestCase):
132
"""Tests for the button color properties."""
134
test_qml = dedent("""\
136
import Ubuntu.Components 0.1
140
text: "Coloured Button"
142
pressedColor: "#00FFFF"
147
def test_button_has_correct_color(self):
148
"""Button component must have correct color."""
150
btn = self.app.select_single('Button')
151
self.assertThat(btn.color, Equals([255,255,0,255]))
154
def test_button_has_correct_pressed_color(self):
155
"""Button component must have correct pressed color."""
157
btn = self.app.select_single('Button')
158
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."""
164
btn = self.app.select_single('Button')
166
self.mouse.move_to_object(btn)
168
self.addCleanup(self.mouse.release)
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])
174
self.assertThat(btnbase, Not(Is(None)))
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()