2 * Copyright (c) 2012 BMW
4 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
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.
15 * For further information see http://www.genivi.org/.
18 #include "CAmRoutingReceiverAsync.h"
20 #include "CAmRoutingReceiver.h"
21 #include "TAmPluginTemplate.h"
22 #include "MockIAmRoutingReceive.h"
23 #include "shared/CAmDltWrapper.h"
27 using namespace testing;
29 static CAmEnvironment* env;
31 am_domainID_t CAmEnvironment::mDomainIDCount = 0;
34 CAmEnvironment::CAmEnvironment() :
37 pReceiveInterface(NULL),
38 ptimerCallback(this, &CAmEnvironment::timerCallback)
40 DefaultValue<am_Error_e>::Set(E_OK); // Sets the default value to be returned.
44 CAmEnvironment::~CAmEnvironment()
48 void CAmEnvironment::SetUp()
50 logInfo("RoutingSendInterface Test started ");
52 pReceiveInterface = new MockIAmRoutingReceive();
54 std::vector<int> domainIDs;
55 domainIDs.push_back(0);
56 domainIDs.push_back(1);
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);
64 IAmRoutingSend* (*createFunc)();
65 void* tempLibHandle = NULL;
66 std::string libname("../plugins/routing/libPluginRoutingInterfaceAsync.so");
67 createFunc = getCreateFunction<IAmRoutingSend*()>(libname, tempLibHandle);
71 logError("RoutingSendInterface Test Entry point of RoutingPlugin not found");
75 pRouter = createFunc();
79 logError("RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable");
83 pRouter->startupInterface(env->pReceiveInterface);
84 pRouter->setRoutingReady(10);
87 t.tv_nsec = 500000000;
90 sh_timerHandle_t handle;
92 //lets use a timeout so the test will finish
93 env->pSocketHandler.addTimer(t, &ptimerCallback, handle, (void*) NULL);
94 env->pSocketHandler.start_listenting();
98 void CAmEnvironment::TearDown()
100 if(pReceiveInterface)
101 delete pReceiveInterface, pReceiveInterface = NULL;
103 delete pRouter, pRouter = NULL;
106 std::vector<std::string> CAmEnvironment::returnListPlugins()
108 std::vector<std::string> list;
109 list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
113 am_Error_e CAmEnvironment::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
115 sourceID = sourceData.sourceID;
119 am_Error_e CAmEnvironment::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
121 sinkID = sinkData.sinkID;
125 am_Error_e CAmEnvironment::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
127 am_Domain_s domain = domainData;
128 domainID = ++mDomainIDCount;
129 domain.domainID = domainID;
133 void CAmEnvironment::timerCallback(sh_timerHandle_t handle, void *userData)
137 env->pSocketHandler.restartTimer(handle);
138 env->pSocketHandler.stop_listening();
142 CAmRoutingReceiverAsync::CAmRoutingReceiverAsync() :
143 ptimerCallback(this, &CAmRoutingReceiverAsync::timerCallback)
147 CAmRoutingReceiverAsync::~CAmRoutingReceiverAsync()
151 void CAmRoutingReceiverAsync::timerCallback(sh_timerHandle_t handle, void *userData)
155 env->pSocketHandler.stop_listening();
158 void CAmRoutingReceiverAsync::SetUp()
164 // sh_timerHandle_t handle;
166 // shTimerCallBack *buf = &ptimerCallback;
167 // //lets use a timeout so the test will finish
168 // env->pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
171 void CAmRoutingReceiverAsync::TearDown()
177 TEST_F(CAmRoutingReceiverAsync,setDomainState)
179 am_domainID_t domainID = 1;
180 am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
182 EXPECT_CALL(*env->pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
184 ASSERT_EQ(E_OK, env->pRouter->setDomainState(domainID,state));
185 env->pSocketHandler.start_listenting();
189 TEST_F(CAmRoutingReceiverAsync,setSourceSoundProperty)
194 handle.handleType = H_SETSOURCESOUNDPROPERTY;
196 am_sourceID_t sourceID = 3;
197 am_SoundProperty_s property;
198 property.type = SP_GENIVI_MID;
201 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
203 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceSoundProperty(handle,sourceID,property));
204 env->pSocketHandler.start_listenting();
207 TEST_F(CAmRoutingReceiverAsync,setSinkSoundProperty)
212 handle.handleType = H_SETSINKSOUNDPROPERTY;
214 am_sinkID_t sinkID = 1;
215 am_SoundProperty_s property;
216 property.type = SP_GENIVI_MID;
219 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
221 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkSoundProperty(handle,sinkID,property));
222 env->pSocketHandler.start_listenting();
225 TEST_F(CAmRoutingReceiverAsync,setSourceState)
230 handle.handleType = H_SETSOURCESTATE;
232 am_sourceID_t sourceID = 1;
233 am_SourceState_e state = SS_OFF;
235 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
237 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceState(handle,sourceID,state));
238 env->pSocketHandler.start_listenting();
241 TEST_F(CAmRoutingReceiverAsync,setSourceVolume)
246 handle.handleType = H_SETSOURCEVOLUME;
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;
253 EXPECT_CALL(*env->pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(AtLeast(1));
254 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
256 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
257 env->pSocketHandler.start_listenting();
260 TEST_F(CAmRoutingReceiverAsync,setSinkVolume)
265 handle.handleType = H_SETSINKVOLUME;
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;
272 EXPECT_CALL(*env->pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(AtLeast(2));
273 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
275 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
276 env->pSocketHandler.start_listenting();
279 TEST_F(CAmRoutingReceiverAsync,setSinkVolumeAbort)
284 handle.handleType = H_SETSINKVOLUME;
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;
291 EXPECT_CALL(*env->pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
292 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
294 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
296 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
297 env->pSocketHandler.start_listenting();
300 TEST_F(CAmRoutingReceiverAsync,disconnectNonExisting)
305 handle.handleType = H_DISCONNECT;
307 am_connectionID_t connectionID = 4;
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();
315 TEST_F(CAmRoutingReceiverAsync,disconnectTooEarly)
318 am_Handle_s handle_c;
320 handle_c.handleType = H_CONNECT;
324 handle.handleType = H_DISCONNECT;
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;
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();
338 TEST_F(CAmRoutingReceiverAsync,disconnectAbort)
341 am_Handle_s handle_c;
343 handle_c.handleType = H_CONNECT;
347 handle.handleType = H_DISCONNECT;
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;
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));
358 ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
359 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
360 env->pSocketHandler.start_listenting();
363 TEST_F(CAmRoutingReceiverAsync,disconnect)
366 am_Handle_s handle_c;
368 handle_c.handleType = H_CONNECT;
372 handle.handleType = H_DISCONNECT;
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;
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));
383 ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
384 env->pSocketHandler.start_listenting();
387 TEST_F(CAmRoutingReceiverAsync,connectAbortTooLate)
392 handle.handleType = H_CONNECT;
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;
399 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
400 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
402 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncAbort(handle));
403 env->pSocketHandler.start_listenting();
406 TEST_F(CAmRoutingReceiverAsync,connectAbort)
411 handle.handleType = H_CONNECT;
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;
418 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
419 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
421 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
422 env->pSocketHandler.start_listenting();
425 TEST_F(CAmRoutingReceiverAsync,connectWrongFormat)
430 handle.handleType = H_CONNECT;
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;
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();
442 TEST_F(CAmRoutingReceiverAsync,connectWrongSink)
447 handle.handleType = H_CONNECT;
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;
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();
459 TEST_F(CAmRoutingReceiverAsync,connectWrongSource)
463 handle.handleType = H_CONNECT;
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;
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();
475 TEST_F(CAmRoutingReceiverAsync,connect)
480 handle.handleType = H_CONNECT;
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;
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();
492 TEST_F(CAmRoutingReceiverAsync,connectNoMoreThreads)
497 handle.handleType = H_CONNECT;
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;
504 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
505 for (int i = 0; i < 10; i++)
509 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
511 ASSERT_EQ(E_NOT_POSSIBLE, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
512 env->pSocketHandler.start_listenting();
515 int main(int argc, char **argv)
517 ::testing::InitGoogleTest(&argc, argv);
518 CAmEnvironment* const env = (CAmEnvironment*)::testing::AddGlobalTestEnvironment(new CAmEnvironment);
520 return RUN_ALL_TESTS();