diff options
9 files changed, 569 insertions, 7 deletions
diff --git a/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/on_service_status_update_notification_test.cc b/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/on_service_status_update_notification_test.cc new file mode 100644 index 0000000000..5df1d6e207 --- /dev/null +++ b/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/on_service_status_update_notification_test.cc @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2019, 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 <stdint.h> + +#include "application_manager/commands/commands_test.h" +#include "application_manager/mock_application.h" +#include "application_manager/mock_application_manager.h" +#include "application_manager/smart_object_keys.h" +#include "gtest/gtest.h" +#include "hmi/on_service_status_update_notification.h" +#include "smart_objects/smart_object.h" + +namespace test { +namespace components { +namespace commands_test { +namespace hmi_commands_test { +namespace on_service_status_update_notification { + +using namespace application_manager; +using sdl_rpc_plugin::commands::hmi::OnServiceStatusUpdateNotification; + +typedef std::shared_ptr<OnServiceStatusUpdateNotification> NotificationPtr; +typedef hmi_apis::Common_ServiceType::eType ServiceType; +typedef hmi_apis::Common_ServiceEvent::eType ServiceEvent; + +namespace { +const uint32_t kConnectionKey = 1232u; +const uint32_t kHmi_app_id = 321u; +} // namespace + +class OnServiceStatusUpdateNotificationTest + : public CommandsTest<CommandsTestMocks::kIsNice> { + public: + OnServiceStatusUpdateNotificationTest() + : message_(CreateMessage(smart_objects::SmartType_Map)) {} + + public: + MessageSharedPtr message_; + NotificationPtr command_; +}; + +TEST_F(OnServiceStatusUpdateNotificationTest, SendNotificationToHMI) { + (*message_)[strings::msg_params][hmi_notification::service_type] = + ServiceType::AUDIO; + (*message_)[strings::msg_params][hmi_notification::service_event] = + ServiceEvent::REQUEST_ACCEPTED; + (*message_)[strings::msg_params][strings::app_id] = kConnectionKey; + command_ = CreateCommand<OnServiceStatusUpdateNotification>(message_); + + EXPECT_CALL(mock_rpc_service_, SendMessageToHMI(message_)).Times(1); + + auto mock_app = std::make_shared<NiceMock<MockApplication> >(); + + ON_CALL(app_mngr_, application(kConnectionKey)) + .WillByDefault(Return(mock_app)); + + ON_CALL(*mock_app, hmi_app_id()).WillByDefault(Return(kHmi_app_id)); + + command_->Init(); + command_->Run(); +} + +} // namespace on_service_status_update_notification +} // namespace hmi_commands_test +} // namespace commands_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/application_manager_impl_test.cc b/src/components/application_manager/test/application_manager_impl_test.cc index 1f0b705f41..cc6c1418c4 100644 --- a/src/components/application_manager/test/application_manager_impl_test.cc +++ b/src/components/application_manager/test/application_manager_impl_test.cc @@ -105,6 +105,13 @@ const std::string kAppName = "appName"; const WindowID kDefaultWindowId = mobile_apis::PredefinedWindows::DEFAULT_WINDOW; +typedef MockMessageHelper::MockServiceStatusUpdateNotificationBuilder* + MockServiceStatusUpdatePtr; +typedef hmi_apis::Common_ServiceUpdateReason::eType ServiceUpdateReason; +typedef hmi_apis::Common_ServiceType::eType ServiceType; +typedef hmi_apis::Common_ServiceEvent::eType ServiceEvent; +typedef utils::Optional<ServiceUpdateReason> UpdateReasonOptional; + #if defined(CLOUD_APP_WEBSOCKET_TRANSPORT_SUPPORT) // Cloud application params const std::string kEndpoint = "endpoint"; @@ -119,7 +126,20 @@ const bool kEnabled = true; #endif // CLOUD_APP_WEBSOCKET_TRANSPORT_SUPPORT } // namespace -class ApplicationManagerImplTest : public ::testing::Test { +struct ServiceStatus { + ServiceType service_type_; + ServiceEvent service_event_; + UpdateReasonOptional reason_; + + ServiceStatus(ServiceType type, + ServiceEvent event, + UpdateReasonOptional reason) + : service_type_(type), service_event_(event), reason_(reason) {} +}; + +class ApplicationManagerImplTest + : public ::testing::Test, + public ::testing::WithParamInterface<ServiceStatus> { public: ApplicationManagerImplTest() : app_id_(0u) @@ -133,6 +153,8 @@ class ApplicationManagerImplTest : public ::testing::Test { new MockAppServiceManager(mock_app_mngr_, mock_last_state_)) , mock_message_helper_( application_manager::MockMessageHelper::message_helper_mock()) + , mock_service_status_update_(application_manager::MockMessageHelper:: + on_service_update_builder_mock()) { #ifdef ENABLE_LOG @@ -149,10 +171,17 @@ class ApplicationManagerImplTest : public ::testing::Test { CreateAppManager(); ON_CALL(*mock_app_ptr_, app_id()).WillByDefault(Return(kConnectionKey)); ON_CALL(*mock_app_ptr_, device()).WillByDefault(Return(kDeviceId)); + ON_CALL(mock_session_observer_, GetDataOnSessionKey(_, _, _, _)) + .WillByDefault(DoAll(SetArgPointee<3u>(kDeviceId), Return(0))); ON_CALL(mock_connection_handler_, GetDataOnSessionKey(_, _, _, &kDeviceId)) .WillByDefault(DoAll(SetArgPointee<3u>(app_id_), Return(0))); ON_CALL(mock_connection_handler_, get_session_observer()) .WillByDefault(ReturnRef(mock_session_observer_)); + std::unique_ptr<rpc_service::RPCService> unique_rpc_service( + new MockRPCService); + app_manager_impl_->SetRPCService(unique_rpc_service); + mock_rpc_service_ = + static_cast<MockRPCService*>(&(app_manager_impl_->GetRPCService())); app_manager_impl_->SetMockRPCService(mock_rpc_service_); app_manager_impl_->resume_controller().set_resumption_storage( mock_storage_); @@ -250,11 +279,83 @@ class ApplicationManagerImplTest : public ::testing::Test { std::unique_ptr<am::ApplicationManagerImpl> app_manager_impl_; MockAppServiceManager* mock_app_service_manager_; application_manager::MockMessageHelper* mock_message_helper_; + MockServiceStatusUpdatePtr mock_service_status_update_; std::shared_ptr<MockApplication> mock_app_ptr_; NiceMock<protocol_handler_test::MockProtocolHandler> mock_protocol_handler_; }; +INSTANTIATE_TEST_CASE_P( + ProcessServiceStatusUpdate_REQUEST_ACCEPTED, + ApplicationManagerImplTest, + ::testing::Values(ServiceStatus(ServiceType::AUDIO, + ServiceEvent::REQUEST_ACCEPTED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::VIDEO, + ServiceEvent::REQUEST_ACCEPTED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::RPC, + ServiceEvent::REQUEST_ACCEPTED, + UpdateReasonOptional::EMPTY))); + +INSTANTIATE_TEST_CASE_P( + ProcessServiceStatusUpdate_REQUEST_RECEIVED, + ApplicationManagerImplTest, + ::testing::Values(ServiceStatus(ServiceType::AUDIO, + ServiceEvent::REQUEST_RECEIVED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::VIDEO, + ServiceEvent::REQUEST_RECEIVED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::RPC, + ServiceEvent::REQUEST_RECEIVED, + UpdateReasonOptional::EMPTY))); + +INSTANTIATE_TEST_CASE_P( + ProcessServiceStatusUpdate_REQUEST_REJECTED, + ApplicationManagerImplTest, + ::testing::Values(ServiceStatus(ServiceType::AUDIO, + ServiceEvent::REQUEST_REJECTED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::VIDEO, + ServiceEvent::REQUEST_REJECTED, + UpdateReasonOptional::EMPTY), + ServiceStatus(ServiceType::RPC, + ServiceEvent::REQUEST_REJECTED, + UpdateReasonOptional::EMPTY))); + +TEST_P(ApplicationManagerImplTest, + ProcessServiceStatusUpdate_SendMessageToHMI) { + smart_objects::SmartObjectSPtr notification_( + new smart_objects::SmartObject(smart_objects::SmartType_Map)); + (*notification_)[strings::msg_params][hmi_notification::service_type] = + GetParam().service_type_; + (*notification_)[strings::msg_params][hmi_notification::service_event] = + GetParam().service_event_; + (*notification_)[strings::msg_params][strings::app_id] = kConnectionKey; + + AddMockApplication(); + + ON_CALL(*mock_app_ptr_, app_id()).WillByDefault(Return(kConnectionKey)); + ON_CALL(*mock_service_status_update_, + CreateBuilder(GetParam().service_type_, GetParam().service_event_)) + .WillByDefault(Return(*mock_service_status_update_)); + ON_CALL(*mock_service_status_update_, AddAppID(kConnectionKey)) + .WillByDefault(ReturnRef(*mock_service_status_update_)); + ON_CALL(*mock_service_status_update_, AddServiceUpdateReason(_)) + .WillByDefault(ReturnRef(*mock_service_status_update_)); + ON_CALL(*mock_service_status_update_, notification()) + .WillByDefault(Return(notification_)); + + EXPECT_CALL(*mock_rpc_service_, ManageHMICommand(notification_)) + .WillOnce(Return(true)); + + app_manager_impl_->ProcessServiceStatusUpdate(kConnectionKey, + GetParam().service_type_, + GetParam().service_event_, + GetParam().reason_); +} + TEST_F(ApplicationManagerImplTest, ProcessQueryApp_ExpectSuccess) { using namespace ns_smart_device_link::ns_smart_objects; SmartObject app_data; @@ -844,6 +945,7 @@ TEST_F(ApplicationManagerImplTest, std::shared_ptr<MockApplication> nonswitching_app_ptr = std::make_shared<MockApplication>(); + const std::string nonswitching_device_id = "nonswitching"; const std::string nonswitching_device_id_hash = encryption::MakeHash(nonswitching_device_id); diff --git a/src/components/application_manager/test/include/application_manager/mock_message_helper.h b/src/components/application_manager/test/include/application_manager/mock_message_helper.h index eec8d464a5..d78946efd3 100644 --- a/src/components/application_manager/test/include/application_manager/mock_message_helper.h +++ b/src/components/application_manager/test/include/application_manager/mock_message_helper.h @@ -354,8 +354,12 @@ class MockMessageHelper { const hmi_apis::Common_ServiceUpdateReason::eType service_update_reason)); - class MockServiceStatusUpdateNotificationBuilder { + class MockServiceStatusUpdateNotificationBuilder + : public MessageHelper::ServiceStatusUpdateNotificationBuilder { public: + MockServiceStatusUpdateNotificationBuilder( + const MockServiceStatusUpdateNotificationBuilder& obj){}; + MockServiceStatusUpdateNotificationBuilder(){}; MOCK_METHOD2(CreateBuilder, MessageHelper::ServiceStatusUpdateNotificationBuilder( hmi_apis::Common_ServiceType::eType, diff --git a/src/components/application_manager/test/message_helper/message_helper_test.cc b/src/components/application_manager/test/message_helper/message_helper_test.cc index 249d7231d7..966548d707 100644 --- a/src/components/application_manager/test/message_helper/message_helper_test.cc +++ b/src/components/application_manager/test/message_helper/message_helper_test.cc @@ -1113,6 +1113,92 @@ TEST_F(MessageHelperTest, ExtractWindowIdFromSmartObject_FromWrongType) { MessageHelper::ExtractWindowIdFromSmartObject(message)); } +TEST(ServiceStatusUpdateNotificationBuilderTest, CreateBuilderIsCorrect) { + auto service_status_update_builder_ = + ServiceStatusUpdateBuilder::CreateBuilder(ServiceType::AUDIO, + ServiceEvent::REQUEST_ACCEPTED); + auto notification_ = service_status_update_builder_.notification(); + + auto message_type_exist_ = + (*notification_)[strings::params].keyExists(strings::message_type); + auto message_type_ = + (*notification_)[strings::params][strings::message_type].asInt(); + auto function_id_exist_ = + (*notification_)[strings::params].keyExists(strings::function_id); + auto function_id_ = + (*notification_)[strings::params][strings::function_id].asInt(); + auto protocol_version_exist_ = + (*notification_)[strings::params].keyExists(strings::protocol_version); + auto protocol_version_ = + (*notification_)[strings::params][strings::protocol_version].asInt(); + auto protocol_type_exist_ = + (*notification_)[strings::params].keyExists(strings::protocol_type); + auto protocol_type_ = + (*notification_)[strings::params][strings::protocol_type].asInt(); + auto service_type_exist_ = (*notification_)[strings::msg_params].keyExists( + hmi_notification::service_type); + auto service_type_ = + (*notification_)[strings::msg_params][hmi_notification::service_type] + .asInt(); + auto service_event_exist_ = (*notification_)[strings::msg_params].keyExists( + hmi_notification::service_event); + auto service_event_ = + (*notification_)[strings::msg_params][hmi_notification::service_event] + .asInt(); + + EXPECT_TRUE(message_type_exist_); + EXPECT_EQ(static_cast<int64_t>(kNotification), message_type_); + EXPECT_TRUE(function_id_exist_); + EXPECT_EQ(static_cast<int64_t>( + hmi_apis::FunctionID::BasicCommunication_OnServiceUpdate), + function_id_); + EXPECT_TRUE(protocol_version_exist_); + EXPECT_EQ(static_cast<int64_t>(commands::CommandImpl::protocol_version_), + protocol_version_); + EXPECT_TRUE(protocol_type_exist_); + EXPECT_EQ(static_cast<int64_t>(commands::CommandImpl::hmi_protocol_type_), + protocol_type_); + EXPECT_TRUE(service_type_exist_); + EXPECT_EQ(static_cast<int64_t>(ServiceType::AUDIO), service_type_); + EXPECT_TRUE(service_event_exist_); + EXPECT_EQ(static_cast<int64_t>(ServiceEvent::REQUEST_ACCEPTED), + service_event_); +} + +TEST(ServiceStatusUpdateNotificationBuilderTest, AddAppID) { + auto service_status_update_builder_ = + ServiceStatusUpdateBuilder::CreateBuilder(ServiceType::RPC, + ServiceEvent::REQUEST_RECEIVED); + const uint32_t kAppId = 1234u; + + service_status_update_builder_.AddAppID(kAppId); + auto notification_ = service_status_update_builder_.notification(); + + auto app_id_exist_ = + (*notification_)[strings::msg_params].keyExists(strings::app_id); + auto app_id_ = (*notification_)[strings::msg_params][strings::app_id].asInt(); + + EXPECT_TRUE(app_id_exist_); + EXPECT_EQ(kAppId, app_id_); +} + +TEST(ServiceStatusUpdateNotificationBuilderTest, AddServiceUpdateReason) { + auto service_status_update_builder_ = + ServiceStatusUpdateBuilder::CreateBuilder(ServiceType::VIDEO, + ServiceEvent::REQUEST_REJECTED); + + service_status_update_builder_.AddServiceUpdateReason( + UpdateReason::INVALID_CERT); + auto notification_ = service_status_update_builder_.notification(); + auto reason_exist_ = + (*notification_)[strings::msg_params].keyExists(hmi_notification::reason); + auto reason_ = + (*notification_)[strings::msg_params][hmi_notification::reason].asInt(); + + EXPECT_TRUE(reason_exist_); + EXPECT_EQ(static_cast<int64_t>(UpdateReason::INVALID_CERT), reason_); +} + } // namespace application_manager_test } // namespace components } // namespace test diff --git a/src/components/protocol_handler/include/protocol_handler/service_status_update_handler_listener.h b/src/components/protocol_handler/include/protocol_handler/service_status_update_handler_listener.h index 6e1e4ee6c2..e784ae6eb6 100644 --- a/src/components/protocol_handler/include/protocol_handler/service_status_update_handler_listener.h +++ b/src/components/protocol_handler/include/protocol_handler/service_status_update_handler_listener.h @@ -42,7 +42,7 @@ namespace protocol_handler { /** * @brief Converts service type enum value from protocol_handler to hmi_apis. * @param service_type protocol_handler enum value. - **/ + */ hmi_apis::Common_ServiceType::eType GetHMIServiceType( protocol_handler::ServiceType service_type); @@ -50,7 +50,7 @@ hmi_apis::Common_ServiceType::eType GetHMIServiceType( * @brief ServiceStatusUpdateHandlerListener provides callbacks interface with * a purpose to notify HMI on successful or failed state updates of different * services - **/ + */ class ServiceStatusUpdateHandlerListener { public: /** @@ -63,7 +63,7 @@ class ServiceStatusUpdateHandlerListener { * service start. * @param service_update_reason enum value containing reason why service_event * occured. - **/ + */ virtual void ProcessServiceStatusUpdate( const uint32_t connection_key, hmi_apis::Common_ServiceType::eType service_type, diff --git a/src/components/protocol_handler/src/service_status_update_handler.cc b/src/components/protocol_handler/src/service_status_update_handler.cc index b0ffaee9b4..27eb679099 100644 --- a/src/components/protocol_handler/src/service_status_update_handler.cc +++ b/src/components/protocol_handler/src/service_status_update_handler.cc @@ -3,6 +3,8 @@ namespace protocol_handler { +CREATE_LOGGERPTR_GLOBAL(logger_, "ServiceStatusUpdateHandler") + hmi_apis::Common_ServiceType::eType GetHMIServiceType( protocol_handler::ServiceType service_type) { using namespace hmi_apis; @@ -76,7 +78,12 @@ void ServiceStatusUpdateHandler::OnServiceUpdate( Common_ServiceEvent::REQUEST_REJECTED, update_reason); } - default: { return; } + default: { + LOG4CXX_WARN(logger_, + "Received unknown ServiceStatus: " + << static_cast<int32_t>(service_status)); + return; + } } } } // namespace protocol_handler diff --git a/src/components/protocol_handler/test/include/protocol_handler/mock_service_status_update_handler_listener.h b/src/components/protocol_handler/test/include/protocol_handler/mock_service_status_update_handler_listener.h new file mode 100644 index 0000000000..37485aa3ac --- /dev/null +++ b/src/components/protocol_handler/test/include/protocol_handler/mock_service_status_update_handler_listener.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019, 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_PROTOCOL_HANDLER_TEST_INCLUDE_MOCK_SERVICE_STATUS_UPDATE_HANDLER_LISTENER_H +#define SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_MOCK_SERVICE_STATUS_UPDATE_HANDLER_LISTENER_H + +#include "gmock/gmock.h" +#include "protocol_handler/service_status_update_handler_listener.h" + +namespace test { +namespace components { +namespace protocol_handler_test { + +class MockServiceStatusUpdateHandlerListener + : public protocol_handler::ServiceStatusUpdateHandlerListener { + public: + MOCK_METHOD4( + ProcessServiceStatusUpdate, + void(const uint32_t, + hmi_apis::Common_ServiceType::eType, + hmi_apis::Common_ServiceEvent::eType, + utils::Optional<hmi_apis::Common_ServiceUpdateReason::eType>)); +}; +} // namespace protocol_handler_test +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_MOCK_SERVICE_STATUS_UPDATE_HANDLER_LISTENER_H diff --git a/src/components/protocol_handler/test/protocol_handler_tm_test.cc b/src/components/protocol_handler/test/protocol_handler_tm_test.cc index 692b1a7134..600884bae1 100644 --- a/src/components/protocol_handler/test/protocol_handler_tm_test.cc +++ b/src/components/protocol_handler/test/protocol_handler_tm_test.cc @@ -39,6 +39,7 @@ #include "protocol_handler/mock_protocol_handler.h" #include "protocol_handler/mock_protocol_handler_settings.h" #include "protocol_handler/mock_protocol_observer.h" +#include "protocol_handler/mock_service_status_update_handler_listener.h" #include "protocol_handler/mock_session_observer.h" #include "protocol_handler/protocol_handler.h" #include "protocol_handler/protocol_handler_impl.h" @@ -108,6 +109,7 @@ using protocol_handler::PROTOCOL_VERSION_MAX; using protocol_handler::ProtocolHandlerImpl; using protocol_handler::RawMessage; using protocol_handler::RawMessagePtr; +using protocol_handler::ServiceStatusUpdateHandler; using protocol_handler::ServiceType; // For TM states using test::components::security_manager_test::MockSystemTimeHandler; @@ -139,6 +141,9 @@ using ::testing::SetArgPointee; using ::testing::SetArgReferee; typedef std::vector<uint8_t> UCharDataVector; +typedef std::shared_ptr< + testing::NiceMock<MockServiceStatusUpdateHandlerListener> > + MockServiceStatusUpdateHandlerListenerPtr; // custom action to call a member function with 6 arguments ACTION_P4(InvokeMemberFuncWithArg2, ptr, memberFunc, a, b) { @@ -186,10 +191,17 @@ class ProtocolHandlerImplTest : public ::testing::Test { session_observer_mock, connection_handler_mock, transport_manager_mock)); + std::unique_ptr<ServiceStatusUpdateHandler> service_status_update_handler_( + new ServiceStatusUpdateHandler( + &(*mock_service_status_update_handler_listener_))); + protocol_handler_impl->set_service_status_update_handler( + std::move(service_status_update_handler_)); tm_listener = protocol_handler_impl.get(); } void SetUp() OVERRIDE { + mock_service_status_update_handler_listener_ = std::make_shared< + testing::NiceMock<MockServiceStatusUpdateHandlerListener> >(); InitProtocolHandlerImpl(0u, 0u); connection_id = 0xAu; session_id = 0xFFu; @@ -390,6 +402,8 @@ class ProtocolHandlerImplTest : public ::testing::Test { testing::NiceMock<MockProtocolHandlerSettings> protocol_handler_settings_mock; std::shared_ptr<ProtocolHandlerImpl> protocol_handler_impl; + MockServiceStatusUpdateHandlerListenerPtr + mock_service_status_update_handler_listener_; TransportManagerListener* tm_listener; // Uniq connection ::transport_manager::ConnectionUID connection_id; @@ -737,7 +751,7 @@ TEST_F(ProtocolHandlerImplTest, AddSecurityManager(); EXPECT_CALL(session_observer_mock, KeyFromPair(connection_id2, session_id2)) - .WillOnce(Return(connection_key)); + .WillRepeatedly(Return(connection_key)); EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, start_service)) diff --git a/src/components/protocol_handler/test/service_status_update_handler_test.cc b/src/components/protocol_handler/test/service_status_update_handler_test.cc new file mode 100644 index 0000000000..fa64ffbb3b --- /dev/null +++ b/src/components/protocol_handler/test/service_status_update_handler_test.cc @@ -0,0 +1,196 @@ +/* + * Copyright (c) 2019, 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 "protocol_handler/service_status_update_handler.h" +#include "gtest/gtest.h" +#include "protocol_handler/mock_service_status_update_handler_listener.h" + +namespace test { +namespace components { +namespace protocol_handler_test { + +namespace { +const uint32_t kConnectioKey = 123u; +} + +using namespace protocol_handler; +using ::testing::_; +using ::testing::Return; +using namespace hmi_apis; +typedef utils::Optional<Common_ServiceUpdateReason::eType> UpdateReasonOptional; +typedef std::shared_ptr<ServiceStatusUpdateHandler> + ServiceStatusUpdateHandlerPtr; +typedef std::shared_ptr<MockServiceStatusUpdateHandlerListener> + MockServiceStatusUpdateHandlerListenerPtr; + +struct ServiceUpdate { + ServiceType service_type_; + ServiceStatus service_status_; + ServiceUpdate(ServiceType type, ServiceStatus status) + : service_type_(type), service_status_(status) {} +}; + +class ServiceStatusUpdateHandlerTest + : public ::testing::TestWithParam<ServiceUpdate> { + public: + ServiceStatusUpdateHandlerTest() { + mock_service_status_update_handler_listener_.reset( + new MockServiceStatusUpdateHandlerListener); + service_status_update_handler_ = + std::make_shared<ServiceStatusUpdateHandler>( + &(*mock_service_status_update_handler_listener_)); + } + + Common_ServiceEvent::eType GetServiceEvent(ServiceStatus status) { + switch (status) { + case ServiceStatus::SERVICE_ACCEPTED: { + return Common_ServiceEvent::REQUEST_ACCEPTED; + } + case ServiceStatus::SERVICE_RECEIVED: { + return Common_ServiceEvent::REQUEST_RECEIVED; + } + case ServiceStatus::SERVICE_START_FAILED: + case ServiceStatus::PTU_FAILED: + case ServiceStatus::CERT_INVALID: + case ServiceStatus::INVALID_TIME: { + return Common_ServiceEvent::REQUEST_REJECTED; + } + default: { return Common_ServiceEvent::INVALID_ENUM; } + } + } + + UpdateReasonOptional GetUpdateReason(ServiceStatus status) { + switch (status) { + case ServiceStatus::SERVICE_START_FAILED: + case ServiceStatus::SERVICE_ACCEPTED: + case ServiceStatus::SERVICE_RECEIVED: { + return UpdateReasonOptional::EMPTY; + } + case ServiceStatus::PTU_FAILED: { + auto reason = Common_ServiceUpdateReason::PTU_FAILED; + return reason; + } + case ServiceStatus::CERT_INVALID: { + auto reason = Common_ServiceUpdateReason::INVALID_CERT; + return reason; + } + case ServiceStatus::INVALID_TIME: { + auto reason = Common_ServiceUpdateReason::INVALID_TIME; + return reason; + } + default: { + auto reason = Common_ServiceUpdateReason::INVALID_ENUM; + return reason; + } + } + } + + public: + MockServiceStatusUpdateHandlerListenerPtr + mock_service_status_update_handler_listener_; + ServiceStatusUpdateHandlerPtr service_status_update_handler_; +}; + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_SERVICE_ACCEPTED, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::SERVICE_ACCEPTED), + ServiceUpdate(ServiceType::kMobileNav, ServiceStatus::SERVICE_ACCEPTED), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::SERVICE_ACCEPTED))); + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_SERVICE_RECEIVED, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::SERVICE_RECEIVED), + ServiceUpdate(ServiceType::kMobileNav, ServiceStatus::SERVICE_RECEIVED), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::SERVICE_RECEIVED))); + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_SERVICE_START_FAILED, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::SERVICE_START_FAILED), + ServiceUpdate(ServiceType::kMobileNav, + ServiceStatus::SERVICE_START_FAILED), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::SERVICE_START_FAILED))); + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_CERT_INVALID, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::CERT_INVALID), + ServiceUpdate(ServiceType::kMobileNav, ServiceStatus::CERT_INVALID), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::CERT_INVALID))); + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_INVALID_TIME, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::INVALID_TIME), + ServiceUpdate(ServiceType::kMobileNav, ServiceStatus::INVALID_TIME), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::INVALID_TIME))); + +INSTANTIATE_TEST_CASE_P( + OnServiceUpdate_PTU_FAILED, + ServiceStatusUpdateHandlerTest, + ::testing::Values( + ServiceUpdate(ServiceType::kAudio, ServiceStatus::PTU_FAILED), + ServiceUpdate(ServiceType::kMobileNav, ServiceStatus::PTU_FAILED), + ServiceUpdate(ServiceType::kRpc, ServiceStatus::PTU_FAILED))); + +TEST_P(ServiceStatusUpdateHandlerTest, OnServiceUpdate) { + auto service_event_ = GetServiceEvent(GetParam().service_status_); + auto reason_ = GetUpdateReason(GetParam().service_status_); + + EXPECT_CALL( + *mock_service_status_update_handler_listener_, + ProcessServiceStatusUpdate(kConnectioKey, _, service_event_, reason_)) + .Times(1); + + service_status_update_handler_->OnServiceUpdate( + kConnectioKey, GetParam().service_type_, GetParam().service_status_); +} + +TEST_F(ServiceStatusUpdateHandlerTest, GetHMIServiceType) { + EXPECT_EQ(Common_ServiceType::RPC, GetHMIServiceType(ServiceType::kRpc)); + EXPECT_EQ(Common_ServiceType::AUDIO, GetHMIServiceType(ServiceType::kAudio)); + EXPECT_EQ(Common_ServiceType::VIDEO, + GetHMIServiceType(ServiceType::kMobileNav)); + EXPECT_EQ(Common_ServiceType::INVALID_ENUM, + GetHMIServiceType(ServiceType::kInvalidServiceType)); +} + +} // namespace protocol_handler_test +} // namespace components +} // namespace test |