* Fixed routingreceiverasync test due a irritating quit without any console output...
[profile/ivi/genivi/genivi-audio-manager.git] / PluginRoutingInterfaceAsync / test / CAmRoutingReceiverAsync.cpp
1 /**
2  *  Copyright (c) 2012 BMW
3  *
4  *  \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
5  *
6  *  \copyright
7  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8  *  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  *  subject to the following conditions:
10  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13  *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14  *
15  *  For further information see http://www.genivi.org/.
16  */
17
18 #include "CAmRoutingReceiverAsync.h"
19 #include "config.h"
20 #include "CAmRoutingReceiver.h"
21 #include "TAmPluginTemplate.h"
22 #include "MockIAmRoutingReceive.h"
23 #include "shared/CAmDltWrapper.h"
24
25
26 using namespace am;
27 using namespace testing;
28
29 static CAmEnvironment* env;
30
31 am_domainID_t CAmEnvironment::mDomainIDCount = 0;
32
33
34 CAmEnvironment::CAmEnvironment() :
35         pSocketHandler(),
36         pRouter(NULL),
37         pReceiveInterface(NULL),
38         ptimerCallback(this, &CAmEnvironment::timerCallback)
39 {
40     DefaultValue<am_Error_e>::Set(E_OK); // Sets the default value to be returned.
41     env=this;
42 }
43
44 CAmEnvironment::~CAmEnvironment()
45 {
46 }
47
48 void CAmEnvironment::SetUp()
49 {
50     logInfo("RoutingSendInterface Test started ");
51
52     pReceiveInterface = new MockIAmRoutingReceive();
53
54     std::vector<int> domainIDs;
55     domainIDs.push_back(0);
56     domainIDs.push_back(1);
57
58     EXPECT_CALL(*env->pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&env->pSocketHandler), Return(E_OK)));
59     EXPECT_CALL(*env->pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleDomainRegister));
60     EXPECT_CALL(*env->pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleSourceRegister));
61     EXPECT_CALL(*env->pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleSinkRegister));
62     EXPECT_CALL(*env->pReceiveInterface,confirmRoutingReady(_,_)).Times(1);
63
64     IAmRoutingSend* (*createFunc)();
65     void* tempLibHandle = NULL;
66     std::string libname("../plugins/routing/libPluginRoutingInterfaceAsync.so");
67     createFunc = getCreateFunction<IAmRoutingSend*()>(libname, tempLibHandle);
68     if (!createFunc)
69     {
70         libname = "/usr/lib/audioManager/routing/libPluginRoutingInterfaceAsync.so";
71         createFunc = getCreateFunction<IAmRoutingSend*()>(libname, tempLibHandle);
72
73         if(pReceiveInterface)
74                 delete pReceiveInterface, pReceiveInterface = NULL;
75         fprintf(stderr, "RoutingSendInterface Test Entry point of RoutingPlugin not found\n");
76         logError("RoutingSendInterface Test Entry point of RoutingPlugin not found");
77         exit(1);
78     }
79
80     pRouter = createFunc();
81     if (!pRouter)
82     {
83         if(pReceiveInterface)
84                 delete pReceiveInterface, pReceiveInterface = NULL;
85         fprintf(stderr, "RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable\n");
86         logError("RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable");
87         exit(1);
88     }
89
90     pRouter->startupInterface(env->pReceiveInterface);
91     pRouter->setRoutingReady(10);
92
93     timespec t;
94     t.tv_nsec = 500000000;
95     t.tv_sec = 1;
96
97     sh_timerHandle_t handle;
98
99     //lets use a timeout so the test will finish
100     env->pSocketHandler.addTimer(t, &ptimerCallback, handle, (void*) NULL);
101     env->pSocketHandler.start_listenting();
102
103 }
104
105 void CAmEnvironment::TearDown()
106 {
107         if(pReceiveInterface)
108                 delete pReceiveInterface, pReceiveInterface = NULL;
109         if(pRouter)
110                 delete pRouter, pRouter = NULL;
111 }
112
113 std::vector<std::string> CAmEnvironment::returnListPlugins()
114 {
115     std::vector<std::string> list;
116     list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
117     return (list);
118 }
119
120 am_Error_e CAmEnvironment::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
121 {
122     sourceID = sourceData.sourceID;
123     return (E_OK);
124 }
125
126 am_Error_e CAmEnvironment::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
127 {
128     sinkID = sinkData.sinkID;
129     return (E_OK);
130 }
131
132 am_Error_e CAmEnvironment::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
133 {
134     am_Domain_s domain = domainData;
135     domainID = ++mDomainIDCount;
136     domain.domainID = domainID;
137     return (E_OK);
138 }
139
140 void CAmEnvironment::timerCallback(sh_timerHandle_t handle, void *userData)
141 {
142     (void) handle;
143     (void) userData;
144     env->pSocketHandler.restartTimer(handle);
145     env->pSocketHandler.stop_listening();
146 }
147
148
149 CAmRoutingReceiverAsync::CAmRoutingReceiverAsync() :
150         ptimerCallback(this, &CAmRoutingReceiverAsync::timerCallback)
151 {
152 }
153
154 CAmRoutingReceiverAsync::~CAmRoutingReceiverAsync()
155 {
156 }
157
158 void CAmRoutingReceiverAsync::timerCallback(sh_timerHandle_t handle, void *userData)
159 {
160     (void) handle;
161     (void) userData;
162     env->pSocketHandler.stop_listening();
163 }
164
165 void CAmRoutingReceiverAsync::SetUp()
166 {
167 //    timespec t;
168 //    t.tv_nsec = 0;
169 //    t.tv_sec = 2;
170 //
171 //    sh_timerHandle_t handle;
172 //
173 //    shTimerCallBack *buf = &ptimerCallback;
174 //    //lets use a timeout so the test will finish
175 //    env->pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
176 }
177
178 void CAmRoutingReceiverAsync::TearDown()
179 {
180
181 }
182
183
184 TEST_F(CAmRoutingReceiverAsync,setDomainState)
185 {
186     am_domainID_t domainID = 1;
187     am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
188
189     EXPECT_CALL(*env->pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
190
191     ASSERT_EQ(E_OK, env->pRouter->setDomainState(domainID,state));
192     env->pSocketHandler.start_listenting();
193 }
194
195
196 TEST_F(CAmRoutingReceiverAsync,setSourceSoundProperty)
197 {
198
199     am_Handle_s handle;
200     handle.handle = 1;
201     handle.handleType = H_SETSOURCESOUNDPROPERTY;
202
203     am_sourceID_t sourceID = 3;
204     am_SoundProperty_s property;
205     property.type = SP_GENIVI_MID;
206     property.value = 24;
207
208     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
209
210     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceSoundProperty(handle,sourceID,property));
211     env->pSocketHandler.start_listenting();
212 }
213
214 TEST_F(CAmRoutingReceiverAsync,setSinkSoundProperty)
215 {
216
217     am_Handle_s handle;
218     handle.handle = 1;
219     handle.handleType = H_SETSINKSOUNDPROPERTY;
220
221     am_sinkID_t sinkID = 1;
222     am_SoundProperty_s property;
223     property.type = SP_GENIVI_MID;
224     property.value = 24;
225
226     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
227
228     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkSoundProperty(handle,sinkID,property));
229     env->pSocketHandler.start_listenting();
230 }
231
232 TEST_F(CAmRoutingReceiverAsync,setSourceState)
233 {
234
235     am_Handle_s handle;
236     handle.handle = 1;
237     handle.handleType = H_SETSOURCESTATE;
238
239     am_sourceID_t sourceID = 1;
240     am_SourceState_e state = SS_OFF;
241
242     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
243
244     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceState(handle,sourceID,state));
245     env->pSocketHandler.start_listenting();
246 }
247
248 TEST_F(CAmRoutingReceiverAsync,setSourceVolume)
249 {
250
251     am_Handle_s handle;
252     handle.handle = 1;
253     handle.handleType = H_SETSOURCEVOLUME;
254
255     am_sourceID_t sourceID = 3;
256     am_volume_t volume = 3;
257     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
258     am_time_t myTime = 25;
259
260     EXPECT_CALL(*env->pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(AtLeast(1));
261     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
262
263     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
264     env->pSocketHandler.start_listenting();
265 }
266
267 TEST_F(CAmRoutingReceiverAsync,setSinkVolume)
268 {
269
270     am_Handle_s handle;
271     handle.handle = 1;
272     handle.handleType = H_SETSINKVOLUME;
273
274     am_sinkID_t sinkID = 1;
275     am_volume_t volume = 9;
276     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
277     am_time_t myTime = 25;
278
279     EXPECT_CALL(*env->pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(AtLeast(2));
280     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
281
282     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
283     env->pSocketHandler.start_listenting();
284 }
285
286 TEST_F(CAmRoutingReceiverAsync,setSinkVolumeAbort)
287 {
288
289     am_Handle_s handle;
290     handle.handle = 1;
291     handle.handleType = H_SETSINKVOLUME;
292
293     am_sinkID_t sinkID = 2;
294     am_volume_t volume = 25;
295     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
296     am_time_t myTime = 25;
297
298     EXPECT_CALL(*env->pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
299     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
300
301     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
302     sleep(0.5);
303     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
304     env->pSocketHandler.start_listenting();
305 }
306
307 TEST_F(CAmRoutingReceiverAsync,disconnectNonExisting)
308 {
309
310     am_Handle_s handle;
311     handle.handle = 1;
312     handle.handleType = H_DISCONNECT;
313
314     am_connectionID_t connectionID = 4;
315
316     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
317     EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
318     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
319     env->pSocketHandler.start_listenting();
320 }
321
322 TEST_F(CAmRoutingReceiverAsync,disconnectTooEarly)
323 {
324
325     am_Handle_s handle_c;
326     handle_c.handle = 1;
327     handle_c.handleType = H_CONNECT;
328
329     am_Handle_s handle;
330     handle.handle = 1;
331     handle.handleType = H_DISCONNECT;
332
333     am_connectionID_t connectionID = 4;
334     am_sourceID_t sourceID = 2;
335     am_sinkID_t sinkID = 1;
336     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
337
338     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
339     EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
340     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
341     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
342     env->pSocketHandler.start_listenting();
343 }
344
345 TEST_F(CAmRoutingReceiverAsync,disconnectAbort)
346 {
347
348     am_Handle_s handle_c;
349     handle_c.handle = 1;
350     handle_c.handleType = H_CONNECT;
351
352     am_Handle_s handle;
353     handle.handle = 1;
354     handle.handleType = H_DISCONNECT;
355
356     am_connectionID_t connectionID = 5;
357     am_sourceID_t sourceID = 2;
358     am_sinkID_t sinkID = 1;
359     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
360
361     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
362     EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
363     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
364     sleep(2);
365     ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
366     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
367     env->pSocketHandler.start_listenting();
368 }
369
370 TEST_F(CAmRoutingReceiverAsync,disconnect)
371 {
372
373     am_Handle_s handle_c;
374     handle_c.handle = 1;
375     handle_c.handleType = H_CONNECT;
376
377     am_Handle_s handle;
378     handle.handle = 1;
379     handle.handleType = H_DISCONNECT;
380
381     am_connectionID_t connectionID = 4;
382     am_sourceID_t sourceID = 2;
383     am_sinkID_t sinkID = 1;
384     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
385
386     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
387     EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
388     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
389     sleep(2);
390     ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
391     env->pSocketHandler.start_listenting();
392 }
393
394 TEST_F(CAmRoutingReceiverAsync,connectAbortTooLate)
395 {
396
397     am_Handle_s handle;
398     handle.handle = 1;
399     handle.handleType = H_CONNECT;
400
401     am_connectionID_t connectionID = 4;
402     am_sourceID_t sourceID = 2;
403     am_sinkID_t sinkID = 1;
404     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
405
406     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
407     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
408     sleep(3);
409     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncAbort(handle));
410     env->pSocketHandler.start_listenting();
411 }
412
413 TEST_F(CAmRoutingReceiverAsync,connectAbort)
414 {
415
416     am_Handle_s handle;
417     handle.handle = 1;
418     handle.handleType = H_CONNECT;
419
420     am_connectionID_t connectionID = 4;
421     am_sourceID_t sourceID = 2;
422     am_sinkID_t sinkID = 1;
423     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
424
425     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
426     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
427     sleep(0.5);
428     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
429     env->pSocketHandler.start_listenting();
430 }
431
432 TEST_F(CAmRoutingReceiverAsync,connectWrongFormat)
433 {
434
435     am_Handle_s handle;
436     handle.handle = 1;
437     handle.handleType = H_CONNECT;
438
439     am_connectionID_t connectionID = 4;
440     am_sourceID_t sourceID = 2;
441     am_sinkID_t sinkID = 1;
442     am_CustomConnectionFormat_t format = CF_GENIVI_MONO;
443
444     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
445     ASSERT_EQ(E_WRONG_FORMAT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
446     env->pSocketHandler.start_listenting();
447 }
448
449 TEST_F(CAmRoutingReceiverAsync,connectWrongSink)
450 {
451
452     am_Handle_s handle;
453     handle.handle = 1;
454     handle.handleType = H_CONNECT;
455
456     am_connectionID_t connectionID = 4;
457     am_sourceID_t sourceID = 2;
458     am_sinkID_t sinkID = 122;
459     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
460
461     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
462     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
463     env->pSocketHandler.start_listenting();
464 }
465
466 TEST_F(CAmRoutingReceiverAsync,connectWrongSource)
467 {
468     am_Handle_s handle;
469     handle.handle = 1;
470     handle.handleType = H_CONNECT;
471
472     am_connectionID_t connectionID = 4;
473     am_sourceID_t sourceID = 25;
474     am_sinkID_t sinkID = 1;
475     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
476
477     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
478     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
479     env->pSocketHandler.start_listenting();
480 }
481
482 TEST_F(CAmRoutingReceiverAsync,connect)
483 {
484
485     am_Handle_s handle;
486     handle.handle = 1;
487     handle.handleType = H_CONNECT;
488
489     am_connectionID_t connectionID = 4;
490     am_sourceID_t sourceID = 2;
491     am_sinkID_t sinkID = 1;
492     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
493
494     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
495     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
496     env->pSocketHandler.start_listenting();
497 }
498
499 TEST_F(CAmRoutingReceiverAsync,connectNoMoreThreads)
500 {
501
502     am_Handle_s handle;
503     handle.handle = 1;
504     handle.handleType = H_CONNECT;
505
506     am_connectionID_t connectionID = 1;
507     am_sourceID_t sourceID = 2;
508     am_sinkID_t sinkID = 1;
509     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
510
511     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
512     for (int i = 0; i < 10; i++)
513     {
514         handle.handle++;
515         connectionID++;
516         ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
517     }
518     ASSERT_EQ(E_NOT_POSSIBLE, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
519     env->pSocketHandler.start_listenting();
520 }
521
522 int main(int argc, char **argv)
523 {
524     ::testing::InitGoogleTest(&argc, argv);
525     CAmEnvironment* const env = (CAmEnvironment*)::testing::AddGlobalTestEnvironment(new CAmEnvironment);
526     (void) env;
527     return RUN_ALL_TESTS();
528 }
529