diff options
Diffstat (limited to 'src/components/application_manager/test/application_manager_impl_test.cc')
-rw-r--r-- | src/components/application_manager/test/application_manager_impl_test.cc | 344 |
1 files changed, 328 insertions, 16 deletions
diff --git a/src/components/application_manager/test/application_manager_impl_test.cc b/src/components/application_manager/test/application_manager_impl_test.cc index d19d3cd8ed..128d8e87a7 100644 --- a/src/components/application_manager/test/application_manager_impl_test.cc +++ b/src/components/application_manager/test/application_manager_impl_test.cc @@ -32,6 +32,8 @@ #include <stdint.h> #include <memory> #include <set> +#include <string> +#include <vector> #include <bson_object.h> #include "gtest/gtest.h" @@ -39,12 +41,14 @@ #include "application_manager/application_impl.h" #include "application_manager/application_manager_impl.h" #include "application_manager/mock_application.h" +#include "application_manager/mock_help_prompt_manager.h" #include "application_manager/mock_application_manager_settings.h" #include "application_manager/mock_resumption_data.h" #include "application_manager/resumption/resume_ctrl_impl.h" #include "application_manager/test/include/application_manager/mock_message_helper.h" #include "connection_handler/mock_connection_handler.h" #include "hmi_message_handler/mock_hmi_message_handler.h" +#include "media_manager/mock_media_manager.h" #include "policy/mock_policy_settings.h" #include "policy/usage_statistics/mock_statistics_manager.h" #include "protocol/bson_object_keys.h" @@ -54,6 +58,7 @@ #include "utils/lock.h" #include "utils/make_shared.h" #include "utils/push_log.h" +#include "encryption/hashing.h" namespace test { namespace components { @@ -64,6 +69,7 @@ namespace policy_test = test::components::policy_handler_test; namespace con_test = connection_handler_test; using testing::_; +using ::testing::Matcher; using ::testing::ByRef; using ::testing::DoAll; using ::testing::Mock; @@ -83,8 +89,7 @@ ACTION_P6(InvokeMemberFuncWithArg4, ptr, memberFunc, a, b, c, d) { namespace { const std::string kDirectoryName = "./test_storage"; const uint32_t kTimeout = 10000u; -sync_primitives::Lock state_lock_; -sync_primitives::ConditionalVariable state_condition_; +connection_handler::DeviceHandle kDeviceId = 12345u; } // namespace class ApplicationManagerImplTest : public ::testing::Test { @@ -92,7 +97,7 @@ class ApplicationManagerImplTest : public ::testing::Test { ApplicationManagerImplTest() : mock_storage_( ::utils::MakeShared<NiceMock<resumption_test::MockResumptionData> >( - app_mngr_)) + mock_app_mngr_)) , mock_message_helper_( application_manager::MockMessageHelper::message_helper_mock()) , app_id_(0u) { @@ -106,8 +111,7 @@ class ApplicationManagerImplTest : public ::testing::Test { protected: void SetUp() OVERRIDE { CreateAppManager(); - - ON_CALL(mock_connection_handler_, GetDataOnSessionKey(_, _, _, _)) + ON_CALL(mock_connection_handler_, GetDataOnSessionKey(_, _, _, &kDeviceId)) .WillByDefault(DoAll(SetArgPointee<3u>(app_id_), Return(0))); ON_CALL(mock_connection_handler_, get_session_observer()) .WillByDefault(ReturnRef(mock_session_observer_)); @@ -128,14 +132,17 @@ class ApplicationManagerImplTest : public ::testing::Test { ON_CALL(mock_application_manager_settings_, app_storage_folder()) .WillByDefault(ReturnRef(kDirectoryName)); ON_CALL(mock_application_manager_settings_, launch_hmi()) - .WillByDefault(Return(true)); + .WillByDefault(Return(false)); ON_CALL(mock_application_manager_settings_, stop_streaming_timeout()) .WillByDefault(Return(stop_streaming_timeout)); ON_CALL(mock_application_manager_settings_, default_timeout()) .WillByDefault(ReturnRef(kTimeout)); + app_manager_impl_.reset(new am::ApplicationManagerImpl( mock_application_manager_settings_, mock_policy_settings_)); mock_app_ptr_ = utils::SharedPtr<MockApplication>(new MockApplication()); + mock_help_prompt_manager_ = + utils::SharedPtr<MockHelpPromptManager>(new MockHelpPromptManager()); ASSERT_TRUE(app_manager_impl_.get()); ASSERT_TRUE(mock_app_ptr_.get()); @@ -145,18 +152,44 @@ class ApplicationManagerImplTest : public ::testing::Test { app_manager_impl_->AddMockApplication(mock_app_ptr_); } + void SetCommonExpectationOnAppReconnection( + const connection_handler::DeviceHandle new_device_id, + const uint32_t new_application_id, + const std::string& mac_address) { + EXPECT_CALL( + mock_session_observer_, + GetDataOnSessionKey(new_application_id, + _, + _, + testing::An<connection_handler::DeviceHandle*>())) + .WillOnce(DoAll(SetArgPointee<3u>(new_device_id), Return(0))); + + const std::string connection_type = "MyConnectionType"; + EXPECT_CALL( + mock_session_observer_, + GetDataOnDeviceID( + ::testing::Matcher<connection_handler::DeviceHandle>(new_device_id), + _, + _, + _, + _)) + .WillOnce(DoAll(SetArgPointee<3u>(mac_address), + SetArgPointee<4u>(connection_type), + Return(0))); + } + NiceMock<policy_test::MockPolicySettings> mock_policy_settings_; utils::SharedPtr<NiceMock<resumption_test::MockResumptionData> > mock_storage_; NiceMock<con_test::MockConnectionHandler> mock_connection_handler_; NiceMock<protocol_handler_test::MockSessionObserver> mock_session_observer_; NiceMock<MockApplicationManagerSettings> mock_application_manager_settings_; - application_manager_test::MockApplicationManager app_mngr_; + application_manager_test::MockApplicationManager mock_app_mngr_; std::auto_ptr<am::ApplicationManagerImpl> app_manager_impl_; application_manager::MockMessageHelper* mock_message_helper_; uint32_t app_id_; - application_manager::MessageHelper* message_helper_; utils::SharedPtr<MockApplication> mock_app_ptr_; + utils::SharedPtr<MockHelpPromptManager> mock_help_prompt_manager_; }; TEST_F(ApplicationManagerImplTest, ProcessQueryApp_ExpectSuccess) { @@ -181,16 +214,20 @@ TEST_F(ApplicationManagerImplTest, ProcessQueryApp_ExpectSuccess) { TEST_F(ApplicationManagerImplTest, SubscribeAppForWayPoints_ExpectSubscriptionApp) { - app_manager_impl_->SubscribeAppForWayPoints(app_id_); - EXPECT_TRUE(app_manager_impl_->IsAppSubscribedForWayPoints(app_id_)); + auto app_ptr = + ApplicationSharedPtr::static_pointer_cast<am::Application>(mock_app_ptr_); + app_manager_impl_->SubscribeAppForWayPoints(app_ptr); + EXPECT_TRUE(app_manager_impl_->IsAppSubscribedForWayPoints(app_ptr)); } TEST_F(ApplicationManagerImplTest, UnsubscribeAppForWayPoints_ExpectUnsubscriptionApp) { - app_manager_impl_->SubscribeAppForWayPoints(app_id_); - EXPECT_TRUE(app_manager_impl_->IsAppSubscribedForWayPoints(app_id_)); - app_manager_impl_->UnsubscribeAppFromWayPoints(app_id_); - EXPECT_FALSE(app_manager_impl_->IsAppSubscribedForWayPoints(app_id_)); + auto app_ptr = + ApplicationSharedPtr::static_pointer_cast<am::Application>(mock_app_ptr_); + app_manager_impl_->SubscribeAppForWayPoints(app_ptr); + EXPECT_TRUE(app_manager_impl_->IsAppSubscribedForWayPoints(app_ptr)); + app_manager_impl_->UnsubscribeAppFromWayPoints(app_ptr); + EXPECT_FALSE(app_manager_impl_->IsAppSubscribedForWayPoints(app_ptr)); const std::set<int32_t> result = app_manager_impl_->GetAppsSubscribedForWayPoints(); EXPECT_TRUE(result.empty()); @@ -207,10 +244,12 @@ TEST_F( TEST_F( ApplicationManagerImplTest, GetAppsSubscribedForWayPoints_SubcribeAppForWayPoints_ExpectCorrectResult) { - app_manager_impl_->SubscribeAppForWayPoints(app_id_); + auto app_ptr = + ApplicationSharedPtr::static_pointer_cast<am::Application>(mock_app_ptr_); + app_manager_impl_->SubscribeAppForWayPoints(app_ptr); std::set<int32_t> result = app_manager_impl_->GetAppsSubscribedForWayPoints(); EXPECT_EQ(1u, result.size()); - EXPECT_TRUE(result.find(app_id_) != result.end()); + EXPECT_TRUE(result.find(app_ptr) != result.end()); } TEST_F(ApplicationManagerImplTest, OnServiceStartedCallback_RpcService) { @@ -639,6 +678,279 @@ TEST_F(ApplicationManagerImplTest, EXPECT_TRUE(rejected_params.empty()); } +TEST_F(ApplicationManagerImplTest, + OnDeviceSwitchingStart_ExpectPutAppsInWaitList) { + utils::SharedPtr<MockApplication> switching_app_ptr = + utils::MakeShared<MockApplication>(); + + const std::string switching_device_id = "switching"; + const std::string switching_device_id_hash = + encryption::MakeHash(switching_device_id); + app_manager_impl_->AddMockApplication(switching_app_ptr); + EXPECT_CALL(*switching_app_ptr, mac_address()) + .WillRepeatedly(ReturnRef(switching_device_id_hash)); + + const std::string policy_app_id_switch = "abc"; + EXPECT_CALL(*switching_app_ptr, policy_app_id()) + .WillRepeatedly(Return(policy_app_id_switch)); + + const auto hmi_level_switching_app = mobile_apis::HMILevel::HMI_FULL; + EXPECT_CALL(*switching_app_ptr, hmi_level()) + .WillRepeatedly(Return(hmi_level_switching_app)); + + utils::SharedPtr<MockApplication> nonswitching_app_ptr = + utils::MakeShared<MockApplication>(); + + const std::string nonswitching_device_id = "nonswitching"; + const std::string nonswitching_device_id_hash = + encryption::MakeHash(nonswitching_device_id); + app_manager_impl_->AddMockApplication(nonswitching_app_ptr); + EXPECT_CALL(*nonswitching_app_ptr, mac_address()) + .WillRepeatedly(ReturnRef(nonswitching_device_id_hash)); + + const std::string policy_app_id_nonswitch = "efg"; + EXPECT_CALL(*nonswitching_app_ptr, policy_app_id()) + .WillRepeatedly(Return(policy_app_id_nonswitch)); + + const auto hmi_level_nonswitching_app = mobile_apis::HMILevel::HMI_LIMITED; + EXPECT_CALL(*nonswitching_app_ptr, hmi_level()) + .WillRepeatedly(Return(hmi_level_nonswitching_app)); + + // Act + const connection_handler::DeviceHandle switching_handle = 1; + const connection_handler::Device switching_device( + switching_handle, "switching_device", switching_device_id, "BLUETOOTH"); + + const connection_handler::DeviceHandle non_switching_handle = 2; + const connection_handler::Device non_switching_device(non_switching_handle, + "non_switching_device", + nonswitching_device_id, + "USB"); + + EXPECT_CALL(*mock_message_helper_, CreateDeviceListSO(_, _, _)) + .WillOnce(Return(smart_objects::SmartObjectSPtr())); + app_manager_impl_->OnDeviceSwitchingStart(switching_device, + non_switching_device); + EXPECT_TRUE(app_manager_impl_->IsAppInReconnectMode(policy_app_id_switch)); + EXPECT_FALSE( + app_manager_impl_->IsAppInReconnectMode(policy_app_id_nonswitch)); +} + +TEST_F(ApplicationManagerImplTest, + OnDeviceSwitchingFinish_ExpectUnregisterAppsInWaitList) { + utils::SharedPtr<MockApplication> switching_app_ptr = + utils::MakeShared<MockApplication>(); + + const std::string switching_device_id = "switching"; + const std::string switching_device_id_hash = + encryption::MakeHash(switching_device_id); + app_manager_impl_->AddMockApplication(switching_app_ptr); + EXPECT_CALL(*switching_app_ptr, mac_address()) + .WillRepeatedly(ReturnRef(switching_device_id_hash)); + + const std::string policy_app_id_switch = "abc"; + EXPECT_CALL(*switching_app_ptr, policy_app_id()) + .WillRepeatedly(Return(policy_app_id_switch)); + + const auto hmi_level_switching_app = mobile_apis::HMILevel::HMI_FULL; + EXPECT_CALL(*switching_app_ptr, hmi_level()) + .WillRepeatedly(Return(hmi_level_switching_app)); + + utils::SharedPtr<MockApplication> nonswitching_app_ptr = + utils::MakeShared<MockApplication>(); + + const std::string nonswitching_device_id = "nonswitching"; + const std::string nonswitching_device_id_hash = + encryption::MakeHash(nonswitching_device_id); + app_manager_impl_->AddMockApplication(nonswitching_app_ptr); + EXPECT_CALL(*nonswitching_app_ptr, mac_address()) + .WillRepeatedly(ReturnRef(nonswitching_device_id_hash)); + + const std::string policy_app_id_nonswitch = "efg"; + EXPECT_CALL(*nonswitching_app_ptr, policy_app_id()) + .WillRepeatedly(Return(policy_app_id_nonswitch)); + + const auto hmi_level_nonswitching_app = mobile_apis::HMILevel::HMI_LIMITED; + EXPECT_CALL(*nonswitching_app_ptr, hmi_level()) + .WillRepeatedly(Return(hmi_level_nonswitching_app)); + + // Act + const connection_handler::DeviceHandle switching_handle = 1; + const connection_handler::Device switching_device( + switching_handle, "switching_device", switching_device_id, "BLUETOOTH"); + + const connection_handler::DeviceHandle non_switching_handle = 2; + const connection_handler::Device non_switching_device(non_switching_handle, + "non_switching_device", + nonswitching_device_id, + "USB"); + + EXPECT_CALL(*mock_message_helper_, CreateDeviceListSO(_, _, _)) + .WillOnce(Return(smart_objects::SmartObjectSPtr())); + + app_manager_impl_->OnDeviceSwitchingStart(switching_device, + non_switching_device); + + EXPECT_TRUE(app_manager_impl_->IsAppInReconnectMode(policy_app_id_switch)); + + EXPECT_CALL(*switching_app_ptr, help_prompt_manager()) + .WillOnce(ReturnRef(*mock_help_prompt_manager_.get())); + + app_manager_impl_->OnDeviceSwitchingFinish(switching_device_id); + EXPECT_FALSE( + app_manager_impl_->application_by_policy_id(policy_app_id_switch)); +} + +TEST_F(ApplicationManagerImplTest, + ProcessReconnection_ExpectChangeAppIdDeviceId) { + const uint32_t application_id = 1; + const std::string policy_app_id = "p_app_id"; + const std::string mac_address = "MA:CA:DD:RE:SS"; + const connection_handler::DeviceHandle device_id = 1; + const custom_str::CustomString app_name(""); + + utils::SharedPtr<ApplicationImpl> app_impl = new ApplicationImpl( + application_id, + policy_app_id, + mac_address, + device_id, + app_name, + utils::SharedPtr<usage_statistics::StatisticsManager>( + new usage_statistics_test::MockStatisticsManager()), + *app_manager_impl_); + + app_manager_impl_->AddMockApplication(app_impl); + + const connection_handler::DeviceHandle new_device_id = 2; + const uint32_t new_application_id = 2; + SetCommonExpectationOnAppReconnection( + new_device_id, new_application_id, mac_address); + + // Act + app_manager_impl_->ProcessReconnection(app_impl, new_application_id); + EXPECT_EQ(new_device_id, app_impl->device()); + EXPECT_EQ(new_application_id, app_impl->app_id()); +} + +TEST_F(ApplicationManagerImplTest, StartStopAudioPassThru) { + std::string dummy_file_name; + ON_CALL(mock_application_manager_settings_, recording_file_name()) + .WillByDefault(ReturnRef(dummy_file_name)); + + NiceMock<test::components::media_manager_test::MockMediaManager> + mock_media_manager; + app_manager_impl_->SetMockMediaManager(&mock_media_manager); + + const uint32_t app_id = 65537; + const int32_t max_duration = 1000; + // below are not used + const int32_t correlation_id = 0; + const int32_t sampling_rate = 0; + const int32_t bits_per_sample = 0; + const int32_t audio_type = 0; + + EXPECT_CALL(mock_media_manager, + StartMicrophoneRecording(app_id, _, max_duration)) + .WillOnce(Return()); + EXPECT_CALL(mock_media_manager, StopMicrophoneRecording(app_id)) + .WillOnce(Return()); + + bool result = app_manager_impl_->BeginAudioPassThru(app_id); + EXPECT_TRUE(result); + if (result) { + app_manager_impl_->StartAudioPassThruThread(app_id, + correlation_id, + max_duration, + sampling_rate, + bits_per_sample, + audio_type); + } + + result = app_manager_impl_->EndAudioPassThru(app_id); + EXPECT_TRUE(result); + if (result) { + app_manager_impl_->StopAudioPassThru(app_id); + } +} + +TEST_F(ApplicationManagerImplTest, UnregisterAnotherAppDuringAudioPassThru) { + std::string dummy_file_name; + ON_CALL(mock_application_manager_settings_, recording_file_name()) + .WillByDefault(ReturnRef(dummy_file_name)); + + const uint32_t app_id_1 = 65537; + const uint32_t app_id_2 = 65538; + + std::string dummy_mac_address; + utils::SharedPtr<MockApplication> mock_app_1 = + utils::SharedPtr<MockApplication>(new MockApplication()); + EXPECT_CALL(*mock_app_1, app_id()).WillRepeatedly(Return(app_id_1)); + EXPECT_CALL(*mock_app_1, device()).WillRepeatedly(Return(0)); + EXPECT_CALL(*mock_app_1, mac_address()) + .WillRepeatedly(ReturnRef(dummy_mac_address)); + EXPECT_CALL(*mock_app_1, policy_app_id()).WillRepeatedly(Return("")); + EXPECT_CALL(*mock_app_1, protocol_version()) + .WillRepeatedly( + Return(protocol_handler::MajorProtocolVersion::PROTOCOL_VERSION_4)); + + utils::SharedPtr<MockApplication> mock_app_2 = + utils::SharedPtr<MockApplication>(new MockApplication()); + EXPECT_CALL(*mock_app_2, app_id()).WillRepeatedly(Return(app_id_2)); + EXPECT_CALL(*mock_app_2, device()).WillRepeatedly(Return(0)); + EXPECT_CALL(*mock_app_2, mac_address()) + .WillRepeatedly(ReturnRef(dummy_mac_address)); + EXPECT_CALL(*mock_app_2, policy_app_id()).WillRepeatedly(Return("")); + EXPECT_CALL(*mock_app_2, protocol_version()) + .WillRepeatedly( + Return(protocol_handler::MajorProtocolVersion::PROTOCOL_VERSION_4)); + + NiceMock<test::components::media_manager_test::MockMediaManager> + mock_media_manager; + app_manager_impl_->SetMockMediaManager(&mock_media_manager); + + app_manager_impl_->AddMockApplication(mock_app_1); + app_manager_impl_->AddMockApplication(mock_app_2); + + const int32_t max_duration = 1000; + // below are not used + const int32_t correlation_id = 0; + const int32_t sampling_rate = 0; + const int32_t bits_per_sample = 0; + const int32_t audio_type = 0; + + EXPECT_CALL(mock_media_manager, + StartMicrophoneRecording(app_id_2, _, max_duration)) + .WillOnce(Return()); + EXPECT_CALL(mock_media_manager, StopMicrophoneRecording(app_id_2)) + .WillOnce(Return()); + + // app 2 starts Audio Pass Thru + bool result = app_manager_impl_->BeginAudioPassThru(app_id_2); + EXPECT_TRUE(result); + if (result) { + app_manager_impl_->StartAudioPassThruThread(app_id_2, + correlation_id, + max_duration, + sampling_rate, + bits_per_sample, + audio_type); + } + + EXPECT_CALL(*mock_app_1, help_prompt_manager()) + .WillOnce(ReturnRef(*mock_help_prompt_manager_.get())); + + // while running APT, app 1 is unregistered + app_manager_impl_->UnregisterApplication( + app_id_1, mobile_apis::Result::SUCCESS, false, true); + + // confirm that APT is still running + result = app_manager_impl_->EndAudioPassThru(app_id_2); + EXPECT_TRUE(result); + if (result) { + app_manager_impl_->StopAudioPassThru(app_id_2); + } +} + } // application_manager_test } // namespace components } // namespace test |