1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2011, Sebastian Wiesner <lunaryorn@googlemail.com>
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
8
# 1. Redistributions of source code must retain the above copyright notice,
9
# this list of conditions and the following disclaimer.
10
# 2. Redistributions in binary form must reproduce the above copyright
11
# notice, this list of conditions and the following disclaimer in the
12
# documentation and/or other materials provided with the distribution.
14
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
# POSSIBILITY OF SUCH DAMAGE.
26
from __future__ import (print_function, division, unicode_literals,
31
config = pytest.importorskip('synaptiks.kde.widgets.config')
33
from PyQt4.QtCore import pyqtSignal
34
from PyQt4.QtGui import QWidget, QHBoxLayout, QCheckBox, QLineEdit
37
class DummyConfig(dict):
39
A dummy configuration object for use in the tests.
44
return {'lineedit': 'spam', 'checkbox': False}
47
class DummyConfigWidget(QWidget, config.ConfigurationWidgetMixin):
50
PROPERTY_MAP = dict(QCheckBox='checked', QLineEdit='text')
51
CHANGED_SIGNAL_MAP = dict(QCheckBox='toggled', QLineEdit='textChanged')
53
configurationChanged = pyqtSignal(bool)
55
def __init__(self, config, parent=None):
56
QWidget.__init__(self, parent)
57
layout = QHBoxLayout(self)
58
self.setLayout(layout)
59
self.checkbox = QCheckBox(self)
60
self.checkbox.setObjectName('dummy_checkbox')
61
layout.addWidget(self.checkbox)
62
self.lineedit = QLineEdit(self)
63
self.lineedit.setObjectName('dummy_lineedit')
64
layout.addWidget(self.lineedit)
67
def change(self, text, check_state):
68
self.lineedit.setText(text)
69
self.checkbox.setChecked(check_state)
71
def check(self, text, check_state):
72
__tracebackhide__ = True
73
assert unicode(self.lineedit.text()) == text
74
assert self.checkbox.isChecked() == check_state
77
def pytest_funcarg__config(request):
78
return DummyConfig({'lineedit': 'spam', 'checkbox': False})
81
def pytest_funcarg__config_widget(request):
82
# we must have a qt app object before we can construct widgets
83
request.getfuncargvalue('qtapp')
84
return DummyConfigWidget(request.getfuncargvalue('config'))
87
class TestConfigurationWidgetMixin(object):
89
def test_setup_no_defaults_attribute(self, qtapp, config):
90
invalid_config = dict(config)
91
assert not hasattr(invalid_config, 'defaults')
92
with pytest.raises(TypeError) as exc_info:
93
DummyConfigWidget(invalid_config)
94
msg = 'The given configuration does not provide defaults'
95
assert str(exc_info.value) == msg
97
def test_setup(self, config_widget):
98
config_widget.check('spam', False)
100
def test_configuration_changed(self, config_widget):
102
config_widget.configurationChanged.connect(signal_calls.append)
103
config_widget.change('eggs', True)
104
assert signal_calls == [True, True]
106
config_widget.apply_configuration()
107
signal_calls == [False]
109
config_widget.load_defaults()
110
assert signal_calls == [True, True]
112
config_widget.load_configuration()
113
assert signal_calls == [True, False]
115
def test_is_configuration_changed(self, config_widget):
116
assert not config_widget.is_configuration_changed
117
config_widget.change('eggs', True)
118
assert config_widget.is_configuration_changed
119
config_widget.apply_configuration()
120
assert not config_widget.is_configuration_changed
122
def test_load_defaults(self, config_widget):
123
config_widget.change('eggs', True)
124
assert not config_widget.shows_defaults()
125
config_widget.load_defaults()
126
assert config_widget.shows_defaults()
127
config_widget.check('spam', False)
129
def test_shows_defaults(self, config, config_widget):
130
assert config_widget.shows_defaults()
131
config_widget.change('eggs', True)
132
assert not config_widget.shows_defaults()
134
def test_load_configuration(self, config, config_widget):
135
config['checkbox'] = True
136
config['lineedit'] = 'eggs'
137
config_widget.load_configuration()
138
config_widget.check('eggs', True)
140
def test_apply_configuration(self, config, config_widget):
141
config_widget.change('eggs', True)
142
config_widget.apply_configuration()
143
assert config == {'lineedit': 'eggs', 'checkbox': True}