summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/test/AmSerializerTest
diff options
context:
space:
mode:
authorAleksandar Donchev <Aleksander.Donchev@partner.bmw.de>2017-03-14 14:55:31 +0100
committerChristian Linke <christian.linke@bmw.de>2017-05-02 06:25:51 -0700
commitda78ca5c2eb7e2ff2f9153361774156f8a454daa (patch)
treefd166672b42da293fc7c41bd9f858e95d590db26 /AudioManagerUtilities/test/AmSerializerTest
parent99c3edf827a16d4bdc1c40a5df7c18bf8626afa3 (diff)
downloadaudiomanager-da78ca5c2eb7e2ff2f9153361774156f8a454daa.tar.gz
CAmSerializer interface extended to support std::function.
Signed-off-by: Christian Linke <christian.linke@bmw.de> Change-Id: I8b4c2c436ac9fbc37c76a21145c731f327cab0e4
Diffstat (limited to 'AudioManagerUtilities/test/AmSerializerTest')
-rw-r--r--AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp158
-rw-r--r--AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.h99
-rw-r--r--AudioManagerUtilities/test/AmSerializerTest/CMakeLists.txt47
3 files changed, 304 insertions, 0 deletions
diff --git a/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp
new file mode 100644
index 0000000..06488ea
--- /dev/null
+++ b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp
@@ -0,0 +1,158 @@
+/**
+ * SPDX license identifier: MPL-2.0
+ *
+ * Copyright (C) 2012, BMW AG
+ *
+ * This file is part of GENIVI Project AudioManager.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
+ *
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * For further information see http://www.genivi.org/.
+ *
+ */
+
+#include <cstdio>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <sys/ioctl.h>
+#include <string.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <sys/un.h>
+#include <sys/poll.h>
+
+#include "CAmSocketHandler.h"
+#include "CAmSerializer.h"
+#include "CAmSerializerTest.h"
+
+using namespace testing;
+using namespace am;
+
+CAmTimerSockethandlerController::CAmTimerSockethandlerController(CAmSocketHandler *myHandler, const timespec &timeout) :
+ MockIAmTimerCb(), mpSocketHandler(myHandler), mUpdateTimeout(timeout), pTimerCallback(this, &CAmTimerSockethandlerController::timerCallback)
+{
+}
+
+am::CAmTimerSockethandlerController::~CAmTimerSockethandlerController()
+{
+}
+
+void am::CAmTimerSockethandlerController::timerCallback(sh_timerHandle_t handle, void* userData)
+{
+ MockIAmTimerCb::timerCallback(handle, userData);
+ mpSocketHandler->stop_listening();
+}
+
+CAmSerializerTest::CAmSerializerTest()
+{
+}
+
+CAmSerializerTest::~CAmSerializerTest()
+{
+}
+
+void CAmSerializerTest::SetUp()
+{
+
+}
+
+void CAmSerializerTest::TearDown()
+{
+}
+
+struct SerializerData
+{
+
+ std::string testStr;
+ int result;
+ MockIAmSerializerCb *pSerCb;
+ CAmSocketHandler *pSocketHandler;
+ CAmSerializer *pSerializer;
+};
+
+void* ptSerializer(void* data)
+{
+ SerializerData *pData = (SerializerData*) data;
+ std::string testStr(pData->testStr);
+ bool result = false;
+ int r = 0;
+ const uint32_t ten = 10;
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+ pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::check);
+ pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::checkInt, pData->result);
+ pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::dispatchData, result, ten, pData->testStr);
+
+ for (uint32_t i = 0; i < 5; i++)
+ pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::dispatchData, i, testStr);
+
+ pData->pSerializer->asyncInvocation(std::bind([]()->bool
+ { return 1;}));
+ pData->pSerializer->asyncInvocation(std::bind([](const int i, int & result)
+ { result = i*10;}, 1, std::ref(r)));
+
+ pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::check);
+ pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::check);
+
+ pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::checkInt);
+#pragma GCC diagnostic pop
+ return (NULL);
+}
+
+ACTION(ActionDispatchData){
+arg1="DispatchData";
+}
+
+TEST(CAmSerializerTest, serializerTest)
+{
+ pthread_t serThread;
+
+ MockIAmSerializerCb serCb;
+ CAmSocketHandler myHandler;
+ std::string testStr("testStr");
+ CAmSerializer serializer(&myHandler);
+ sh_timerHandle_t handle;
+ timespec timeout4;
+ timeout4.tv_nsec = 0;
+ timeout4.tv_sec = 3;
+ CAmTimerSockethandlerController testCallback4(&myHandler, timeout4);
+ myHandler.addTimer(timeout4, &testCallback4.pTimerCallback, handle, NULL);
+ EXPECT_CALL(testCallback4,timerCallback(handle,NULL)).Times(1);
+
+ SerializerData serializerData;
+ serializerData.result = 0;
+ serializerData.testStr = testStr;
+ serializerData.pSerCb = &serCb;
+ serializerData.pSocketHandler = &myHandler;
+ serializerData.pSerializer = &serializer;
+ pthread_create(&serThread, NULL, ptSerializer, &serializerData);
+
+ EXPECT_CALL(serCb,check()).Times(3);
+ EXPECT_CALL(serCb,checkInt()).Times(2).WillRepeatedly(Return(100));
+
+ EXPECT_CALL(serCb,dispatchData(10,testStr)).WillOnce(DoAll(ActionDispatchData(), Return(true)));
+ for (int i = 0; i < 5; i++)
+ EXPECT_CALL(serCb,dispatchData(i,testStr)).WillOnce(DoAll(ActionDispatchData(), Return(true)));
+ myHandler.start_listenting();
+
+ pthread_join(serThread, NULL);
+ ASSERT_TRUE(serializerData.testStr == "DispatchData");
+ ASSERT_TRUE(serializerData.result == 100);
+}
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
+
diff --git a/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.h b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.h
new file mode 100644
index 0000000..8ae3737
--- /dev/null
+++ b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.h
@@ -0,0 +1,99 @@
+/**
+ * SPDX license identifier: MPL-2.0
+ *
+ * Copyright (C) 2012, BMW AG
+ *
+ * This file is part of GENIVI Project AudioManager.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
+ *
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * For further information see http://www.genivi.org/.
+ *
+ */
+
+#ifndef SERIALIZERTEST_H_
+#define SERIALIZERTEST_H_
+
+#define WITH_DLT
+
+#include <ctime>
+#include <chrono>
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include <queue>
+#include "CAmSocketHandler.h"
+
+namespace am
+{
+
+ class IAmSerializerCb
+ {
+ public:
+ virtual ~IAmSerializerCb()
+ {
+ }
+ virtual bool dispatchData(const uint32_t handle, std::string & outString)=0;
+ virtual void check()=0;
+ virtual int checkInt()=0;
+ };
+
+ class IAmTimerCb
+ {
+ public:
+ virtual ~IAmTimerCb()
+ {
+ }
+ virtual void timerCallback(sh_timerHandle_t handle, void * userData)=0;
+ };
+
+ class MockIAmTimerCb: public IAmTimerCb
+ {
+ public:
+ MOCK_CONST_METHOD2(timerCallback,
+ void(sh_timerHandle_t handle, void *userData));
+ };
+
+ class CAmTimerSockethandlerController: public MockIAmTimerCb
+ {
+ CAmSocketHandler *mpSocketHandler;
+ timespec mUpdateTimeout;
+ public:
+ explicit CAmTimerSockethandlerController(CAmSocketHandler *SocketHandler, const timespec &timeout);
+ virtual ~CAmTimerSockethandlerController();
+
+ void timerCallback(sh_timerHandle_t handle, void * userData);
+
+ TAmShTimerCallBack<CAmTimerSockethandlerController> pTimerCallback;
+ };
+
+ class MockIAmSerializerCb: public IAmSerializerCb
+ {
+ public:
+ MOCK_METHOD2(dispatchData,
+ bool(const uint32_t handle, std::string & outString));
+ MOCK_METHOD0(check,
+ void());
+ MOCK_METHOD0(checkInt,
+ int());
+ };
+
+ class CAmSerializerTest: public ::testing::Test
+ {
+ public:
+ CAmSerializerTest();
+ ~CAmSerializerTest();
+ void SetUp();
+ void TearDown();
+ };
+
+} /* namespace am */
+#endif /* SOCKETHANDLERTEST_H_ */
diff --git a/AudioManagerUtilities/test/AmSerializerTest/CMakeLists.txt b/AudioManagerUtilities/test/AmSerializerTest/CMakeLists.txt
new file mode 100644
index 0000000..3e89267
--- /dev/null
+++ b/AudioManagerUtilities/test/AmSerializerTest/CMakeLists.txt
@@ -0,0 +1,47 @@
+# Copyright (C) 2012, BMW AG
+#
+# This file is part of GENIVI Project AudioManager.
+#
+# Contributions are licensed to the GENIVI Alliance under one or more
+# Contribution License Agreements.
+#
+# copyright
+# This Source Code Form is subject to the terms of the
+# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+# this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+#
+# For further information see http://www.genivi.org/.
+#
+
+cmake_minimum_required(VERSION 3.0)
+
+project(AmSerializerTest LANGUAGES CXX VERSION ${DAEMONVERSION})
+
+INCLUDE_DIRECTORIES(
+ ${AUDIOMANAGER_UTILITIES_INCLUDE}
+ ${GMOCK_INCLUDE_DIRS}
+ ${GTEST_INCLUDE_DIRS})
+
+file(GLOB Socket_SRCS_CXX
+ "*.cpp"
+)
+
+ADD_EXECUTABLE(AmSerializerTest ${Socket_SRCS_CXX})
+
+TARGET_LINK_LIBRARIES(AmSerializerTest
+ ${GTEST_LIBRARIES}
+ ${GMOCK_LIBRARIES}
+ AudioManagerUtilities
+)
+
+ADD_DEPENDENCIES(AmSerializerTest AudioManagerUtilities)
+
+INSTALL(TARGETS AmSerializerTest
+ DESTINATION ${TEST_EXECUTABLE_INSTALL_PATH}
+ PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
+ COMPONENT tests
+)
+
+