JD2022-TU1/main/extern/Hue/libhuestream/test/TestConnectionFlowNewBridge.cpp

311 lines
No EOL
12 KiB
C++

#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <memory>
#include <huestream/connect/ConnectionFlow.h>
#include "MockBridgeAuthenticator.h"
#include "MockBridgeSearcher.h"
#include "TestableConnectionFlow.h"
#include "MockConnectionFlowFactory.h"
#include "MockBridgeStorageAccesser.h"
#include "MessageDispatcherStub.h"
#include "TestConnectionFlowBase.h"
#include "huestream/config/HueConfig.h"
using namespace testing;
using namespace huestream;
using namespace huestream::connectionflow;
class TestConnectionFlow_NewBridge: public TestConnectionFlowBase {
public:
void connect_with_no_bridge_configured_starts_new_bridge_search(bool background = false) {
expect_message(PROCEDURE_STARTED, INFO);
expect_message(START_LOADING, INFO);
expect_on_storage_accesser_load();
if (background) {
_connectionFlow->ConnectToBridgeBackground();
} else {
_connectionFlow->ConnectToBridge();
}
_messageDispatcher->ExecutePendingActions();
expect_on_searcher_search_new(false);
expect_message(FINISH_LOADING_NO_BRIDGE_CONFIGURED, INFO, make_shared<Bridge>());
expect_message(START_SEARCHING, INFO);
_storageAccesser->load_callback(OPERATION_FAILED, make_shared<Bridge>());
_messageDispatcher->ExecutePendingActions();
}
void connect_with_background_result_starts_pushlink(int index) {
expect_message(PROCEDURE_STARTED, INFO);
expect_message(START_AUTHORIZING, INFO);
expect_message(PRESS_PUSH_LINK, USER);
expect_on_authenticator_authenticate(index);
_connectionFlow->ConnectToBridge();
_messageDispatcher->ExecutePendingActions();
}
void connect_background_with_no_bridge_configured_starts_new_bridge_search() {
expect_message(PROCEDURE_STARTED, INFO);
expect_message(START_LOADING, INFO);
expect_on_storage_accesser_load();
_connectionFlow->ConnectToBridge();
_messageDispatcher->ExecutePendingActions();
expect_on_searcher_search_new(false);
expect_message(FINISH_LOADING_NO_BRIDGE_CONFIGURED, INFO, make_shared<Bridge>());
expect_message(START_SEARCHING, INFO);
_storageAccesser->load_callback(OPERATION_FAILED, make_shared<Bridge>());
_messageDispatcher->ExecutePendingActions();
}
void searching_with_one_bridge_found_but_is_ignored() {
auto searcher = get_last_searcher();
expect_message(FINISH_SEARCHING_NO_BRIDGES_FOUND, INFO);
expect_message(START_SEARCHING, INFO);
expect_on_searcher_search_new(true);
finish_search_with_one_bridge(searcher, 0);
}
void finish_authorization_unsuccessfully_start_new_authentication(int index) {
auto authenticator = _authenticators->at(index);
expect_on_authenticator_authenticate(index);
authenticator->autenticate_callback(_bridges->at(index));
_messageDispatcher->ExecutePendingActions();
}
void authenticate_connection_failure(int index) {
auto callback = _authenticators->at(index);
_bridges->at(index)->SetIsValidIp(false);
callback->autenticate_callback(_bridges->at(index));
_messageDispatcher->ExecutePendingActions();
}
void finish_authorization_with_connection_failure_ignores_bridge(int index) {
expect_no_actions();
authenticate_connection_failure(index);
}
void finish_authorization_with_connection_failure_ignores_bridge_and_starts_new_search(int index) {
auto callback = _authenticators->at(index);
expect_on_searcher_search_new(false);
expect_message(FINISH_AUTHORIZING_FAILED, INFO);
expect_message(START_SEARCHING, INFO);
authenticate_connection_failure(index);
}
void finish_authorization_when_another_bridge_already_authorized_ignores_bridge(int index) {
authorize_bridge(index);
expect_no_actions();
_authenticators->at(index)->autenticate_callback(_bridges->at(index));
_messageDispatcher->ExecutePendingActions();
}
void connect_to_bridge_successfully(int index, int numGroups = 1) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_one_bridge_found_starts_pushlink();
finish_authorization_successfully_and_retrieve_full_config(index, numGroups);
if (numGroups == 1) {
finish(_bridges->at(index));
} else {
finish_without_stream_start(false);
}
}
void expect_successful_start_stream_messages(int index) {
expect_message(PROCEDURE_STARTED, INFO);
expect_message(START_ACTIVATING, INFO);
expect_message(FINISH_ACTIVATING_ACTIVE, INFO, _bridges->at(index), BRIDGE_OK);
expect_message(DONE_COMPLETED, INFO);
expect_message(PROCEDURE_FINISHED, INFO);
}
};
TEST_F(TestConnectionFlow_NewBridge, searching_can_be_aborted) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_once_with_no_bridges_found_starts_new_bridge_search();
searching_with_abort_finalizes();
}
INSTANTIATE_TEST_CASE_P(search_twice_and_authenticate_with_multiple_bridges_found,
TestConnectionFlow_NewBridge, Values(0, 1, 2));
TEST_P(TestConnectionFlow_NewBridge, search_twice_and_authenticate_with_multiple_bridges_found) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_once_with_no_bridges_found_starts_new_bridge_search();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_successfully_and_retrieve_full_config(GetParam());
finish(_bridges->at(GetParam()));
}
INSTANTIATE_TEST_CASE_P(search_once_and_authenticate_single_new_bridge,
TestConnectionFlow_NewBridge, Values(0, 1, 2));
TEST_P(TestConnectionFlow_NewBridge, search_once_and_authenticate_single_new_bridge) {
connect_to_bridge_successfully(0, GetParam());
}
INSTANTIATE_TEST_CASE_P(connect_succesfully_with_or_without_stream_start,
TestConnectionFlow_NewBridge, Values(0, 1));
TEST_P(TestConnectionFlow_NewBridge, connect_succesfully_with_or_without_stream_start) {
set_autostart_based_on_param(GetParam());
connect_to_bridge_successfully(0, 1);
}
TEST_F(TestConnectionFlow_NewBridge, ignores_bridge_from_searcher_with_invalid_ip) {
connect_with_no_bridge_configured_starts_new_bridge_search();
_bridges->at(0)->SetIsValidIp(false);
searching_with_one_bridge_found_but_is_ignored();
}
TEST_F(TestConnectionFlow_NewBridge, search_twice_finish_with_no_bridges_found) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_once_with_no_bridges_found_starts_new_bridge_search();
searching_twice_with_no_bridges_found_finalizes();
}
TEST_F(TestConnectionFlow_NewBridge, authentication_repeats_until_autenticated) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_one_bridge_found_starts_pushlink();
finish_authorization_unsuccessfully_start_new_authentication(0);
finish_authorization_unsuccessfully_start_new_authentication(0);
finish_authorization_unsuccessfully_start_new_authentication(0);
finish_authorization_successfully_and_retrieve_full_config(0);
}
TEST_F(TestConnectionFlow_NewBridge, authentication_repeats_until_abort) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_one_bridge_found_starts_pushlink();
finish_authorization_unsuccessfully_start_new_authentication(0);
abort_finalizes();
}
TEST_F(TestConnectionFlow_NewBridge, authentication_repeats_until_abort_multiple_bridges) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_unsuccessfully_start_new_authentication(0);
finish_authorization_unsuccessfully_start_new_authentication(1);
finish_authorization_unsuccessfully_start_new_authentication(2);
abort_finalizes();
}
TEST_F(TestConnectionFlow_NewBridge, authentication_connection_failure_triggers_new_search) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_one_bridge_found_starts_pushlink();
finish_authorization_with_connection_failure_ignores_bridge_and_starts_new_search(0);
}
TEST_F(TestConnectionFlow_NewBridge, authentication_connection_failure_multiple_bridges_triggers_new_search) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_with_connection_failure_ignores_bridge(0);
finish_authorization_with_connection_failure_ignores_bridge(1);
finish_authorization_with_connection_failure_ignores_bridge_and_starts_new_search(2);
}
TEST_F(TestConnectionFlow_NewBridge, authentication_connection_failure_and_success) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_with_connection_failure_ignores_bridge(0);
finish_authorization_unsuccessfully_start_new_authentication(1);
finish_authorization_successfully_and_retrieve_full_config(2);
}
TEST_F(TestConnectionFlow_NewBridge, multiple_authentications_only_returns_one_bridge) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_successfully(0);
finish_authorization_when_another_bridge_already_authorized_ignores_bridge(1);
retrieve_full_config(0, 1);
}
TEST_F(TestConnectionFlow_NewBridge, connect_twice_while_searching_is_ignored_but_ok_after_abort) {
connect_with_no_bridge_configured_starts_new_bridge_search();
expect_no_actions();
_connectionFlow->ConnectToBridge();
_messageDispatcher->ExecutePendingActions();
searching_with_abort_finalizes();
connect_with_no_bridge_configured_starts_new_bridge_search();
_connectionFlow->ConnectToBridge();
}
TEST_F(TestConnectionFlow_NewBridge, connect_twice_while_autenticating_is_ignored) {
connect_with_no_bridge_configured_starts_new_bridge_search();
searching_with_one_bridge_found_starts_pushlink();
expect_no_actions();
_connectionFlow->ConnectToBridge();
_messageDispatcher->ExecutePendingActions();
}
TEST_F(TestConnectionFlow_NewBridge, select_existing_group_saves_bridge) {
connect_to_bridge_successfully(0, 2);
select_group_existing(0, 12);
finish(_bridges->at(0));
}
TEST_F(TestConnectionFlow_NewBridge, select_not_existing_group_saves_bridge) {
connect_to_bridge_successfully(0);
select_group_not_existing(0, 1);
finish_without_stream_start(false);
}
TEST_F(TestConnectionFlow_NewBridge, start_successful) {
HueConfig::GetInstance()->GetHueSettings()->SetUseRenderThread(false);
connect_to_bridge_successfully(0, 1);
expect_successful_start_stream_messages(0);
EXPECT_CALL(*_stream, Start(_bridges->at(0))).WillOnce(Return(true));
_connectionFlow->StartStream();
_messageDispatcher->ExecutePendingActions();
}
TEST_F(TestConnectionFlow_NewBridge, start_with_renderthread_successful) {
HueConfig::GetInstance()->GetHueSettings()->SetUseRenderThread(true);
connect_to_bridge_successfully(0, 1);
expect_successful_start_stream_messages(0);
EXPECT_CALL(*_stream, StartWithRenderThread(_bridges->at(0))).WillOnce(Return(true));
_connectionFlow->StartStream();
_messageDispatcher->ExecutePendingActions();
}
TEST_F(TestConnectionFlow_NewBridge, start_fail) {
connect_to_bridge_successfully(0, 1);
expect_message(PROCEDURE_STARTED, INFO);
expect_message(START_ACTIVATING, INFO);
expect_message(FINISH_ACTIVATING_FAILED, INFO, _bridges->at(0), BRIDGE_BUSY);
expect_message(DONE_ACTION_REQUIRED, INFO, _bridges->at(0), BRIDGE_BUSY);
expect_message(BUSY_STREAMING, USER);
expect_message(PROCEDURE_FINISHED, INFO);
EXPECT_CALL(*_stream, StartWithRenderThread(_)).WillOnce(Invoke(FailBridgeBusy));
_connectionFlow->StartStream();
_messageDispatcher->ExecutePendingActions();
}
TEST_P(TestConnectionFlow_NewBridge, connect_background_bridge_found) {
connect_with_no_bridge_configured_starts_new_bridge_search(true);
searching_background_with_one_bridge_found_finishes(0);
connect_with_background_result_starts_pushlink(0);
finish_authorization_successfully_and_retrieve_full_config(0, 1);
finish(_bridges->at(0));
}
TEST_P(TestConnectionFlow_NewBridge, connect_background_no_bridge_found) {
connect_with_no_bridge_configured_starts_new_bridge_search(true);
searching_background_with_no_bridge_found_finishes();
}