diff options
Diffstat (limited to 'src/components/application_manager/test/resumption')
8 files changed, 3239 insertions, 0 deletions
diff --git a/src/components/application_manager/test/resumption/include/application_mock.h b/src/components/application_manager/test/resumption/include/application_mock.h new file mode 100644 index 0000000000..39c4b33539 --- /dev/null +++ b/src/components/application_manager/test/resumption/include/application_mock.h @@ -0,0 +1,266 @@ +/* +* Copyright (c) 2015, Ford Motor Company +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following +* disclaimer in the documentation and/or other materials provided with the +* distribution. +* +* Neither the name of the Ford Motor Company nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_APPLICATION_MOCK_H_ +#define SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_APPLICATION_MOCK_H_ +#include <string> +#include "gmock/gmock.h" +#include "application_manager/application.h" + +namespace test { +namespace components { +namespace resumption_test { + +class ApplicationMock : public ::application_manager::Application { + public: + MOCK_CONST_METHOD0(active_message, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(curHash, const std::string&()); + MOCK_METHOD0(UpdateHash, void()); + MOCK_CONST_METHOD0(flag_sending_hash_change_after_awake, bool()); + MOCK_METHOD1(set_flag_sending_hash_change_after_awake, void(bool flag)); + MOCK_CONST_METHOD0(is_application_data_changed, bool()); + MOCK_METHOD1(set_is_application_data_changed, + void(bool state_application_data)); + MOCK_METHOD0(CloseActiveMessage, void()); + MOCK_CONST_METHOD0(IsFullscreen, bool()); + MOCK_METHOD0(ChangeSupportingAppHMIType, void()); + MOCK_CONST_METHOD0(is_navi, bool()); + MOCK_METHOD1(set_is_navi, void(bool allow)); + MOCK_CONST_METHOD0(video_streaming_approved, bool()); + MOCK_METHOD1(set_video_streaming_approved, void(bool state)); + MOCK_CONST_METHOD0(audio_streaming_approved, bool()); + MOCK_METHOD1(set_audio_streaming_approved, void(bool state)); + MOCK_CONST_METHOD0(video_streaming_allowed, bool()); + MOCK_METHOD1(set_video_streaming_allowed, void(bool state)); + MOCK_CONST_METHOD0(audio_streaming_allowed, bool()); + MOCK_METHOD1(set_audio_streaming_allowed, void(bool state)); + MOCK_METHOD1(StartStreaming, + void(protocol_handler::ServiceType service_type)); + MOCK_METHOD1(StopStreaming, void(protocol_handler::ServiceType service_type)); + MOCK_METHOD1(SuspendStreaming, + void(protocol_handler::ServiceType service_type)); + MOCK_METHOD1(WakeUpStreaming, + void(protocol_handler::ServiceType service_type)); + MOCK_CONST_METHOD0(is_voice_communication_supported, bool()); + MOCK_METHOD1(set_voice_communication_supported, + void(bool is_voice_communication_supported)); + MOCK_CONST_METHOD0(app_allowed, bool()); + MOCK_CONST_METHOD0(has_been_activated, bool()); + MOCK_METHOD1(set_activated, bool(bool is_active)); + MOCK_CONST_METHOD0(version, const ::application_manager::Version&()); + MOCK_METHOD1(set_hmi_application_id, void(uint32_t hmi_app_id)); + MOCK_CONST_METHOD0(hmi_app_id, uint32_t()); + MOCK_CONST_METHOD0(name, const std::string&()); + MOCK_METHOD1(set_folder_name, void(const std::string& folder_name)); + MOCK_CONST_METHOD0(folder_name, const std::string()); + MOCK_CONST_METHOD0(is_media_application, bool()); + MOCK_CONST_METHOD0(hmi_level, const mobile_apis::HMILevel::eType()); + MOCK_CONST_METHOD0(put_file_in_none_count, const uint32_t()); + MOCK_CONST_METHOD0(delete_file_in_none_count, const uint32_t()); + MOCK_CONST_METHOD0(list_files_in_none_count, const uint32_t()); + MOCK_CONST_METHOD0(system_context, const mobile_apis::SystemContext::eType()); + MOCK_CONST_METHOD0(audio_streaming_state, + const mobile_apis::AudioStreamingState::eType()); + MOCK_CONST_METHOD0(app_icon_path, const std::string&()); + MOCK_CONST_METHOD0(device, connection_handler::DeviceHandle()); + MOCK_METHOD0(tts_speak_state, bool()); + MOCK_CONST_METHOD0(CurrentHmiState, + ::application_manager::HmiStatePtr()); + MOCK_CONST_METHOD0(RegularHmiState, + ::application_manager::HmiStatePtr()); + MOCK_CONST_METHOD0(PostponedHmiState, + ::application_manager::HmiStatePtr()); + MOCK_METHOD1(set_tts_properties_in_none, void(bool active)); + MOCK_METHOD0(tts_properties_in_none, bool()); + MOCK_METHOD1(set_tts_properties_in_full, void(bool active)); + MOCK_METHOD0(tts_properties_in_full, bool()); + MOCK_METHOD1(set_version, + void(const ::application_manager::Version& version)); + MOCK_METHOD1(set_name, void(const std::string& name)); + MOCK_METHOD1(set_is_media_application, void(bool is_media)); + MOCK_METHOD0(increment_put_file_in_none_count, void()); + MOCK_METHOD0(increment_delete_file_in_none_count, void()); + MOCK_METHOD0(increment_list_files_in_none_count, void()); + MOCK_METHOD1(set_app_icon_path, bool(const std::string& file_name)); + MOCK_METHOD1(set_app_allowed, void(const bool& allowed)); + MOCK_METHOD1(set_device, void(connection_handler::DeviceHandle device)); + MOCK_CONST_METHOD0(get_grammar_id, uint32_t()); + MOCK_METHOD1(set_grammar_id, void(uint32_t value)); + MOCK_METHOD1( + set_protocol_version, + void(const ::application_manager::ProtocolVersion& protocol_version)); + MOCK_CONST_METHOD0(protocol_version, + ::application_manager::ProtocolVersion()); + MOCK_METHOD1(set_is_resuming, void(bool)); + MOCK_CONST_METHOD0(is_resuming, bool()); + MOCK_METHOD1(AddFile, bool(const ::application_manager::AppFile& file)); + MOCK_CONST_METHOD0(getAppFiles, const ::application_manager::AppFilesMap&()); + MOCK_METHOD1(UpdateFile, bool(const ::application_manager::AppFile& file)); + MOCK_METHOD1(DeleteFile, bool(const std::string& file_name)); + MOCK_METHOD1(GetFile, const ::application_manager::AppFile*( + const std::string& file_name)); + MOCK_METHOD1(SubscribeToButton, + bool(mobile_apis::ButtonName::eType btn_name)); + MOCK_METHOD1(IsSubscribedToButton, + bool(mobile_apis::ButtonName::eType btn_name)); + MOCK_METHOD1(UnsubscribeFromButton, + bool(mobile_apis::ButtonName::eType btn_name)); + MOCK_METHOD1(SubscribeToIVI, bool(uint32_t vehicle_info_type)); + MOCK_CONST_METHOD1(IsSubscribedToIVI, bool(uint32_t vehicle_info_type)); + MOCK_METHOD1(UnsubscribeFromIVI, bool(uint32_t vehicle_info_type)); + MOCK_METHOD0(ResetDataInNone, void()); + MOCK_METHOD2(IsCommandLimitsExceeded, + bool(mobile_apis::FunctionID::eType cmd_id, + ::application_manager::TLimitSource source)); + MOCK_METHOD0(usage_report, ::application_manager::UsageStatistics&()); + MOCK_METHOD1(SetRegularState, void(::application_manager::HmiStatePtr state)); + MOCK_METHOD1(SetPostponedState, void(::application_manager::HmiStatePtr state)); + MOCK_METHOD1(AddHMIState, void(::application_manager::HmiStatePtr state)); + MOCK_METHOD1(RemoveHMIState, + void(::application_manager::HmiState::StateID state_id)); + MOCK_METHOD2(SubscribeToSoftButtons, + void(int32_t cmd_id, + const ::application_manager::SoftButtonID& softbuttons_id)); + MOCK_METHOD1(IsSubscribedToSoftButton, bool(const uint32_t softbutton_id)); + MOCK_METHOD1(UnsubscribeFromSoftButtons, void(int32_t cmd_id)); + MOCK_CONST_METHOD0(IsAudioApplication, bool()); + MOCK_METHOD0(LoadPersistentFiles, void()); + // InitialApplicationData methods + MOCK_CONST_METHOD0(app_types, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(vr_synonyms, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(policy_app_id, std::string()); + MOCK_CONST_METHOD0(tts_name, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(ngn_media_screen_name, + const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(language, const mobile_apis::Language::eType&()); + MOCK_CONST_METHOD0(ui_language, const mobile_apis::Language::eType&()); + MOCK_METHOD1(set_app_types, + void(const smart_objects::SmartObject& app_types)); + MOCK_METHOD1(set_vr_synonyms, + void(const smart_objects::SmartObject& vr_synonyms)); + MOCK_METHOD1(set_policy_app_id, void(const std::string& policy_app_id)); + MOCK_METHOD1(set_tts_name, void(const smart_objects::SmartObject& tts_name)); + MOCK_METHOD1(set_ngn_media_screen_name, + void(const smart_objects::SmartObject& ngn_name)); + MOCK_METHOD1(set_language, + void(const mobile_apis::Language::eType& language)); + MOCK_METHOD1(set_ui_language, + void(const mobile_apis::Language::eType& ui_language)); + // DynamicApplicationData methods + MOCK_CONST_METHOD0(help_prompt, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(timeout_prompt, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(vr_help_title, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(vr_help, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(tbt_state, const mobile_apis::TBTState::eType&()); + MOCK_CONST_METHOD0(show_command, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(tbt_show_command, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0( + SubscribedButtons, + DataAccessor< ::application_manager::ButtonSubscriptions >()); + MOCK_CONST_METHOD0( + SubscribedIVI, + DataAccessor< ::application_manager::VehicleInfoSubscriptions >()); + MOCK_CONST_METHOD0(keyboard_props, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(menu_title, const smart_objects::SmartObject*()); + MOCK_CONST_METHOD0(menu_icon, const smart_objects::SmartObject*()); + MOCK_METHOD1(load_global_properties, + void(const smart_objects::SmartObject& so)); + MOCK_METHOD1(set_help_prompt, + void(const smart_objects::SmartObject& help_prompt)); + MOCK_METHOD1(set_timeout_prompt, + void(const smart_objects::SmartObject& timeout_prompt)); + MOCK_METHOD1(set_vr_help_title, + void(const smart_objects::SmartObject& vr_help_title)); + MOCK_METHOD0(reset_vr_help_title, void()); + MOCK_METHOD1(set_vr_help, void(const smart_objects::SmartObject& vr_help)); + MOCK_METHOD0(reset_vr_help, void()); + MOCK_METHOD1(set_tbt_state, + void(const mobile_apis::TBTState::eType& tbt_state)); + MOCK_METHOD1(set_show_command, + void(const smart_objects::SmartObject& show_command)); + MOCK_METHOD1(set_tbt_show_command, + void(const smart_objects::SmartObject& tbt_show)); + MOCK_METHOD1(set_keyboard_props, + void(const smart_objects::SmartObject& keyboard_props)); + MOCK_METHOD1(set_menu_title, + void(const smart_objects::SmartObject& menu_title)); + MOCK_METHOD1(set_menu_icon, + void(const smart_objects::SmartObject& menu_icon)); + MOCK_CONST_METHOD0(audio_stream_retry_number, uint32_t()); + MOCK_METHOD1(set_audio_stream_retry_number, + void(const uint32_t& audio_stream_retry_number)); + MOCK_CONST_METHOD0(video_stream_retry_number, uint32_t()); + MOCK_METHOD1(set_video_stream_retry_number, + void(const uint32_t& video_stream_retry_number)); + MOCK_METHOD2(AddCommand, void(uint32_t cmd_id, + const smart_objects::SmartObject& command)); + MOCK_METHOD1(RemoveCommand, void(uint32_t cmd_id)); + MOCK_METHOD1(FindCommand, smart_objects::SmartObject*(uint32_t cmd_id)); + MOCK_METHOD2(AddSubMenu, + void(uint32_t menu_id, const smart_objects::SmartObject& menu)); + MOCK_METHOD1(RemoveSubMenu, void(uint32_t menu_id)); + MOCK_CONST_METHOD1(FindSubMenu, + smart_objects::SmartObject*(uint32_t menu_id)); + MOCK_METHOD1(IsSubMenuNameAlreadyExist, bool(const std::string& name)); + MOCK_METHOD2(AddChoiceSet, + void(uint32_t choice_set_id, + const smart_objects::SmartObject& choice_set)); + MOCK_METHOD1(RemoveChoiceSet, void(uint32_t choice_set_id)); + MOCK_METHOD1(FindChoiceSet, + smart_objects::SmartObject*(uint32_t choice_set_id)); + MOCK_METHOD3(AddPerformInteractionChoiceSet, + void(uint32_t correlation_id, uint32_t choice_set_id, + const smart_objects::SmartObject& choice_set)); + MOCK_METHOD1(DeletePerformInteractionChoiceSet, + void(uint32_t correlation_id)); + MOCK_CONST_METHOD0( + performinteraction_choice_set_map, + DataAccessor< ::application_manager::PerformChoiceSetMap >()); + MOCK_CONST_METHOD0(commands_map, + DataAccessor< ::application_manager::CommandsMap >()); + MOCK_CONST_METHOD0(sub_menu_map, + DataAccessor< ::application_manager::SubMenuMap >()); + MOCK_CONST_METHOD0(choice_set_map, + DataAccessor< ::application_manager::ChoiceSetMap >()); + MOCK_METHOD1(set_perform_interaction_active, void(uint32_t active)); + MOCK_CONST_METHOD0(is_perform_interaction_active, uint32_t()); + MOCK_METHOD1(set_perform_interaction_mode, void(int32_t mode)); + MOCK_CONST_METHOD0(perform_interaction_mode, int32_t()); + MOCK_METHOD1(set_reset_global_properties_active, void(bool active)); + MOCK_CONST_METHOD0(is_reset_global_properties_active, bool()); + MOCK_CONST_METHOD0(app_id, uint32_t()); +}; + +} // namespace resumption_test +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_APPLICATION_MOCK_H_ diff --git a/src/components/application_manager/test/resumption/include/resumption_data_mock.h b/src/components/application_manager/test/resumption/include/resumption_data_mock.h new file mode 100644 index 0000000000..2c9285b025 --- /dev/null +++ b/src/components/application_manager/test/resumption/include/resumption_data_mock.h @@ -0,0 +1,86 @@ +/* +* Copyright (c) 2015, Ford Motor Company +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following +* disclaimer in the documentation and/or other materials provided with the +* distribution. +* +* Neither the name of the Ford Motor Company nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_RESUMPTION_DATA_MOCK_H_ +#define SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_RESUMPTION_DATA_MOCK_H_ +#include <string> +#include "gmock/gmock.h" +#include "application_manager/resumption/resumption_data.h" +#include "application_manager/application.h" + +namespace test { +namespace components { +namespace resumption_test { +namespace app_mngr = application_manager; +namespace smart_objects = NsSmartDeviceLink::NsSmartObjects; + +class ResumptionDataMock : public ::resumption::ResumptionData { + public: + MOCK_METHOD1(SaveApplication, + void(app_mngr::ApplicationSharedPtr application)); + MOCK_CONST_METHOD2(GetStoredHMILevel, + int32_t(const std::string& policy_app_id, + const std::string& device_id)); + MOCK_CONST_METHOD1(IsHMIApplicationIdExist, bool(uint32_t hmi_app_id)); + MOCK_METHOD2(CheckSavedApplication, bool(const std::string& policy_app_id, + const std::string& device_id)); + MOCK_CONST_METHOD2(GetHMIApplicationID, + uint32_t(const std::string& policy_app_id, + const std::string& device_id)); + MOCK_METHOD0(OnSuspend, void()); + MOCK_CONST_METHOD3(GetHashId, + bool(const std::string& policy_app_id, + const std::string& device_id, std::string& hash_id)); + MOCK_METHOD0(OnAwake, void()); + MOCK_CONST_METHOD3(GetSavedApplication, + bool(const std::string& policy_app_id, + const std::string& device_id, + smart_objects::SmartObject& saved_app)); + MOCK_METHOD2(RemoveApplicationFromSaved, + bool(const std::string& policy_app_id, + const std::string& device_id)); + MOCK_CONST_METHOD0(GetIgnOffTime, uint32_t()); + MOCK_CONST_METHOD2(IsApplicationSaved, + ssize_t(const std::string& policy_app_id, + const std::string& device_id)); + MOCK_CONST_METHOD1(GetDataForLoadResumeData, + void(smart_objects::SmartObject& saved_data)); + MOCK_METHOD3(UpdateHmiLevel, void(const std::string& policy_app_id, + const std::string& device_id, + mobile_apis::HMILevel::eType hmi_level)); + MOCK_METHOD0(Init, bool()); +}; + +} // namespace resumption_test +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_APPLICATION_MANAGER_TEST_RESUMPTION_INCLUDE_RESUMPTION_DATA_MOCK_H_ diff --git a/src/components/application_manager/test/resumption/include/resumption_data_test.h b/src/components/application_manager/test/resumption/include/resumption_data_test.h new file mode 100644 index 0000000000..55767225b5 --- /dev/null +++ b/src/components/application_manager/test/resumption/include/resumption_data_test.h @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string> +#include <algorithm> +#include "gtest/gtest.h" +#include "application_manager/usage_statistics.h" +#include "application_mock.h" +#include "application_manager/application_manager_impl.h" +#include "utils/data_accessor.h" + +namespace test { +namespace components { +namespace resumption_test { + +using ::testing::NiceMock; +namespace am = application_manager; +namespace sm = smart_objects; +using namespace Json; + +using namespace resumption; +using namespace mobile_apis; + +class ResumptionDataTest : public ::testing::Test { + protected: + // Check structure in saved application + void CheckSavedApp(sm::SmartObject& saved_data); + + // Set data for resumption + virtual void PrepareData(); + utils::SharedPtr<NiceMock<ApplicationMock>> app_mock; + + HMILevel::eType hmi_level_; + size_t app_id_; + size_t hmi_app_id_; + std::string policy_app_id_; + size_t ign_off_count_; + const size_t tts_chunks_count = 4; + + size_t grammar_id_; + std::string hash_; + bool is_audio_; + const connection_handler::DeviceHandle device_handle_ = 10; + + sm::SmartObject* help_prompt_; + sm::SmartObject* timeout_prompt_; + sm::SmartObject* vr_help_; + sm::SmartObject* vr_help_title_; + sm::SmartObject* vr_synonyms_; + sm::SmartObject* keyboard_props_; + sm::SmartObject* menu_title_; + sm::SmartObject* menu_icon_; + + void SetCommands(); + void SetSubmenues(); + void SetChoiceSet(); + void SetAppFiles(); + void SetGlobalProporties(); + void SetKeyboardProperties(); + void SetMenuTitleAndIcon(); + void SetHelpAndTimeoutPrompt(); + void SetVRHelpTitle(); + void SetSubscriptions(); + + void CheckCommands(sm::SmartObject& res_list); + void CheckGlobalProporties(sm::SmartObject& res_list); + void CheckSubmenues(sm::SmartObject& res_list); + void CheckChoiceSet(sm::SmartObject& res_list); + void CheckAppFiles(sm::SmartObject& res_list); + void CheckKeyboardProperties(sm::SmartObject& res_list); + void CheckMenuTitle(sm::SmartObject& res_list); + void CheckMenuIcon(sm::SmartObject& res_list); + void CheckHelpPrompt(sm::SmartObject& res_list); + void CheckTimeoutPrompt(sm::SmartObject& res_list); + void CheckVRHelp(NsSmartDeviceLink::NsSmartObjects::SmartObject& res_list); + void CheckVRTitle(sm::SmartObject& res_list); + void CheckSubscriptions(sm::SmartObject& res_list); + + const size_t count_of_commands = 5; + const size_t count_of_choice = 2; + const size_t count_of_choice_sets = 4; + const size_t count_of_submenues = 3; + const size_t count_of_files = 8; + const size_t count_of_vrhelptitle = 2; + const std::string device_id_ = "12345"; + + am::CommandsMap test_commands_map; + am::SubMenuMap test_submenu_map; + am::ChoiceSetMap test_choiceset_map; + am::AppFilesMap app_files_map_; + + am::ButtonSubscriptions btn_subscr; + am::VehicleInfoSubscriptions ivi; + + sync_primitives::Lock sublock_; + sync_primitives::Lock comlock_; + sync_primitives::Lock setlock_; + sync_primitives::Lock btnlock_; + sync_primitives::Lock ivilock_; +}; + +} // namespace resumption_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/resumption/resume_ctrl_test.cc b/src/components/application_manager/test/resumption/resume_ctrl_test.cc new file mode 100644 index 0000000000..bfd41b0f08 --- /dev/null +++ b/src/components/application_manager/test/resumption/resume_ctrl_test.cc @@ -0,0 +1,937 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "application_manager/resumption/resume_ctrl.h" +#include <string> +#include <algorithm> +#include "gtest/gtest.h" +#include "application_manager/usage_statistics.h" +#include "include/application_mock.h" +#include "include/resumption_data_mock.h" +#include "interfaces/MOBILE_API.h" +#include "application_manager/application_manager_impl.h" +#include "application_manager/application.h" +#include "config_profile/profile.h" +#include "utils/data_accessor.h" +#include "application_manager/test/mock_message_helper.h" + +namespace test { +namespace components { +namespace resumption_test { + +using ::testing::_; +using ::testing::Return; +using ::testing::DoAll; +using ::testing::SetArgReferee; +using ::testing::Mock; +using ::testing::NiceMock; +using ::testing::AtLeast; + +using namespace resumption; +using namespace mobile_apis::HMILevel; + +class ResumeCtrlTest : public ::testing::Test { + public: + virtual void SetUp() OVERRIDE { + app_mngr = application_manager::ApplicationManagerImpl::instance(); + // Singleton should not be destroyed + Mock::AllowLeak(app_mngr); + mock_storage = new NiceMock<ResumptionDataMock>(); + app_mock = new NiceMock<ApplicationMock>(); + res_ctrl.set_resumption_storage(mock_storage); + test_audio_state = mobile_apis::AudioStreamingState::NOT_AUDIBLE; + test_app_id = 10; + default_testType = eType::HMI_NONE; + test_dev_id = 5; + test_policy_app_id = "test_policy_app_id"; + test_grammar_id = 10; + hash = "saved_hash"; + } + + protected: + application_manager::ApplicationManagerImpl* app_mngr; + ResumeCtrl res_ctrl; + NiceMock<ResumptionDataMock>* mock_storage; + NiceMock<ApplicationMock>* app_mock; + mobile_apis::AudioStreamingState::eType test_audio_state; + // app_mock.app_id() will return this value + uint32_t test_app_id; + std::string test_policy_app_id; + mobile_apis::HMILevel::eType default_testType; + + // app_mock.Device() will return this value + uint32_t test_dev_id; + uint32_t test_grammar_id; + std::string hash; +}; + +/** + * @brief Group of tests which check starting resumption with different data + */ + +TEST_F(ResumeCtrlTest, StartResumption_AppWithGrammarId) { + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_CALL(*app_mock, UpdateHash()); + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +MATCHER_P4(CheckAppFile, is_persistent, is_download, file_name, file_type, "") { + application_manager::AppFile app_file = arg; + return app_file.is_persistent == is_persistent && + app_file.is_download_complete == is_download && + app_file.file_name == file_name && app_file.file_type == file_type; +} + +TEST_F(ResumeCtrlTest, StartResumption_WithoutGrammarId) { + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_CALL(*app_mock, UpdateHash()); + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)).Times(0); + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_FALSE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithFiles) { + smart_objects::SmartObject test_application_files; + smart_objects::SmartObject test_file; + const uint32_t count_of_files = 8; + + int file_types[count_of_files]; + std::string file_names[count_of_files]; + const size_t max_size = 12; + char numb[max_size]; + for (uint32_t i = 0; i < count_of_files; i++) { + file_types[i] = i; + std::snprintf(numb, max_size, "%d", i); + file_names[i] = "test_file" + std::string(numb); + } + + // Should not been added + test_file[application_manager::strings::persistent_file] = false; + test_application_files[0] = test_file; + + for (uint32_t i = 0; i < count_of_files; ++i) { + test_file[application_manager::strings::persistent_file] = true; + test_file[application_manager::strings::is_download_complete] = true; + test_file[application_manager::strings::file_type] = file_types[i]; + test_file[application_manager::strings::sync_file_name] = file_names[i]; + test_application_files[i + 1] = test_file; + } + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_files] = + test_application_files; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_CALL(*app_mock, UpdateHash()); + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + for (uint32_t i = 0; i < count_of_files; ++i) { + EXPECT_CALL(*app_mock, + AddFile(CheckAppFile( + true, true, file_names[i], + static_cast<mobile_apis::FileType::eType>(file_types[i])))); + } + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithSubmenues) { + smart_objects::SmartObject test_application_submenues; + smart_objects::SmartObject test_submenu; + + const uint32_t count_of_submenues = 20; + for (uint32_t i = 0; i < count_of_submenues; ++i) { + test_submenu[application_manager::strings::menu_id] = i; + test_application_submenues[i] = test_submenu; + } + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_submenus] = + test_application_submenues; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + for (uint32_t i = 0; i < count_of_submenues; ++i) { + EXPECT_CALL(*app_mock, AddSubMenu(i, test_application_submenues[i])); + } + smart_objects::SmartObjectList requests; + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + CreateAddSubMenuRequestToHMI(_)).WillRepeatedly(Return(requests)); + + EXPECT_CALL(*app_mock, UpdateHash()); + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithCommands) { + smart_objects::SmartObject test_application_commands; + smart_objects::SmartObject test_commands; + const uint32_t count_of_commands = 20; + + for (uint32_t i = 0; i < count_of_commands; ++i) { + test_commands[application_manager::strings::cmd_id] = i; + test_application_commands[i] = test_commands; + } + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_commands] = + test_application_commands; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_CALL(*app_mock, UpdateHash()); + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + for (uint32_t i = 0; i < count_of_commands; ++i) { + EXPECT_CALL(*app_mock, AddCommand(i, test_application_commands[i])); + } + + smart_objects::SmartObjectList requests; + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + CreateAddCommandRequestToHMI(_)).WillRepeatedly(Return(requests)); + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithChoiceSet) { + smart_objects::SmartObject application_choice_sets; + smart_objects::SmartObject app_choice_set; + + const uint32_t count_of_choice = 10; + smart_objects::SmartObject choice_vector; + smart_objects::SmartObject choice; + const size_t max_size = 12; + char numb[max_size]; + for (uint32_t i = 0; i < count_of_choice; ++i) { + std::snprintf(numb, max_size, "%d", i); + choice[application_manager::strings::vr_commands] = + "VrCommand" + std::string(numb); + choice[application_manager::strings::choice_id] = i; + choice_vector[i] = choice; + } + const uint32_t count_of_choice_sets = 5; + for (uint32_t i = 0; i < count_of_choice_sets; ++i) { + app_choice_set[application_manager::strings::interaction_choice_set_id] = i; + app_choice_set[application_manager::strings::choice_set] = choice_vector; + application_choice_sets[i] = app_choice_set; + } + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_choice_sets] = + application_choice_sets; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_CALL(*app_mock, UpdateHash()); + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + for (uint32_t i = 0; i < count_of_choice_sets; ++i) { + EXPECT_CALL(*app_mock, AddChoiceSet(i, application_choice_sets[i])); + } + + smart_objects::SmartObjectList requests; + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + CreateAddVRCommandRequestFromChoiceToHMI(_)) + .WillRepeatedly(Return(requests)); + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithGlobalProperties) { + // Prepare Data + smart_objects::SmartObject test_global_properties; + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_global_properties] = + test_global_properties; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + SendGlobalPropertiesToHMI(_)); + + EXPECT_CALL(*app_mock, load_global_properties(test_global_properties)); + + EXPECT_CALL(*app_mock, UpdateHash()); + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithSubscribeOnButtons) { + // Prepare Data + smart_objects::SmartObject test_subscriptions; + smart_objects::SmartObject app_buttons; + + uint32_t count_of_buttons = 17; + for (uint32_t i = 0; i < count_of_buttons; ++i) { + app_buttons[i] = i; + } + + test_subscriptions[application_manager::strings::application_buttons] = + app_buttons; + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_subscribtions] = + test_subscriptions; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + for (uint32_t i = 0; i < count_of_buttons; ++i) { + EXPECT_CALL(*app_mock, SubscribeToButton( + static_cast<mobile_apis::ButtonName::eType>(i))); + } + EXPECT_CALL(*app_mock, UpdateHash()); + + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + SendAllOnButtonSubscriptionNotificationsForApp(_)).Times(2); + + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumption_AppWithSubscriptionToIVI) { + // Prepare Data + smart_objects::SmartObject test_subscriptions; + smart_objects::SmartObject app_vi; + + int vtype = application_manager::VehicleDataType::GPS; + uint i = 0; + for (; vtype < application_manager::VehicleDataType::STEERINGWHEEL; + ++i, ++vtype) { + app_vi[i] = vtype; + } + + test_subscriptions[application_manager::strings::application_vehicle_info] = + app_vi; + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::application_subscribtions] = + test_subscriptions; + + // Check RestoreApplicationData + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .Times(3) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mock, set_grammar_id(test_grammar_id)); + + for (size_t i = 0; i < app_vi.length(); ++i) { + EXPECT_CALL( + *app_mock, + SubscribeToIVI(static_cast<application_manager::VehicleDataType>(i))); + } + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + smart_objects::SmartObjectList requests; + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + GetIVISubscriptionRequests(_)).WillRepeatedly(Return(requests)); + + EXPECT_CALL(*app_mock, UpdateHash()); + bool res = res_ctrl.StartResumption(app_mock, hash); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartResumptionOnlyHMILevel) { + smart_objects::SmartObject saved_app; + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(test_policy_app_id, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .Times(3) + .WillRepeatedly(Return(default_testType)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, SetState(test_app_id, default_testType, + test_audio_state)).Times(2); + bool res = res_ctrl.StartResumptionOnlyHMILevel(app_mock); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, StartAppHmiStateResumption_AppInFull) { + mobile_apis::HMILevel::eType restored_testType = eType::HMI_FULL; + uint32_t ign_off_count = 0; + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::ign_off_count] = ign_off_count; + saved_app[application_manager::strings::hmi_level] = restored_testType; + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(true)); + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, + SetHmiState(test_app_id, restored_testType)).Times(AtLeast(1)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(test_policy_app_id, _, _)) + .Times(2) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + EXPECT_CALL(*mock_storage, RemoveApplicationFromSaved(_, _)) + .WillOnce(Return(true)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("12345")) + .WillRepeatedly(Return(policy::kDeviceAllowed)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + + res_ctrl.StartAppHmiStateResumption(app_mock); +} + +TEST_F(ResumeCtrlTest, StartAppHmiStateResumption_AppInBackground) { + uint32_t ign_off_count = 0; + smart_objects::SmartObject saved_app; + + mobile_apis::HMILevel::eType restored_testType = eType::HMI_BACKGROUND; + saved_app[application_manager::strings::ign_off_count] = ign_off_count; + saved_app[application_manager::strings::hmi_level] = restored_testType; + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(test_policy_app_id, _, _)) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("12345")) + .WillOnce(Return(policy::kDeviceAllowed)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + + res_ctrl.StartAppHmiStateResumption(app_mock); +} + +/** + * @brief Group of tests which check restoring resumption with different data + */ + +TEST_F(ResumeCtrlTest, RestoreAppHMIState_RestoreHMILevelFull) { + mobile_apis::HMILevel::eType restored_testType = eType::HMI_FULL; + + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::hash_id] = hash; + saved_app[application_manager::strings::grammar_id] = test_grammar_id; + saved_app[application_manager::strings::hmi_level] = restored_testType; + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")) + .WillOnce(Return(policy::kDeviceAllowed)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillRepeatedly(Return(false)); + + utils::SharedPtr<application_manager::Application> null_app; + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(null_app)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + EXPECT_CALL(*app_mngr, + SetState(test_app_id, restored_testType, test_audio_state)); + + bool res = res_ctrl.RestoreAppHMIState(app_mock); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetupDefaultHMILevel) { + smart_objects::SmartObject saved_app; + + saved_app[application_manager::strings::hmi_level] = default_testType; + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillRepeatedly(Return(default_testType)); + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + EXPECT_CALL(*app_mngr, + SetHmiState(test_app_id, default_testType)).Times(AtLeast(1)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, SetState(test_app_id, default_testType, + test_audio_state)).Times(2); + + res_ctrl.SetupDefaultHMILevel(app_mock); +} + +/** + * @brief group of tests which check correct SetAppHMIState +*/ + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMINone_NotMedia_WithoutCheckPolicy) { + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, SetState(test_app_id, default_testType, + test_audio_state)).Times(2); + + bool res = res_ctrl.SetAppHMIState(app_mock, default_testType, false); + EXPECT_TRUE(res); +} + +// TODO(VVeremjova) APPLINK-16718 +TEST_F(ResumeCtrlTest, + DISABLED_SetAppHMIState_HMILimited_NotMedia_WithoutCheckPolicy) { + mobile_apis::HMILevel::eType testType = eType::HMI_LIMITED; + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")) + .WillRepeatedly(Return(policy::DeviceConsent::kDeviceAllowed)); + EXPECT_CALL(*app_mngr, SetState(test_app_id, testType, test_audio_state)); + + bool res = res_ctrl.SetAppHMIState(app_mock, testType, false); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMIFull_NotMedia_WithoutCheckPolicy) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + ::testing::InSequence seq; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(false)); + + // Only mocked application is exist + utils::SharedPtr<application_manager::Application> null_app; + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(null_app)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + // GetDefaultHmiLevel should not be called + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)).Times(0); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + EXPECT_CALL(*app_mngr, SetState(test_app_id, testType, test_audio_state)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + bool res = res_ctrl.SetAppHMIState(app_mock, testType, false); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, + SetAppHMIState_HMIFull_NotMedia_WithoutPolicy_AnotherFullActiveExists) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + ::testing::InSequence seq; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(false)); + + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + // Expect set default HMI state + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + bool res = res_ctrl.SetAppHMIState(app_mock, testType, false); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, + SetAppHMIState_HMIFull_MediaAudio_WithoutPolicy_AnotherActiveAppExists) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + + // In case application is media, audio state will be AUDIBLE + test_audio_state = mobile_apis::AudioStreamingState::AUDIBLE; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + // Only our app in FULL + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(false)); + + // Active app exists + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)).Times(0); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(true)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + mobile_apis::HMILevel::eType expected_testType = eType::HMI_LIMITED; + EXPECT_CALL(*app_mngr, + SetState(test_app_id, expected_testType, test_audio_state)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + bool res = res_ctrl.SetAppHMIState(app_mock, testType, false); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMIFull_Media_WithoutPolicy_FullExists) { + default_testType = eType::HMI_FULL; + + // In case application is media, audio state will be AUDIBLE + test_audio_state = mobile_apis::AudioStreamingState::AUDIBLE; + ::testing::InSequence seq; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(true)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("")).Times(0); + bool res = res_ctrl.SetAppHMIState(app_mock, default_testType, false); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMIFull_NotMedia_WithPolicy_DevAllowed) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("12345")) + .WillOnce(Return(policy::kDeviceAllowed)); + + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(true)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + + bool res = res_ctrl.SetAppHMIState(app_mock, testType, true); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMIFull_Media_WithCheckPolicy) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + + // In case application is media, audio state will be AUDIBLE + test_audio_state = mobile_apis::AudioStreamingState::AUDIBLE; + ::testing::InSequence seq; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + // App is allowed + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("12345")) + .WillOnce(Return(policy::kDeviceAllowed)); + EXPECT_CALL(*app_mngr, IsAppTypeExistsInFullOrLimited(_)) + .WillOnce(Return(false)); + EXPECT_CALL(*app_mngr, active_application()).WillOnce(Return(app_mock)); + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)).Times(0); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(true)); + + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + mobile_apis::HMILevel::eType expected_testType = eType::HMI_LIMITED; + EXPECT_CALL(*app_mngr, + SetState(test_app_id, expected_testType, test_audio_state)); + + bool res = res_ctrl.SetAppHMIState(app_mock, testType, true); + EXPECT_TRUE(res); +} + +TEST_F(ResumeCtrlTest, SetAppHMIState_HMIFull_Media_WithPolicy_DevDisallowed) { + mobile_apis::HMILevel::eType testType = eType::HMI_FULL; + + ::testing::InSequence seq; + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + EXPECT_CALL(*app_mngr, GetUserConsentForDevice("12345")) + .WillOnce(Return(policy::kDeviceDisallowed)); + + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(true)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + + bool res = res_ctrl.SetAppHMIState(app_mock, testType, true); + EXPECT_FALSE(res); +} + +TEST_F(ResumeCtrlTest, SaveApplication) { + utils::SharedPtr<application_manager::Application> app_sh_mock = + new ApplicationMock(); + + EXPECT_CALL(*mock_storage, SaveApplication(app_sh_mock)); + res_ctrl.SaveApplication(app_sh_mock); +} + +TEST_F(ResumeCtrlTest, OnAppActivated_ResumptionHasStarted) { + ::profile::Profile::instance()->config_file_name("smartDeviceLink_test.ini"); + + smart_objects::SmartObject saved_app; + EXPECT_CALL(*app_mngr, GetDefaultHmiLevel(_)) + .WillOnce(Return(default_testType)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, is_media_application()).WillByDefault(Return(false)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + EXPECT_CALL(*app_mngr, + SetState(test_app_id, default_testType, test_audio_state)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(test_policy_app_id, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(test_app_id)); + + bool res = res_ctrl.StartResumptionOnlyHMILevel(app_mock); + EXPECT_TRUE(res); + + utils::SharedPtr<ApplicationMock> app_sh_mock = new ApplicationMock(); + + EXPECT_CALL(*app_sh_mock, app_id()).WillOnce(Return(test_app_id)); + res_ctrl.OnAppActivated(app_sh_mock); +} + +TEST_F(ResumeCtrlTest, OnAppActivated_ResumptionNotActive) { + utils::SharedPtr<ApplicationMock> app_sh_mock = new ApplicationMock(); + EXPECT_CALL(*app_sh_mock, app_id()).Times(0); + res_ctrl.OnAppActivated(app_sh_mock); +} + +TEST_F(ResumeCtrlTest, IsHMIApplicationIdExist) { + uint32_t hmi_app_id = 10; + + EXPECT_CALL(*mock_storage, IsHMIApplicationIdExist(hmi_app_id)) + .WillOnce(Return(true)); + EXPECT_TRUE(res_ctrl.IsHMIApplicationIdExist(hmi_app_id)); +} + +TEST_F(ResumeCtrlTest, GetHMIApplicationID) { + uint32_t hmi_app_id = 10; + std::string device_id = "test_device_id"; + + EXPECT_CALL(*mock_storage, GetHMIApplicationID(test_policy_app_id, device_id)) + .WillOnce(Return(hmi_app_id)); + EXPECT_EQ(hmi_app_id, + res_ctrl.GetHMIApplicationID(test_policy_app_id, device_id)); +} + +TEST_F(ResumeCtrlTest, IsApplicationSaved) { + std::string policy_app_id = "policy_app_id"; + std::string device_id = "device_id"; + + EXPECT_CALL(*mock_storage, IsApplicationSaved(policy_app_id, device_id)) + .WillOnce(Return(true)); + EXPECT_TRUE(res_ctrl.IsApplicationSaved(policy_app_id, device_id)); +} + +TEST_F(ResumeCtrlTest, CheckPersistenceFiles_WithoutCommandAndChoiceSets) { + uint32_t ign_off_count = 0; + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::ign_off_count] = ign_off_count; + saved_app[application_manager::strings::hmi_level] = HMI_FULL; + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_TRUE(res_ctrl.CheckPersistenceFilesForResumption(app_mock)); +} + +TEST_F(ResumeCtrlTest, CheckPersistenceFilesForResumption_WithCommands) { + smart_objects::SmartObject test_application_commands; + uint32_t ign_off_count = 0; + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::ign_off_count] = ign_off_count; + saved_app[application_manager::strings::hmi_level] = HMI_FULL; + saved_app[application_manager::strings::application_commands] = + test_application_commands; + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_CALL(*application_manager::MockMessageHelper::message_helper_mock(), + VerifyImageFiles(_, _)) + .WillRepeatedly(Return(mobile_apis::Result::SUCCESS)); + + EXPECT_TRUE(res_ctrl.CheckPersistenceFilesForResumption(app_mock)); +} + +TEST_F(ResumeCtrlTest, CheckPersistenceFilesForResumption_WithChoiceSet) { + smart_objects::SmartObject test_choice_sets; + uint32_t ign_off_count = 0; + smart_objects::SmartObject saved_app; + saved_app[application_manager::strings::ign_off_count] = ign_off_count; + saved_app[application_manager::strings::hmi_level] = HMI_FULL; + saved_app[application_manager::strings::application_choice_sets] = + test_choice_sets; + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + + EXPECT_CALL(*mock_storage, GetSavedApplication(_, _, _)) + .WillOnce(DoAll(SetArgReferee<2>(saved_app), Return(true))); + + EXPECT_TRUE(res_ctrl.CheckPersistenceFilesForResumption(app_mock)); +} + +// TODO (VVeremjova) APPLINK-16718 +TEST_F(ResumeCtrlTest, DISABLED_OnSuspend) { + EXPECT_CALL(*mock_storage, OnSuspend()); + res_ctrl.OnSuspend(); +} + +TEST_F(ResumeCtrlTest, OnAwake) { + EXPECT_CALL(*mock_storage, OnAwake()); + res_ctrl.OnAwake(); +} + +TEST_F(ResumeCtrlTest, RemoveApplicationFromSaved) { + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + EXPECT_CALL(*mock_storage, RemoveApplicationFromSaved(_, _)) + .WillOnce(Return(true)); + EXPECT_TRUE(res_ctrl.RemoveApplicationFromSaved(app_mock)); +} + +TEST_F(ResumeCtrlTest, CheckApplicationHash) { + smart_objects::SmartObject saved_app; + + std::string test_hash = "saved_hash"; + saved_app[application_manager::strings::hash_id] = test_hash; + + ON_CALL(*app_mock, device()).WillByDefault(Return(test_dev_id)); + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(test_policy_app_id)); + EXPECT_CALL(*mock_storage, GetSavedApplication(test_policy_app_id, _, _)) + .WillRepeatedly(DoAll(SetArgReferee<2>(saved_app), Return(true))); + EXPECT_TRUE(res_ctrl.CheckApplicationHash(app_mock, test_hash)); +} + +} // namespace resumption_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/resumption/resumption_data_db_test.cc b/src/components/application_manager/test/resumption/resumption_data_db_test.cc new file mode 100644 index 0000000000..a6ced14349 --- /dev/null +++ b/src/components/application_manager/test/resumption/resumption_data_db_test.cc @@ -0,0 +1,837 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string> +#include <algorithm> +#include "gtest/gtest.h" +#include "include/application_mock.h" +#include "interfaces/MOBILE_API.h" +#include "sql_database.h" +#include "sql_query.h" + +#include "application_manager/application_manager_impl.h" +#include "config_profile/profile.h" +#include "utils/file_system.h" +#include "include/resumption_data_test.h" + +#include "application_manager/resumption/resumption_sql_queries.h" +#include "application_manager/resumption/resumption_data_db.h" + +namespace test { +namespace components { +namespace resumption_test { + +using ::testing::NiceMock; + +namespace am = application_manager; +using namespace file_system; + +using namespace resumption; +using namespace mobile_apis; + +class TestResumptionDataDB : public ResumptionDataDB { + public: + utils::dbms::SQLDatabase* get_db_handle() { return db(); } + + TestResumptionDataDB(DbStorage db_storage) : ResumptionDataDB(db_storage) {} +}; + +class ResumptionDataDBTest : public ResumptionDataTest { + protected: + virtual void SetUp() { + app_mock = new NiceMock<ApplicationMock>(); + policy_app_id_ = "test_policy_app_id"; + app_id_ = 10; + is_audio_ = true; + hash_ = "saved_hash"; + hmi_level_ = HMILevel::eType::HMI_FULL; + hmi_app_id_ = 8; + ign_off_count_ = 0; + grammar_id_ = 16; + } + virtual void TearDown() { + utils::dbms::SQLQuery query(test_db()); + EXPECT_TRUE(query.Prepare(remove_all_tables)); + EXPECT_TRUE(query.Exec()); + } + + static void SetUpTestCase() { + kDatabaseName = "resumption"; + if (is_in_file) { + ::profile::Profile::instance()->config_file_name( + "smartDeviceLink_test.ini"); + path_ = profile::Profile::instance()->app_storage_folder(); + CreateDirectory("./" + path_); + test_db_ = new utils::dbms::SQLDatabase(kDatabaseName); + test_db_->set_path(path_ + "/"); + res_db_ = new TestResumptionDataDB(In_File_Storage); + } else { + res_db_ = new TestResumptionDataDB(In_Memory_Storage); + test_db_ = res_db_->get_db_handle(); + } + + EXPECT_TRUE(test_db_->Open()); + EXPECT_TRUE(test_db_->IsReadWrite()); + } + + static utils::dbms::SQLDatabase* test_db_; + static std::string kDatabaseName; + static std::string path_; + + static void TearDownTestCase() { + test_db_->Close(); + if (is_in_file) { + ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); + RemoveDirectory("./" + path_, true); + } + delete res_db_; + } + + utils::dbms::SQLDatabase* test_db() { return test_db_; } + std::string path() { return path_; } + + void SetZeroIgnOffTime() { + utils::dbms::SQLQuery query(test_db()); + EXPECT_TRUE(query.Prepare(KUpdateLastIgnOffTime)); + query.Bind(0, 0); + EXPECT_TRUE(query.Exec()); + } + + static TestResumptionDataDB* res_db_; + + TestResumptionDataDB* res_db() { return res_db_; } + + // Check that db includes tables with given elements + void CheckSavedDB(); + + static const bool is_in_file = false; + const std::string tables_exist = + "SELECT COUNT(*) FROM sqlite_master WHERE `type` = 'table';"; + const std::string init_last_ign_count = + "SELECT `last_ign_off_time` FROM resumption;"; + const std::string internal_data = "SELECT COUNT(*) FROM _internal_data;"; + + const std::string remove_all_tables = + "DELETE FROM `resumption`; " + "DELETE FROM `image`; " + "DELETE FROM `applicationChoiceSet`; " + "DELETE FROM `file`; " + "DELETE FROM `subMenu`; " + "DELETE FROM `TTSChunk`; " + "DELETE FROM `vrHelpItem`; " + "DELETE FROM `tableLimitedCharacterList`; " + "DELETE FROM `characterArray`; " + "DELETE FROM `choice`; " + "DELETE FROM `command`; " + "DELETE FROM `globalProperties`; " + "DELETE FROM `choiceArray`; " + "DELETE FROM `vrCommandsArray`; " + "DELETE FROM `helpTimeoutPromptArray`; " + "DELETE FROM `vrHelpItemArray`; " + "DELETE FROM `application`; " + "DELETE FROM `applicationChoiceSetArray`; " + "DELETE FROM `applicationCommandsArray`; " + "DELETE FROM `applicationFilesArray`; " + "DELETE FROM `applicationSubMenuArray`; " + "DELETE FROM `applicationSubscribtionsArray`; " + "DELETE FROM `_internal_data`; "; + + private: + void CheckExistenceApplication(); + void CheckAppData(); + void CheckAppFilesData(); + void CheckSubmenuData(); + void CheckCommandsData(); + void CheckChoiceSetData(); + + void CheckGlobalProportiesData(); + void CheckCharacters(int64_t global_properties_key); + void CheckVRHelpItem(int64_t global_properties_key); + + void BindId(utils::dbms::SQLQuery& query); +}; + +utils::dbms::SQLDatabase* ResumptionDataDBTest::test_db_ = NULL; +TestResumptionDataDB* ResumptionDataDBTest::res_db_ = NULL; +std::string ResumptionDataDBTest::kDatabaseName = ""; +std::string ResumptionDataDBTest::path_ = ""; + +void ResumptionDataDBTest::CheckSavedDB() { + utils::dbms::SQLQuery query_checks(test_db()); + EXPECT_TRUE(query_checks.Prepare(kChecksResumptionData)); + EXPECT_TRUE(query_checks.Exec()); + EXPECT_EQ(1, query_checks.GetInteger(0)); + + CheckExistenceApplication(); + CheckAppData(); + + CheckAppFilesData(); + CheckSubmenuData(); + CheckCommandsData(); + + CheckChoiceSetData(); + CheckGlobalProportiesData(); +} + +void ResumptionDataDBTest::CheckExistenceApplication() { + utils::dbms::SQLQuery query(test_db()); + EXPECT_TRUE(query.Prepare(kCheckApplication)); + query.Bind(0, device_id_); + query.Bind(1, policy_app_id_); + EXPECT_TRUE(query.Exec()); + EXPECT_EQ(1, query.GetInteger(0)); +} + +void ResumptionDataDBTest::CheckAppData() { + utils::dbms::SQLQuery query(test_db()); + EXPECT_TRUE(query.Prepare(kSelectAppTable)); + BindId(query); + EXPECT_TRUE(query.Exec()); + EXPECT_EQ(policy_app_id_, query.GetString(0)); + EXPECT_EQ(app_id_, query.GetUInteger(1)); + EXPECT_EQ(grammar_id_, query.GetUInteger(2)); + EXPECT_EQ(hash_, query.GetString(3)); + EXPECT_EQ(hmi_app_id_, query.GetUInteger(4)); + EXPECT_EQ(hmi_level_, query.GetInteger(5)); + + EXPECT_EQ(ign_off_count_, query.GetUInteger(6)); + + EXPECT_EQ(device_id_, query.GetString(8)); + EXPECT_EQ(is_audio_, query.GetBoolean(9)); +} + +void ResumptionDataDBTest::CheckGlobalProportiesData() { + utils::dbms::SQLQuery select_globalproperties(test_db()); + + EXPECT_TRUE(select_globalproperties.Prepare(kSelectCountGlobalProperties)); + BindId(select_globalproperties); + EXPECT_TRUE(select_globalproperties.Exec()); + EXPECT_EQ(1, select_globalproperties.GetInteger(0)); + + EXPECT_TRUE(select_globalproperties.Prepare(kSelectGlobalProperties)); + BindId(select_globalproperties); + + size_t help_prompt_idx = 0; + size_t timeout_prompt_idx = 0; + int64_t global_properties_key = 0; + while (select_globalproperties.Next()) { + if (global_properties_key != select_globalproperties.GetLongInt(0)) { + global_properties_key = select_globalproperties.GetLongInt(0); + EXPECT_EQ((*vr_help_title_).asString(), + select_globalproperties.GetString(1)); + EXPECT_EQ((*menu_title_).asString(), + select_globalproperties.GetString(2)); + EXPECT_EQ((*keyboard_props_)[am::strings::language].asInt(), + select_globalproperties.GetInteger(4)); + EXPECT_EQ((*keyboard_props_)[am::hmi_request::keyboard_layout].asInt(), + select_globalproperties.GetInteger(5)); + EXPECT_EQ((*keyboard_props_)[am::strings::key_press_mode].asInt(), + select_globalproperties.GetInteger(6)); + EXPECT_EQ((*keyboard_props_)[am::strings::auto_complete_text].asString(), + select_globalproperties.GetString(7)); + + EXPECT_FALSE(select_globalproperties.IsNull(3)); + utils::dbms::SQLQuery select_image(test_db()); + EXPECT_TRUE(select_image.Prepare(kSelectImage)); + select_image.Bind(0, select_globalproperties.GetLongInt(3)); + EXPECT_TRUE(select_image.Exec()); + EXPECT_EQ((*menu_icon_)[am::strings::image_type].asInt(), + select_image.GetInteger(0)); + EXPECT_EQ((*menu_icon_)[am::strings::value].asString(), + select_image.GetString(1)); + } + if (!select_globalproperties.IsNull(8)) { + utils::dbms::SQLQuery select_tts_chunk(test_db()); + EXPECT_TRUE(select_tts_chunk.Prepare(kSelectTTSChunk)); + select_tts_chunk.Bind(0, select_globalproperties.GetLongInt(8)); + EXPECT_TRUE(select_tts_chunk.Exec()); + + std::string text = + (*help_prompt_)[help_prompt_idx][am::strings::text].asString(); + int type = (*help_prompt_)[help_prompt_idx][am::strings::type].asInt(); + EXPECT_EQ(text, select_tts_chunk.GetString(0)); + EXPECT_EQ(type, select_tts_chunk.GetInteger(1)); + help_prompt_idx++; + } + if (!select_globalproperties.IsNull(9)) { + utils::dbms::SQLQuery select_tts_chunk(test_db()); + EXPECT_TRUE(select_tts_chunk.Prepare(kSelectTTSChunk)); + select_tts_chunk.Bind(0, select_globalproperties.GetLongInt(9)); + EXPECT_TRUE(select_tts_chunk.Exec()); + + std::string text = + (*timeout_prompt_)[timeout_prompt_idx][am::strings::text].asString(); + int type = + (*timeout_prompt_)[timeout_prompt_idx][am::strings::type].asInt(); + EXPECT_EQ(text, select_tts_chunk.GetString(0)); + EXPECT_EQ(type, select_tts_chunk.GetInteger(1)); + timeout_prompt_idx++; + } + CheckCharacters(global_properties_key); + CheckVRHelpItem(global_properties_key); + } +} +void ResumptionDataDBTest::CheckVRHelpItem(int64_t global_properties_key) { + utils::dbms::SQLQuery checks_vrhelp_item(test_db()); + EXPECT_TRUE(checks_vrhelp_item.Prepare(kChecksVrHelpItem)); + checks_vrhelp_item.Bind(0, global_properties_key); + EXPECT_TRUE(checks_vrhelp_item.Exec()); + EXPECT_NE(0, checks_vrhelp_item.GetInteger(0)); + if (!checks_vrhelp_item.GetInteger(0)) { + utils::dbms::SQLQuery select_vrhelp_item(test_db()); + EXPECT_TRUE(select_vrhelp_item.Prepare(kSelectVrHelpItem)); + select_vrhelp_item.Bind(0, global_properties_key); + size_t vr_help_item_idx = 0; + while (select_vrhelp_item.Next()) { + std::string vr_text = + (*vr_help_)[vr_help_item_idx][am::strings::text].asString(); + std::string vr_position = + (*vr_help_)[vr_help_item_idx++][am::strings::position].asString(); + EXPECT_EQ(vr_text, select_vrhelp_item.GetString(0)); + EXPECT_EQ(vr_position, select_vrhelp_item.GetString(1)); + } + } +} + +void ResumptionDataDBTest::CheckCharacters(int64_t global_properties_key) { + utils::dbms::SQLQuery checks_characters(test_db()); + EXPECT_TRUE(checks_characters.Prepare(kChecksCharacter)); + checks_characters.Bind(0, global_properties_key); + EXPECT_TRUE(checks_characters.Exec()); + EXPECT_NE(0, checks_characters.GetInteger(0)); + if (!checks_characters.GetInteger(0)) { + utils::dbms::SQLQuery select_characters(test_db()); + EXPECT_TRUE(select_characters.Prepare(kSelectCharacter)); + select_characters.Bind(0, global_properties_key); + size_t characters_idx = 0; + while (select_characters.Next()) { + std::string character = + (*keyboard_props_)[am::strings::limited_character_list] + [characters_idx++].asString(); + EXPECT_EQ(character, select_characters.GetString(0)); + } + } +} + +void ResumptionDataDBTest::CheckSubmenuData() { + utils::dbms::SQLQuery select_submenu(test_db()); + + EXPECT_TRUE(select_submenu.Prepare(kSelectCountSubMenu)); + BindId(select_submenu); + EXPECT_TRUE(select_submenu.Exec()); + EXPECT_EQ(count_of_submenues, select_submenu.GetUInteger(0)); + + EXPECT_TRUE(select_submenu.Prepare(kSelectSubMenu)); + BindId(select_submenu); + int i = 10; + while (select_submenu.Next()) { + uint32_t test_id = (*test_submenu_map[i])[am::strings::menu_id].asUInt(); + std::string name = + (*test_submenu_map[i])[am::strings::menu_name].asString(); + int position = (*test_submenu_map[i])[am::strings::position].asInt(); + EXPECT_EQ(test_id, select_submenu.GetUInteger(0)); + EXPECT_EQ(name, select_submenu.GetString(1)); + EXPECT_EQ(position, select_submenu.GetInteger(2)); + i++; + } +} + +void ResumptionDataDBTest::CheckCommandsData() { + utils::dbms::SQLQuery select_commands(test_db()); + + EXPECT_TRUE(select_commands.Prepare(kSelectCountCommands)); + BindId(select_commands); + EXPECT_TRUE(select_commands.Exec()); + EXPECT_EQ(count_of_commands, select_commands.GetUInteger(0)); + + EXPECT_TRUE(select_commands.Prepare(kSelectCommands)); + BindId(select_commands); + + int32_t i = -1; + int64_t command_key = 0; + int j = 0; + while (select_commands.Next()) { + if (command_key != select_commands.GetLongInt(0)) { + ++i; + uint cmd = (*test_commands_map[i])[am::strings::cmd_id].asUInt(); + EXPECT_EQ(cmd, select_commands.GetUInteger(1)); + std::string name = + (*test_commands_map[i])[am::strings::menu_params] + [am::strings::menu_name].asString(); + EXPECT_EQ(name, select_commands.GetString(2)); + int position = (*test_commands_map[i])[am::strings::menu_params] + [am::strings::position].asInt(); + EXPECT_EQ(position, select_commands.GetInteger(4)); + int parent_id = + (*test_commands_map[i])[am::strings::menu_params] + [am::hmi_request::parent_id].asInt(); + EXPECT_EQ(parent_id, select_commands.GetInteger(3)); + std::string icon_name = + (*test_commands_map[i])[am::strings::cmd_icon][am::strings::value] + .asString(); + EXPECT_EQ(icon_name, select_commands.GetString(5)); + + int icon_type = (*test_commands_map[i])[am::strings::cmd_icon] + [am::strings::image_type].asInt(); + EXPECT_EQ(icon_type, select_commands.GetInteger(6)); + + j = 0; + command_key = select_commands.GetLongInt(0); + } + std::string vr = + (*test_commands_map[i])[am::strings::vr_commands][j].asString(); + EXPECT_EQ(vr, select_commands.GetString(7)); + j++; + } +} + +void ResumptionDataDBTest::CheckChoiceSetData() { + utils::dbms::SQLQuery select_choice_set(test_db()); + EXPECT_TRUE(select_choice_set.Prepare(kSelectCountChoiceSet)); + BindId(select_choice_set); + EXPECT_TRUE(select_choice_set.Exec()); + EXPECT_EQ(count_of_choice_sets, select_choice_set.GetUInteger(0)); + + EXPECT_TRUE(select_choice_set.Prepare(kSelectChoiceSets)); + int64_t app_set_key = 0; + int64_t choice_key = 0; + int32_t choice_set_idx = -1; + int32_t choice_idx = -1; + size_t vr_cmd_idx = 0; + sm::SmartObject command; + while (select_choice_set.Next()) { + if (app_set_key != select_choice_set.GetLongInt(0)) { + command = (*test_choiceset_map[app_set_key]); + ++choice_set_idx; + + int choice_set_id = + command[am::strings::interaction_choice_set_id].asUInt(); + int grammar_id = command[am::strings::grammar_id].asUInt(); + + EXPECT_EQ(grammar_id, select_choice_set.GetInteger(1)); + EXPECT_EQ(choice_set_id, select_choice_set.GetInteger(2)); + + app_set_key = select_choice_set.GetLongInt(0); + choice_idx = -1; + } + + if (choice_key != select_choice_set.GetLongInt(3)) { + ++choice_idx; + choice_key = select_choice_set.GetLongInt(3); + int choice_id = + command[am::strings::choice_set][choice_idx][am::strings::choice_id] + .asUInt(); + std::string menu_name = + command[am::strings::choice_set][choice_idx][am::strings::menu_name] + .asString(); + std::string secondary_text = + command[am::strings::choice_set][choice_idx] + [am::strings::secondary_text].asString(); + std::string tertiary_text = + command[am::strings::choice_set][choice_idx] + [am::strings::tertiary_text].asString(); + + EXPECT_EQ(choice_id, select_choice_set.GetInteger(4)); + EXPECT_EQ(menu_name, select_choice_set.GetString(5)); + EXPECT_EQ(secondary_text, select_choice_set.GetString(6)); + EXPECT_EQ(tertiary_text, select_choice_set.GetString(7)); + + EXPECT_FALSE(select_choice_set.IsNull(8)); + utils::dbms::SQLQuery select_image(test_db()); + EXPECT_TRUE(select_image.Prepare(kSelectImage)); + select_image.Bind(0, select_choice_set.GetLongInt(8)); + EXPECT_TRUE(select_image.Exec()); + std::string image_value = + command[am::strings::choice_set][choice_idx][am::strings::image] + [am::strings::value].asString(); + int image_type = + command[am::strings::choice_set][choice_idx][am::strings::image] + [am::strings::image_type].asInt(); + EXPECT_EQ(image_value, select_image.GetString(1)); + EXPECT_EQ(image_type, select_image.GetInteger(0)); + + EXPECT_FALSE(select_choice_set.IsNull(9)); + EXPECT_TRUE(select_image.Prepare(kSelectImage)); + select_image.Bind(0, select_choice_set.GetLongInt(9)); + EXPECT_TRUE(select_image.Exec()); + image_value = + command[am::strings::choice_set][choice_idx] + [am::strings::secondary_image][am::strings::value].asString(); + image_type = + command[am::strings::choice_set][choice_idx] + [am::strings::secondary_image][am::strings::image_type] + .asInt(); + EXPECT_EQ(image_value, select_image.GetString(1)); + EXPECT_EQ(image_type, select_image.GetInteger(0)); + + vr_cmd_idx = 0; + } + std::string vr_comm = + command[am::strings::choice_set][choice_idx][am::strings::vr_commands] + [vr_cmd_idx++].asString(); + EXPECT_EQ(vr_comm, select_choice_set.GetString(10)); + } +} + +void ResumptionDataDBTest::CheckAppFilesData() { + utils::dbms::SQLQuery query(test_db()); + EXPECT_TRUE(query.Prepare(kSelectCountFiles)); + BindId(query); + EXPECT_TRUE(query.Exec()); + EXPECT_EQ(count_of_files, query.GetUInteger(0)); + + EXPECT_TRUE(query.Prepare(kSelectFiles)); + BindId(query); + am::AppFile check_file; + int i = 0; + while (query.Next()) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + check_file = app_files_map_["test_file " + std::string(numb)]; + + EXPECT_EQ(check_file.file_type, query.GetInteger(0)); + EXPECT_EQ(check_file.is_download_complete, query.GetBoolean(1)); + EXPECT_EQ(check_file.is_persistent, query.GetBoolean(2)); + EXPECT_EQ(check_file.file_name, query.GetString(3)); + i++; + } +} + +void ResumptionDataDBTest::BindId(utils::dbms::SQLQuery& query) { + query.Bind(0, policy_app_id_); + query.Bind(1, device_id_); +} + +TEST_F(ResumptionDataDBTest, Init) { + utils::dbms::SQLQuery query_checks(test_db()); + + EXPECT_TRUE(res_db()->Init()); + + EXPECT_TRUE(query_checks.Prepare(tables_exist)); + EXPECT_TRUE(query_checks.Exec()); + EXPECT_NE(0, query_checks.GetInteger(0)); + + EXPECT_TRUE(query_checks.Prepare(kChecksResumptionData)); + EXPECT_TRUE(query_checks.Exec()); + EXPECT_NE(0, query_checks.GetInteger(0)); + + EXPECT_TRUE(query_checks.Prepare(init_last_ign_count)); + EXPECT_TRUE(query_checks.Exec()); + EXPECT_EQ(0, query_checks.GetInteger(0)); + + EXPECT_TRUE(query_checks.Prepare(internal_data)); + EXPECT_TRUE(query_checks.Exec()); + EXPECT_LE(0, query_checks.GetInteger(0)); +} + +TEST_F(ResumptionDataDBTest, SaveApplication) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, RemoveApplicationFromSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + EXPECT_TRUE(res_db()->RemoveApplicationFromSaved(policy_app_id_, device_id_)); + + sm::SmartObject remove_app; + EXPECT_FALSE( + res_db()->GetSavedApplication(policy_app_id_, device_id_, remove_app)); + EXPECT_TRUE(remove_app.empty()); +} + +TEST_F(ResumptionDataDBTest, RemoveApplicationFromSaved_AppNotSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + + sm::SmartObject saved_app; + EXPECT_FALSE( + res_db()->GetSavedApplication(policy_app_id_, "54321", saved_app)); + EXPECT_TRUE(saved_app.empty()); +} + +TEST_F(ResumptionDataDBTest, SavedApplicationTwice) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, SavedApplicationTwice_UpdateApp) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + (*vr_help_)[0][am::strings::position] = 2; + + res_db()->SaveApplication(app_mock); + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, IsApplicationSaved_ApplicationSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + ssize_t result = res_db()->IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(0, result); +} + +TEST_F(ResumptionDataDBTest, IsApplicationSaved_ApplicationRemoved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + EXPECT_TRUE(res_db()->RemoveApplicationFromSaved(policy_app_id_, device_id_)); + ssize_t result = res_db()->IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(-1, result); +} + +TEST_F(ResumptionDataDBTest, GetSavedApplication) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + sm::SmartObject saved_app; + EXPECT_TRUE( + res_db()->GetSavedApplication(policy_app_id_, device_id_, saved_app)); + CheckSavedApp(saved_app); +} + +TEST_F(ResumptionDataDBTest, GetSavedApplication_AppNotSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + sm::SmartObject saved_app; + EXPECT_FALSE( + res_db()->GetSavedApplication(policy_app_id_, "54321", saved_app)); + EXPECT_TRUE(saved_app.empty()); +} + +TEST_F(ResumptionDataDBTest, GetDataForLoadResumeData) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + sm::SmartObject saved_app; + res_db()->GetDataForLoadResumeData(saved_app); + + EXPECT_EQ(policy_app_id_, saved_app[0][am::strings::app_id].asString()); + EXPECT_EQ(device_id_, saved_app[0][am::strings::device_id].asString()); + EXPECT_EQ(hmi_level_, static_cast<HMILevel::eType>( + saved_app[0][am::strings::hmi_level].asInt())); + EXPECT_EQ(ign_off_count_, saved_app[0][am::strings::ign_off_count].asUInt()); +} + +TEST_F(ResumptionDataDBTest, GetDataForLoadResumeData_AppRemove) { + sm::SmartObject saved_app; + + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + EXPECT_TRUE(res_db()->RemoveApplicationFromSaved(policy_app_id_, device_id_)); + res_db()->GetDataForLoadResumeData(saved_app); + EXPECT_TRUE(saved_app.empty()); +} + +TEST_F(ResumptionDataDBTest, UpdateHmiLevel) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + HMILevel::eType new_hmi_level = HMILevel::HMI_LIMITED; + res_db()->UpdateHmiLevel(policy_app_id_, device_id_, new_hmi_level); + hmi_level_ = new_hmi_level; + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, IsHMIApplicationIdExist_AppIsSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + EXPECT_TRUE(res_db()->IsHMIApplicationIdExist(hmi_app_id_)); +} + +TEST_F(ResumptionDataDBTest, IsHMIApplicationIdExist_AppNotSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + uint32_t new_hmi_app_id_ = hmi_app_id_ + 10; + EXPECT_FALSE(res_db()->IsHMIApplicationIdExist(new_hmi_app_id_)); +} + +TEST_F(ResumptionDataDBTest, GetHMIApplicationID) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + EXPECT_EQ(hmi_app_id_, + res_db()->GetHMIApplicationID(policy_app_id_, device_id_)); +} + +TEST_F(ResumptionDataDBTest, GetHMIApplicationID_AppNotSaved) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + EXPECT_EQ(0u, res_db()->GetHMIApplicationID(policy_app_id_, "other_dev_id")); +} + +TEST_F(ResumptionDataDBTest, OnSuspend) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + res_db()->OnSuspend(); + ign_off_count_++; + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, OnSuspendFourTimes) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + res_db()->OnSuspend(); + ign_off_count_++; + CheckSavedDB(); + + res_db()->OnSuspend(); + ign_off_count_++; + CheckSavedDB(); + res_db()->OnSuspend(); + ign_off_count_++; + CheckSavedDB(); + + res_db()->OnSuspend(); + + ssize_t result = res_db()->IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(-1, result); +} + +TEST_F(ResumptionDataDBTest, OnSuspendOnAwake) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + res_db()->OnSuspend(); + + ign_off_count_++; + CheckSavedDB(); + res_db()->OnAwake(); + ign_off_count_ = 0; + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, Awake_AppNotSuspended) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + res_db()->OnAwake(); + ign_off_count_ = 0; + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, TwiceAwake_AppNotSuspended) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + + res_db()->SaveApplication(app_mock); + CheckSavedDB(); + + res_db()->OnSuspend(); + res_db()->OnAwake(); + ign_off_count_ = 0; + CheckSavedDB(); + + res_db()->OnAwake(); + CheckSavedDB(); +} + +TEST_F(ResumptionDataDBTest, GetHashId) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + + res_db()->SaveApplication(app_mock); + + std::string test_hash; + EXPECT_TRUE(res_db()->GetHashId(policy_app_id_, device_id_, test_hash)); + EXPECT_EQ(hash_, test_hash); +} + +TEST_F(ResumptionDataDBTest, GetIgnOffTime_AfterSuspendAndAwake) { + PrepareData(); + EXPECT_TRUE(res_db()->Init()); + SetZeroIgnOffTime(); + uint32_t last_ign_off_time; + + res_db()->SaveApplication(app_mock); + + last_ign_off_time = res_db()->GetIgnOffTime(); + EXPECT_EQ(0u, last_ign_off_time); + + res_db()->OnSuspend(); + + uint32_t after_suspend; + after_suspend = res_db()->GetIgnOffTime(); + EXPECT_LE(last_ign_off_time, after_suspend); + + uint32_t after_awake; + res_db()->OnAwake(); + + after_awake = res_db()->GetIgnOffTime(); + EXPECT_LE(after_suspend, after_awake); +} + +} // namespace resumption_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/resumption/resumption_data_json_test.cc b/src/components/application_manager/test/resumption/resumption_data_json_test.cc new file mode 100644 index 0000000000..24a16596eb --- /dev/null +++ b/src/components/application_manager/test/resumption/resumption_data_json_test.cc @@ -0,0 +1,394 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string> +#include <algorithm> +#include "gtest/gtest.h" + +#include "application_manager/usage_statistics.h" +#include "include/application_mock.h" +#include "include/resumption_data_mock.h" +#include "interfaces/MOBILE_API.h" +#include "resumption/last_state.h" + +#include "include/resumption_data_test.h" +#include "formatters/CFormatterJsonBase.hpp" +#include "config_profile/profile.h" +#include "utils/file_system.h" + +#include "application_manager/resumption/resumption_data_json.h" +namespace test { +namespace components { +namespace resumption_test { + +using ::testing::_; +using ::testing::Return; +using ::testing::NiceMock; + +namespace am = application_manager; +using namespace Json; +using namespace file_system; + +using namespace resumption; +using namespace mobile_apis; +namespace Formatters = NsSmartDeviceLink::NsJSONHandler::Formatters; + +class ResumptionDataJsonTest : public ResumptionDataTest { + protected: + virtual void SetUp() { + app_mock = new NiceMock<ApplicationMock>(); + + policy_app_id_ = "test_policy_app_id"; + app_id_ = 10; + is_audio_ = true; + grammar_id_ = 20; + hash_ = "saved_hash"; + hmi_level_ = HMILevel::eType::HMI_FULL; + hmi_app_id_ = 8; + ign_off_count_ = 0; + } + + void CheckSavedJson() { + Value& dictionary = LastState::instance()->dictionary; + ASSERT_TRUE(dictionary[am::strings::resumption].isObject()); + ASSERT_TRUE( + dictionary[am::strings::resumption][am::strings::resume_app_list] + .isArray()); + Value& resume_app_list = + dictionary[am::strings::resumption][am::strings::resume_app_list]; + sm::SmartObject res_app_list; + for (uint32_t i = 0; i < resume_app_list.size(); i++) { + Formatters::CFormatterJsonBase::jsonValueToObj(resume_app_list[i], + res_app_list); + CheckSavedApp(res_app_list); + } + } + + void SetZeroIgnOff() { + Value& dictionary = LastState::instance()->dictionary; + Value& res = dictionary[am::strings::resumption]; + res[am::strings::last_ign_off_time] = 0; + LastState::instance()->SaveToFileSystem(); + } +}; + +TEST_F(ResumptionDataJsonTest, SaveApplication) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, SavedApplicationTwice) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, SavedApplicationTwice_UpdateApp) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + (*vr_help_)[0][am::strings::position] = 2; + + res_json.SaveApplication(app_mock); + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, RemoveApplicationFromSaved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + EXPECT_TRUE(res_json.RemoveApplicationFromSaved(policy_app_id_, device_id_)); + + // Check that application was deleted + smart_objects::SmartObject remove_app; + EXPECT_FALSE( + res_json.GetSavedApplication(policy_app_id_, device_id_, remove_app)); + EXPECT_TRUE(remove_app.empty()); +} + +TEST_F(ResumptionDataJsonTest, RemoveApplicationFromSaved_AppNotSaved) { + ResumptionDataJson res_json; + EXPECT_FALSE(res_json.RemoveApplicationFromSaved(policy_app_id_, "54321")); +} + +TEST_F(ResumptionDataJsonTest, IsApplicationSaved_ApplicationSaved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + ssize_t result = res_json.IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(0, result); +} + +TEST_F(ResumptionDataJsonTest, IsApplicationSaved_ApplicationRemoved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + EXPECT_TRUE(res_json.RemoveApplicationFromSaved(policy_app_id_, device_id_)); + ssize_t result = res_json.IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(-1, result); +} + +TEST_F(ResumptionDataJsonTest, GetSavedApplication) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + smart_objects::SmartObject saved_app; + EXPECT_TRUE( + res_json.GetSavedApplication(policy_app_id_, device_id_, saved_app)); + CheckSavedApp(saved_app); +} + +TEST_F(ResumptionDataJsonTest, GetSavedApplication_AppNotSaved) { + ResumptionDataJson res_json; + smart_objects::SmartObject saved_app; + EXPECT_FALSE( + res_json.GetSavedApplication(policy_app_id_, "54321", saved_app)); + EXPECT_TRUE(saved_app.empty()); +} + +TEST_F(ResumptionDataJsonTest, GetDataForLoadResumeData) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + smart_objects::SmartObject saved_app; + res_json.GetDataForLoadResumeData(saved_app); + + EXPECT_EQ(policy_app_id_, saved_app[0][am::strings::app_id].asString()); + EXPECT_EQ(device_id_, saved_app[0][am::strings::device_id].asString()); + EXPECT_EQ(hmi_level_, static_cast<HMILevel::eType>( + saved_app[0][am::strings::hmi_level].asInt())); + EXPECT_EQ(ign_off_count_, saved_app[0][am::strings::ign_off_count].asUInt()); +} + +TEST_F(ResumptionDataJsonTest, GetDataForLoadResumeData_AppRemove) { + ResumptionDataJson res_json; + smart_objects::SmartObject saved_app; + + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + EXPECT_TRUE(res_json.RemoveApplicationFromSaved(policy_app_id_, device_id_)); + res_json.GetDataForLoadResumeData(saved_app); + EXPECT_TRUE(saved_app.empty()); +} + +TEST_F(ResumptionDataJsonTest, UpdateHmiLevel) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + HMILevel::eType new_hmi_level = HMILevel::HMI_LIMITED; + res_json.UpdateHmiLevel(policy_app_id_, device_id_, new_hmi_level); + hmi_level_ = new_hmi_level; + + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, IsHMIApplicationIdExist_AppIsSaved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + EXPECT_TRUE(res_json.IsHMIApplicationIdExist(hmi_app_id_)); +} + +TEST_F(ResumptionDataJsonTest, IsHMIApplicationIdExist_AppNotSaved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + + CheckSavedJson(); + uint32_t new_hmi_app_id_ = hmi_app_id_ + 10; + EXPECT_FALSE(res_json.IsHMIApplicationIdExist(new_hmi_app_id_)); +} + +TEST_F(ResumptionDataJsonTest, GetHMIApplicationID) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + EXPECT_EQ(hmi_app_id_, + res_json.GetHMIApplicationID(policy_app_id_, device_id_)); +} + +TEST_F(ResumptionDataJsonTest, GetHMIApplicationID_AppNotSaved) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + EXPECT_EQ(0u, res_json.GetHMIApplicationID(policy_app_id_, "other_dev_id")); +} + +TEST_F(ResumptionDataJsonTest, OnSuspend) { + ResumptionDataJson res_json; + ::profile::Profile::instance()->config_file_name("smartDeviceLink_test.ini"); + SetZeroIgnOff(); + PrepareData(); + + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + res_json.OnSuspend(); + ign_off_count_++; + CheckSavedJson(); + + EXPECT_TRUE(FileExists("./test_app_info.dat")); + EXPECT_TRUE(DirectoryExists("./test_storage")); + EXPECT_TRUE(RemoveDirectory("./test_storage", true)); + EXPECT_TRUE(DeleteFile("./test_app_info.dat")); + ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); +} + +TEST_F(ResumptionDataJsonTest, OnSuspendFourTimes) { + ResumptionDataJson res_json; + PrepareData(); + ::profile::Profile::instance()->config_file_name("smartDeviceLink_test.ini"); + SetZeroIgnOff(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + res_json.OnSuspend(); + ign_off_count_++; + CheckSavedJson(); + + res_json.OnSuspend(); + res_json.OnSuspend(); + res_json.OnSuspend(); + + ssize_t result = res_json.IsApplicationSaved(policy_app_id_, device_id_); + EXPECT_EQ(-1, result); + + EXPECT_TRUE(FileExists("./test_app_info.dat")); + EXPECT_TRUE(DirectoryExists("./test_storage")); + EXPECT_TRUE(RemoveDirectory("./test_storage", true)); + EXPECT_TRUE(DeleteFile("./test_app_info.dat")); + ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); +} + +TEST_F(ResumptionDataJsonTest, OnSuspendOnAwake) { + ResumptionDataJson res_json; + PrepareData(); + ::profile::Profile::instance()->config_file_name("smartDeviceLink_test.ini"); + SetZeroIgnOff(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + res_json.OnSuspend(); + ign_off_count_++; + CheckSavedJson(); + + res_json.OnAwake(); + ign_off_count_ = 0; + CheckSavedJson(); + EXPECT_TRUE(RemoveDirectory("./test_storage", true)); + EXPECT_TRUE(DeleteFile("./test_app_info.dat")); + ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); +} + +TEST_F(ResumptionDataJsonTest, Awake_AppNotSuspended) { + ResumptionDataJson res_json; + SetZeroIgnOff(); + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + res_json.OnAwake(); + ign_off_count_ = 0; + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, TwiceAwake_AppNotSuspended) { + ResumptionDataJson res_json; + SetZeroIgnOff(); + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + res_json.OnSuspend(); + res_json.OnAwake(); + ign_off_count_ = 0; + CheckSavedJson(); + + res_json.OnAwake(); + CheckSavedJson(); +} + +TEST_F(ResumptionDataJsonTest, GetHashId) { + ResumptionDataJson res_json; + PrepareData(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + + std::string test_hash; + EXPECT_TRUE(res_json.GetHashId(policy_app_id_, device_id_, test_hash)); + EXPECT_EQ(hash_, test_hash); +} + +TEST_F(ResumptionDataJsonTest, GetIgnOffTime_AfterSuspendAndAwake) { + ResumptionDataJson res_json; + uint32_t last_ign_off_time; + PrepareData(); + ::profile::Profile::instance()->config_file_name("smartDeviceLink_test.ini"); + SetZeroIgnOff(); + res_json.SaveApplication(app_mock); + CheckSavedJson(); + last_ign_off_time = res_json.GetIgnOffTime(); + EXPECT_EQ(0u, last_ign_off_time); + + res_json.OnSuspend(); + + uint32_t after_suspend; + after_suspend = res_json.GetIgnOffTime(); + EXPECT_LE(last_ign_off_time, after_suspend); + + uint32_t after_awake; + res_json.OnAwake(); + + after_awake = res_json.GetIgnOffTime(); + EXPECT_LE(after_suspend, after_awake); + + EXPECT_TRUE(RemoveDirectory("./test_storage", true)); + EXPECT_TRUE(DeleteFile("./test_app_info.dat")); + ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); +} + +} // namespace resumption_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/resumption/resumption_data_test.cc b/src/components/application_manager/test/resumption/resumption_data_test.cc new file mode 100644 index 0000000000..b8054b0d85 --- /dev/null +++ b/src/components/application_manager/test/resumption/resumption_data_test.cc @@ -0,0 +1,549 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string> +#include <algorithm> +#include "gtest/gtest.h" + +#include "application_manager/usage_statistics.h" +#include "include/application_mock.h" +#include "include/resumption_data_mock.h" + +#include "application_manager/application_manager_impl.h" +#include "application_manager/application.h" +#include "utils/data_accessor.h" +#include "application_manager/message_helper.h" + +#include "include/resumption_data_test.h" + +std::string application_manager::MessageHelper::GetDeviceMacAddressForHandle( + const uint32_t device_handle) { + std::string device_mac_address = "12345"; + return device_mac_address; +} + +namespace test { +namespace components { +namespace resumption_test { + +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::ReturnPointee; + +void ResumptionDataTest::CheckSavedApp(sm::SmartObject& resume_app_list) { + EXPECT_EQ(policy_app_id_, resume_app_list[am::strings::app_id].asString()); + EXPECT_EQ(grammar_id_, resume_app_list[am::strings::grammar_id].asUInt()); + EXPECT_EQ(app_id_, resume_app_list[am::strings::connection_key].asUInt()); + EXPECT_EQ(hmi_app_id_, resume_app_list[am::strings::hmi_app_id].asUInt()); + EXPECT_EQ(ign_off_count_, + resume_app_list[am::strings::ign_off_count].asUInt()); + EXPECT_EQ(hmi_level_, static_cast<HMILevel::eType>( + resume_app_list[am::strings::hmi_level].asInt())); + EXPECT_EQ(is_audio_, + resume_app_list[am::strings::is_media_application].asBool()); + EXPECT_EQ("12345", resume_app_list[am::strings::device_id].asString()); + + CheckCommands(resume_app_list[am::strings::application_commands]); + CheckSubmenues(resume_app_list[am::strings::application_submenus]); + CheckChoiceSet(resume_app_list[am::strings::application_choice_sets]); + CheckAppFiles(resume_app_list[am::strings::application_files]); + + CheckGlobalProporties( + resume_app_list[am::strings::application_global_properties]); + CheckSubscriptions(resume_app_list[am::strings::application_subscribtions]); +} + +void ResumptionDataTest::CheckCommands(sm::SmartObject& res_list) { + for (uint32_t i = 0; i < count_of_commands; ++i) { + EXPECT_EQ(i, res_list[i][am::strings::cmd_id].asUInt()); + std::string name = + (*test_commands_map[i])[am::strings::menu_params] + [am::strings::menu_name].asString(); + EXPECT_EQ(name, + res_list[i][am::strings::menu_params][am::strings::menu_name] + .asString()); + int position = + (*test_commands_map[i])[am::strings::menu_params][am::strings::position] + .asInt(); + EXPECT_EQ( + position, + res_list[i][am::strings::menu_params][am::strings::position].asInt()); + + int parent_id = (*test_commands_map[i])[am::strings::menu_params] + [am::hmi_request::parent_id].asInt(); + EXPECT_EQ(parent_id, + res_list[i][am::strings::menu_params][am::hmi_request::parent_id] + .asInt()); + + std::string icon_name = + (*test_commands_map[i])[am::strings::cmd_icon][am::strings::value] + .asString(); + EXPECT_EQ( + icon_name, + res_list[i][am::strings::cmd_icon][am::strings::value].asString()); + + int icon_type = + (*test_commands_map[i])[am::strings::cmd_icon][am::strings::image_type] + .asInt(); + EXPECT_EQ( + icon_type, + res_list[i][am::strings::cmd_icon][am::strings::image_type].asInt()); + + for (uint32_t j = 0; j < count_of_choice; ++j) { + std::string vr = + (*test_commands_map[i])[am::strings::vr_commands][j].asString(); + EXPECT_EQ(vr, res_list[i][am::strings::vr_commands][j].asString()); + } + } +} + +void ResumptionDataTest::CheckSubmenues(sm::SmartObject& res_list) { + for (uint32_t i = 0; i < count_of_submenues; ++i) { + uint32_t test_id = + (*test_submenu_map[i + 10])[am::strings::menu_id].asUInt(); + std::string name = + (*test_submenu_map[i + 10])[am::strings::menu_name].asString(); + int position = (*test_submenu_map[i + 10])[am::strings::position].asInt(); + EXPECT_EQ(position, res_list[i][am::strings::position].asInt()); + EXPECT_EQ(test_id, res_list[i][am::strings::menu_id].asUInt()); + EXPECT_EQ(name, res_list[i][am::strings::menu_name].asString()); + } +} + +void ResumptionDataTest::CheckSubscriptions(sm::SmartObject& res_list) { + EXPECT_EQ(static_cast<uint32_t>(ButtonName::eType::OK), + res_list[am::strings::application_buttons][0].asUInt()); + EXPECT_EQ(static_cast<uint32_t>(ButtonName::eType::CUSTOM_BUTTON), + res_list[am::strings::application_buttons][1].asUInt()); + EXPECT_EQ(0u, res_list[am::strings::application_vehicle_info][0].asUInt()); + EXPECT_EQ(5u, res_list[am::strings::application_vehicle_info][1].asUInt()); +} + +void ResumptionDataTest::CheckChoiceSet(sm::SmartObject& res_list) { + for (uint32_t i = 0; i < res_list.length(); ++i) { + for (uint32_t j = 0; j < res_list[i][am::strings::choice_set].length(); + ++j) { + sm::SmartObject command = res_list[i][am::strings::choice_set][j]; + EXPECT_EQ(i + j, command[am::strings::choice_id].asUInt()); + char numb[12]; + std::snprintf(numb, 12, "%d", i + j); + std::string test_choice = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::vr_commands][0].asString(); + EXPECT_EQ(test_choice, command[am::strings::vr_commands][0].asString()); + std::string menu_name = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::menu_name].asString(); + EXPECT_EQ(menu_name, command[am::strings::menu_name].asString()); + std::string secondary_text = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::secondary_text].asString(); + EXPECT_EQ(secondary_text, + command[am::strings::secondary_text].asString()); + std::string tertiary_text = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::tertiary_text].asString(); + EXPECT_EQ(tertiary_text, command[am::strings::tertiary_text].asString()); + + std::string image_value = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::image][am::strings::value] + .asString(); + EXPECT_EQ(image_value, + command[am::strings::image][am::strings::value].asString()); + int image_type = + (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::image][am::strings::image_type] + .asInt(); + EXPECT_EQ(image_type, + command[am::strings::image][am::strings::image_type].asInt()); + + image_value = (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::secondary_image] + [am::strings::value].asString(); + EXPECT_EQ( + image_value, + command[am::strings::secondary_image][am::strings::value].asString()); + image_type = (*test_choiceset_map[i])[am::strings::choice_set][j] + [am::strings::secondary_image] + [am::strings::image_type].asInt(); + EXPECT_EQ(image_type, + command[am::strings::secondary_image][am::strings::image_type] + .asInt()); + } + + int choice_set_id = + (*test_choiceset_map[i])[am::strings::interaction_choice_set_id] + .asUInt(); + EXPECT_EQ(choice_set_id, + res_list[i][am::strings::interaction_choice_set_id].asInt()); + + int grammar_id = (*test_choiceset_map[i])[am::strings::grammar_id].asUInt(); + EXPECT_EQ(grammar_id, res_list[i][am::strings::grammar_id].asInt()); + } +} + +void ResumptionDataTest::CheckAppFiles(sm::SmartObject& res_list) { + am::AppFile check_file; + + for (uint i = 0; i < count_of_files; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + check_file = app_files_map_["test_file " + std::string(numb)]; + EXPECT_EQ(check_file.file_name, + res_list[i][am::strings::sync_file_name].asString()); + EXPECT_EQ(check_file.file_type, + static_cast<FileType::eType>( + res_list[i][am::strings::file_type].asInt())); + EXPECT_EQ(check_file.is_download_complete, + res_list[i][am::strings::is_download_complete].asBool()); + EXPECT_EQ(check_file.is_persistent, + res_list[i][am::strings::persistent_file].asBool()); + } +} + +void ResumptionDataTest::CheckGlobalProporties(sm::SmartObject& res_list) { + CheckHelpPrompt(res_list[am::strings::help_prompt]); + CheckTimeoutPrompt(res_list[am::strings::timeout_prompt]); + CheckVRHelp(res_list[am::strings::vr_help]); + CheckVRTitle(res_list[am::strings::vr_help_title]); + CheckKeyboardProperties(res_list[am::strings::keyboard_properties]); + CheckMenuTitle(res_list[am::strings::menu_title]); + CheckMenuIcon(res_list[am::strings::menu_icon]); +} + +void ResumptionDataTest::CheckKeyboardProperties(sm::SmartObject& res_list) { + Language::eType testlanguage = static_cast<Language::eType>( + (*keyboard_props_)[am::strings::language].asInt()); + KeyboardLayout::eType testlayout = static_cast<KeyboardLayout::eType>( + (*keyboard_props_)[am::hmi_request::keyboard_layout].asInt()); + KeypressMode::eType testmode = static_cast<KeypressMode::eType>( + (*keyboard_props_)[am::strings::key_press_mode].asInt()); + + std::string auto_complete_text = + (*keyboard_props_)[am::strings::auto_complete_text].asString(); + + for (uint i = 0; + i < (*keyboard_props_)[am::strings::limited_character_list].length(); + i++) { + std::string character = + (*keyboard_props_)[am::strings::limited_character_list][i].asString(); + EXPECT_EQ(character, + res_list[am::strings::limited_character_list][i].asString()); + } + + EXPECT_EQ(testlanguage, static_cast<Language::eType>( + res_list[am::strings::language].asInt())); + EXPECT_EQ(testlayout, + static_cast<KeyboardLayout::eType>( + res_list[am::hmi_request::keyboard_layout].asInt())); + EXPECT_EQ(testmode, static_cast<KeypressMode::eType>( + res_list[am::strings::key_press_mode].asInt())); + EXPECT_EQ(auto_complete_text, + res_list[am::strings::auto_complete_text].asString()); +} + +void ResumptionDataTest::CheckMenuTitle(sm::SmartObject& res_list) { + std::string value = (*menu_title_)[am::strings::menu_title].asString(); + EXPECT_EQ(value, res_list[am::strings::menu_title].asString()); +} + +void ResumptionDataTest::CheckMenuIcon(sm::SmartObject& res_list) { + std::string value = (*menu_icon_)[am::strings::value].asString(); + ImageType::eType type = static_cast<ImageType::eType>( + (*menu_icon_)[am::strings::image_type].asInt()); + + EXPECT_EQ(value, res_list[am::strings::value].asString()); + EXPECT_EQ(type, static_cast<ImageType::eType>( + res_list[am::strings::image_type].asInt())); +} + +void ResumptionDataTest::CheckHelpPrompt(sm::SmartObject& res_list) { + for (uint i = 0; i < tts_chunks_count; ++i) { + std::string promt = (*help_prompt_)[i][am::strings::help_prompt].asString(); + std::string dict_promt = res_list[i][am::strings::help_prompt].asString(); + EXPECT_EQ(promt, dict_promt); + } +} + +void ResumptionDataTest::CheckTimeoutPrompt( + NsSmartDeviceLink::NsSmartObjects::SmartObject& res_list) { + for (uint i = 0; i < tts_chunks_count; ++i) { + std::string text = (*timeout_prompt_)[i][am::strings::text].asString(); + SpeechCapabilities::eType speech = static_cast<SpeechCapabilities::eType>( + (*timeout_prompt_)[i][am::strings::type].asInt()); + EXPECT_EQ(text, res_list[i][am::strings::text].asString()); + EXPECT_EQ(speech, static_cast<SpeechCapabilities::eType>( + res_list[i][am::strings::type].asInt())); + } +} + +void ResumptionDataTest::CheckVRHelp(sm::SmartObject& res_list) { + std::string text; + int position; + for (uint i = 0; i < count_of_vrhelptitle; ++i) { + text = (*vr_help_)[i][am::strings::text].asString(); + EXPECT_EQ(text, res_list[i][am::strings::text].asString()); + position = (*vr_help_)[i][am::strings::position].asInt(); + EXPECT_EQ(position, res_list[i][am::strings::position].asInt()); + } +} + +void ResumptionDataTest::CheckVRTitle( + NsSmartDeviceLink::NsSmartObjects::SmartObject& res_list) { + std::string vtitle = (*vr_help_title_)[am::strings::vr_help_title].asString(); + EXPECT_EQ(vtitle, res_list[am::strings::vr_help_title].asString()); +} + +void ResumptionDataTest::PrepareData() { + SetGlobalProporties(); + SetCommands(); + SetSubmenues(); + SetChoiceSet(); + SetAppFiles(); + + DataAccessor<am::SubMenuMap> sub_menu_m(test_submenu_map, sublock_); + DataAccessor<am::CommandsMap> commands_m(test_commands_map, comlock_); + DataAccessor<am::ChoiceSetMap> choice_set_m(test_choiceset_map, setlock_); + + SetSubscriptions(); + DataAccessor<am::ButtonSubscriptions> btn_sub(btn_subscr, btnlock_); + DataAccessor<am::VehicleInfoSubscriptions> ivi_access(ivi, ivilock_); + + ON_CALL(*app_mock, is_application_data_changed()).WillByDefault(Return(true)); + + ON_CALL(*app_mock, policy_app_id()).WillByDefault(Return(policy_app_id_)); + ON_CALL(*app_mock, curHash()).WillByDefault(ReturnRef(hash_)); + ON_CALL(*app_mock, get_grammar_id()).WillByDefault(Return(grammar_id_)); + ON_CALL(*app_mock, device()).WillByDefault(Return(device_handle_)); + ON_CALL(*app_mock, hmi_level()).WillByDefault(Return(hmi_level_)); + ON_CALL(*app_mock, app_id()).WillByDefault(Return(app_id_)); + ON_CALL(*app_mock, hmi_app_id()).WillByDefault(Return(hmi_app_id_)); + ON_CALL(*app_mock, IsAudioApplication()).WillByDefault(Return(is_audio_)); + ON_CALL(*app_mock, commands_map()).WillByDefault(Return(commands_m)); + ON_CALL(*app_mock, sub_menu_map()).WillByDefault(Return(sub_menu_m)); + ON_CALL(*app_mock, choice_set_map()).WillByDefault(Return(choice_set_m)); + + ON_CALL(*app_mock, help_prompt()).WillByDefault(ReturnPointee(&help_prompt_)); + ON_CALL(*app_mock, timeout_prompt()) + .WillByDefault(ReturnPointee(&timeout_prompt_)); + ON_CALL(*app_mock, vr_help()).WillByDefault(ReturnPointee(&vr_help_)); + ON_CALL(*app_mock, vr_help_title()) + .WillByDefault(ReturnPointee(&vr_help_title_)); + + ON_CALL(*app_mock, keyboard_props()) + .WillByDefault(ReturnPointee(&keyboard_props_)); + ON_CALL(*app_mock, menu_title()).WillByDefault(ReturnPointee(&menu_title_)); + ON_CALL(*app_mock, menu_icon()).WillByDefault(ReturnPointee(&menu_icon_)); + + ON_CALL(*app_mock, SubscribedButtons()).WillByDefault(Return(btn_sub)); + ON_CALL(*app_mock, SubscribedIVI()).WillByDefault(Return(ivi_access)); + + ON_CALL(*app_mock, getAppFiles()).WillByDefault(ReturnRef(app_files_map_)); +} + +void ResumptionDataTest::SetGlobalProporties() { + SetKeyboardProperties(); + SetMenuTitleAndIcon(); + SetHelpAndTimeoutPrompt(); + SetVRHelpTitle(); +} + +void ResumptionDataTest::SetMenuTitleAndIcon() { + sm::SmartObject sm_icon; + sm_icon[am::strings::value] = "test icon"; + sm_icon[am::strings::image_type] = ImageType::STATIC; + + sm::SmartObject sm_title; + sm_title = "test title"; + menu_title_ = new sm::SmartObject(sm_title); + menu_icon_ = new sm::SmartObject(sm_icon); +} + +void ResumptionDataTest::SetHelpAndTimeoutPrompt() { + sm::SmartObject help_prompt; + sm::SmartObject timeout_prompt; + + for (uint i = 0; i < tts_chunks_count + 3; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + help_prompt[i][am::strings::text] = "help prompt name" + std::string(numb); + help_prompt[i][am::strings::type] = SpeechCapabilities::PRE_RECORDED; + } + help_prompt_ = new sm::SmartObject(help_prompt); + for (uint i = 0; i < tts_chunks_count; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + timeout_prompt[i][am::strings::text] = "timeout test" + std::string(numb); + timeout_prompt[i][am::strings::type] = SpeechCapabilities::SC_TEXT; + } + + timeout_prompt_ = new sm::SmartObject(timeout_prompt); +} + +void ResumptionDataTest::SetVRHelpTitle() { + sm::SmartObject vr_help_title; + vr_help_title = "vr help title"; + + sm::SmartObject vr_help; + for (uint i = 0; i < count_of_vrhelptitle; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + vr_help[i][am::strings::text] = "vr help " + std::string(numb); + vr_help[i][am::strings::position] = i; + } + + vr_help_ = new sm::SmartObject(vr_help); + vr_help_title_ = new sm::SmartObject(vr_help_title); +} + +void ResumptionDataTest::SetCommands() { + sm::SmartObject sm_comm; + + sm::SmartObject vr_commandsvector; + sm::SmartObject sm_icon; + for (uint32_t i = 0; i < count_of_commands; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + sm_comm[am::strings::cmd_id] = i; + sm_comm[am::strings::menu_params][am::strings::position] = i; + sm_comm[am::strings::menu_params][am::hmi_request::parent_id] = i; + sm_comm[am::strings::menu_params][am::strings::menu_name] = + "Command" + std::string(numb); + + for (uint32_t j = 0; j < count_of_choice; ++j) { + char vr[12]; + std::snprintf(vr, 12, "%d", i + j); + vr_commandsvector[j] = "VrCommand " + std::string(vr); + } + sm_comm[am::strings::vr_commands] = vr_commandsvector; + + sm_icon[am::strings::value] = "command icon " + std::string(numb); + sm_icon[am::strings::image_type] = ImageType::STATIC; + + sm_comm[am::strings::cmd_icon] = sm_icon; + + test_commands_map[i] = new sm::SmartObject(sm_comm); + } +} + +void ResumptionDataTest::SetSubmenues() { + sm::SmartObject sm_comm; + for (uint32_t i = 10; i < count_of_submenues + 10; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + sm_comm[am::strings::menu_id] = i; + sm_comm[am::strings::position] = i; + sm_comm[am::strings::menu_name] = "SubMenu" + std::string(numb); + test_submenu_map[i] = new sm::SmartObject(sm_comm); + } +} + +void ResumptionDataTest::SetChoiceSet() { + sm::SmartObject choice_vector; + sm::SmartObject choice; + sm::SmartObject vr_commandsvector; + sm::SmartObject sm_icon; + sm::SmartObject sec_icon; + sm::SmartObject app_choice_set; + sm::SmartObject application_choice_sets; + for (uint32_t i = 0; i < count_of_choice_sets; ++i) { + for (uint32_t j = 0; j < count_of_choice; ++j) { + char numb[12]; + std::snprintf(numb, 12, "%d", i + j); + + choice[am::strings::choice_id] = i + j; + vr_commandsvector[0] = "ChoiceSet VrCommand " + std::string(numb); + choice[am::strings::vr_commands] = vr_commandsvector; + choice[am::strings::menu_name] = "Choice name " + std::string(numb); + choice[am::strings::secondary_text] = + "secondaryText " + std::string(numb); + choice[am::strings::tertiary_text] = "tertiaryText " + std::string(numb); + + sm_icon[am::strings::value] = "Choiceset icon " + std::string(numb); + sm_icon[am::strings::image_type] = ImageType::STATIC; + + sec_icon[am::strings::value] = + "Second choiceset icon " + std::string(numb); + sec_icon[am::strings::image_type] = ImageType::STATIC; + + choice[am::strings::image] = sm_icon; + choice[am::strings::secondary_image] = sec_icon; + + choice_vector[j] = choice; + } + app_choice_set[am::strings::interaction_choice_set_id] = i; + app_choice_set[am::strings::grammar_id] = 100 + i; + app_choice_set[am::strings::choice_set] = choice_vector; + application_choice_sets[i] = app_choice_set; + + test_choiceset_map[i] = new sm::SmartObject(application_choice_sets[i]); + } +} + +void ResumptionDataTest::SetAppFiles() { + am::AppFile test_file; + int file_types; + for (uint i = 0; i < count_of_files; ++i) { + char numb[12]; + std::snprintf(numb, 12, "%d", i); + file_types = i; + test_file.is_persistent = true; + test_file.is_download_complete = true; + test_file.file_type = static_cast<FileType::eType>(file_types); + test_file.file_name = "test_file " + std::string(numb); + + app_files_map_[test_file.file_name] = test_file; + } +} + +void ResumptionDataTest::SetKeyboardProperties() { + sm::SmartObject keyboard; + keyboard[am::strings::language] = Language::EN_US; + keyboard[am::hmi_request::keyboard_layout] = KeyboardLayout::QWERTY; + keyboard[am::strings::key_press_mode] = KeypressMode::SINGLE_KEYPRESS; + keyboard[am::strings::auto_complete_text] = "complete"; + keyboard[am::strings::limited_character_list][0] = "y"; + keyboard[am::strings::limited_character_list][1] = "n"; + keyboard_props_ = new sm::SmartObject(keyboard); +} + +void ResumptionDataTest::SetSubscriptions() { + btn_subscr.insert(ButtonName::eType::CUSTOM_BUTTON); + btn_subscr.insert(ButtonName::eType::OK); + ivi.insert(0); + ivi.insert(5); +} + +} // namespace resumption_test +} // namespace components +} // namespace test diff --git a/src/components/application_manager/test/resumption/smartDeviceLink_test.ini b/src/components/application_manager/test/resumption/smartDeviceLink_test.ini new file mode 100644 index 0000000000..cf51439d8c --- /dev/null +++ b/src/components/application_manager/test/resumption/smartDeviceLink_test.ini @@ -0,0 +1,37 @@ +[MAIN] + +; For resume_ctrl tests +LogsEnabled = false +; Contains .json/.ini files +AppConfigFolder = +; Contains output files, e.g. .wav. Changed for tests +AppStorageFolder = test_storage + +[AppInfo] +; The path for applications info storage. Changed for tests +AppInfoStorage = test_app_info.dat + +[Resumption] + +# Timeout in milliseconds for resumption Application HMILevel +# and resolving conflicts in case if multiple applications initiate resumption +# Time changed for test onAppActivated +ApplicationResumingTimeout = 30000000 + +# Timeout in milliseconds for periodical saving resumption persistent data +AppSavePersistentDataTimeout = 10000 + +# Timeout in seconds to store hmi_level for media app before ign_off +ResumptionDelayBeforeIgn = 30; + +# Timeout in seconds to restore hmi_level for media app after sdl run +ResumptionDelayAfterIgn = 30; + +# Resumption ctrl uses JSON if UseDBForResumption=false for store data otherwise uses DB +UseDBForResumption = false + +# Number of attempts to open resumption DB +AttemptsToOpenResumptionDB = 5 + +# Timeout between attempts during opening DB in milliseconds +OpenAttemptTimeoutMsResumptionDB = 500 |