~ken-vandine/ubuntu-system-settings/slotslayout

« back to all changes in this revision

Viewing changes to tests/plugins/bluetooth/tst_bluetooth.cpp

merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "bluetooth.h"
24
24
#include "device.h"
25
25
#include "agent.h"
 
26
#include "bluez_helper.h"
26
27
#include "fakebluez.h"
27
28
 
28
29
using namespace Bluez;
36
37
    Bluetooth *m_bluetooth;
37
38
    QDBusConnection *m_dbus;
38
39
 
 
40
    void processEvents(unsigned int msecs = 500);
 
41
    void setDiscovering(bool value);
 
42
 
39
43
private Q_SLOTS:
40
44
    void init();
41
45
    void testGotAdapter();
48
52
 
49
53
};
50
54
 
 
55
void BluetoothTest::processEvents(unsigned int msecs)
 
56
{
 
57
    QTimer::singleShot(msecs, [=]() { QCoreApplication::instance()->exit(); });
 
58
    QCoreApplication::instance()->exec();
 
59
}
 
60
 
 
61
void BluetoothTest::setDiscovering(bool value)
 
62
{
 
63
    m_bluezMock->setProperty(m_bluezMock->currentAdapterPath(),
 
64
                             BLUEZ_ADAPTER_IFACE,
 
65
                             "Discovering",
 
66
                             QVariant(value));
 
67
}
 
68
 
51
69
void BluetoothTest::init()
52
70
{
 
71
    qWarning() << "init test";
 
72
 
 
73
    qDBusRegisterMetaType<InterfaceList>();
 
74
    qDBusRegisterMetaType<ManagedObjectList>();
 
75
 
53
76
    m_bluezMock = new FakeBluez();
54
77
    m_bluezMock->addAdapter("new0", "bluetoothTest");
55
78
    m_dbus = new QDBusConnection(m_bluezMock->dbus());
56
79
    m_bluetooth = new Bluetooth(*m_dbus);
 
80
 
 
81
    processEvents();
57
82
}
58
83
 
59
84
void BluetoothTest::cleanup()
60
85
{
 
86
    qWarning() << "cleanup";
61
87
    delete m_bluezMock;
62
88
    delete m_bluetooth;
63
89
}
67
93
    QString expected = "bluetoothTest";
68
94
    QString result;
69
95
 
 
96
    processEvents();
 
97
 
70
98
    result = m_bluetooth->adapterName();
71
99
 
72
100
    QCOMPARE(result, expected);
74
102
 
75
103
void BluetoothTest::testStartDiscovery()
76
104
{
77
 
    bool expected = true;
78
105
    QVariant result;
79
106
 
80
 
    QSKIP("Fails due to a bug in bluez4 dbusmock template", SkipAll);
 
107
    // This is what our test expects the adapter to have set
 
108
    setDiscovering(false);
 
109
    processEvents();
81
110
 
82
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
83
 
                                      "org.bluez.Adapter",
 
111
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
112
                                      BLUEZ_ADAPTER_IFACE,
84
113
                                      "Discovering");
85
114
    qWarning() << result;
86
 
    QCOMPARE(result.toBool(), !expected);
 
115
    QCOMPARE(result.toBool(), false);
87
116
 
88
117
    m_bluetooth->startDiscovery();
89
 
 
90
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
91
 
                                      "org.bluez.Adapter",
 
118
    setDiscovering(true);
 
119
 
 
120
    processEvents();
 
121
 
 
122
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
123
                                      BLUEZ_ADAPTER_IFACE,
92
124
                                      "Discovering");
93
125
    qWarning() << result;
94
 
    QCOMPARE(result.toBool(), expected);
 
126
    QCOMPARE(result.toBool(), true);
95
127
}
96
128
 
97
129
void BluetoothTest::testStopDiscovery()
98
130
{
99
 
    bool expected = false;
100
131
    QVariant result;
101
132
 
102
 
    QSKIP("Fails due to a bug in bluez4 dbusmock template", SkipAll);
 
133
    // This is what our test expects the adapter to have set
 
134
    setDiscovering(true);
 
135
    processEvents();
103
136
 
104
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
105
 
                                      "org.bluez.Adapter",
 
137
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
138
                                      BLUEZ_ADAPTER_IFACE,
106
139
                                      "Discovering");
