* wrapping DLT calls in a new Class because of performance, codesize and lazyness...
[profile/ivi/audiomanager.git] / PluginRoutingInterfaceAsync / test / testRoutingInterfaceAsync.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file testRoutingItnerfaceAsync.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  */
24
25 #include "testRoutingInterfaceAsync.h"
26 #include "config.h"
27 #include "DLTWrapper.h"
28
29 using namespace am;
30 using namespace testing;
31
32 std::vector<std::string> testRoutingInterfaceAsync::pListRoutingPluginDirs = returnListPlugins();
33 am_domainID_t testRoutingInterfaceAsync::mDomainIDCount = 0;
34 RoutingSender testRoutingInterfaceAsync::pRoutingSender = RoutingSender(pListRoutingPluginDirs);
35
36 testRoutingInterfaceAsync::testRoutingInterfaceAsync() :
37         pSocketHandler(), //
38         pReceiveInterface(), //
39         ptimerCallback(this, &testRoutingInterfaceAsync::timerCallback)
40 {
41 }
42
43 testRoutingInterfaceAsync::~testRoutingInterfaceAsync()
44 {
45 }
46
47 void testRoutingInterfaceAsync::SetUp()
48 {
49     logInfo("RoutingSendInterface Test started ");
50
51     std::vector<int> domainIDs;
52     domainIDs.push_back(0);
53     domainIDs.push_back(1);
54
55     EXPECT_CALL(pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&pSocketHandler), Return(E_OK)));
56     EXPECT_CALL(pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleDomainRegister));
57     EXPECT_CALL(pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSourceRegister));
58     EXPECT_CALL(pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSinkRegister));
59
60     pRoutingSender.startupRoutingInterface(&pReceiveInterface);
61     pRoutingSender.routingInterfacesReady();
62
63     timespec t;
64     t.tv_nsec = 0;
65     t.tv_sec = 4;
66
67     sh_timerHandle_t handle;
68
69     shTimerCallBack *buf = &ptimerCallback;
70     //lets use a timeout so the test will finish
71     pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
72 }
73
74 std::vector<std::string> am::testRoutingInterfaceAsync::returnListPlugins()
75 {
76     std::vector<std::string> list;
77     list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
78     return (list);
79 }
80
81 am_Error_e am::testRoutingInterfaceAsync::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
82 {
83     sourceID = sourceData.sourceID;
84     pRoutingSender.addSourceLookup(sourceData);
85     return (E_OK);
86 }
87
88 am_Error_e am::testRoutingInterfaceAsync::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
89 {
90     sinkID = sinkData.sinkID;
91     pRoutingSender.addSinkLookup(sinkData);
92     return (E_OK);
93 }
94
95 am_Error_e am::testRoutingInterfaceAsync::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
96 {
97     am_Domain_s domain = domainData;
98     domainID = mDomainIDCount++;
99     domain.domainID = domainID;
100     pRoutingSender.addDomainLookup(domain);
101     return (E_OK);
102 }
103
104 void am::testRoutingInterfaceAsync::timerCallback(sh_timerHandle_t handle, void *userData)
105 {
106     (void) handle;
107     (void) userData;
108     pSocketHandler.stop_listening();
109 }
110
111 void testRoutingInterfaceAsync::TearDown()
112 {
113 }
114
115 TEST_F(testRoutingInterfaceAsync,setDomainState)
116 {
117     am_domainID_t domainID = 1;
118     am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
119
120     EXPECT_CALL(pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
121
122     ASSERT_EQ(E_OK, pRoutingSender.setDomainState(domainID,state));
123     pSocketHandler.start_listenting();
124 }
125
126 TEST_F(testRoutingInterfaceAsync,setSourceSoundProperty)
127 {
128
129     am_Handle_s handle;
130     handle.handle = 1;
131     handle.handleType = H_SETSOURCESOUNDPROPERTY;
132
133     am_sourceID_t sourceID = 3;
134     am_SoundProperty_s property;
135     property.type = SP_MID;
136     property.value = 24;
137
138     EXPECT_CALL(pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
139
140     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceSoundProperty(handle,sourceID,property));
141     pSocketHandler.start_listenting();
142 }
143
144 TEST_F(testRoutingInterfaceAsync,setSinkSoundProperty)
145 {
146
147     am_Handle_s handle;
148     handle.handle = 1;
149     handle.handleType = H_SETSINKSOUNDPROPERTY;
150
151     am_sinkID_t sinkID = 1;
152     am_SoundProperty_s property;
153     property.type = SP_MID;
154     property.value = 24;
155
156     EXPECT_CALL(pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
157
158     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkSoundProperty(handle,sinkID,property));
159     pSocketHandler.start_listenting();
160 }
161
162 TEST_F(testRoutingInterfaceAsync,setSourceState)
163 {
164
165     am_Handle_s handle;
166     handle.handle = 1;
167     handle.handleType = H_SETSOURCEVOLUME;
168
169     am_sourceID_t sourceID = 1;
170     am_SourceState_e state = SS_OFF;
171
172     EXPECT_CALL(pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
173
174     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceState(handle,sourceID,state));
175     pSocketHandler.start_listenting();
176 }
177
178 TEST_F(testRoutingInterfaceAsync,setSourceVolume)
179 {
180
181     am_Handle_s handle;
182     handle.handle = 1;
183     handle.handleType = H_SETSOURCEVOLUME;
184
185     am_sourceID_t sourceID = 3;
186     am_volume_t volume = 3;
187     am_RampType_e ramp = RAMP_DIRECT;
188     am_time_t myTime = 25;
189
190     EXPECT_CALL(pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(3);
191     EXPECT_CALL(pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
192
193     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
194     pSocketHandler.start_listenting();
195 }
196
197 TEST_F(testRoutingInterfaceAsync,setSinkVolume)
198 {
199
200     am_Handle_s handle;
201     handle.handle = 1;
202     handle.handleType = H_SETSINKVOLUME;
203
204     am_sinkID_t sinkID = 1;
205     am_volume_t volume = 9;
206     am_RampType_e ramp = RAMP_DIRECT;
207     am_time_t myTime = 25;
208
209     EXPECT_CALL(pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(9);
210     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
211
212     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
213     pSocketHandler.start_listenting();
214 }
215
216 TEST_F(testRoutingInterfaceAsync,setSinkVolumeAbort)
217 {
218
219     am_Handle_s handle;
220     handle.handle = 1;
221     handle.handleType = H_SETSINKVOLUME;
222
223     am_sinkID_t sinkID = 2;
224     am_volume_t volume = 25;
225     am_RampType_e ramp = RAMP_DIRECT;
226     am_time_t myTime = 25;
227
228     EXPECT_CALL(pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
229     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
230
231     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
232     sleep(0.5);
233     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
234     pSocketHandler.start_listenting();
235 }
236
237 TEST_F(testRoutingInterfaceAsync,disconnectTooEarly)
238 {
239
240     am_Handle_s handle;
241     handle.handle = 1;
242     handle.handleType = H_CONNECT;
243
244     am_connectionID_t connectionID = 4;
245     am_sourceID_t sourceID = 2;
246     am_sinkID_t sinkID = 1;
247     am_ConnectionFormat_e format = CF_ANALOG;
248
249     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
250     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
251     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
252     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
253     pSocketHandler.start_listenting();
254 }
255
256 TEST_F(testRoutingInterfaceAsync,disconnectAbort)
257 {
258
259     am_Handle_s handle;
260     handle.handle = 1;
261     handle.handleType = H_CONNECT;
262
263     am_connectionID_t connectionID = 4;
264     am_sourceID_t sourceID = 2;
265     am_sinkID_t sinkID = 1;
266     am_ConnectionFormat_e format = CF_ANALOG;
267
268     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
269     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
270     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
271     sleep(2);
272     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
273     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
274     pSocketHandler.start_listenting();
275 }
276
277 TEST_F(testRoutingInterfaceAsync,disconnectNonExisting)
278 {
279
280     am_Handle_s handle;
281     handle.handle = 1;
282     handle.handleType = H_CONNECT;
283
284     am_connectionID_t connectionID = 4;
285
286     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
287     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
288     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
289     pSocketHandler.start_listenting();
290 }
291
292 TEST_F(testRoutingInterfaceAsync,disconnect)
293 {
294
295     am_Handle_s handle;
296     handle.handle = 1;
297     handle.handleType = H_CONNECT;
298
299     am_connectionID_t connectionID = 4;
300     am_sourceID_t sourceID = 2;
301     am_sinkID_t sinkID = 1;
302     am_ConnectionFormat_e format = CF_ANALOG;
303
304     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
305     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
306     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
307     sleep(2);
308     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
309     pSocketHandler.start_listenting();
310 }
311
312 TEST_F(testRoutingInterfaceAsync,connectNoMoreThreads)
313 {
314
315     am_Handle_s handle;
316     handle.handle = 1;
317     handle.handleType = H_CONNECT;
318
319     am_connectionID_t connectionID = 1;
320     am_sourceID_t sourceID = 2;
321     am_sinkID_t sinkID = 1;
322     am_ConnectionFormat_e format = CF_ANALOG;
323
324     EXPECT_CALL(pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
325     for (int i = 0; i < 10; i++)
326     {
327         handle.handle++;
328         connectionID++;
329         ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
330     }ASSERT_EQ(E_NOT_POSSIBLE, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
331     pSocketHandler.start_listenting();
332 }
333
334 TEST_F(testRoutingInterfaceAsync,connectAbortTooLate)
335 {
336
337     am_Handle_s handle;
338     handle.handle = 1;
339     handle.handleType = H_CONNECT;
340
341     am_connectionID_t connectionID = 4;
342     am_sourceID_t sourceID = 2;
343     am_sinkID_t sinkID = 1;
344     am_ConnectionFormat_e format = CF_ANALOG;
345
346     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
347     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
348     sleep(3);
349     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncAbort(handle));
350     pSocketHandler.start_listenting();
351 }
352
353 TEST_F(testRoutingInterfaceAsync,connectAbort)
354 {
355
356     am_Handle_s handle;
357     handle.handle = 1;
358     handle.handleType = H_CONNECT;
359
360     am_connectionID_t connectionID = 4;
361     am_sourceID_t sourceID = 2;
362     am_sinkID_t sinkID = 1;
363     am_ConnectionFormat_e format = CF_ANALOG;
364
365     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
366     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
367     sleep(0.5);
368     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
369     pSocketHandler.start_listenting();
370 }
371
372 TEST_F(testRoutingInterfaceAsync,connectWrongFormat)
373 {
374
375     am_Handle_s handle;
376     handle.handle = 1;
377     handle.handleType = H_CONNECT;
378
379     am_connectionID_t connectionID = 4;
380     am_sourceID_t sourceID = 2;
381     am_sinkID_t sinkID = 1;
382     am_ConnectionFormat_e format = CF_MONO;
383
384     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
385     ASSERT_EQ(E_WRONG_FORMAT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
386     pSocketHandler.start_listenting();
387 }
388
389 TEST_F(testRoutingInterfaceAsync,connectWrongSink)
390 {
391
392     am_Handle_s handle;
393     handle.handle = 1;
394     handle.handleType = H_CONNECT;
395
396     am_connectionID_t connectionID = 4;
397     am_sourceID_t sourceID = 2;
398     am_sinkID_t sinkID = 122;
399     am_ConnectionFormat_e format = CF_ANALOG;
400
401     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
402     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
403     pSocketHandler.start_listenting();
404 }
405
406 TEST_F(testRoutingInterfaceAsync,connectWrongSource)
407 {
408     am_Handle_s handle;
409     handle.handle = 1;
410     handle.handleType = H_CONNECT;
411
412     am_connectionID_t connectionID = 4;
413     am_sourceID_t sourceID = 25;
414     am_sinkID_t sinkID = 1;
415     am_ConnectionFormat_e format = CF_ANALOG;
416
417     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
418     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
419     pSocketHandler.start_listenting();
420 }
421
422 TEST_F(testRoutingInterfaceAsync,connect)
423 {
424
425     am_Handle_s handle;
426     handle.handle = 1;
427     handle.handleType = H_CONNECT;
428
429     am_connectionID_t connectionID = 4;
430     am_sourceID_t sourceID = 2;
431     am_sinkID_t sinkID = 1;
432     am_ConnectionFormat_e format = CF_ANALOG;
433
434     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
435     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
436     pSocketHandler.start_listenting();
437 }
438
439 int main(int argc, char **argv)
440 {
441     ::testing::InitGoogleTest(&argc, argv);
442     return RUN_ALL_TESTS();
443 }
444