qtmeetings sources to Maemo garage
[qtmeetings] / tests / IO / CommunicationManager / TestCommunicationManager.cpp
1 #include <QtTest/QtTest>
2 #include "Configuration.h"
3 #include "ConnectionSettings.h"
4 #include "TestCommunicationManager.h"
5 #include "Communication.h"
6 #include "Room.h"
7 #include "Meeting.h"
8 #include <QString>
9 #include <QSignalSpy>
10 #include <QDateTime>
11
12 static const QString server = "192.168.0.35";
13 static const QString user   = "maemo";
14 static const QString pass   = "P@ssw0rd";
15 static const unsigned int refresh = 1;
16
17 void TestCommunicationManager::initTestCase()
18 {
19     ConnectionSettings *conn = new ConnectionSettings( server, user, pass, refresh );
20     QVERIFY( conn != 0 );
21         iMgr = new CommunicationManager( *conn );
22         QVERIFY( iMgr != 0 );
23     delete conn;
24     conn = NULL;
25 }
26
27 void TestCommunicationManager::cleanupTestCase()
28 {
29         delete iMgr;
30 }
31
32 void TestCommunicationManager::init() { }
33
34 void TestCommunicationManager::cleanup() { }
35
36 void TestCommunicationManager::testErrorSignal()
37 {
38         ConnectionSettings *conn = new ConnectionSettings( QString(""), user, pass, refresh );
39         CommunicationManager *mgr = new CommunicationManager( *conn );
40         delete conn;
41
42         QDateTime start = QDateTime::currentDateTime();
43         QDateTime end = start.addDays(14);
44         Room room( "Room name","Room address" );
45
46         connect( mgr,
47                          SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
48                          this,
49                          SLOT( handleError(int, CommunicationManager::CommunicationType  ) )
50                         );
51         
52         iError = 0;
53         mgr->fetchMeetings( start, end, room );
54         for(int i=0; iError == 0 && i < 5000/250; i++ )
55                 QTest::qWait( 250 );
56         
57         if( iError == 0 )
58         {
59                 QFAIL( "error signal not emitted" );
60         }
61         
62         delete mgr;
63 }
64
65 void TestCommunicationManager::testFetchMeetings()
66 {
67         qRegisterMetaType<CommunicationManager::CommunicationType>("CommunicationManager::CommunicationType");
68         QSignalSpy rp( iMgr, SIGNAL(readProgress( int, int, CommunicationManager::CommunicationType )) );
69         QCOMPARE( rp.isValid(), true );
70
71         connect( iMgr,
72                          SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
73                          this,
74                          SLOT( meetingsFetched( const QList<Meeting*>& ) )
75                         );
76         connect( iMgr,
77                          SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
78                          this,
79                          SLOT( handleError(int, CommunicationManager::CommunicationType  ) )
80                         );
81         QDateTime start = QDateTime::fromString( QString("04.05.2009"), QString("dd.MM.yyyy") );
82         QDateTime end = QDateTime::fromString( QString("09.05.2009"), QString("dd.MM.yyyy") );
83         Room room( "TestRoom", "meetingroomtest@test.local" );
84         iMgr->fetchMeetings( start, end, room );
85         iError = 0;
86         for(int i=0; iError == 0 && i < 5000/250; i++ )
87                 QTest::qWait( 250 );
88
89         if( iError == 0 )
90         {
91                 QFAIL("signal meetingsFetched not emitted");
92         }
93
94         QVERIFY( iMeetings.count() > 0 );
95         for( int i=0; i < iMeetings.count(); i++)
96         {
97                 Meeting *m = iMeetings[i];
98                 qDebug() << m->toString();
99         }
100
101         //verify readProgress
102         QCOMPARE( rp.count(), 1 );
103         QList<QVariant> args = rp.takeFirst();
104         QCOMPARE( args.at(0).type(), QVariant::Int );
105         QCOMPARE( args.at(1).type(), QVariant::Int );
106         QCOMPARE( args.at(2).typeName(), "CommunicationManager::CommunicationType" );
107 }
108
109 void TestCommunicationManager::testFetchMeetingDetails()
110 {
111         connect( iMgr,
112                          SIGNAL( meetingDetailsFetched( Meeting& ) ),
113                          this,
114                          SLOT( meetingDetailsFetched( Meeting& ) )
115                         );
116         Meeting* m = iMeetings[0];
117         QVERIFY( m->secondaryId().isEmpty() );
118 //      QVERIFY( !m->primaryId().isEmpty() );
119         iMgr->fetchMeetingDetails( *m );
120         iError = 0;
121         for(int i=0; iError == 0 && i < 5000/250; i++ )
122                 QTest::qWait( 250 );
123
124         if( iError == 0 )
125         {
126                 QFAIL("signal meetingDetailsFetched not emitted");
127         }
128         QVERIFY( !m->secondaryId().isEmpty() );
129         QVERIFY( m->detailsAvailable() );
130         qDebug() << m->toString();
131 }
132
133 void TestCommunicationManager::handleError( int aCode, CommunicationManager::CommunicationType aType )
134 {
135         QWARN("TestCommunicationManager::handleError");
136         QCOMPARE( aType, CommunicationManager::FetchingCommunication );
137         QVERIFY( aCode != 0 );
138         iError = aCode;
139 }
140
141 void TestCommunicationManager::meetingsFetched( const QList<Meeting*> &aMeetings )
142 {
143         iMeetings = aMeetings;
144         iError = 1;
145 }
146
147 void TestCommunicationManager::meetingDetailsFetched( Meeting &aDetailedMeeting )
148 {
149         QCOMPARE( &aDetailedMeeting, iMeetings[0] );
150         iError = 1;
151 }