diff options
author | Andrey Oleynik (GitHub) <aoleynik@luxoft.com> | 2017-10-03 12:16:57 +0300 |
---|---|---|
committer | Andriy Byzhynar <AByzhynar@luxoft.com> | 2018-01-18 12:03:50 +0200 |
commit | e10f6296b5050f7b56076bfecdb15be0b955cc3c (patch) | |
tree | e2e98d1147438f437cc1243aef5a8f4dfae69c71 /src | |
parent | afbf4df6653fcb0d722b01da253acddaabba0d90 (diff) | |
download | sdl_core-e10f6296b5050f7b56076bfecdb15be0b955cc3c.tar.gz |
Test coverage for postponing commands during switching
Diffstat (limited to 'src')
7 files changed, 569 insertions, 45 deletions
diff --git a/src/components/application_manager/test/CMakeLists.txt b/src/components/application_manager/test/CMakeLists.txt index 100b4c0210..3e9010e22c 100644 --- a/src/components/application_manager/test/CMakeLists.txt +++ b/src/components/application_manager/test/CMakeLists.txt @@ -31,12 +31,6 @@ include(${CMAKE_SOURCE_DIR}/tools/cmake/helpers/platform.cmake) include(${CMAKE_SOURCE_DIR}/tools/cmake/helpers/sources.cmake) -# TODO{ALeshin}: APPLINK-10792. Do not write tests which use -# application manager(AM) singleton while refactoring of AM is finished. - -# TODO{ILytvynenko}: SDLOPEN-797 Uncomment application_manager_impl_test and -# cover with UT missed files. - include_directories( ${GMOCK_INCLUDE_DIRECTORY} ${CMAKE_BINARY_DIR}/src/components/ @@ -68,10 +62,15 @@ set(testSources ${AM_TEST_DIR}/application_state_test.cc ${AM_TEST_DIR}/usage_statistics_test.cc ${AM_TEST_DIR}/policy_handler_test.cc - ${AM_TEST_DIR}/mock_message_helper.cc ${AM_TEST_DIR}/application_manager_impl_test.cc ${AM_TEST_DIR}/application_helper_test.cc + ${AM_TEST_DIR}/command_holder_test.cc +) +set(testSourcesMockHmi + ${AM_SOURCE_DIR}/src/message_helper/message_helper.cc + ${AM_TEST_DIR}/application_manager_impl_mock_hmi_test.cc + ${AM_TEST_DIR}/mock_hmi_command_factory.cc ) if(REMOTE_CONTROL) @@ -131,10 +130,11 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}" ) create_test("application_manager_test" "${testSources}" "${LIBRARIES}") +create_test("application_manager_mock_hmi_test" "${testSourcesMockHmi}" "${LIBRARIES}") add_dependencies("application_manager_test" libbson) + create_test("request_controller_test" "${RequestController_SOURCES}" "${LIBRARIES}") -# TODO [AKozoriz] : Fix not buildable tests set(ResumptionData_SOURCES ${AM_TEST_DIR}/resumption/resumption_data_test.cc ${AM_TEST_DIR}/resumption/resumption_data_db_test.cc 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 new file mode 100644 index 0000000000..7ca2ff9d98 --- /dev/null +++ b/src/components/application_manager/test/application_manager_impl_mock_hmi_test.cc @@ -0,0 +1,239 @@ +/* + * 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 <stdint.h> +#include <memory> +#include <string> + +#include "gtest/gtest.h" +#include "application_manager/application.h" +#include "application_manager/application_impl.h" +#include "application_manager/application_manager_impl.h" +#include "utils/custom_string.h" +#include "utils/make_shared.h" + +#include "application_manager/mock_application_manager_settings.h" +#include "application_manager/mock_resumption_data.h" +#include "application_manager/mock_hmi_command_factory.h" +#include "application_manager/mock_request.h" +#include "connection_handler/mock_connection_handler.h" +#include "policy/mock_policy_settings.h" +#include "policy/usage_statistics/mock_statistics_manager.h" +#include "protocol_handler/mock_session_observer.h" + +namespace test { +namespace components { +namespace application_manager_test { + +namespace am = application_manager; + +using testing::_; +using ::testing::DoAll; +using ::testing::Mock; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::NiceMock; +using ::testing::SetArgPointee; + +using namespace application_manager; + +namespace { +const uint32_t kApplicationId = 0u; +const std::string kDirectoryName = "./test_storage"; +const uint32_t kTimeout = 10000u; +} // namespace + +/** + * @brief The ApplicationManagerImplMockHmiTest class uses mocked HMI factory + * which calls its static method and real (non-mocked) MessageHelper. + * Class is separated from ApplicationManagerImplTest since it requires real + * HMI factory and mocked MessageHelper. So it is not possible to combine + * this code together. + */ +class ApplicationManagerImplMockHmiTest : public ::testing::Test { + public: + ApplicationManagerImplMockHmiTest() + : mock_storage_( + ::utils::MakeShared<NiceMock<resumption_test::MockResumptionData> >( + mock_app_mngr_)) {} + + protected: + void SetUp() OVERRIDE { + const uint8_t expected_tread_pool_size = 2u; + const uint8_t stop_streaming_timeout = 1u; + + ON_CALL(mock_application_manager_settings_, thread_pool_size()) + .WillByDefault(Return(expected_tread_pool_size)); + ON_CALL(mock_application_manager_settings_, app_icons_folder()) + .WillByDefault(ReturnRef(kDirectoryName)); + ON_CALL(mock_application_manager_settings_, app_storage_folder()) + .WillByDefault(ReturnRef(kDirectoryName)); + ON_CALL(mock_application_manager_settings_, launch_hmi()) + .WillByDefault(Return(true)); + ON_CALL(mock_application_manager_settings_, stop_streaming_timeout()) + .WillByDefault(Return(stop_streaming_timeout)); + ON_CALL(mock_application_manager_settings_, default_timeout()) + .WillByDefault(ReturnRef(kTimeout)); + + ON_CALL(mock_connection_handler_, get_session_observer()) + .WillByDefault(ReturnRef(mock_session_observer_)); + + app_manager_impl_.reset(new am::ApplicationManagerImpl( + mock_application_manager_settings_, mock_policy_settings_)); + + app_manager_impl_->set_connection_handler(&mock_connection_handler_); + app_manager_impl_->resume_controller().set_resumption_storage( + mock_storage_); + } + + void SetCommonExpectationOnAppReconnection( + const connection_handler::DeviceHandle new_device_id, + const uint32_t new_application_id, + const std::string& mac_address) { + EXPECT_CALL( + mock_session_observer_, + GetDataOnSessionKey(new_application_id, + _, + _, + testing::An<connection_handler::DeviceHandle*>())) + .WillOnce(DoAll(SetArgPointee<3u>(new_device_id), Return(0))); + + const std::string connection_type = "MyConnectionType"; + EXPECT_CALL( + mock_session_observer_, + GetDataOnDeviceID( + ::testing::Matcher<connection_handler::DeviceHandle>(new_device_id), + _, + _, + _, + _)) + .WillOnce(DoAll(SetArgPointee<3u>(mac_address), + SetArgPointee<4u>(connection_type), + Return(0))); + } + + utils::SharedPtr<NiceMock<resumption_test::MockResumptionData> > + mock_storage_; + application_manager_test::MockApplicationManager mock_app_mngr_; + NiceMock<policy_handler_test::MockPolicySettings> mock_policy_settings_; + NiceMock<connection_handler_test::MockConnectionHandler> + mock_connection_handler_; + NiceMock<protocol_handler_test::MockSessionObserver> mock_session_observer_; + NiceMock<MockApplicationManagerSettings> mock_application_manager_settings_; + std::unique_ptr<am::ApplicationManagerImpl> app_manager_impl_; +}; + +TEST_F(ApplicationManagerImplMockHmiTest, + PostponeCommandsDuringSwitching_ExpectSendingOnSwitchingEnd) { + const uint32_t application_id = 1u; + const std::string policy_app_id = "p_app_id"; + const std::string mac_address = "MA:CA:DD:RE:SS"; + const connection_handler::DeviceHandle device_id = 1u; + const custom_str::CustomString app_name(""); + + utils::SharedPtr<ApplicationImpl> app_impl = new ApplicationImpl( + application_id, + policy_app_id, + mac_address, + device_id, + app_name, + utils::SharedPtr<usage_statistics::StatisticsManager>( + new usage_statistics_test::MockStatisticsManager()), + *app_manager_impl_); + + app_manager_impl_->AddMockApplication(app_impl); + + app_manager_impl_->OnDeviceSwitchingStart(mac_address); + + MockHMICommandFactory* mock_hmi_factory = + MockHMICommandFactory::mock_hmi_command_factory(); + + const uint32_t connection_key = 1u; + const uint32_t correlation_id_1 = 1u; + const uint32_t correlation_id_2 = 2u; + const uint32_t correlation_id_3 = 3u; + utils::SharedPtr<NiceMock<MockRequest> > cmd_1 = + utils::MakeShared<NiceMock<MockRequest> >(connection_key, + correlation_id_1); + utils::SharedPtr<NiceMock<MockRequest> > cmd_2 = + utils::MakeShared<NiceMock<MockRequest> >(connection_key, + correlation_id_2); + utils::SharedPtr<NiceMock<MockRequest> > cmd_3 = + utils::MakeShared<NiceMock<MockRequest> >(connection_key, + correlation_id_3); + + EXPECT_CALL(*mock_hmi_factory, CreateCommand(_, _)) + .WillOnce(Return(cmd_1)) + .WillOnce(Return(cmd_2)) + .WillOnce(Return(cmd_3)); + + commands::MessageSharedPtr hmi_msg_1 = + utils::MakeShared<smart_objects::SmartObject>(); + commands::MessageSharedPtr hmi_msg_2 = + utils::MakeShared<smart_objects::SmartObject>(); + commands::MessageSharedPtr hmi_msg_3 = + utils::MakeShared<smart_objects::SmartObject>(); + + EXPECT_CALL(*cmd_1, Init()).Times(0); + EXPECT_CALL(*cmd_2, Init()).Times(0); + EXPECT_CALL(*cmd_3, Init()).Times(0); + + // Act + app_manager_impl_->ManageHMICommand(hmi_msg_1); + app_manager_impl_->ManageHMICommand(hmi_msg_2); + app_manager_impl_->ManageHMICommand(hmi_msg_3); + + EXPECT_CALL(*mock_hmi_factory, CreateCommand(_, _)) + .WillOnce(Return(cmd_1)) + .WillOnce(Return(cmd_2)) + .WillOnce(Return(cmd_3)); + + EXPECT_CALL(*cmd_1, Init()).WillOnce(Return(true)); + EXPECT_CALL(*cmd_1, Run()); + EXPECT_CALL(*cmd_2, Init()).WillOnce(Return(true)); + EXPECT_CALL(*cmd_2, Run()); + EXPECT_CALL(*cmd_3, Init()).WillOnce(Return(true)); + EXPECT_CALL(*cmd_3, Run()); + + const connection_handler::DeviceHandle new_device_id = 2; + const uint32_t new_application_id = 2; + SetCommonExpectationOnAppReconnection( + new_device_id, new_application_id, mac_address); + + app_manager_impl_->ProcessReconnection(app_impl, new_application_id); + app_manager_impl_->OnApplicationSwitched(app_impl); + + Mock::VerifyAndClearExpectations(&mock_hmi_factory); +} + +} // application_manager_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 0d659e3557..16ca54b903 100644 --- a/src/components/application_manager/test/application_manager_impl_test.cc +++ b/src/components/application_manager/test/application_manager_impl_test.cc @@ -94,7 +94,7 @@ class ApplicationManagerImplTest : public ::testing::Test { ApplicationManagerImplTest() : mock_storage_( ::utils::MakeShared<NiceMock<resumption_test::MockResumptionData> >( - app_mngr_)) + mock_app_mngr_)) , mock_message_helper_( application_manager::MockMessageHelper::message_helper_mock()) , app_id_(0u) { @@ -148,13 +148,39 @@ class ApplicationManagerImplTest : public ::testing::Test { app_manager_impl_->AddMockApplication(mock_app_ptr_); } + void SetCommonExpectationOnAppReconnection( + const connection_handler::DeviceHandle new_device_id, + const uint32_t new_application_id, + const std::string& mac_address) { + EXPECT_CALL( + mock_session_observer_, + GetDataOnSessionKey(new_application_id, + _, + _, + testing::An<connection_handler::DeviceHandle*>())) + .WillOnce(DoAll(SetArgPointee<3u>(new_device_id), Return(0))); + + const std::string connection_type = "MyConnectionType"; + EXPECT_CALL( + mock_session_observer_, + GetDataOnDeviceID( + ::testing::Matcher<connection_handler::DeviceHandle>(new_device_id), + _, + _, + _, + _)) + .WillOnce(DoAll(SetArgPointee<3u>(mac_address), + SetArgPointee<4u>(connection_type), + Return(0))); + } + NiceMock<policy_test::MockPolicySettings> mock_policy_settings_; utils::SharedPtr<NiceMock<resumption_test::MockResumptionData> > mock_storage_; NiceMock<con_test::MockConnectionHandler> mock_connection_handler_; NiceMock<protocol_handler_test::MockSessionObserver> mock_session_observer_; NiceMock<MockApplicationManagerSettings> mock_application_manager_settings_; - application_manager_test::MockApplicationManager app_mngr_; + application_manager_test::MockApplicationManager mock_app_mngr_; std::auto_ptr<am::ApplicationManagerImpl> app_manager_impl_; application_manager::MockMessageHelper* mock_message_helper_; uint32_t app_id_; @@ -646,10 +672,10 @@ TEST_F(ApplicationManagerImplTest, utils::SharedPtr<MockApplication> switching_app_ptr = utils::MakeShared<MockApplication>(); - const std::string switching_device_id = "switching"; + const std::string switching_mac_address = "switching"; app_manager_impl_->AddMockApplication(switching_app_ptr); EXPECT_CALL(*switching_app_ptr, mac_address()) - .WillRepeatedly(ReturnRef(switching_device_id)); + .WillRepeatedly(ReturnRef(switching_mac_address)); const std::string policy_app_id_switch = "abc"; EXPECT_CALL(*switching_app_ptr, policy_app_id()) @@ -658,17 +684,17 @@ TEST_F(ApplicationManagerImplTest, utils::SharedPtr<MockApplication> nonswitching_app_ptr = utils::MakeShared<MockApplication>(); - const std::string nonswitching_device_id = "nonswitching"; + const std::string nonswitching_mac_address = "nonswitching"; app_manager_impl_->AddMockApplication(nonswitching_app_ptr); EXPECT_CALL(*nonswitching_app_ptr, mac_address()) - .WillRepeatedly(ReturnRef(nonswitching_device_id)); + .WillRepeatedly(ReturnRef(nonswitching_mac_address)); const std::string policy_app_id_nonswitch = "efg"; EXPECT_CALL(*nonswitching_app_ptr, policy_app_id()) .WillRepeatedly(Return(policy_app_id_nonswitch)); // Act - app_manager_impl_->OnDeviceSwitchingStart(switching_device_id); + app_manager_impl_->OnDeviceSwitchingStart(switching_mac_address); EXPECT_TRUE(app_manager_impl_->IsAppInReconnectMode(policy_app_id_switch)); EXPECT_FALSE( app_manager_impl_->IsAppInReconnectMode(policy_app_id_nonswitch)); @@ -679,10 +705,18 @@ TEST_F(ApplicationManagerImplTest, utils::SharedPtr<MockApplication> switching_app_ptr = utils::MakeShared<MockApplication>(); - const std::string switching_device_id = "switching"; + const uint32_t switching_app_id = 1u; + EXPECT_CALL(*switching_app_ptr, app_id()) + .WillRepeatedly(Return(switching_app_id)); + + const uint32_t switching_device_id = 1u; + EXPECT_CALL(*switching_app_ptr, device()) + .WillRepeatedly(Return(switching_device_id)); + + const std::string switching_mac_address = "switching"; app_manager_impl_->AddMockApplication(switching_app_ptr); EXPECT_CALL(*switching_app_ptr, mac_address()) - .WillRepeatedly(ReturnRef(switching_device_id)); + .WillRepeatedly(ReturnRef(switching_mac_address)); const std::string policy_app_id_switch = "abc"; EXPECT_CALL(*switching_app_ptr, policy_app_id()) @@ -691,22 +725,42 @@ TEST_F(ApplicationManagerImplTest, utils::SharedPtr<MockApplication> nonswitching_app_ptr = utils::MakeShared<MockApplication>(); - const std::string nonswitching_device_id = "nonswitching"; + const uint32_t nonswitching_app_id = 2u; + EXPECT_CALL(*nonswitching_app_ptr, app_id()) + .WillRepeatedly(Return(nonswitching_app_id)); + + const uint32_t nonswitching_device_id = 2u; + EXPECT_CALL(*nonswitching_app_ptr, device()) + .WillRepeatedly(Return(nonswitching_device_id)); + + const std::string nonswitching_mac_address = "nonswitching"; app_manager_impl_->AddMockApplication(nonswitching_app_ptr); EXPECT_CALL(*nonswitching_app_ptr, mac_address()) - .WillRepeatedly(ReturnRef(nonswitching_device_id)); + .WillRepeatedly(ReturnRef(nonswitching_mac_address)); const std::string policy_app_id_nonswitch = "efg"; EXPECT_CALL(*nonswitching_app_ptr, policy_app_id()) .WillRepeatedly(Return(policy_app_id_nonswitch)); // Act - app_manager_impl_->OnDeviceSwitchingStart(switching_device_id); + app_manager_impl_->OnDeviceSwitchingStart(switching_mac_address); EXPECT_TRUE(app_manager_impl_->IsAppInReconnectMode(policy_app_id_switch)); + EXPECT_FALSE( + app_manager_impl_->IsAppInReconnectMode(policy_app_id_nonswitch)); + + smart_objects::SmartObjectSPtr sptr = + MakeShared<smart_objects::SmartObject>(); + + ON_CALL(*mock_message_helper_, CreateModuleInfoSO(_, _)) + .WillByDefault(Return(sptr)); + ON_CALL(*mock_message_helper_, CreateNegativeResponse(_, _, _, _)) + .WillByDefault(Return(sptr)); - app_manager_impl_->OnDeviceSwitchFinish(switching_device_id); + app_manager_impl_->OnDeviceSwitchFinish(switching_mac_address); EXPECT_FALSE( app_manager_impl_->application_by_policy_id(policy_app_id_switch)); + EXPECT_TRUE( + app_manager_impl_->application_by_policy_id(policy_app_id_nonswitch)); } TEST_F(ApplicationManagerImplTest, @@ -731,26 +785,8 @@ TEST_F(ApplicationManagerImplTest, const connection_handler::DeviceHandle new_device_id = 2; const uint32_t new_application_id = 2; - EXPECT_CALL( - mock_session_observer_, - GetDataOnSessionKey(new_application_id, - _, - _, - testing::An<connection_handler::DeviceHandle*>())) - .WillOnce(DoAll(SetArgPointee<3u>(new_device_id), Return(0))); - - const std::string connection_type = "MyConnectionType"; - EXPECT_CALL( - mock_session_observer_, - GetDataOnDeviceID( - ::testing::Matcher<connection_handler::DeviceHandle>(new_device_id), - _, - _, - _, - _)) - .WillOnce(DoAll(SetArgPointee<3u>(mac_address), - SetArgPointee<4u>(connection_type), - Return(0))); + SetCommonExpectationOnAppReconnection( + new_device_id, new_application_id, mac_address); // Act app_manager_impl_->ProcessReconnection(app_impl, new_application_id); diff --git a/src/components/application_manager/test/command_holder_test.cc b/src/components/application_manager/test/command_holder_test.cc new file mode 100644 index 0000000000..0a365dca73 --- /dev/null +++ b/src/components/application_manager/test/command_holder_test.cc @@ -0,0 +1,126 @@ +/* + * 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 <gmock/gmock.h> + +#include "application_manager/command_holder_impl.h" +#include "smart_objects/smart_object.h" +#include "utils/shared_ptr.h" + +#include "application_manager/mock_application_manager.h" + +namespace test { +namespace components { +namespace application_manager_test { + +using testing::_; + +namespace am = application_manager; + +class CommandHolderImplTest : public testing::Test { + public: + CommandHolderImplTest() + : cmd_ptr_(new smart_objects::SmartObject), kPolicyAppId_("p_app_id") {} + + MockApplicationManager mock_app_manager_; + utils::SharedPtr<smart_objects::SmartObject> cmd_ptr_; + const std::string kPolicyAppId_; +}; + +TEST_F(CommandHolderImplTest, HoldOne_ExpectReleaseOne) { + am::CommandHolderImpl cmd_holder; + cmd_holder.SetCommandsProcessor(&mock_app_manager_); + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + + // Act + EXPECT_CALL(mock_app_manager_, ManageHMICommand(cmd_ptr_)); + cmd_holder.Release(kPolicyAppId_); +} + +TEST_F(CommandHolderImplTest, HoldMany_ExpectReleaseSame) { + am::CommandHolderImpl cmd_holder; + cmd_holder.SetCommandsProcessor(&mock_app_manager_); + + int32_t iterations = 0; + do { + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + ++iterations; + } while (iterations < 5); + + // Act + EXPECT_CALL(mock_app_manager_, ManageHMICommand(cmd_ptr_)).Times(iterations); + cmd_holder.Release(kPolicyAppId_); +} + +TEST_F(CommandHolderImplTest, Hold_Drop_ExpectNoReleased) { + am::CommandHolderImpl cmd_holder; + cmd_holder.SetCommandsProcessor(&mock_app_manager_); + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + + // Act + cmd_holder.Drop(kPolicyAppId_); + EXPECT_CALL(mock_app_manager_, ManageHMICommand(cmd_ptr_)).Times(0); + cmd_holder.Release(kPolicyAppId_); +} + +TEST_F(CommandHolderImplTest, Hold_ReleaseAnotherId_ExpectNoReleased) { + am::CommandHolderImpl cmd_holder; + cmd_holder.SetCommandsProcessor(&mock_app_manager_); + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + + // Act + EXPECT_CALL(mock_app_manager_, ManageHMICommand(cmd_ptr_)).Times(0); + cmd_holder.Release("anotherId"); +} + +TEST_F(CommandHolderImplTest, Hold_DropAnotherId_ExpectReleased) { + am::CommandHolderImpl cmd_holder; + cmd_holder.SetCommandsProcessor(&mock_app_manager_); + + int32_t iterations = 0; + do { + cmd_holder.Hold(kPolicyAppId_, cmd_ptr_); + ++iterations; + } while (iterations < 3); + + // Act + cmd_holder.Drop("anotherId"); + + EXPECT_CALL(mock_app_manager_, ManageHMICommand(cmd_ptr_)).Times(iterations); + cmd_holder.Release(kPolicyAppId_); +} + +} // application_manager_test +} // components +} // test diff --git a/src/components/application_manager/test/commands/mobile/register_app_interface_request_test.cc b/src/components/application_manager/test/commands/mobile/register_app_interface_request_test.cc index 9dca9a23e8..b0928044fc 100644 --- a/src/components/application_manager/test/commands/mobile/register_app_interface_request_test.cc +++ b/src/components/application_manager/test/commands/mobile/register_app_interface_request_test.cc @@ -185,7 +185,7 @@ class RegisterAppInterfaceRequestTest } void SetCommonExpectionsOnSwitchedApplication( - mobile_apis::Result::eType response_result_code) { + MockAppPtr mock_app, mobile_apis::Result::eType response_result_code) { EXPECT_CALL(mock_policy_handler_, AddApplication(_, _)).Times(0); EXPECT_CALL( @@ -213,6 +213,12 @@ class RegisterAppInterfaceRequestTest EXPECT_CALL(app_mngr_, ManageHMICommand(HMIResultCodeIs( hmi_apis::FunctionID::VR_ChangeRegistration))).Times(0); + + EXPECT_CALL( + app_mngr_, + OnApplicationSwitched( + MockAppPtr::static_pointer_cast<application_manager::Application>( + mock_app))); } MessageSharedPtr msg_; @@ -439,7 +445,8 @@ TEST_F(RegisterAppInterfaceRequestTest, EXPECT_CALL(app_mngr_, application(kConnectionKey)) .WillRepeatedly(Return(mock_app)); - SetCommonExpectionsOnSwitchedApplication(mobile_apis::Result::SUCCESS); + SetCommonExpectionsOnSwitchedApplication(mock_app, + mobile_apis::Result::SUCCESS); command_->Run(); } @@ -482,7 +489,8 @@ TEST_F(RegisterAppInterfaceRequestTest, EXPECT_CALL(app_mngr_, application(kConnectionKey)) .WillRepeatedly(Return(mock_app)); - SetCommonExpectionsOnSwitchedApplication(mobile_apis::Result::RESUME_FAILED); + SetCommonExpectionsOnSwitchedApplication(mock_app, + mobile_apis::Result::RESUME_FAILED); command_->Run(); } @@ -515,7 +523,8 @@ TEST_F(RegisterAppInterfaceRequestTest, EXPECT_CALL(app_mngr_, application(kConnectionKey)) .WillRepeatedly(Return(mock_app)); - SetCommonExpectionsOnSwitchedApplication(mobile_apis::Result::RESUME_FAILED); + SetCommonExpectionsOnSwitchedApplication(mock_app, + mobile_apis::Result::RESUME_FAILED); command_->Run(); } diff --git a/src/components/application_manager/test/include/application_manager/mock_hmi_command_factory.h b/src/components/application_manager/test/include/application_manager/mock_hmi_command_factory.h new file mode 100644 index 0000000000..6ff40394a2 --- /dev/null +++ b/src/components/application_manager/test/include/application_manager/mock_hmi_command_factory.h @@ -0,0 +1,57 @@ +/* + 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. + */ + +#pragma once + +#include <gmock/gmock.h> +#include "application_manager/application_manager.h" +#include "application_manager/commands/command.h" +#include "smart_objects/smart_object.h" +#include "utils/shared_ptr.h" + +namespace test { +namespace components { +namespace application_manager_test { + +class MockHMICommandFactory { + public: + MOCK_METHOD2(CreateCommand, + utils::SharedPtr<application_manager::commands::Command>( + const utils::SharedPtr<smart_objects::SmartObject>&, + application_manager::ApplicationManager&)); + + static MockHMICommandFactory* mock_hmi_command_factory(); +}; + +} // application_manager +} // components +} // test diff --git a/src/components/application_manager/test/mock_hmi_command_factory.cc b/src/components/application_manager/test/mock_hmi_command_factory.cc new file mode 100644 index 0000000000..19ad44f482 --- /dev/null +++ b/src/components/application_manager/test/mock_hmi_command_factory.cc @@ -0,0 +1,57 @@ +/* + 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 <gmock/gmock.h> +#include "application_manager/hmi_command_factory.h" +#include "application_manager/mock_hmi_command_factory.h" + +namespace test { +namespace components { +namespace application_manager_test { + +MockHMICommandFactory* MockHMICommandFactory::mock_hmi_command_factory() { + static MockHMICommandFactory mock_hmi_command_factory; + return &mock_hmi_command_factory; +} + +} // application_manager +} // components +} // test + +namespace application_manager { +CommandSharedPtr HMICommandFactory::CreateCommand( + const commands::MessageSharedPtr& message, + ApplicationManager& application_manager) { + return test::components::application_manager_test::MockHMICommandFactory:: + mock_hmi_command_factory()->CreateCommand(message, application_manager); +} +} // application_manager |