Modified mainwindow.cpp fetch method.
[situare] / tests / testMap / testmapfetcher.cpp
1 #include <QtTest/QtTest>
2 #include <QUrl>
3 #include <QImage>
4 #include <QNetworkAccessManager>
5
6 #include "mapfetcher.h"
7
8 class TestMapFetcher : public QObject
9 {
10     Q_OBJECT
11 public:
12     TestMapFetcher();
13
14
15 private slots:
16     void testFetchImage();
17     //void testDownloadFinished();
18     void testSignals();
19
20
21 private:
22     MapFetcher *mapFetcher;
23 };
24
25 TestMapFetcher::TestMapFetcher()
26 {
27     mapFetcher = new MapFetcher();
28 }
29
30 void TestMapFetcher::testFetchImage()
31 {
32     QUrl url("");
33     mapFetcher->fetchMapImage(url);
34 }
35
36 /*void TestMapFetcher::testDownloadFinished()
37 {
38     NetworkReplyDummy *reply = new NetworkReplyDummy();
39     reply->setError(QNetworkReply::ConnectionRefusedError, QString("Connection refused"));
40
41     QSignalSpy errorSpy(mapFetcher, SIGNAL(error(QString)));
42     mapFetcher->downloadFinished(reply);
43     QTest::qWait(500);
44
45     QCOMPARE(errorSpy.count(), 1);
46
47     QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
48     //QList<QVariant> signalArgs1 = imageReceivedErrorSpy.takeFirst();
49 }*/
50
51 void TestMapFetcher::testSignals()
52 {
53     QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)));
54     QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString)));
55
56     QVERIFY(imageReceivedSpy.isValid());
57
58     //Incorrect URL
59     QUrl url1("http://tile.openstreetmap.org/7/63/22.gi");
60     mapFetcher->fetchMapImage(url1);
61     QTest::qWait(1000);
62     QCOMPARE(imageReceivedErrorSpy.count(), 1);
63     QList<QVariant> signalArgs1 = imageReceivedErrorSpy.takeFirst();
64     qDebug() << signalArgs1.at(0).toString();
65
66     /*//Correct URL
67     QUrl url2("http://tile.openstreetmap.org/7/63/42.png");
68     mapFetcher->fetchMapImage(url2);
69     QTest::qWait(1000);
70     QCOMPARE(imageReceivedSpy.count(), 1);
71     QList<QVariant> signalArgs2 = imageReceivedSpy.takeLast();
72     QCOMPARE(url2, signalArgs2.at(0).toUrl());
73
74     //20 requests immediately
75     for (int i = 1; i < 3; ++i) {
76         for (int j = 0; j < 10; ++j) {
77             QUrl url(QString("http://tile.openstreetmap.org/7/53/%1%2.png").arg(i).arg(j));
78             mapFetcher->fetchMapImage(url);
79         }
80     }
81     QTest::qWait(400);
82
83     //50 requests immediately
84     for (int i = 1; i < 6; ++i) {
85         for (int j = 0; j < 10; ++j) {
86             QUrl url(QString("http://tile.openstreetmap.org/7/63/%1%2.png").arg(i).arg(j));
87             mapFetcher->fetchMapImage(url);
88         }
89     }
90
91     QTest::qWait(2000);*/
92
93 }
94
95 QTEST_MAIN(TestMapFetcher)
96 #include "testmapfetcher.moc"