Remove unnecessary 'request' prefixes
[profile/ivi/ofono-qt.git] / tests / test_ofonointerface.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Alexander Kanavin <alexander.kanavin@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <QtTest/QtTest>
25 #include <QtCore/QObject>
26
27 #include <ofonointerface.h>
28
29 #include <QtDebug>
30
31 class TestOfonoInterface : public QObject
32 {
33     Q_OBJECT
34
35 private slots:
36
37     void initTestCase()
38     {
39         oi = new OfonoInterface("/phonesim", "org.ofono.Modem", OfonoGetAllOnStartup, this);
40
41         if (oi->properties()["Powered"].toBool() == false) {
42             oi->setProperty("Powered", qVariantFromValue(true));
43             QTest::qWait(5000);
44         }
45         if (oi->properties()["Online"].toBool() == false) {
46             oi->setProperty("Online", qVariantFromValue(true));
47             QTest::qWait(5000);
48         }
49     }
50
51     void testGetProperties()
52     {
53         QCOMPARE(oi->properties()["Manufacturer"].toString(), QString("MeeGo"));
54
55         oi_async = new OfonoInterface("/phonesim", "org.ofono.Modem", OfonoGetAllOnFirstRequest, this);
56         QCOMPARE(oi_async->properties().count(), 0);
57
58         QSignalSpy spy_request(oi_async, SIGNAL(requestPropertyComplete(bool, const QString &, const QVariant &)));
59         oi_async->requestProperty("Manufacturer");
60         oi_async->requestProperty("Model");
61
62         QCOMPARE(spy_request.count(), 1);
63         QVariantList list = spy_request.takeFirst();
64         QCOMPARE(list[0].toBool(), false);
65         QCOMPARE(list[1].toString(), QString("Model"));
66         QCOMPARE(oi_async->errorMessage(), QString("Already in progress"));
67
68         while (spy_request.count() != 1) {
69             QTest::qWait(100);
70         }
71         list = spy_request.takeFirst();
72         QCOMPARE(list[0].toBool(), true);
73         QCOMPARE(list[1].toString(), QString("Manufacturer"));
74         QCOMPARE(list[2].value<QVariant>().toString(), QString("MeeGo"));
75
76         oi_async->requestProperty("Model");
77         QCOMPARE(spy_request.count(), 1);
78         list = spy_request.takeFirst();
79         QCOMPARE(list[0].toBool(), true);
80         QCOMPARE(list[1].toString(), QString("Model"));
81         QCOMPARE(list[2].value<QVariant>().toString(), QString("Synthetic Device"));
82         
83         oi_async->requestProperty("UnknownProperty");
84         while (spy_request.count() != 1) {
85             QTest::qWait(100);
86         }
87         QCOMPARE(spy_request.count(), 1);
88         list = spy_request.takeFirst();
89         QCOMPARE(list[0].toBool(), false);
90         QCOMPARE(list[1].toString(), QString("UnknownProperty"));
91         QCOMPARE(oi_async->errorMessage(), QString("Property not available"));
92     }
93     
94     void testSetProperty()
95     {
96         QSignalSpy spy_changed(oi, SIGNAL(propertyChanged(const QString &, const QVariant &)));
97         QSignalSpy spy_failed(oi, SIGNAL(setPropertyFailed(const QString &)));
98         QVariantList list;
99         bool online;
100         bool online_found;
101     
102         oi->setProperty("Online", qVariantFromValue(false));
103         while (spy_changed.count() != 3 && spy_failed.count() == 0) {
104             QTest::qWait(100);
105         }
106         QCOMPARE(spy_changed.count(), 3);
107         QCOMPARE(spy_failed.count(), 0);
108         online = false;
109         online_found = false;
110         while (spy_changed.count() > 0) {
111             list = spy_changed.takeFirst();
112             if (list[0] == "Online") {
113                 online = list[1].value<QVariant>().toBool();
114                 online_found = true;
115             }
116         }
117         QCOMPARE(online_found, true);
118         QCOMPARE(online, false);
119         
120         oi->setProperty("Online", qVariantFromValue(true));
121         while (spy_changed.count() < 3 && spy_failed.count() == 0) {
122             QTest::qWait(100);
123         }
124         QVERIFY(spy_changed.count() > 3);
125         QCOMPARE(spy_failed.count(), 0);
126         online = false;
127         online_found = false;
128         while (spy_changed.count() > 0) {
129             list = spy_changed.takeFirst();
130             if (list[0] == "Online") {
131                 online = list[1].value<QVariant>().toBool();
132                 online_found = true;
133             }
134         }
135         QCOMPARE(online_found, true);
136         QCOMPARE(online, true);
137         QTest::qWait(5000);
138     }
139
140     void testSetPropertyFailed()
141     {
142         QSignalSpy spy_changed(oi, SIGNAL(propertyChanged(const QString &, const QVariant &)));
143         QSignalSpy spy_failed(oi, SIGNAL(setPropertyFailed(const QString &)));
144         QVariantList list;
145     
146         oi->setProperty("Manufacturer", qVariantFromValue(QString("Nokia")));
147         while (spy_changed.count() == 0 && spy_failed.count() == 0) {
148             QTest::qWait(100);
149         }
150         QCOMPARE(spy_changed.count(), 0);
151         QCOMPARE(spy_failed.count(), 1);
152         list = spy_failed.takeFirst();
153         QCOMPARE(list[0].toString(), QString("Manufacturer"));
154         QCOMPARE(oi->errorName(), QString("org.ofono.Error.InvalidArguments"));
155         QCOMPARE(oi->errorMessage(), QString("Invalid arguments in method call"));
156     }
157
158
159     void cleanupTestCase()
160     {
161
162     }
163
164
165 private:
166     OfonoInterface *oi;
167     OfonoInterface *oi_async;
168 };
169
170 QTEST_MAIN(TestOfonoInterface)
171 #include "test_ofonointerface.moc"