107
 
    QCOMPARE(result.toBool(), !expected);
 
140
    QCOMPARE(result.toBool(), true);
108
141
 
109
142
    m_bluetooth->stopDiscovery();
110
 
 
111
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
112
 
                                      "org.bluez.Adapter",
 
143
    setDiscovering(false);
 
144
 
 
145
    processEvents();
 
146
 
 
147
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
148
                                      BLUEZ_ADAPTER_IFACE,
113
149
                                      "Discovering");
114
 
    QCOMPARE(result.toBool(), expected);
 
150
    QCOMPARE(result.toBool(), false);
115
151
}
116
152
 
 
153
/*
 
154
 * NOTE: The bluez5 mock template currently doesn't send PropertiesChanged
 
155
 * events when StartDiscovery/StopDiscovery is called on the adapter interface.
 
156
 * To accomondate this we're calling the org.freedesktop.DBus.Properties.Set
 
157
 * method here manually to simulate a property change. However this means
 
158
 * that other than doing a dumb call to StartDiscovery/StopDiscovery nothing
 
159
 * else will happen when those methods are called of the Bluetooth class we're
 
160
 * testing here.
 
161
 *
 
162
 * This affects the following tested methods:
 
163
 * - Bluetooth::startDiscovering
 
164
 * - Bluetooth::stopDiscovery
 
165
 * - Bluetooth::toggleDiscovery
 
166
 */
 
167
 
117
168
void BluetoothTest::testToggleDiscovery()
118
169
{
119
170
    QVariant result;
120
171
 
121
 
    QSKIP("Fails due to a bug in bluez4 dbusmock template", SkipAll);
122
 
 
123
172
    m_bluetooth->stopDiscovery();
124
 
 
125
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
126
 
                                      "org.bluez.Adapter",
 
173
    setDiscovering(false);
 
174
 
 
175
    processEvents();
 
176
 
 
177
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
178
                                      BLUEZ_ADAPTER_IFACE,
127
179
                                      "Discovering");
128
180
    QCOMPARE(result.toBool(), false);
129
181
 
130
182
    m_bluetooth->toggleDiscovery();
131
 
 
132
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
133
 
                                      "org.bluez.Adapter",
 
183
    setDiscovering(true);
 
184
 
 
185
    processEvents();
 
186
 
 
187
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
188
                                      BLUEZ_ADAPTER_IFACE,
134
189
                                      "Discovering");
135
190
    QCOMPARE(result.toBool(), true);
136
191
 
137
192
    m_bluetooth->toggleDiscovery();
138
 
 
139
 
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapter(),
140
 
                                      "org.bluez.Adapter",
 
193
    setDiscovering(false);
 
194
 
 
195
    processEvents();
 
196
 
 
197
    result = m_bluezMock->getProperty(m_bluezMock->currentAdapterPath(),
 
198
                                      BLUEZ_ADAPTER_IFACE,
141
199
                                      "Discovering");
142
200
    QCOMPARE(result.toBool(), false);
143
201
}
149
207
    QCOMPARE(Bluetooth::isSupportedType(Device::Type::Tablet), false);
150
208
}
151
209
 
 
210
 
152
211
void BluetoothTest::testIsDiscovering()
153
212
{
154
213
    m_bluetooth->stopDiscovery();
 
214
    setDiscovering(false);
 
215
 
 
216
    processEvents();
155
217
 
156
218
    QCOMPARE(m_bluetooth->isDiscovering(), false);
157
219
 
158
220
    m_bluetooth->startDiscovery();
 
221
    setDiscovering(true);
 
222
 
 
223
    processEvents();
159
224
 
160
225
    QCOMPARE(m_bluetooth->isDiscovering(), true);
161
226
 
162
227
    m_bluetooth->toggleDiscovery();
 
228
    setDiscovering(false);
 
229
 
 
230
    processEvents();
163
231
 
164
232
    QCOMPARE(m_bluetooth->isDiscovering(), false);
165
233
 
166
234
    m_bluetooth->toggleDiscovery();
 
235
    setDiscovering(true);
 
236
 
 
237
    processEvents();
167
238
 
168
239
    QCOMPARE(m_bluetooth->isDiscovering(), true);
169
240
}