deskflow/src/test/unittests/gui/core/ClientConnectionTests.cpp
Nick Bolton de21030892
Refactor settings dialog to simplify enable/disable logic (#7428)
* Move screen config files to new GUI lib

* Fixed bugs in config dialog related to service checkbox

* Test status tip

* Revert "Test status tip"

This reverts commit d11ad5bc22dab7edcf7ef0b3baada2fce240398a.

* Explain why service disabled

* Corrected client error message (written by non-native English writer) to make the message easier to understand for native English speakers

* Clearer logic

* Improve readability

* Coverage for 'add client' and client error messages

* Coverage for `Screen` class

* Clarify test names

* Use `const QString &` for backward compatibility

* Fixed lint warnings

* Prevent return paths after qFatal

* Use auto and abort to solve lint warnings

* Remove unused fake error

* Make operator hidden friend

* Remove redundant access specifier

* Convert int explicitly

* Swap && logic for linter

* Update ChangeLog
2024-08-08 18:41:59 +01:00

129 lines
4 KiB
C++

/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2024 Symless Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui/core/ClientConnection.h"
#include "shared/gui/mocks/AppConfigMock.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class QWidget;
using testing::_;
using testing::NiceMock;
using namespace synergy::gui;
using enum messages::ClientError;
namespace {
struct DepsMock : public ClientConnection::Deps {
MOCK_METHOD(
void, showError,
(QWidget * parent, messages::ClientError error, const QString &address),
(const, override));
};
} // namespace
class ClientConnectionTests : public testing::Test {
public:
ClientConnectionTests() {
ON_CALL(m_appConfig, serverHostname())
.WillByDefault(testing::ReturnRef(stub));
}
std::shared_ptr<DepsMock> m_pDeps = std::make_shared<NiceMock<DepsMock>>();
NiceMock<AppConfigMock> m_appConfig;
private:
const QString stub = "stub";
};
TEST_F(ClientConnectionTests, handleLogLine_alreadyConnected_showError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
const QString serverName = "test server";
ON_CALL(m_appConfig, serverHostname())
.WillByDefault(testing::ReturnRef(serverName));
EXPECT_CALL(*m_pDeps, showError(_, AlreadyConnected, serverName));
clientConnection.handleLogLine(
"failed to connect to server\n"
"server already has a connected client with our name");
}
TEST_F(ClientConnectionTests, handleLogLine_withHostname_showError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
const QString serverName = "test-hostname";
ON_CALL(m_appConfig, serverHostname())
.WillByDefault(testing::ReturnRef(serverName));
EXPECT_CALL(*m_pDeps, showError(_, HostnameError, serverName));
clientConnection.handleLogLine("failed to connect to server");
}
TEST_F(ClientConnectionTests, handleLogLine_withIpAddress_showError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
const QString serverName = "1.1.1.1";
ON_CALL(m_appConfig, serverHostname())
.WillByDefault(testing::ReturnRef(serverName));
EXPECT_CALL(*m_pDeps, showError(_, GenericError, serverName));
clientConnection.handleLogLine("failed to connect to server");
}
TEST_F(ClientConnectionTests, handleLogLine_messageShown_shouldNotShowAgain) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
clientConnection.handleLogLine("failed to connect to server");
EXPECT_CALL(*m_pDeps, showError(_, _, _)).Times(0);
clientConnection.handleLogLine("failed to connect to server");
}
TEST_F(
ClientConnectionTests,
handleLogLine_serverRefusedClient_shouldNotShowError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
EXPECT_CALL(*m_pDeps, showError(_, _, _)).Times(0);
clientConnection.handleLogLine("failed to connect to server\n"
"server refused client with our name");
}
TEST_F(
ClientConnectionTests, handleLogLine_connected_shouldPreventFutureError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
clientConnection.handleLogLine("connected to server");
EXPECT_CALL(*m_pDeps, showError(_, _, _)).Times(0);
clientConnection.handleLogLine("failed to connect to server");
}
TEST_F(ClientConnectionTests, handleLogLine_otherMessage_shouldNotShowError) {
ClientConnection clientConnection(nullptr, m_appConfig, m_pDeps);
EXPECT_CALL(*m_pDeps, showError(_, _, _)).Times(0);
clientConnection.handleLogLine("hello world");
}