6fff1a490a96e9cd9ab7384d65d29f42e4fcbdc2
[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
69     if (!createFunc)
70     {
71         logError("RoutingSendInterface Test Entry point of RoutingPlugin not found");
72         exit(1);
73     }
74
75     pRouter = createFunc();
76
77     if (!pRouter)
78     {
79         logError("RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable");
80         exit(1);
81     }
82
83     pRouter->startupInterface(env->pReceiveInterface);
84     pRouter->setRoutingReady(10);
85
86     timespec t;
87     t.tv_nsec = 500000000;
88     t.tv_sec = 1;
89
90     sh_timerHandle_t handle;
91
92     //lets use a timeout so the test will finish
93     env->pSocketHandler.addTimer(t, &ptimerCallback, handle, (void*) NULL);
94     env->pSocketHandler.start_listenting();
95
96 }
97
98 void CAmEnvironment::TearDown()
99 {
100         if(pReceiveInterface)
101                 delete pReceiveInterface, pReceiveInterface = NULL;
102         if(pRouter)
103                 delete pRouter, pRouter = NULL;
104 }
105
106 std::vector<std::string> CAmEnvironment::returnListPlugins()
107 {
108     std::vector<std::string> list;
109     list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
110     return (list);
111 }
112
113 am_Error_e CAmEnvironment::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
114 {
115     sourceID = sourceData.sourceID;
116     return (E_OK);
117 }
118
119 am_Error_e CAmEnvironment::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
120 {
121     sinkID = sinkData.sinkID;
122     return (E_OK);
123 }
124
125 am_Error_e CAmEnvironment::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
126 {
127     am_Domain_s domain = domainData;
128     domainID = ++mDomainIDCount;
129     domain.domainID = domainID;
130     return (E_OK);
131 }
132
133 void CAmEnvironment::timerCallback(sh_timerHandle_t handle, void *userData)
134 {
135     (void) handle;
136     (void) userData;
137     env->pSocketHandler.restartTimer(handle);
138     env->pSocketHandler.stop_listening();
139 }
140
141
142 CAmRoutingReceiverAsync::CAmRoutingReceiverAsync() :
143         ptimerCallback(this, &CAmRoutingReceiverAsync::timerCallback)
144 {
145 }
146
147 CAmRoutingReceiverAsync::~CAmRoutingReceiverAsync()
148 {
149 }
150
151 void CAmRoutingReceiverAsync::timerCallback(sh_timerHandle_t handle, void *userData)
152 {
153     (void) handle;
154     (void) userData;
155     env->pSocketHandler.stop_listening();
156 }
157
158 void CAmRoutingReceiverAsync::SetUp()
159 {
160 //    timespec t;
161 //    t.tv_nsec = 0;
162 //    t.tv_sec = 2;
163 //
164 //    sh_timerHandle_t handle;
165 //
166 //    shTimerCallBack *buf = &ptimerCallback;
167 //    //lets use a timeout so the test will finish
168 //    env->pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
169 }
170
171 void CAmRoutingReceiverAsync::TearDown()
172 {
173
174 }
175
176
177 TEST_F(CAmRoutingReceiverAsync,setDomainState)
178 {
179     am_domainID_t domainID = 1;
180     am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
181
182     EXPECT_CALL(*env->pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
183
184     ASSERT_EQ(E_OK, env->pRouter->setDomainState(domainID,state));
185     env->pSocketHandler.start_listenting();
186 }
187
188
189 TEST_F(CAmRoutingReceiverAsync,setSourceSoundProperty)
190 {
191
192     am_Handle_s handle;
193     handle.handle = 1;
194     handle.handleType = H_SETSOURCESOUNDPROPERTY;
195
196     am_sourceID_t sourceID = 3;
197     am_SoundProperty_s property;
198     property.type = SP_GENIVI_MID;
199     property.value = 24;
200
201     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
202
203     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceSoundProperty(handle,sourceID,property));
204     env->pSocketHandler.start_listenting();
205 }
206
207 TEST_F(CAmRoutingReceiverAsync,setSinkSoundProperty)
208 {
209
210     am_Handle_s handle;
211     handle.handle = 1;
212     handle.handleType = H_SETSINKSOUNDPROPERTY;
213
214     am_sinkID_t sinkID = 1;
215     am_SoundProperty_s property;
216     property.type = SP_GENIVI_MID;
217     property.value = 24;
218
219     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
220
221     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkSoundProperty(handle,sinkID,property));
222     env->pSocketHandler.start_listenting();
223 }
224
225 TEST_F(CAmRoutingReceiverAsync,setSourceState)
226 {
227
228     am_Handle_s handle;
229     handle.handle = 1;
230     handle.handleType = H_SETSOURCESTATE;
231
232     am_sourceID_t sourceID = 1;
233     am_SourceState_e state = SS_OFF;
234
235     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
236
237     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceState(handle,sourceID,state));
238     env->pSocketHandler.start_listenting();
239 }
240
241 TEST_F(CAmRoutingReceiverAsync,setSourceVolume)
242 {
243
244     am_Handle_s handle;
245     handle.handle = 1;
246     handle.handleType = H_SETSOURCEVOLUME;
247
248     am_sourceID_t sourceID = 3;
249     am_volume_t volume = 3;
250     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
251     am_time_t myTime = 25;
252
253     EXPECT_CALL(*env->pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(AtLeast(1));
254     EXPECT_CALL(*env->pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
255
256     ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
257     env->pSocketHandler.start_listenting();
258 }
259
260 TEST_F(CAmRoutingReceiverAsync,setSinkVolume)
261 {
262
263     am_Handle_s handle;
264     handle.handle = 1;
265     handle.handleType = H_SETSINKVOLUME;
266
267     am_sinkID_t sinkID = 1;
268     am_volume_t volume = 9;
269     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
270     am_time_t myTime = 25;
271
272     EXPECT_CALL(*env->pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(AtLeast(2));
273     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
274
275     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
276     env->pSocketHandler.start_listenting();
277 }
278
279 TEST_F(CAmRoutingReceiverAsync,setSinkVolumeAbort)
280 {
281
282     am_Handle_s handle;
283     handle.handle = 1;
284     handle.handleType = H_SETSINKVOLUME;
285
286     am_sinkID_t sinkID = 2;
287     am_volume_t volume = 25;
288     am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
289     am_time_t myTime = 25;
290
291     EXPECT_CALL(*env->pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
292     EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
293
294     ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
295     sleep(0.5);
296     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
297     env->pSocketHandler.start_listenting();
298 }
299
300 TEST_F(CAmRoutingReceiverAsync,disconnectNonExisting)
301 {
302
303     am_Handle_s handle;
304     handle.handle = 1;
305     handle.handleType = H_DISCONNECT;
306
307     am_connectionID_t connectionID = 4;
308
309     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
310     EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
311     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
312     env->pSocketHandler.start_listenting();
313 }
314
315 TEST_F(CAmRoutingReceiverAsync,disconnectTooEarly)
316 {
317
318     am_Handle_s handle_c;
319     handle_c.handle = 1;
320     handle_c.handleType = H_CONNECT;
321
322     am_Handle_s handle;
323     handle.handle = 1;
324     handle.handleType = H_DISCONNECT;
325
326     am_connectionID_t connectionID = 4;
327     am_sourceID_t sourceID = 2;
328     am_sinkID_t sinkID = 1;
329     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
330
331     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
332     EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
333     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
334     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
335     env->pSocketHandler.start_listenting();
336 }
337
338 TEST_F(CAmRoutingReceiverAsync,disconnectAbort)
339 {
340
341     am_Handle_s handle_c;
342     handle_c.handle = 1;
343     handle_c.handleType = H_CONNECT;
344
345     am_Handle_s handle;
346     handle.handle = 1;
347     handle.handleType = H_DISCONNECT;
348
349     am_connectionID_t connectionID = 5;
350     am_sourceID_t sourceID = 2;
351     am_sinkID_t sinkID = 1;
352     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
353
354     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
355     EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
356     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
357     sleep(2);
358     ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
359     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
360     env->pSocketHandler.start_listenting();
361 }
362
363 TEST_F(CAmRoutingReceiverAsync,disconnect)
364 {
365
366     am_Handle_s handle_c;
367     handle_c.handle = 1;
368     handle_c.handleType = H_CONNECT;
369
370     am_Handle_s handle;
371     handle.handle = 1;
372     handle.handleType = H_DISCONNECT;
373
374     am_connectionID_t connectionID = 4;
375     am_sourceID_t sourceID = 2;
376     am_sinkID_t sinkID = 1;
377     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
378
379     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
380     EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
381     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
382     sleep(2);
383     ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
384     env->pSocketHandler.start_listenting();
385 }
386
387 TEST_F(CAmRoutingReceiverAsync,connectAbortTooLate)
388 {
389
390     am_Handle_s handle;
391     handle.handle = 1;
392     handle.handleType = H_CONNECT;
393
394     am_connectionID_t connectionID = 4;
395     am_sourceID_t sourceID = 2;
396     am_sinkID_t sinkID = 1;
397     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
398
399     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
400     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
401     sleep(3);
402     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncAbort(handle));
403     env->pSocketHandler.start_listenting();
404 }
405
406 TEST_F(CAmRoutingReceiverAsync,connectAbort)
407 {
408
409     am_Handle_s handle;
410     handle.handle = 1;
411     handle.handleType = H_CONNECT;
412
413     am_connectionID_t connectionID = 4;
414     am_sourceID_t sourceID = 2;
415     am_sinkID_t sinkID = 1;
416     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
417
418     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
419     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
420     sleep(0.5);
421     ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
422     env->pSocketHandler.start_listenting();
423 }
424
425 TEST_F(CAmRoutingReceiverAsync,connectWrongFormat)
426 {
427
428     am_Handle_s handle;
429     handle.handle = 1;
430     handle.handleType = H_CONNECT;
431
432     am_connectionID_t connectionID = 4;
433     am_sourceID_t sourceID = 2;
434     am_sinkID_t sinkID = 1;
435     am_CustomConnectionFormat_t format = CF_GENIVI_MONO;
436
437     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
438     ASSERT_EQ(E_WRONG_FORMAT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
439     env->pSocketHandler.start_listenting();
440 }
441
442 TEST_F(CAmRoutingReceiverAsync,connectWrongSink)
443 {
444
445     am_Handle_s handle;
446     handle.handle = 1;
447     handle.handleType = H_CONNECT;
448
449     am_connectionID_t connectionID = 4;
450     am_sourceID_t sourceID = 2;
451     am_sinkID_t sinkID = 122;
452     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
453
454     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
455     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
456     env->pSocketHandler.start_listenting();
457 }
458
459 TEST_F(CAmRoutingReceiverAsync,connectWrongSource)
460 {
461     am_Handle_s handle;
462     handle.handle = 1;
463     handle.handleType = H_CONNECT;
464
465     am_connectionID_t connectionID = 4;
466     am_sourceID_t sourceID = 25;
467     am_sinkID_t sinkID = 1;
468     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
469
470     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
471     ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
472     env->pSocketHandler.start_listenting();
473 }
474
475 TEST_F(CAmRoutingReceiverAsync,connect)
476 {
477
478     am_Handle_s handle;
479     handle.handle = 1;
480     handle.handleType = H_CONNECT;
481
482     am_connectionID_t connectionID = 4;
483     am_sourceID_t sourceID = 2;
484     am_sinkID_t sinkID = 1;
485     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
486
487     EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
488     ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
489     env->pSocketHandler.start_listenting();
490 }
491
492 TEST_F(CAmRoutingReceiverAsync,connectNoMoreThreads)
493 {
494
495     am_Handle_s handle;
496     handle.handle = 1;
497     handle.handleType = H_CONNECT;
498
499     am_connectionID_t connectionID = 1;
500     am_sourceID_t sourceID = 2;
501     am_sinkID_t sinkID = 1;
502     am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
503
504     EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
505     for (int i = 0; i < 10; i++)
506     {
507         handle.handle++;
508         connectionID++;
509         ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
510     }
511     ASSERT_EQ(E_NOT_POSSIBLE, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
512     env->pSocketHandler.start_listenting();
513 }
514
515 int main(int argc, char **argv)
516 {
517     ::testing::InitGoogleTest(&argc, argv);
518     CAmEnvironment* const env = (CAmEnvironment*)::testing::AddGlobalTestEnvironment(new CAmEnvironment);
519     (void) env;
520     return RUN_ALL_TESTS();
521 }
522