File: tst_device.cpp

package info (click to toggle)
lomiri-system-settings 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,484 kB
  • sloc: cpp: 15,892; python: 5,994; xml: 362; javascript: 80; makefile: 46; sh: 5
file content (210 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 3 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <QTest>
#include <QSignalSpy>
#include <QThread>

#include "bluetooth.h"
#include "device.h"
#include "fakebluez.h"

using namespace Bluez;

class DeviceTest: public QObject
{
    Q_OBJECT

private:
    FakeBluez *m_bluezMock;
    Device *m_device;
    QDBusConnection *m_dbus;

private:
    void processEvents(unsigned int msecs = 500);

private Q_SLOTS:
    void init();
    void testGetName();
    void testGetAddress();
    void testGetIconName();
    void testGetType();
    void testIsPaired();
    void testIsTrusted();
    void testGetConnection();
    void testGetStrength();
    void testGetPath();
    void testMakeTrusted();
    void testConnect();
    void testDisconnect();

    void cleanup();

};

void DeviceTest::processEvents(unsigned int msecs)
{
    QTimer::singleShot(msecs, [=]() { QCoreApplication::instance()->exit(); });
    QCoreApplication::instance()->exec();
}

void DeviceTest::init()
{
    qDBusRegisterMetaType<InterfaceList>();
    qDBusRegisterMetaType<ManagedObjectList>();

    m_bluezMock = new FakeBluez();
    m_bluezMock->addAdapter("new0", "bluetoothTest");
    m_bluezMock->addDevice("My Phone", "00:00:de:ad:be:ef");
#if MODERN_PYTHON_DBUSMOCK
    m_bluezMock->setProperty("/org/bluez/new0/dev_00_00_DE_AD_BE_EF", "org.bluez.Device1", "Class", QVariant(MOCK_PHONE_CLASS));
#else
    // Only this will set the 'Class' and 'Icon' properties for the device ...
    m_bluezMock->pairDevice("00:00:de:ad:be:ef");
#endif
    m_dbus = new QDBusConnection(m_bluezMock->dbus());

    QList<QString> devices = m_bluezMock->devices();
    if (devices.isEmpty())
        QFAIL("No devices in mock to be tested.");

    m_device = new Device(devices.first(), *m_dbus);

    processEvents();
}

void DeviceTest::cleanup()
{
    delete m_bluezMock;
    delete m_device;
}

void DeviceTest::testGetName()
{
    QCOMPARE(m_device->getName(), QString("My Phone"));
}

void DeviceTest::testGetAddress()
{
    QCOMPARE(m_device->getAddress(), QString("00:00:de:ad:be:ef"));
}

void DeviceTest::testGetIconName()
{
    QCOMPARE(m_device->getIconName(), QString("image://theme/phone-smartphone-symbolic"));
}

void DeviceTest::testGetType()
{
    QCOMPARE(m_device->getType(), Device::Type::Smartphone);
}

void DeviceTest::testIsPaired()
{
#if MODERN_PYTHON_DBUSMOCK
    m_bluezMock->pairDevice("00:00:de:ad:be:ef");

    processEvents();
#endif

    QCOMPARE(m_device->isPaired(), true);

    m_bluezMock->setProperty("/org/bluez/new0/dev_00_00_DE_AD_BE_EF", "org.bluez.Device1", "Paired", QVariant(false));

    processEvents();

    QCOMPARE(m_device->isPaired(), false);

    m_bluezMock->setProperty("/org/bluez/new0/dev_00_00_DE_AD_BE_EF", "org.bluez.Device1", "Paired", QVariant(true));

    processEvents();

    QCOMPARE(m_device->isPaired(), true);
}

void DeviceTest::testIsTrusted()
{
    QCOMPARE(m_device->isTrusted(), false);

    m_bluezMock->setProperty("/org/bluez/new0/dev_00_00_DE_AD_BE_EF", "org.bluez.Device1", "Trusted", QVariant(true));

    processEvents();

    QCOMPARE(m_device->isTrusted(), true);
}

void DeviceTest::testGetConnection()
{
    QCOMPARE(m_device->getConnection(), Device::Connection::Disconnected);
}

void DeviceTest::testGetStrength()
{
    QCOMPARE(m_device->getStrength(), Device::Strength::Fair);
}

void DeviceTest::testGetPath()
{
    QCOMPARE(m_device->getPath(),
             m_bluezMock->currentAdapterPath() + "/"
             + "dev_00_00_DE_AD_BE_EF");
}

void DeviceTest::testMakeTrusted()
{
    QCOMPARE(m_device->isTrusted(), false);

    m_device->makeTrusted(true);

    processEvents();

    QCOMPARE(m_device->isTrusted(), true);

    m_device->makeTrusted(false);

    processEvents();

    QCOMPARE(m_device->isTrusted(), false);
}

void DeviceTest::testConnect()
{
    m_device->connect();

    m_bluezMock->connectDevice("00:00:de:ad:be:ef");

    processEvents();

    QCOMPARE(m_device->getConnection(), Device::Connected);
}

void DeviceTest::testDisconnect()
{
    testConnect();

    m_device->disconnect();

    m_bluezMock->disconnectDevice("00:00:de:ad:be:ef");

    processEvents();

    QCOMPARE(m_device->getConnection(), Device::Disconnected);
}

QTEST_MAIN(DeviceTest)
#include "tst_device.moc"