summaryrefslogtreecommitdiff
path: root/src/components/hmi_message_handler
diff options
context:
space:
mode:
authorHerasym Oleh <oolleehh@gmail.com>2015-12-02 10:40:51 +0200
committerHerasym Oleh <oolleehh@gmail.com>2015-12-03 10:57:35 +0200
commit0187bded637eeb338ea9debdf2807d1cd66806d2 (patch)
tree41330927eddf5bc4a884fd0b0ee36b266fba42e1 /src/components/hmi_message_handler
parenta2bab2cd053a67cd4b1754ed6f38abbeb3779fde (diff)
downloadsdl_core-0187bded637eeb338ea9debdf2807d1cd66806d2.tar.gz
hottfix/Fix_Unit_tests_for_HMIMessageHandler_in_genivi
Added hmi_message_handler unit tests. Added Log warnings. Related: APPLINK-19074
Diffstat (limited to 'src/components/hmi_message_handler')
-rw-r--r--src/components/hmi_message_handler/CMakeLists.txt6
-rw-r--r--src/components/hmi_message_handler/include/hmi_message_handler/hmi_message_handler_impl.h10
-rw-r--r--src/components/hmi_message_handler/src/hmi_message_handler_impl.cc10
-rw-r--r--src/components/hmi_message_handler/test/CMakeLists.txt7
-rw-r--r--src/components/hmi_message_handler/test/hmi_message_handler_impl_test.cc131
-rw-r--r--src/components/hmi_message_handler/test/include/hmi_message_handler/mock_hmi_message_observer.h58
6 files changed, 216 insertions, 6 deletions
diff --git a/src/components/hmi_message_handler/CMakeLists.txt b/src/components/hmi_message_handler/CMakeLists.txt
index 9559b6b00f..3f06d07349 100644
--- a/src/components/hmi_message_handler/CMakeLists.txt
+++ b/src/components/hmi_message_handler/CMakeLists.txt
@@ -76,6 +76,6 @@ endif()
# Tests temporary are inactivated. For details please check
# Readme.txt in test directory of hmi_message_handler
-#if(BUILD_TESTS)
-# add_subdirectory(test)
-#endif()
+if(BUILD_TESTS)
+ add_subdirectory(test)
+endif()
diff --git a/src/components/hmi_message_handler/include/hmi_message_handler/hmi_message_handler_impl.h b/src/components/hmi_message_handler/include/hmi_message_handler/hmi_message_handler_impl.h
index 9e275be047..f1f58eaf2e 100644
--- a/src/components/hmi_message_handler/include/hmi_message_handler/hmi_message_handler_impl.h
+++ b/src/components/hmi_message_handler/include/hmi_message_handler/hmi_message_handler_impl.h
@@ -93,6 +93,16 @@ class HMIMessageHandlerImpl
void AddHMIMessageAdapter(HMIMessageAdapter* adapter);
void RemoveHMIMessageAdapter(HMIMessageAdapter* adapter);
+#ifdef BUILD_TESTS
+ std::set<HMIMessageAdapter*> message_adapters() const {
+ return message_adapters_;
+ }
+
+ HMIMessageObserver* observer() const {
+ return observer_;
+ }
+#endif // BUILD_TESTS
+
private:
HMIMessageHandlerImpl();
diff --git a/src/components/hmi_message_handler/src/hmi_message_handler_impl.cc b/src/components/hmi_message_handler/src/hmi_message_handler_impl.cc
index d2e81e708f..c6d8cf4a50 100644
--- a/src/components/hmi_message_handler/src/hmi_message_handler_impl.cc
+++ b/src/components/hmi_message_handler/src/hmi_message_handler_impl.cc
@@ -89,15 +89,21 @@ void HMIMessageHandlerImpl::OnErrorSending(MessageSharedPointer message) {
void HMIMessageHandlerImpl::AddHMIMessageAdapter(
HMIMessageAdapter* adapter) {
LOG4CXX_AUTO_TRACE(logger_);
+ if (!adapter) {
+ LOG4CXX_WARN(logger_, "HMIMessageAdapter is not valid!");
+ return;
+ }
message_adapters_.insert(adapter);
}
void HMIMessageHandlerImpl::RemoveHMIMessageAdapter(
HMIMessageAdapter* adapter) {
LOG4CXX_AUTO_TRACE(logger_);
- if (adapter != NULL) {
- message_adapters_.erase(adapter);
+ if (!adapter) {
+ LOG4CXX_WARN(logger_, "HMIMessageAdapter is not valid!");
+ return;
}
+ message_adapters_.erase(adapter);
}
void HMIMessageHandlerImpl::Handle(const impl::MessageFromHmi message) {
diff --git a/src/components/hmi_message_handler/test/CMakeLists.txt b/src/components/hmi_message_handler/test/CMakeLists.txt
index 4a97e299f0..ff2d99828f 100644
--- a/src/components/hmi_message_handler/test/CMakeLists.txt
+++ b/src/components/hmi_message_handler/test/CMakeLists.txt
@@ -33,16 +33,21 @@ if(BUILD_TESTS)
include_directories (
${GMOCK_INCLUDE_DIRECTORY}
${COMPONENTS_DIR}/hmi_message_handler/include
+ ${COMPONENTS_DIR}/hmi_message_handler/test/include
)
set(LIBRARIES
gmock
ApplicationManager
HMIMessageHandler
+ jsoncpp
+ MessageBrokerClient
+ MessageBrokerServer
+ ConfigProfile
)
set(SOURCES
- ${COMPONENTS_DIR}/hmi_message_handler/test/mqueue_adapter_test.cc
+ ${COMPONENTS_DIR}/hmi_message_handler/test/hmi_message_handler_impl_test.cc
)
if(${QT_HMI})
diff --git a/src/components/hmi_message_handler/test/hmi_message_handler_impl_test.cc b/src/components/hmi_message_handler/test/hmi_message_handler_impl_test.cc
new file mode 100644
index 0000000000..3d16b2f40c
--- /dev/null
+++ b/src/components/hmi_message_handler/test/hmi_message_handler_impl_test.cc
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2015, Ford Motor Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "gmock/gmock.h"
+#include "application_manager/message.h"
+#include "config_profile/profile.h"
+#include "hmi_message_handler/hmi_message_handler_impl.h"
+#include "hmi_message_handler/messagebroker_adapter.h"
+#include "hmi_message_handler/mock_hmi_message_observer.h"
+
+namespace test {
+namespace components {
+namespace hmi_message_handler_test {
+
+class HMIMessageHandlerImplTest : public ::testing::Test {
+ public:
+ HMIMessageHandlerImplTest()
+ : mb_adapter_(NULL),
+ hmi_handler_(NULL),
+ mock_hmi_message_observer_(NULL) {}
+
+ protected:
+ hmi_message_handler::MessageBrokerAdapter* mb_adapter_;
+ hmi_message_handler::HMIMessageHandlerImpl* hmi_handler_;
+ hmi_message_handler::MockHMIMessageObserver* mock_hmi_message_observer_;
+
+ virtual void SetUp() OVERRIDE {
+ hmi_handler_ = hmi_message_handler::HMIMessageHandlerImpl::instance();
+ ASSERT_TRUE(NULL != hmi_handler_);
+ mb_adapter_ = new hmi_message_handler::MessageBrokerAdapter(
+ hmi_handler_, "localhost", 22);
+ ASSERT_TRUE(NULL != mb_adapter_);
+ mock_hmi_message_observer_ =
+ hmi_message_handler::MockHMIMessageObserver::instance();
+ ASSERT_TRUE(NULL != mock_hmi_message_observer_);
+ hmi_handler_->set_message_observer(mock_hmi_message_observer_);
+ EXPECT_TRUE(NULL != hmi_handler_->observer());
+ }
+
+ virtual void TearDown() OVERRIDE {
+ hmi_handler_->set_message_observer(NULL);
+ hmi_message_handler::MockHMIMessageObserver::destroy();
+ hmi_message_handler::HMIMessageHandlerImpl::destroy();
+ delete mb_adapter_;
+ }
+};
+
+TEST_F(HMIMessageHandlerImplTest,
+ OnErrorSending_EmptyMessage_OnErrorSendingProceeded) {
+ // Arrange
+ hmi_message_handler::MessageSharedPointer empty_message;
+ EXPECT_CALL(*mock_hmi_message_observer_, OnErrorSending(empty_message));
+ // Act
+ hmi_handler_->OnErrorSending(empty_message);
+}
+
+TEST_F(HMIMessageHandlerImplTest,
+ OnErrorSending_NotEmptyMessage_ExpectOnErrorSendingProceeded) {
+ // Arrange
+ utils::SharedPtr<application_manager::Message> message(
+ new application_manager::Message(
+ protocol_handler::MessagePriority::FromServiceType(
+ protocol_handler::ServiceType::kControl)));
+
+ EXPECT_CALL(*mock_hmi_message_observer_, OnErrorSending(message));
+ // Act
+ hmi_handler_->OnErrorSending(message);
+}
+
+TEST_F(HMIMessageHandlerImplTest,
+ AddHMIMessageAdapter_AddExistedAdapter_ExpectAdded) {
+ // Check before action
+ EXPECT_TRUE(hmi_handler_->message_adapters().empty());
+ // Act
+ hmi_handler_->AddHMIMessageAdapter(mb_adapter_);
+ // Check after action
+ EXPECT_EQ(1u, hmi_handler_->message_adapters().size());
+}
+
+TEST_F(HMIMessageHandlerImplTest,
+ AddHMIMessageAdapter_AddUnexistedAdapter_ExpectNotAdded) {
+ // Check before action
+ EXPECT_TRUE(hmi_handler_->message_adapters().empty());
+ // Act
+ mb_adapter_ = NULL;
+ hmi_handler_->AddHMIMessageAdapter(mb_adapter_);
+ // Check adapter not added
+ EXPECT_TRUE(hmi_handler_->message_adapters().empty());
+}
+
+TEST_F(HMIMessageHandlerImplTest, RemoveHMIMessageAdapter_ExpectRemoved) {
+ // Arrange
+ hmi_handler_->AddHMIMessageAdapter(mb_adapter_);
+ // Act
+ hmi_handler_->RemoveHMIMessageAdapter(mb_adapter_);
+ // Check after action
+ EXPECT_TRUE(hmi_handler_->message_adapters().empty());
+}
+
+} // namespace hmi_message_handler_test
+} // namespace components
+} // namespace test
diff --git a/src/components/hmi_message_handler/test/include/hmi_message_handler/mock_hmi_message_observer.h b/src/components/hmi_message_handler/test/include/hmi_message_handler/mock_hmi_message_observer.h
new file mode 100644
index 0000000000..cee922cdaa
--- /dev/null
+++ b/src/components/hmi_message_handler/test/include/hmi_message_handler/mock_hmi_message_observer.h
@@ -0,0 +1,58 @@
+/*
+* Copyright (c) 2015, Ford Motor Company
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+*
+* Redistributions of source code must retain the above copyright notice, this
+* list of conditions and the following disclaimer.
+*
+* Redistributions in binary form must reproduce the above copyright notice,
+* this list of conditions and the following
+* disclaimer in the documentation and/or other materials provided with the
+* distribution.
+*
+* Neither the name of the Ford Motor Company nor the names of its contributors
+* may be used to endorse or promote products derived from this software
+* without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+* POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef SRC_COMPONENTS_HMI_MESSAGE_HANDLER_TEST_INCLUDE_HMI_MESSAGE_HANDLER_MOCK_HMI_MESSAGE_OBSERVER_H_
+#define SRC_COMPONENTS_HMI_MESSAGE_HANDLER_TEST_INCLUDE_HMI_MESSAGE_HANDLER_MOCK_HMI_MESSAGE_OBSERVER_H_
+
+#include "gmock/gmock.h"
+#include "utils/shared_ptr.h"
+#include "utils/singleton.h"
+#include "hmi_message_handler/hmi_message_observer.h"
+#include "application_manager/message.h"
+
+namespace hmi_message_handler {
+
+using ::hmi_message_handler::HMIMessageObserver;
+
+class MockHMIMessageObserver : public HMIMessageObserver,
+ public utils::Singleton<MockHMIMessageObserver> {
+ public:
+ MOCK_METHOD1(OnMessageReceived,
+ void(utils::SharedPtr<application_manager::Message> message));
+ MOCK_METHOD1(OnErrorSending,
+ void(utils::SharedPtr<application_manager::Message> message));
+ virtual ~MockHMIMessageObserver() {}
+};
+} // namespace hmi_message_handler
+
+
+#endif // SRC_COMPONENTS_HMI_MESSAGE_HANDLER_TEST_INCLUDE_HMI_MESSAGE_HANDLER_MOCK_HMI_MESSAGE_OBSERVER_H_