fe74f42c785d96cddad803ab620343b0fc432e3f
[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
28 using namespace am;
29 using namespace testing;
30
31 DLT_DECLARE_CONTEXT(DLT_CONTEXT)
32
33 std::vector<std::string> testRoutingInterfaceAsync::pListRoutingPluginDirs = returnListPlugins();
34 am_domainID_t testRoutingInterfaceAsync::mDomainIDCount = 0;
35 RoutingSender testRoutingInterfaceAsync::pRoutingSender = RoutingSender(pListRoutingPluginDirs);
36
37 testRoutingInterfaceAsync::testRoutingInterfaceAsync() :
38         pSocketHandler(), //
39         pReceiveInterface(), //
40         ptimerCallback(this, &testRoutingInterfaceAsync::timerCallback)
41 {
42 }
43
44 testRoutingInterfaceAsync::~testRoutingInterfaceAsync()
45 {
46 }
47
48 void testRoutingInterfaceAsync::SetUp()
49 {
50     DLT_REGISTER_APP("DPtest", "RoutingInterfacetest");
51     DLT_REGISTER_CONTEXT(DLT_CONTEXT, "Main", "Main Context");
52     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("RoutingSendInterface Test started "));
53
54     std::vector<int> domainIDs;
55     domainIDs.push_back(0);
56     domainIDs.push_back(1);
57
58     EXPECT_CALL(pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&pSocketHandler), Return(E_OK)));
59     EXPECT_CALL(pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleDomainRegister));
60     EXPECT_CALL(pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSourceRegister));
61     EXPECT_CALL(pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSinkRegister));
62
63     pRoutingSender.startupRoutingInterface(&pReceiveInterface);
64     pRoutingSender.routingInterfacesReady();
65
66     timespec t;
67     t.tv_nsec = 0;
68     t.tv_sec = 4;
69
70     sh_timerHandle_t handle;
71
72     shTimerCallBack *buf = &ptimerCallback;
73     //lets use a timeout so the test will finish
74     pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
75 }
76
77 std::vector<std::string> am::testRoutingInterfaceAsync::returnListPlugins()
78 {
79     std::vector<std::string> list;
80     list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
81     return (list);
82 }
83
84 am_Error_e am::testRoutingInterfaceAsync::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
85 {
86     sourceID = sourceData.sourceID;
87     pRoutingSender.addSourceLookup(sourceData);
88     return (E_OK);
89 }
90
91 am_Error_e am::testRoutingInterfaceAsync::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
92 {
93     sinkID = sinkData.sinkID;
94     pRoutingSender.addSinkLookup(sinkData);
95     return (E_OK);
96 }
97
98 am_Error_e am::testRoutingInterfaceAsync::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
99 {
100     am_Domain_s domain = domainData;
101     domainID = mDomainIDCount++;
102     domain.domainID = domainID;
103     pRoutingSender.addDomainLookup(domain);
104     return (E_OK);
105 }
106
107 void am::testRoutingInterfaceAsync::timerCallback(sh_timerHandle_t handle, void *userData)
108 {
109     (void) handle;
110     (void) userData;
111     pSocketHandler.stop_listening();
112 }
113
114 void testRoutingInterfaceAsync::TearDown()
115 {
116     DLT_UNREGISTER_CONTEXT(DLT_CONTEXT);
117 }
118
119 TEST_F(testRoutingInterfaceAsync,setDomainState)
120 {
121     am_domainID_t domainID = 1;
122     am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
123
124     EXPECT_CALL(pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
125
126     ASSERT_EQ(E_OK, pRoutingSender.setDomainState(domainID,state));
127     pSocketHandler.start_listenting();
128 }
129
130 TEST_F(testRoutingInterfaceAsync,setSourceSoundProperty)
131 {
132
133     am_Handle_s handle;
134     handle.handle = 1;
135     handle.handleType = H_SETSOURCESOUNDPROPERTY;
136
137     am_sourceID_t sourceID = 3;
138     am_SoundProperty_s property;
139     property.type = SP_MID;
140     property.value = 24;
141
142     EXPECT_CALL(pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
143
144     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceSoundProperty(handle,sourceID,property));
145     pSocketHandler.start_listenting();
146 }
147
148 TEST_F(testRoutingInterfaceAsync,setSinkSoundProperty)
149 {
150
151     am_Handle_s handle;
152     handle.handle = 1;
153     handle.handleType = H_SETSINKSOUNDPROPERTY;
154
155     am_sinkID_t sinkID = 1;
156     am_SoundProperty_s property;
157     property.type = SP_MID;
158     property.value = 24;
159
160     EXPECT_CALL(pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
161
162     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkSoundProperty(handle,sinkID,property));
163     pSocketHandler.start_listenting();
164 }
165
166 TEST_F(testRoutingInterfaceAsync,setSourceState)
167 {
168
169     am_Handle_s handle;
170     handle.handle = 1;
171     handle.handleType = H_SETSOURCEVOLUME;
172
173     am_sourceID_t sourceID = 1;
174     am_SourceState_e state = SS_OFF;
175
176     EXPECT_CALL(pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
177
178     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceState(handle,sourceID,state));
179     pSocketHandler.start_listenting();
180 }
181
182 TEST_F(testRoutingInterfaceAsync,setSourceVolume)
183 {
184
185     am_Handle_s handle;
186     handle.handle = 1;
187     handle.handleType = H_SETSOURCEVOLUME;
188
189     am_sourceID_t sourceID = 3;
190     am_volume_t volume = 3;
191     am_RampType_e ramp = RAMP_DIRECT;
192     am_time_t myTime = 25;
193
194     EXPECT_CALL(pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(3);
195     EXPECT_CALL(pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
196
197     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
198     pSocketHandler.start_listenting();
199 }
200
201 TEST_F(testRoutingInterfaceAsync,setSinkVolume)
202 {
203
204     am_Handle_s handle;
205     handle.handle = 1;
206     handle.handleType = H_SETSINKVOLUME;
207
208     am_sinkID_t sinkID = 1;
209     am_volume_t volume = 9;
210     am_RampType_e ramp = RAMP_DIRECT;
211     am_time_t myTime = 25;
212
213     EXPECT_CALL(pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(9);
214     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
215
216     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
217     pSocketHandler.start_listenting();
218 }
219
220 TEST_F(testRoutingInterfaceAsync,setSinkVolumeAbort)
221 {
222
223     am_Handle_s handle;
224     handle.handle = 1;
225     handle.handleType = H_SETSINKVOLUME;
226
227     am_sinkID_t sinkID = 2;
228     am_volume_t volume = 25;
229     am_RampType_e ramp = RAMP_DIRECT;
230     am_time_t myTime = 25;
231
232     EXPECT_CALL(pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
233     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
234
235     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
236     sleep(0.5);
237     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
238     pSocketHandler.start_listenting();
239 }
240
241 TEST_F(testRoutingInterfaceAsync,disconnectTooEarly)
242 {
243
244     am_Handle_s handle;
245     handle.handle = 1;
246     handle.handleType = H_CONNECT;
247
248     am_connectionID_t connectionID = 4;
249     am_sourceID_t sourceID = 2;
250     am_sinkID_t sinkID = 1;
251     am_ConnectionFormat_e format = CF_ANALOG;
252
253     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
254     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
255     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
256     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
257     pSocketHandler.start_listenting();
258 }
259
260 TEST_F(testRoutingInterfaceAsync,disconnectAbort)
261 {
262
263     am_Handle_s handle;
264     handle.handle = 1;
265     handle.handleType = H_CONNECT;
266
267     am_connectionID_t connectionID = 4;
268     am_sourceID_t sourceID = 2;
269     am_sinkID_t sinkID = 1;
270     am_ConnectionFormat_e format = CF_ANALOG;
271
272     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
273     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
274     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
275     sleep(2);
276     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
277     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
278     pSocketHandler.start_listenting();
279 }
280
281 TEST_F(testRoutingInterfaceAsync,disconnectNonExisting)
282 {
283
284     am_Handle_s handle;
285     handle.handle = 1;
286     handle.handleType = H_CONNECT;
287
288     am_connectionID_t connectionID = 4;
289
290     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
291     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
292     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
293     pSocketHandler.start_listenting();
294 }
295
296 TEST_F(testRoutingInterfaceAsync,disconnect)
297 {
298
299     am_Handle_s handle;
300     handle.handle = 1;
301     handle.handleType = H_CONNECT;
302
303     am_connectionID_t connectionID = 4;
304     am_sourceID_t sourceID = 2;
305     am_sinkID_t sinkID = 1;
306     am_ConnectionFormat_e format = CF_ANALOG;
307
308     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
309     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
310     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
311     sleep(2);
312     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
313     pSocketHandler.start_listenting();
314 }
315
316 TEST_F(testRoutingInterfaceAsync,connectNoMoreThreads)
317 {
318
319     am_Handle_s handle;
320     handle.handle = 1;
321     handle.handleType = H_CONNECT;
322
323     am_connectionID_t connectionID = 1;
324     am_sourceID_t sourceID = 2;
325     am_sinkID_t sinkID = 1;
326     am_ConnectionFormat_e format = CF_ANALOG;
327
328     EXPECT_CALL(pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
329     for (int i = 0; i < 10; i++)
330     {
331         handle.handle++;
332         connectionID++;
333         ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
334     }ASSERT_EQ(E_NOT_POSSIBLE, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
335     pSocketHandler.start_listenting();
336 }
337
338 TEST_F(testRoutingInterfaceAsync,connectAbortTooLate)
339 {
340
341     am_Handle_s handle;
342     handle.handle = 1;
343     handle.handleType = H_CONNECT;
344
345     am_connectionID_t connectionID = 4;
346     am_sourceID_t sourceID = 2;
347     am_sinkID_t sinkID = 1;
348     am_ConnectionFormat_e format = CF_ANALOG;
349
350     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
351     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
352     sleep(3);
353     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncAbort(handle));
354     pSocketHandler.start_listenting();
355 }
356
357 TEST_F(testRoutingInterfaceAsync,connectAbort)
358 {
359
360     am_Handle_s handle;
361     handle.handle = 1;
362     handle.handleType = H_CONNECT;
363
364     am_connectionID_t connectionID = 4;
365     am_sourceID_t sourceID = 2;
366     am_sinkID_t sinkID = 1;
367     am_ConnectionFormat_e format = CF_ANALOG;
368
369     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
370     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
371     sleep(0.5);
372     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
373     pSocketHandler.start_listenting();
374 }
375
376 TEST_F(testRoutingInterfaceAsync,connectWrongFormat)
377 {
378
379     am_Handle_s handle;
380     handle.handle = 1;
381     handle.handleType = H_CONNECT;
382
383     am_connectionID_t connectionID = 4;
384     am_sourceID_t sourceID = 2;
385     am_sinkID_t sinkID = 1;
386     am_ConnectionFormat_e format = CF_MONO;
387
388     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
389     ASSERT_EQ(E_WRONG_FORMAT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
390     pSocketHandler.start_listenting();
391 }
392
393 TEST_F(testRoutingInterfaceAsync,connectWrongSink)
394 {
395
396     am_Handle_s handle;
397     handle.handle = 1;
398     handle.handleType = H_CONNECT;
399
400     am_connectionID_t connectionID = 4;
401     am_sourceID_t sourceID = 2;
402     am_sinkID_t sinkID = 122;
403     am_ConnectionFormat_e format = CF_ANALOG;
404
405     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
406     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
407     pSocketHandler.start_listenting();
408 }
409
410 TEST_F(testRoutingInterfaceAsync,connectWrongSource)
411 {
412     am_Handle_s handle;
413     handle.handle = 1;
414     handle.handleType = H_CONNECT;
415
416     am_connectionID_t connectionID = 4;
417     am_sourceID_t sourceID = 25;
418     am_sinkID_t sinkID = 1;
419     am_ConnectionFormat_e format = CF_ANALOG;
420
421     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
422     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
423     pSocketHandler.start_listenting();
424 }
425
426 TEST_F(testRoutingInterfaceAsync,connect)
427 {
428
429     am_Handle_s handle;
430     handle.handle = 1;
431     handle.handleType = H_CONNECT;
432
433     am_connectionID_t connectionID = 4;
434     am_sourceID_t sourceID = 2;
435     am_sinkID_t sinkID = 1;
436     am_ConnectionFormat_e format = CF_ANALOG;
437
438     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
439     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
440     pSocketHandler.start_listenting();
441 }
442
443 int main(int argc, char **argv)
444 {
445     ::testing::InitGoogleTest(&argc, argv);
446     return RUN_ALL_TESTS();
447 }
448