diff options
author | Ira Lytvynenko <ILytvynenko@luxoft.com> | 2018-02-18 18:32:17 +0200 |
---|---|---|
committer | Ira Lytvynenko (GitHub) <ILytvynenko@luxoft.com> | 2018-06-26 12:01:45 +0300 |
commit | 0f058ffca2685c2630b8b31561b2b77f799b7cf5 (patch) | |
tree | d1b8bd0451363c3a6067ee6586a20024b47de440 | |
parent | a07dc2b3df6324d8d238f08c266251214e2cb683 (diff) | |
download | sdl_core-0f058ffca2685c2630b8b31561b2b77f799b7cf5.tar.gz |
Fix UTs
Fixed application_manager_mock_hmi_test (SEGFAULT)
Added mock_rpc_plugin.h & mock_rpc_plugin_manager.h
Changed application_manager_impl.h, application_manager.h &
mock_application_manager.h
Fixed application_manager_impl.cc, rpc_plugin_manager.h
application_manager_impl_mock_hmi_test.cc
14 files changed, 1239 insertions, 6 deletions
diff --git a/src/components/application_manager/include/application_manager/application_manager_impl.h b/src/components/application_manager/include/application_manager/application_manager_impl.h index b7477edc14..a10b740355 100644 --- a/src/components/application_manager/include/application_manager/application_manager_impl.h +++ b/src/components/application_manager/include/application_manager/application_manager_impl.h @@ -1476,9 +1476,9 @@ bool IsSOStructValid(const hmi_apis::StructIdentifiers::eType struct_id, std::auto_ptr<policy::PolicyHandlerInterface> policy_handler_; protocol_handler::ProtocolHandler* protocol_handler_; request_controller::RequestController request_ctrl_; + std::unique_ptr<plugin_manager::RPCPluginManager> plugin_manager_; #ifdef SDL_REMOTE_CONTROL - std::unique_ptr<plugin_manager::RPCPluginManager> plugin_manager_; /** * @brief Map contains apps with HMI state before incoming call @@ -1576,6 +1576,12 @@ bool IsSOStructValid(const hmi_apis::StructIdentifiers::eType struct_id, */ void SetMockMediaManager(media_manager::MediaManager* mock_media_manager); + virtual void SetPluginManager( + std::unique_ptr<plugin_manager::RPCPluginManager>& plugin_manager) + OVERRIDE { + plugin_manager_.reset(plugin_manager.release()); + } + private: #endif diff --git a/src/components/application_manager/include/application_manager/plugin_manager/rpc_plugin_manager.h b/src/components/application_manager/include/application_manager/plugin_manager/rpc_plugin_manager.h index ecf51b861a..410eacd9e1 100644 --- a/src/components/application_manager/include/application_manager/plugin_manager/rpc_plugin_manager.h +++ b/src/components/application_manager/include/application_manager/plugin_manager/rpc_plugin_manager.h @@ -12,6 +12,7 @@ namespace plugin_manager { class RPCPluginManager { public: + virtual ~RPCPluginManager() {} /** * @brief LoadPlugins load plugins from plugin folder. * Already loaded plugins should continue be loaded. diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/CMakeLists.txt b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/CMakeLists.txt new file mode 100644 index 0000000000..d6725bd4be --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/CMakeLists.txt @@ -0,0 +1,62 @@ +# Copyright (c) 2017, 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(${CMAKE_SOURCE_DIR}/tools/cmake/helpers/sources.cmake) + +include_directories( + ${GMOCK_INCLUDE_DIRECTORY} + ${COMPONENTS_DIR}/rc_rpc_plugin/include/ + ${COMPONENTS_DIR}/rc_rpc_plugin/include/rc_rpc_plugin/ + ${COMPONENTS_DIR}/rc_rpc_plugin/include/rc_rpc_plugin/commands/ + ${COMPONENTS_DIR}/rc_rpc_plugin/test/include +) + +set(RC_COMMANDS_TEST_DIR ${RC_TEST_DIR}/commands) + +file(GLOB SOURCES + ${COMPONENTS_DIR}/application_manager/test/mock_message_helper.cc + ${RC_COMMANDS_TEST_DIR}/* +) + +set(LIBRARIES + gmock + RemoteControlModule + Utils + SmartObjects + jsoncpp + HMI_API + MOBILE_API + ApplicationManager + AMHMICommandsLibrary + AMMobileCommandsLibrary + connectionHandler +) + +create_test("rc_commands_test" "${SOURCES}" "${LIBRARIES}" ) diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/button_press_request_test.cc b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/button_press_request_test.cc new file mode 100644 index 0000000000..5098f6472e --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/button_press_request_test.cc @@ -0,0 +1,319 @@ +/* + * Copyright (c) 2018, 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 "gtest/gtest.h" +#include "rc_rpc_plugin/commands/mobile/button_press_request.h" +#include "rc_rpc_plugin/rc_command_factory.h" +#include "rc_rpc_plugin/rc_app_extension.h" +#include "rc_rpc_plugin/rc_rpc_plugin.h" +#include "rc_rpc_plugin/rc_module_constants.h" +#include "rc_rpc_plugin/mock/mock_resource_allocation_manager.h" +#include "application_manager/mock_application.h" +#include "application_manager/mock_application_manager.h" +#include "application_manager/commands/command_request_test.h" +#include "application_manager/policies/mock_policy_handler_interface.h" +#include "utils/shared_ptr.h" +#include "utils/make_shared.h" +#include "interfaces/HMI_API.h" +#include "interfaces/MOBILE_API.h" + +using test::components::application_manager_test::MockApplication; +using test::components::commands_test::CommandRequestTest; +using test::components::commands_test::CommandsTestMocks; +using test::components::commands_test::HMIResultCodeIs; +using test::components::commands_test::MobileResultCodeIs; +using ::testing::_; +using ::testing::Mock; +using ::testing::NiceMock; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::SaveArg; +using ::application_manager::Message; +using ::application_manager::MessageType; +using application_manager::commands::MessageSharedPtr; +using ::protocol_handler::MessagePriority; + +namespace { +const int kModuleId = 153u; +const uint32_t kConnectionKey = 1u; +const uint32_t kAppId = 0u; +const std::string kPolicyAppId = "Test"; +} + +namespace rc_rpc_plugin_test { + +using namespace rc_rpc_plugin; + +class ButtonPressRequestTest + : public CommandRequestTest<CommandsTestMocks::kIsNice> { + public: + ButtonPressRequestTest() + : rc_capabilities_(smart_objects::SmartType_Map) + , mock_app_(utils::MakeShared<NiceMock<MockApplication> >()) + , rc_app_extention_( + utils::MakeShared<rc_rpc_plugin::RCAppExtension>(kModuleId)) {} + + smart_objects::SmartObject ButtonCapability( + const mobile_apis::ButtonName::eType button_name) { + smart_objects::SmartObject button(smart_objects::SmartType_Map); + button["name"] = button_name; + return button; + } + + void SetUp() OVERRIDE { + using namespace mobile_apis; + + std::vector<ButtonName::eType> button_names = {ButtonName::AC_MAX, + ButtonName::AC, + ButtonName::RECIRCULATE, + ButtonName::FAN_UP, + ButtonName::FAN_DOWN, + ButtonName::TEMP_UP, + ButtonName::TEMP_DOWN, + ButtonName::DEFROST_MAX, + ButtonName::DEFROST, + ButtonName::DEFROST_REAR, + ButtonName::UPPER_VENT, + ButtonName::LOWER_VENT, + ButtonName::VOLUME_UP, + ButtonName::VOLUME_DOWN, + ButtonName::EJECT, + ButtonName::SOURCE, + ButtonName::SHUFFLE, + ButtonName::REPEAT}; + + smart_objects::SmartObject button_caps(smart_objects::SmartType_Array); + for (size_t i = 0; i < button_names.size(); i++) { + button_caps[i] = ButtonCapability(button_names[i]); + } + rc_capabilities_[strings::kbuttonCapabilities] = button_caps; + ON_CALL(app_mngr_, application(_)).WillByDefault(Return(mock_app_)); + ON_CALL(mock_allocation_manager_, GetApplicationExtention(_)) + .WillByDefault(Return(rc_app_extention_)); + ON_CALL(app_mngr_, GetPolicyHandler()) + .WillByDefault(ReturnRef(mock_policy_handler_)); + ON_CALL(app_mngr_, hmi_capabilities()) + .WillByDefault(ReturnRef(mock_hmi_capabilities_)); + ON_CALL(mock_hmi_capabilities_, rc_capability()) + .WillByDefault(Return(&rc_capabilities_)); + ON_CALL(*mock_app_, policy_app_id()).WillByDefault(Return(kPolicyAppId)); + ON_CALL(mock_policy_handler_, + CheckHMIType(kPolicyAppId, + mobile_apis::AppHMIType::eType::REMOTE_CONTROL, + nullptr)).WillByDefault(Return(true)); + } + + MessageSharedPtr CreateBasicMessage() { + MessageSharedPtr message = CreateMessage(); + (*message)[application_manager::strings::params] + [application_manager::strings::function_id] = + mobile_apis::FunctionID::ButtonPressID; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kConnectionKey; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kAppId; + return message; + } + + template <class Command> + application_manager::SharedPtr<Command> CreateRCCommand( + MessageSharedPtr& msg) { + InitCommand(kDefaultTimeout_); + return ::utils::MakeShared<Command>(msg ? msg : msg = CreateMessage(), + app_mngr_, + mock_rpc_service_, + mock_hmi_capabilities_, + mock_policy_handler_, + mock_allocation_manager_); + } + + protected: + smart_objects::SmartObject rc_capabilities_; + utils::SharedPtr<MockApplication> mock_app_; + utils::SharedPtr<rc_rpc_plugin::RCAppExtension> rc_app_extention_; + test::components::policy_test::MockPolicyHandlerInterface + mock_policy_handler_; + testing::NiceMock<rc_rpc_plugin_test::MockResourceAllocationManager> + mock_allocation_manager_; +}; + +TEST_F(ButtonPressRequestTest, + Execute_ButtonNameMatchesModuleType_ExpectCorrectMessageSentToHMI) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + msg_params[message_params::kButtonName] = mobile_apis::ButtonName::AC; + msg_params[message_params::kButtonPressMode] = + mobile_apis::ButtonPressMode::SHORT; + + // Expectations + const std::string resource = "CLIMATE"; + ON_CALL(mock_policy_handler_, CheckModule(_, _)).WillByDefault(Return(true)); + EXPECT_CALL(mock_allocation_manager_, IsResourceFree(resource)) + .WillOnce(Return(true)); + EXPECT_CALL(mock_allocation_manager_, AcquireResource(resource, _)) + .WillOnce(Return(rc_rpc_plugin::AcquireResult::ALLOWED)); + EXPECT_CALL( + mock_allocation_manager_, + SetResourceState(resource, kAppId, rc_rpc_plugin::ResourceState::BUSY)); + EXPECT_CALL(mock_rpc_service_, + ManageHMICommand( + HMIResultCodeIs(hmi_apis::FunctionID::Buttons_ButtonPress))) + .WillOnce(Return(true)); + + // Act + application_manager::SharedPtr<rc_rpc_plugin::commands::ButtonPressRequest> + command = CreateRCCommand<rc_rpc_plugin::commands::ButtonPressRequest>( + mobile_message); + ASSERT_TRUE(command->Init()); + command->Run(); +} + +TEST_F( + ButtonPressRequestTest, + Execute_ButtonNameDoesNotMatchModuleType_ExpectMessageNotSentToHMI_AndFalseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::RADIO; + msg_params[message_params::kButtonName] = mobile_apis::ButtonName::AC; + msg_params[message_params::kButtonPressMode] = + mobile_apis::ButtonPressMode::SHORT; + // Expectations + + const std::string resource = "RADIO"; + ON_CALL(mock_policy_handler_, CheckModule(_, _)).WillByDefault(Return(true)); + EXPECT_CALL(mock_allocation_manager_, IsResourceFree(resource)) + .WillOnce(Return(true)); + EXPECT_CALL(mock_allocation_manager_, AcquireResource(resource, _)) + .WillOnce(Return(rc_rpc_plugin::AcquireResult::ALLOWED)); + EXPECT_CALL(mock_allocation_manager_, SetResourceState(resource, kAppId, _)) + .Times(2); + + EXPECT_CALL(mock_rpc_service_, + ManageHMICommand(HMIResultCodeIs( + hmi_apis::FunctionID::Buttons_ButtonPress))).Times(0); + MessageSharedPtr command_result; + EXPECT_CALL(mock_rpc_service_, + ManageMobileCommand( + MobileResultCodeIs(mobile_apis::Result::INVALID_DATA), _)) + .WillOnce(DoAll(SaveArg<0>(&command_result), Return(true))); + + // Act + application_manager::SharedPtr<rc_rpc_plugin::commands::ButtonPressRequest> + command = CreateRCCommand<rc_rpc_plugin::commands::ButtonPressRequest>( + mobile_message); + ASSERT_TRUE(command->Init()); + command->Run(); +} + +TEST_F(ButtonPressRequestTest, OnEvent_ExpectSuccessfullResponseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + msg_params[message_params::kButtonName] = mobile_apis::ButtonName::AC; + msg_params[message_params::kButtonPressMode] = + mobile_apis::ButtonPressMode::SHORT; + + MessageSharedPtr hmi_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& hmi_msg_params = + (*hmi_message)[application_manager::strings::msg_params]; + hmi_apis::Common_Result::eType response_code = + hmi_apis::Common_Result::SUCCESS; + hmi_msg_params[application_manager::hmi_response::code] = response_code; + hmi_msg_params[application_manager::strings::connection_key] = kConnectionKey; + + // Expectations + EXPECT_CALL(mock_allocation_manager_, + SetResourceState(_, kAppId, rc_rpc_plugin::ResourceState::FREE)) + .Times(2); + + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand(MobileResultCodeIs(mobile_apis::Result::SUCCESS), _)) + .WillOnce(Return(true)); + + // Act + application_manager::event_engine::Event event( + hmi_apis::FunctionID::Buttons_ButtonPress); + event.set_smart_object(*hmi_message); + application_manager::SharedPtr<rc_rpc_plugin::commands::ButtonPressRequest> + command = CreateRCCommand<rc_rpc_plugin::commands::ButtonPressRequest>( + mobile_message); + command->on_event(event); +} + +TEST_F(ButtonPressRequestTest, + OnEvent_ReadOnlyParamFromHMI_ExpectFalseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + msg_params[message_params::kButtonName] = mobile_apis::ButtonName::AC; + msg_params[message_params::kButtonPressMode] = + mobile_apis::ButtonPressMode::SHORT; + + MessageSharedPtr hmi_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& hmi_msg_params = + (*hmi_message)[application_manager::strings::params]; + hmi_apis::Common_Result::eType response_code = + hmi_apis::Common_Result::READ_ONLY; + hmi_msg_params[application_manager::hmi_response::code] = response_code; + hmi_msg_params[application_manager::strings::connection_key] = kConnectionKey; + + // Expectations + EXPECT_CALL(mock_allocation_manager_, + SetResourceState(_, kAppId, rc_rpc_plugin::ResourceState::FREE)) + .Times(2); + + EXPECT_CALL(mock_rpc_service_, + ManageMobileCommand( + MobileResultCodeIs(mobile_apis::Result::GENERIC_ERROR), _)) + .WillOnce(Return(false)); + + // Act + application_manager::event_engine::Event event( + hmi_apis::FunctionID::Buttons_ButtonPress); + event.set_smart_object(*hmi_message); + application_manager::SharedPtr<rc_rpc_plugin::commands::ButtonPressRequest> + command = CreateRCCommand<rc_rpc_plugin::commands::ButtonPressRequest>( + mobile_message); + command->on_event(event); +} + +} // namespace rc_rpc_plugin_test diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/get_interior_vehicle_data_request_test.cc b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/get_interior_vehicle_data_request_test.cc new file mode 100644 index 0000000000..5aecb8664b --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/get_interior_vehicle_data_request_test.cc @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2017, 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 "rc_rpc_plugin/commands/mobile/get_interior_vehicle_data_request.h" +#include "gtest/gtest.h" +#include "application_manager/mock_application.h" +#include "rc_rpc_plugin/rc_app_extension.h" +#include "rc_rpc_plugin/rc_module_constants.h" +#include "application_manager/message_helper.h" +#include "rc_rpc_plugin/rc_command_factory.h" +#include "application_manager/event_engine/event_dispatcher.h" +#include "application_manager/commands/command_request_test.h" +#include "rc_rpc_plugin/mock/mock_resource_allocation_manager.h" +#include "utils/shared_ptr.h" +#include "utils/make_shared.h" + +using ::testing::_; +using ::testing::Mock; +using ::testing::NiceMock; +using ::testing::StrictMock; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::SaveArg; +using ::application_manager::Message; +using ::application_manager::MessageType; +using application_manager::commands::MessageSharedPtr; +using ::application_manager::ApplicationSharedPtr; +using ::protocol_handler::MessagePriority; +using test::components::application_manager_test::MockApplication; +using test::components::application_manager_test::MockApplicationManager; +using test::components::commands_test::MockApplicationManager; +using test::components::commands_test::CommandRequestTest; +using test::components::commands_test::CommandsTestMocks; +using test::components::commands_test::HMIResultCodeIs; +using test::components::commands_test::MobileResultCodeIs; + +namespace { +const int32_t kConnectionKey = 5u; +const uint32_t kAppId = 0u; +const int kModuleId = 153u; +} + +namespace rc_rpc_plugin_test { + +using namespace rc_rpc_plugin; +class GetInteriorVehicleDataRequestTest + : public CommandRequestTest<CommandsTestMocks::kIsNice> { + public: + GetInteriorVehicleDataRequestTest() + : mock_app_(utils::MakeShared<NiceMock<MockApplication> >()) + , rc_app_extention_(utils::MakeShared<RCAppExtension>(kModuleId)) { + ON_CALL(*mock_app_, app_id()).WillByDefault(Return(kAppId)); + ON_CALL(app_mngr_, hmi_interfaces()) + .WillByDefault(ReturnRef(mock_hmi_interfaces_)); + + ON_CALL( + mock_hmi_interfaces_, + GetInterfaceState(application_manager::HmiInterfaces::HMI_INTERFACE_RC)) + .WillByDefault(Return(application_manager::HmiInterfaces:: + InterfaceState::STATE_AVAILABLE)); + ON_CALL(app_mngr_, application(kAppId)).WillByDefault(Return(mock_app_)); + ON_CALL(mock_allocation_manager_, GetApplicationExtention(_)) + .WillByDefault(Return(rc_app_extention_)); + ON_CALL(app_mngr_, GetPolicyHandler()) + .WillByDefault(ReturnRef(mock_policy_handler_)); + ON_CALL(app_mngr_, hmi_capabilities()) + .WillByDefault(ReturnRef(mock_hmi_capabilities_)); + ON_CALL(mock_hmi_capabilities_, rc_capability()) + .WillByDefault(Return(&rc_capabilities_)); + ON_CALL(mock_policy_handler_, + CheckHMIType( + _, mobile_apis::AppHMIType::eType::REMOTE_CONTROL, nullptr)) + .WillByDefault(Return(true)); + } + + MessageSharedPtr CreateBasicMessage() { + MessageSharedPtr message = CreateMessage(); + (*message)[application_manager::strings::params] + [application_manager::strings::function_id] = + mobile_apis::FunctionID::GetInteriorVehicleDataID; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kConnectionKey; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kAppId; + return message; + } + template <class Command> + application_manager::SharedPtr<Command> CreateRCCommand( + MessageSharedPtr& msg) { + InitCommand(kDefaultTimeout_); + return ::utils::MakeShared<Command>(msg ? msg : msg = CreateMessage(), + app_mngr_, + mock_rpc_service_, + mock_hmi_capabilities_, + mock_policy_handler_, + mock_allocation_manager_); + } + + protected: + smart_objects::SmartObject rc_capabilities_; + utils::SharedPtr<MockApplication> mock_app_; + utils::SharedPtr<RCAppExtension> rc_app_extention_; + testing::NiceMock<rc_rpc_plugin_test::MockResourceAllocationManager> + mock_allocation_manager_; +}; +TEST_F(GetInteriorVehicleDataRequestTest, + Execute_MessageValidationOk_ExpectCorrectMessageSentToHMI) { + // Arrange + ON_CALL(mock_policy_handler_, CheckModule(_, _)).WillByDefault(Return(true)); + EXPECT_CALL(mock_hmi_capabilities_, rc_capability()) + .WillOnce(Return(nullptr)); + EXPECT_CALL(mock_rpc_service_, + ManageHMICommand(HMIResultCodeIs( + hmi_apis::FunctionID::RC_GetInteriorVehicleData))) + .WillOnce(Return(true)); + MessageSharedPtr mobile_message = CreateBasicMessage(); + application_manager::SharedPtr< + rc_rpc_plugin::commands::GetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::GetInteriorVehicleDataRequest>( + mobile_message); + command->Run(); +} + +TEST_F( + GetInteriorVehicleDataRequestTest, + Execute_MessageValidationFailed_ExpectMessageNotSentToHMI_AndFalseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::RADIO; + ON_CALL(mock_policy_handler_, CheckModule(_, _)).WillByDefault(Return(true)); + MessageSharedPtr command_result; + EXPECT_CALL(mock_rpc_service_, + ManageHMICommand(HMIResultCodeIs( + hmi_apis::FunctionID::RC_GetInteriorVehicleData))).Times(0); + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand( + MobileResultCodeIs(mobile_apis::Result::UNSUPPORTED_RESOURCE), _)) + .WillOnce(DoAll(SaveArg<0>(&command_result), Return(true))); + application_manager::SharedPtr< + rc_rpc_plugin::commands::GetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::GetInteriorVehicleDataRequest>( + mobile_message); + command->Run(); +} + +TEST_F(GetInteriorVehicleDataRequestTest, + OnEvent_ValidHmiResponse_ExpectSuccessfullResponseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + + MessageSharedPtr hmi_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& hmi_msg_params = + (*hmi_message)[application_manager::strings::msg_params]; + hmi_apis::Common_Result::eType response_code = + hmi_apis::Common_Result::SUCCESS; + hmi_msg_params[application_manager::hmi_response::code] = response_code; + hmi_msg_params[application_manager::strings::connection_key] = kConnectionKey; + + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand(MobileResultCodeIs(mobile_apis::Result::SUCCESS), _)) + .WillOnce(Return(true)); + + application_manager::event_engine::Event event( + hmi_apis::FunctionID::RC_GetInteriorVehicleData); + event.set_smart_object(*hmi_message); + application_manager::SharedPtr< + rc_rpc_plugin::commands::GetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::GetInteriorVehicleDataRequest>( + mobile_message); + command->on_event(event); +} + +TEST_F(GetInteriorVehicleDataRequestTest, + OnEvent_InvalidHmiResponse_ExpectGenericErrorResponseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + + MessageSharedPtr hmi_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& hmi_msg_params = + (*hmi_message)[application_manager::strings::params]; + hmi_apis::Common_Result::eType response_code = + hmi_apis::Common_Result::READ_ONLY; + hmi_msg_params[application_manager::hmi_response::code] = response_code; + hmi_msg_params[application_manager::strings::connection_key] = kConnectionKey; + EXPECT_CALL(mock_rpc_service_, + ManageMobileCommand( + MobileResultCodeIs(mobile_apis::Result::GENERIC_ERROR), _)) + .WillOnce(Return(false)); + + application_manager::event_engine::Event event( + hmi_apis::FunctionID::RC_GetInteriorVehicleData); + event.set_smart_object(*hmi_message); + application_manager::SharedPtr< + rc_rpc_plugin::commands::GetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::GetInteriorVehicleDataRequest>( + mobile_message); + command->on_event(event); +} +} // namespace rc_rpc_plugin_test diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_interior_vehicle_data_notification_test.cc b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_interior_vehicle_data_notification_test.cc new file mode 100644 index 0000000000..bbf0680052 --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_interior_vehicle_data_notification_test.cc @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2018, 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 "application_manager/application.h" +#include "application_manager/mock_application.h" +#include "application_manager/commands/commands_test.h" +#include "rc_rpc_plugin/commands/mobile/on_interior_vehicle_data_notification.h" +#include "rc_rpc_plugin/rc_rpc_plugin.h" +#include "rc_rpc_plugin/rc_module_constants.h" +#include "rc_rpc_plugin/mock/mock_resource_allocation_manager.h" +#include "gtest/gtest.h" +#include "interfaces/MOBILE_API.h" + +using ::testing::_; +using ::testing::Return; +using ::testing::NiceMock; +using ::testing::SaveArg; +using ::testing::Mock; +using application_manager::ApplicationSet; +using application_manager::commands::MessageSharedPtr; +using test::components::application_manager_test::MockApplication; +using test::components::application_manager_test::MockApplicationManager; +using test::components::commands_test::CommandsTest; +using test::components::commands_test::CommandsTestMocks; + +namespace { +const uint32_t kAppId = 0u; +const uint32_t kConnectionKey = 1u; +const std::string kPolicyAppId = "Test"; +} + +namespace rc_rpc_plugin_test { +using namespace rc_rpc_plugin; +using application_manager::ApplicationSet; + +class OnInteriorVehicleDataNotificationTest + : public CommandsTest<CommandsTestMocks::kIsNice> { + public: + OnInteriorVehicleDataNotificationTest() + : mock_app_(utils::MakeShared<NiceMock<MockApplication> >()) {} + + MessageSharedPtr CreateBasicMessage() { + MessageSharedPtr message = CreateMessage(); + (*message)[application_manager::strings::params] + [application_manager::strings::function_id] = + mobile_apis::FunctionID::SetInteriorVehicleDataID; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kConnectionKey; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kAppId; + smart_objects::SmartObject& msg_param = + (*message)[application_manager::strings::msg_params]; + msg_param[message_params::kModuleType] = mobile_apis::ModuleType::CLIMATE; + + return message; + } + + template <class Command> + application_manager::SharedPtr<Command> CreateRCCommand( + MessageSharedPtr& msg) { + InitCommand(kDefaultTimeout_); + return ::utils::MakeShared<Command>(msg ? msg : msg = CreateMessage(), + app_mngr_, + mock_rpc_service_, + mock_hmi_capabilities_, + mock_policy_handler_, + mock_allocation_manager_); + } + + protected: + utils::SharedPtr<MockApplication> mock_app_; + testing::NiceMock<rc_rpc_plugin_test::MockResourceAllocationManager> + mock_allocation_manager_; +}; + +TEST_F(OnInteriorVehicleDataNotificationTest, + Run_SendMessageToMobile_Notification) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + ApplicationSet app_set = {mock_app_}; + MessageSharedPtr message; + const sync_primitives::Lock apps_lock; + DataAccessor<ApplicationSet> accessor(app_set, apps_lock); + // Expectations + EXPECT_CALL(app_mngr_, applications()).WillOnce(Return(accessor)); + + RCAppExtensionPtr rc_extention_ptr = + utils::MakeShared<RCAppExtension>(application_manager::AppExtensionUID( + rc_rpc_plugin::RCRPCPlugin::kRCPluginID)); + rc_extention_ptr->SubscribeToInteriorVehicleData(enums_value::kClimate); + ON_CALL(*mock_app_, QueryInterface(_)) + .WillByDefault(Return(rc_extention_ptr)); + + EXPECT_CALL(mock_rpc_service_, SendMessageToMobile(_, false)) + .WillOnce(SaveArg<0>(&message)); + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::OnInteriorVehicleDataNotification> command = + CreateRCCommand< + rc_rpc_plugin::commands::OnInteriorVehicleDataNotification>( + mobile_message); + command->Run(); + // Assertions + ASSERT_TRUE(message); + Mock::VerifyAndClearExpectations(&app_mngr_); +} + +} // namespace rc_rpc_plugin_test diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_remote_control_settings_test.cc b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_remote_control_settings_test.cc new file mode 100644 index 0000000000..0c61a7d118 --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/on_remote_control_settings_test.cc @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2018, 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 "application_manager/application.h" +#include "application_manager/commands/command_request_test.h" +#include "application_manager/mock_application.h" +#include "application_manager/commands/commands_test.h" +#include "rc_rpc_plugin/commands/hmi/rc_on_remote_control_settings_notification.h" +#include "rc_rpc_plugin/rc_rpc_plugin.h" +#include "rc_rpc_plugin/rc_module_constants.h" +#include "rc_rpc_plugin/mock/mock_resource_allocation_manager.h" +#include "gtest/gtest.h" +#include "interfaces/MOBILE_API.h" + +using ::testing::_; +using ::testing::Return; +using ::testing::NiceMock; +using ::testing::SaveArg; +using ::testing::Mock; +using application_manager::ApplicationSet; +using application_manager::commands::MessageSharedPtr; +using test::components::application_manager_test::MockApplication; +using test::components::application_manager_test::MockApplicationManager; +using test::components::commands_test::CommandsTest; +using test::components::commands_test::CommandsTestMocks; + +namespace { +const uint32_t kAppId = 0u; +const uint32_t kConnectionKey = 1u; +const std::string kPolicyAppId = "Test"; +} + +namespace rc_rpc_plugin_test { +using namespace rc_rpc_plugin; +using application_manager::ApplicationSet; + +class RCOnRemoteControlSettingsNotificationTest + : public CommandsTest<CommandsTestMocks::kIsNice> { + public: + RCOnRemoteControlSettingsNotificationTest() + : mock_app_(utils::MakeShared<NiceMock<MockApplication> >()) {} + MessageSharedPtr CreateBasicMessage() { + MessageSharedPtr message = CreateMessage(); + (*message)[application_manager::strings::params] + [application_manager::strings::function_id] = + mobile_apis::FunctionID::SetInteriorVehicleDataID; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kConnectionKey; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kAppId; + smart_objects::SmartObject& msg_param = + (*message)[application_manager::strings::msg_params]; + msg_param[message_params::kModuleType] = enums_value::kClimate; + + return message; + } + + template <class Command> + application_manager::SharedPtr<Command> CreateRCCommand( + MessageSharedPtr& msg) { + InitCommand(kDefaultTimeout_); + return ::utils::MakeShared<Command>(msg ? msg : msg = CreateMessage(), + app_mngr_, + mock_rpc_service_, + mock_hmi_capabilities_, + mock_policy_handler_, + mock_allocation_manager_); + } + + protected: + utils::SharedPtr<MockApplication> mock_app_; + testing::NiceMock<rc_rpc_plugin_test::MockResourceAllocationManager> + mock_allocation_manager_; +}; + +TEST_F(RCOnRemoteControlSettingsNotificationTest, + Run_Allowed_SetAccessMode) { // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + (*mobile_message)[application_manager::strings::msg_params] + [message_params::kAllowed] = true; + + // Expectations + + ON_CALL(mock_allocation_manager_, GetAccessMode()) + .WillByDefault(Return(hmi_apis::Common_RCAccessMode::ASK_DRIVER)); + + EXPECT_CALL(mock_allocation_manager_, + SetAccessMode(hmi_apis::Common_RCAccessMode::ASK_DRIVER)); + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::RCOnRemoteControlSettingsNotification> command = + CreateRCCommand< + rc_rpc_plugin::commands::RCOnRemoteControlSettingsNotification>( + mobile_message); + + command->Run(); +} + +TEST_F(RCOnRemoteControlSettingsNotificationTest, + Run_NotAllowed_ResetAllAllocations) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + (*mobile_message)[application_manager::strings::msg_params] + [message_params::kAllowed] = false; + + ApplicationSet app_set = {mock_app_}; + + const sync_primitives::Lock apps_lock; + DataAccessor<ApplicationSet> accessor(app_set, apps_lock); + // Expectations + EXPECT_CALL(app_mngr_, applications()).WillOnce(Return(accessor)); + + RCAppExtensionPtr rc_extention_ptr = + utils::MakeShared<RCAppExtension>(application_manager::AppExtensionUID( + rc_rpc_plugin::RCRPCPlugin::kRCPluginID)); + rc_extention_ptr->SubscribeToInteriorVehicleData(enums_value::kClimate); + ON_CALL(*mock_app_, QueryInterface(_)) + .WillByDefault(Return(rc_extention_ptr)); + + EXPECT_CALL(mock_allocation_manager_, ResetAllAllocations()); + + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::RCOnRemoteControlSettingsNotification> command = + CreateRCCommand< + rc_rpc_plugin::commands::RCOnRemoteControlSettingsNotification>( + mobile_message); + + command->Run(); + Mock::VerifyAndClearExpectations(&app_mngr_); +} + +} // namespace rc_rpc_plugin_test diff --git a/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/set_interior_vehicle_data_request_test.cc b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/set_interior_vehicle_data_request_test.cc new file mode 100644 index 0000000000..68985e5f94 --- /dev/null +++ b/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/commands/set_interior_vehicle_data_request_test.cc @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2018, 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 "application_manager/application.h" +#include "application_manager/commands/command_request_test.h" +#include "application_manager/mock_application.h" +#include "rc_rpc_plugin/commands/mobile/set_interior_vehicle_data_request.h" +#include "rc_rpc_plugin/rc_rpc_plugin.h" +#include "rc_rpc_plugin/rc_module_constants.h" +#include "rc_rpc_plugin/mock/mock_resource_allocation_manager.h" +#include "gtest/gtest.h" +#include "interfaces/MOBILE_API.h" + +using ::testing::_; +using ::testing::Return; +using ::testing::NiceMock; +using application_manager::ApplicationSet; +using application_manager::commands::MessageSharedPtr; +using test::components::application_manager_test::MockApplication; +using test::components::application_manager_test::MockApplicationManager; +using test::components::commands_test::CommandRequestTest; +using test::components::commands_test::CommandsTestMocks; + +namespace { +const uint32_t kAppId = 0u; +const int kModuleId = 153u; +const uint32_t kConnectionKey = 1u; +const std::string kPolicyAppId = "Test"; +} + +namespace rc_rpc_plugin_test { +using namespace rc_rpc_plugin; + +class SetInteriorVehicleDataRequestTest + : public CommandRequestTest<CommandsTestMocks::kIsNice> { + public: + SetInteriorVehicleDataRequestTest() + : mock_app_(utils::MakeShared<NiceMock<MockApplication> >()) + , rc_app_extention_(utils::MakeShared<RCAppExtension>(kModuleId)) {} + + void SetUp() OVERRIDE { + ON_CALL(app_mngr_, hmi_interfaces()) + .WillByDefault(ReturnRef(mock_hmi_interfaces_)); + ON_CALL( + mock_hmi_interfaces_, + GetInterfaceState(application_manager::HmiInterfaces::HMI_INTERFACE_RC)) + .WillByDefault(Return(application_manager::HmiInterfaces:: + InterfaceState::STATE_AVAILABLE)); + ON_CALL(app_mngr_, application(kAppId)).WillByDefault(Return(mock_app_)); + ON_CALL(mock_allocation_manager_, GetApplicationExtention(_)) + .WillByDefault(Return(rc_app_extention_)); + + ON_CALL(*mock_app_, policy_app_id()).WillByDefault(Return(kPolicyAppId)); + ON_CALL(mock_allocation_manager_, IsResourceFree(_)) + .WillByDefault(Return(true)); + ON_CALL(mock_allocation_manager_, AcquireResource(_, _)) + .WillByDefault(Return(AcquireResult::ALLOWED)); + ON_CALL(*mock_app_, app_id()).WillByDefault(Return(kAppId)); + ON_CALL(mock_policy_handler_, + CheckHMIType(kPolicyAppId, + mobile_apis::AppHMIType::eType::REMOTE_CONTROL, + nullptr)).WillByDefault(Return(true)); + } + + MessageSharedPtr CreateBasicMessage() { + MessageSharedPtr message = CreateMessage(); + (*message)[application_manager::strings::params] + [application_manager::strings::function_id] = + mobile_apis::FunctionID::SetInteriorVehicleDataID; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kConnectionKey; + (*message)[application_manager::strings::params] + [application_manager::strings::connection_key] = kAppId; + return message; + } + + template <class Command> + application_manager::SharedPtr<Command> CreateRCCommand( + MessageSharedPtr& msg) { + InitCommand(kDefaultTimeout_); + return ::utils::MakeShared<Command>(msg ? msg : msg = CreateMessage(), + app_mngr_, + mock_rpc_service_, + mock_hmi_capabilities_, + mock_policy_handler_, + mock_allocation_manager_); + } + + protected: + testing::NiceMock<rc_rpc_plugin_test::MockResourceAllocationManager> + mock_allocation_manager_; + utils::SharedPtr<MockApplication> mock_app_; + utils::SharedPtr<RCAppExtension> rc_app_extention_; +}; + +TEST_F(SetInteriorVehicleDataRequestTest, + Execute_ValidWithoutReadOnlyParams_ExpectResendToHMI) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleData][message_params::kModuleType] = + mobile_apis::ModuleType::CLIMATE; + msg_params[message_params::kModuleData][message_params::kClimateControlData] = + smart_objects::SmartObject(smart_objects::SmartType_Map); + // Expectations + EXPECT_CALL(mock_policy_handler_, CheckModule(kPolicyAppId, _)) + .WillOnce(Return(rc_rpc_plugin::TypeAccess::kAllowed)); + + EXPECT_CALL(mock_hmi_capabilities_, rc_capability()) + .WillOnce(Return(nullptr)); + + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand(test::components::commands_test::MobileResultCodeIs( + mobile_apis::Result::READ_ONLY), + application_manager::commands::Command::SOURCE_SDL)); + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::SetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::SetInteriorVehicleDataRequest>( + mobile_message); + command->Run(); +} + +TEST_F( + SetInteriorVehicleDataRequestTest, + Execute_ValidWithSettableAndReadOnlyParams_ExpectCutReadOnlyAndResendToHMI) { + // Arrange + + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleData][message_params::kModuleType] = + mobile_apis::ModuleType::RADIO; + smart_objects::SmartObject radio_control_data(smart_objects::SmartType_Map); + radio_control_data[message_params::kState] = true; + radio_control_data[message_params::kRadioEnable] = true; + msg_params[message_params::kModuleData][message_params::kRadioControlData] = + radio_control_data; + + // Expectations + EXPECT_CALL(mock_policy_handler_, CheckModule(kPolicyAppId, _)) + .WillOnce(Return(rc_rpc_plugin::TypeAccess::kAllowed)); + + EXPECT_CALL(mock_hmi_capabilities_, rc_capability()) + .WillOnce(Return(nullptr)); + + EXPECT_CALL(app_mngr_, RemoveHMIFakeParameters(_)); + + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand(test::components::commands_test::MobileResultCodeIs( + mobile_apis::Result::OUT_OF_MEMORY), + application_manager::commands::Command::SOURCE_SDL)); + + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::SetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::SetInteriorVehicleDataRequest>( + mobile_message); + command->Run(); +} + +TEST_F( + SetInteriorVehicleDataRequestTest, + Execute_MessageValidationFailed_ExpectMessageNotSentToHMI_AndFalseSentToMobile) { + // Arrange + MessageSharedPtr mobile_message = CreateBasicMessage(); + NsSmartDeviceLink::NsSmartObjects::SmartObject& msg_params = + (*mobile_message)[application_manager::strings::msg_params]; + msg_params[message_params::kModuleData][message_params::kModuleType] = + mobile_apis::ModuleType::RADIO; + + msg_params[message_params::kModuleData][message_params::kClimateControlData] = + smart_objects::SmartObject(smart_objects::SmartType_Map); + + // Expectations + EXPECT_CALL(mock_policy_handler_, CheckModule(kPolicyAppId, _)) + .WillOnce(Return(rc_rpc_plugin::TypeAccess::kAllowed)); + + EXPECT_CALL( + mock_rpc_service_, + ManageMobileCommand(test::components::commands_test::MobileResultCodeIs( + mobile_apis::Result::INVALID_DATA), + application_manager::commands::Command::SOURCE_SDL)); + + // Act + application_manager::SharedPtr< + rc_rpc_plugin::commands::SetInteriorVehicleDataRequest> command = + CreateRCCommand<rc_rpc_plugin::commands::SetInteriorVehicleDataRequest>( + mobile_message); + command->Run(); +} + +} // namespace rc_rpc_plugin_test diff --git a/src/components/application_manager/src/application_manager_impl.cc b/src/components/application_manager/src/application_manager_impl.cc index 108d9acf8a..7b73a79397 100644 --- a/src/components/application_manager/src/application_manager_impl.cc +++ b/src/components/application_manager/src/application_manager_impl.cc @@ -191,12 +191,12 @@ ApplicationManagerImpl::ApplicationManagerImpl( clearing_timer->Start(timeout_ms, timer::kSingleShot); timer_pool_.push_back(clearing_timer); rpc_handler_.reset(new rpc_handler::RPCHandlerImpl(*this)); + commands_holder_.reset(new CommandHolderImpl(*this)); rpc_service_.reset(new rpc_service::RPCServiceImpl(*this, request_ctrl_, protocol_handler_, hmi_handler_, *commands_holder_)); - commands_holder_.reset(new CommandHolderImpl(*this)); } ApplicationManagerImpl::~ApplicationManagerImpl() { diff --git a/src/components/application_manager/test/application_manager_impl_mock_hmi_test.cc b/src/components/application_manager/test/application_manager_impl_mock_hmi_test.cc index 34a07dc450..9ae388c809 100644 --- a/src/components/application_manager/test/application_manager_impl_mock_hmi_test.cc +++ b/src/components/application_manager/test/application_manager_impl_mock_hmi_test.cc @@ -50,6 +50,9 @@ #include "policy/usage_statistics/mock_statistics_manager.h" #include "protocol_handler/mock_session_observer.h" #include "protocol_handler/mock_protocol_handler.h" +#include "application_manager/mock_rpc_plugin_manager.h" +#include "application_manager/mock_rpc_plugin.h" +#include "utils/optional.h" namespace test { namespace components { @@ -162,6 +165,8 @@ TEST_F(ApplicationManagerImplMockHmiTest, const std::string mac_address = "MA:CA:DD:RE:SS"; const connection_handler::DeviceHandle device_id = 1u; const custom_str::CustomString app_name(""); + plugin_manager::MockRPCPluginManager* mock_rpc_plugin_manager = + new plugin_manager::MockRPCPluginManager; utils::SharedPtr<ApplicationImpl> app_impl = new ApplicationImpl( application_id, @@ -172,6 +177,10 @@ TEST_F(ApplicationManagerImplMockHmiTest, utils::SharedPtr<usage_statistics::StatisticsManager>( new usage_statistics_test::MockStatisticsManager()), *app_manager_impl_); + std::unique_ptr<plugin_manager::RPCPluginManager> mock_rpc_plugin_manager_ptr( + mock_rpc_plugin_manager); + + app_manager_impl_->SetPluginManager(mock_rpc_plugin_manager_ptr); app_manager_impl_->AddMockApplication(app_impl); @@ -183,10 +192,20 @@ TEST_F(ApplicationManagerImplMockHmiTest, MockCommandFactory mock_command_factory; - // Skip sending notification on device switching as it is not the goal here + // // Skip sending notification on device switching as it is not the goal + // here EXPECT_CALL(mock_command_factory, CreateCommand(_, _)) .WillOnce(Return(utils::SharedPtr<commands::Command>())); + plugin_manager::MockRPCPlugin mock_rpc_plugin; + utils::Optional<plugin_manager::RPCPlugin> mock_rpc_plugin_opt = + mock_rpc_plugin; + + EXPECT_CALL(*mock_rpc_plugin_manager, FindPluginToProcess(_, _)) + .WillRepeatedly(Return(mock_rpc_plugin_opt)); + ON_CALL(mock_rpc_plugin, GetCommandFactory()) + .WillByDefault(ReturnRef(mock_command_factory)); + app_manager_impl_->OnDeviceSwitchingStart(bt, usb); const uint32_t connection_key = 1u; diff --git a/src/components/include/application_manager/application_manager.h b/src/components/include/application_manager/application_manager.h index 543b4aa2b6..fec8e13ef3 100644 --- a/src/components/include/application_manager/application_manager.h +++ b/src/components/include/application_manager/application_manager.h @@ -186,8 +186,13 @@ class ApplicationManager { virtual std::vector<std::string> devices( const std::string& policy_app_id) const = 0; - virtual plugin_manager::RPCPluginManager& GetPluginManager() = 0; #endif // SDL_REMOTE_CONTROL + virtual plugin_manager::RPCPluginManager& GetPluginManager() = 0; + +#ifdef BUILD_TESTS + virtual void SetPluginManager( + std::unique_ptr<plugin_manager::RPCPluginManager>& plugin_manager) = 0; +#endif virtual std::vector<ApplicationSharedPtr> applications_with_mobile_projection() = 0; diff --git a/src/components/include/test/application_manager/mock_application_manager.h b/src/components/include/test/application_manager/mock_application_manager.h index da227c7b60..22d0b2288b 100644 --- a/src/components/include/test/application_manager/mock_application_manager.h +++ b/src/components/include/test/application_manager/mock_application_manager.h @@ -35,6 +35,7 @@ #include <string> #include <vector> +#include <memory> #include "gmock/gmock.h" @@ -57,6 +58,7 @@ namespace test { namespace components { namespace application_manager_test { +using application_manager::plugin_manager::RPCPluginManager; class MockApplicationManager : public application_manager::ApplicationManager { public: @@ -84,11 +86,13 @@ class MockApplicationManager : public application_manager::ApplicationManager { const std::string& policy_app_id)); MOCK_METHOD2(ChangeAppsHMILevel, void(uint32_t app_id, mobile_apis::HMILevel::eType level)); - MOCK_METHOD0(GetPluginManager, - application_manager::plugin_manager::RPCPluginManager&()); MOCK_CONST_METHOD1( devices, std::vector<std::string>(const std::string& policy_app_id)); #endif // SDL_REMOTE_CONTROL + MOCK_METHOD0(GetPluginManager, RPCPluginManager&()); + + MOCK_METHOD1(SetPluginManager, + void(std::unique_ptr<RPCPluginManager>& plugin_manager)); MOCK_CONST_METHOD1( application_by_hmi_app, diff --git a/src/components/include/test/application_manager/mock_rpc_plugin.h b/src/components/include/test/application_manager/mock_rpc_plugin.h new file mode 100644 index 0000000000..4059132b50 --- /dev/null +++ b/src/components/include/test/application_manager/mock_rpc_plugin.h @@ -0,0 +1,31 @@ +#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_PLUGIN_MANAGER_MOCK_RPC_PLUGIN_H +#define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_PLUGIN_MANAGER_MOCK_RPC_PLUGIN_H + +#include "gmock/gmock.h" +#include "application_manager/plugin_manager/rpc_plugin.h" + +namespace application_manager { +class CommandFactory; +namespace plugin_manager { + +namespace plugin_manager {} +class MockRPCPlugin : public RPCPlugin { + public: + MOCK_METHOD4(Init, + bool(ApplicationManager& app_manager, + rpc_service::RPCService& rpc_service, + HMICapabilities& hmi_capabilities, + policy::PolicyHandlerInterface& policy_handler)); + MOCK_METHOD2(IsAbleToProcess, + bool(const int32_t function_id, + const commands::Command::CommandSource message_source)); + MOCK_METHOD0(PluginName, std::string()); + MOCK_METHOD0(GetCommandFactory, CommandFactory&()); + MOCK_METHOD1(OnPolicyEvent, void(PolicyEvent event)); + MOCK_METHOD2(OnApplicationEvent, + void(ApplicationEvent event, + application_manager::ApplicationSharedPtr application)); +}; +} // namespace plugin_manager +} // namespace application_manager +#endif // SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_PLUGIN_MANAGER_MOCK_RPC_PLUGIN_H diff --git a/src/components/include/test/application_manager/mock_rpc_plugin_manager.h b/src/components/include/test/application_manager/mock_rpc_plugin_manager.h new file mode 100644 index 0000000000..74763f00d6 --- /dev/null +++ b/src/components/include/test/application_manager/mock_rpc_plugin_manager.h @@ -0,0 +1,24 @@ +#ifndef SRC_COMPONENTS_INCLUDE_TEST_APPLICATION_MANAGER_MOCK_RPC_PLUGIN_MANAGER_H_ +#define SRC_COMPONENTS_INCLUDE_TEST_APPLICATION_MANAGER_MOCK_RPC_PLUGIN_MANAGER_H_ +#include <string> + +#include "gmock/gmock.h" +#include "application_manager/plugin_manager/rpc_plugin_manager.h" + +namespace application_manager { +namespace plugin_manager { + +class MockRPCPluginManager : public RPCPluginManager { + public: + MOCK_METHOD1(LoadPlugins, uint32_t(const std::string& plugins_path)); + MOCK_METHOD0(GetPlugins, std::vector<RPCPluginPtr>&()); + MOCK_METHOD2(FindPluginToProcess, + utils::Optional<RPCPlugin>( + const int32_t function_id, + const commands::Command::CommandSource message_source)); + MOCK_METHOD1(ForEachPlugin, void(std::function<void(RPCPlugin&)> functor)); +}; + +} // namespace plugin_manager +} // namespace application_manager +#endif // SRC_COMPONENTS_INCLUDE_TEST_APPLICATION_MANAGER_MOCK_RPC_PLUGIN_MANAGER_H_ |