~ubuntu-branches/ubuntu/raring/synaptiks/raring-proposed

« back to all changes in this revision

Viewing changes to tests/kde/widgets/test_config.py

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-05-21 17:49:59 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110521174959-wt5odxlwc5kssw3b
Tags: 0.6.1-0ubuntu1
* New upstream release.
* Update watch file.
* Remove the transitional package kde-config-synaptiks.
* Drop ${python:Breaks} as it's not used anymore by dh_python2.
* Add libxtst6, python-dbus and upower to Recommends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (c) 2011, Sebastian Wiesner <lunaryorn@googlemail.com>
 
3
# All rights reserved.
 
4
 
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are met:
 
7
 
 
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.
 
13
 
 
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.
 
25
 
 
26
from __future__ import (print_function, division, unicode_literals,
 
27
                        absolute_import)
 
28
 
 
29
import pytest
 
30
 
 
31
config = pytest.importorskip('synaptiks.kde.widgets.config')
 
32
 
 
33
from PyQt4.QtCore import pyqtSignal
 
34
from PyQt4.QtGui import QWidget, QHBoxLayout, QCheckBox, QLineEdit
 
35
 
 
36
 
 
37
class DummyConfig(dict):
 
38
    """
 
39
    A dummy configuration object for use in the tests.
 
40
    """
 
41
 
 
42
    @property
 
43
    def defaults(self):
 
44
        return {'lineedit': 'spam', 'checkbox': False}
 
45
 
 
46
 
 
47
class DummyConfigWidget(QWidget, config.ConfigurationWidgetMixin):
 
48
 
 
49
    NAME_PREFIX = 'dummy'
 
50
    PROPERTY_MAP = dict(QCheckBox='checked', QLineEdit='text')
 
51
    CHANGED_SIGNAL_MAP = dict(QCheckBox='toggled', QLineEdit='textChanged')
 
52
 
 
53
    configurationChanged = pyqtSignal(bool)
 
54
 
 
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)
 
65
        self._setup(config)
 
66
 
 
67
    def change(self, text, check_state):
 
68
        self.lineedit.setText(text)
 
69
        self.checkbox.setChecked(check_state)
 
70
 
 
71
    def check(self, text, check_state):
 
72
        __tracebackhide__ = True
 
73
        assert unicode(self.lineedit.text()) == text
 
74
        assert self.checkbox.isChecked() == check_state
 
75
 
 
76
 
 
77
def pytest_funcarg__config(request):
 
78
    return DummyConfig({'lineedit': 'spam', 'checkbox': False})
 
79
 
 
80
 
 
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'))
 
85
 
 
86
 
 
87
class TestConfigurationWidgetMixin(object):
 
88
 
 
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
 
96
 
 
97
    def test_setup(self, config_widget):
 
98
        config_widget.check('spam', False)
 
99
 
 
100
    def test_configuration_changed(self, config_widget):
 
101
        signal_calls = []
 
102
        config_widget.configurationChanged.connect(signal_calls.append)
 
103
        config_widget.change('eggs', True)
 
104
        assert signal_calls == [True, True]
 
105
        del signal_calls[:]
 
106
        config_widget.apply_configuration()
 
107
        signal_calls == [False]
 
108
        del signal_calls[:]
 
109
        config_widget.load_defaults()
 
110
        assert signal_calls == [True, True]
 
111
        del signal_calls[:]
 
112
        config_widget.load_configuration()
 
113
        assert signal_calls == [True, False]
 
114
 
 
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
 
121
 
 
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)
 
128
 
 
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()
 
133
 
 
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)
 
139
 
 
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}