a18142d5793fcd78b3546bdced29636f3c1da8fb
[situare] / tests / testmap / testmapfetcher.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QtTest/QtTest>
23 #include <QUrl>
24 #include <QImage>
25 #include <QNetworkAccessManager>
26 #include <QLabel>
27
28 #include "mapfetcher.h"
29 #include "networkaccessmanagermock.h"
30
31 /**
32 * @brief
33 *
34 * @class TestMapFetcher testmapfetcher.cpp "tests/testMap/testmapfetcher.cpp"
35 */
36 class TestMapFetcher : public QObject
37 {
38     Q_OBJECT
39 public:
40     /**
41     * @brief TestMapFetcher is a test class for MapFetcher class.
42     *
43     * @fn TestMapFetcher
44     */
45     TestMapFetcher();
46
47
48 private slots:
49     /**
50     * @brief Tests fetchImage method with empty URL as parameter.
51     *
52     * @fn testFetchImageEmptyURL
53     */
54     void testFetchImageEmptyURL();
55     /**
56     * @brief Tests fetchImage method with incorrect URL as parameter.
57     *
58     * @fn testFetchImageIncorrectURL
59     */
60     void testFetchImageIncorrectURL();
61     /**
62     * @brief Tests fetchImage method with correct URL as parameter.
63     *
64     * @fn testFetchImageCorrectURL
65     */
66     void testFetchImageCorrectURL();
67
68     void testFetchCorruptedImage();
69
70     /**
71     * @brief Tests fetchImage method 20 times with correct URLs
72     * as parameters.
73     *
74     * @fn testFetchImage20URLs
75     */
76     void testFetchImage20URLs();
77     /**
78     * @brief Tests fetchImage method 50 times with correct URLs
79     * as parameters.
80     *
81     * @fn testFetchImage50URLs
82     */
83     void testFetchImage50URLs();
84
85 private:
86     MapFetcher *mapFetcher;
87 };
88
89 TestMapFetcher::TestMapFetcher()
90 {
91     QNetworkAccessManager *manager = new QNetworkAccessManager();
92     NetworkAccessManagerMock *managerMock = new NetworkAccessManagerMock(manager, this);
93     mapFetcher = new MapFetcher(this, managerMock);
94 }
95
96 void TestMapFetcher::testFetchImageEmptyURL()
97 {
98     QUrl url("");
99     mapFetcher->fetchMapImage(url);
100 }
101
102 void TestMapFetcher::testFetchImageIncorrectURL()
103 {
104 //    QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString)));
105 //
106 //    QVERIFY(imageReceivedErrorSpy.isValid());
107 //
108 //    //Incorrect URL
109 //    QUrl url1("http://tile.openstreetmap.org/7/63/22.gi");
110 //    qDebug() << QTime::currentTime().toString();
111 //    mapFetcher->fetchMapImage(url1);
112 //    QTest::qWait(5000);
113 //    QCOMPARE(imageReceivedErrorSpy.count(), 1);
114 }
115
116 void TestMapFetcher::testFetchImageCorrectURL()
117 {
118 //    QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
119 //
120 //    QVERIFY(imageReceivedSpy.isValid());
121 //
122 //    //Correct URL
123 //    QUrl url2("http://tile.openstreetmap.org/7/63/42.png");
124 //    mapFetcher->fetchMapImage(url2);
125 //    QTest::qWait(2000);
126 //    QCOMPARE(imageReceivedSpy.count(), 1);
127 //    QList<QVariant> signalArgs2 = imageReceivedSpy.takeLast();
128 //    QCOMPARE(url2, signalArgs2.at(0).toUrl());
129 }
130
131 void TestMapFetcher::testFetchCorruptedImage()
132 {
133 //    QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
134 //
135 //    QVERIFY(imageReceivedSpy.isValid());
136 //
137 //    //Correct URL
138 //    QUrl url2("http://tile.openstreetmap.org/7/63/1.png");
139 //    mapFetcher->fetchMapImage(url2);
140 //    QTest::qWait(1000);
141 //    QCOMPARE(imageReceivedSpy.count(), 1);
142 //    QList<QVariant> signalArgs2 = imageReceivedSpy.takeLast();
143 //    QCOMPARE(url2, signalArgs2.at(0).toUrl());
144 }
145
146 void TestMapFetcher::testFetchImage20URLs()
147 {
148 //    QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
149 //    QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString)));
150 //
151 //    QVERIFY(imageReceivedSpy.isValid());
152 //
153 //    //20 requests
154 //    for (int i = 1; i < 3; ++i) {
155 //        for (int j = 0; j < 10; ++j) {
156 //            QUrl url(QString("http://tile.openstreetmap.org/18/23/%1%2.png").arg(i).arg(j));
157 //            mapFetcher->fetchMapImage(url);
158 //        }
159 //    }
160 //    QTest::qWait(2000);
161 //    int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count();
162 //    QCOMPARE(totalCount, 20);
163 }
164
165 void TestMapFetcher::testFetchImage50URLs()
166 {
167 //    QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
168 //    QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString)));
169 //
170 //    QVERIFY(imageReceivedSpy.isValid());
171 //
172 //    //50 requests
173 //    for (int i = 1; i < 6; ++i) {
174 //        for (int j = 0; j < 10; ++j) {
175 //            QUrl url(QString("http://tile.openstreetmap.org/18/24/%1%2.png").arg(i).arg(j));
176 //            mapFetcher->fetchMapImage(url);
177 //        }
178 //    }
179 //
180 //    QTest::qWait(5000);
181 //    int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count();
182 //    QCOMPARE(totalCount, 50);
183 }
184
185 QTEST_MAIN(TestMapFetcher)
186 #include "testmapfetcher.moc"