Skip to content
Merged

Tests #254

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,31 @@ if (LT_WINDOWS)
endif()

set_code_analysis(lt_module_app ${LT_ENABLE_CODE_ANALYSIS})

if (LT_ENABLE_TEST AND BUILD_TESTING)
add_executable(test_friendly_error_code
${CMAKE_CURRENT_SOURCE_DIR}/views/friendly_error_code_tests.cpp
)
target_link_libraries(test_friendly_error_code
GTest::gtest
GTest::gtest_main
lt_module_app
ltproto
protobuf::libprotobuf-lite
Qt6::Widgets
Qt6::Gui
)
add_test(NAME test_friendly_error_code COMMAND test_friendly_error_code)

add_executable(test_access_token_validator
${CMAKE_CURRENT_SOURCE_DIR}/views/components/access_token_validator_tests.cpp
)
target_link_libraries(test_access_token_validator
GTest::gtest
GTest::gtest_main
lt_module_app
Qt6::Widgets
Qt6::Gui
)
add_test(NAME test_access_token_validator COMMAND test_access_token_validator)
endif()
50 changes: 50 additions & 0 deletions src/app/views/components/access_token_validator_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <gtest/gtest.h>

#include <app/views/components/access_token_validator.h>

namespace {

TEST(AccessTokenValidatorTest, ValidateTrimsTruncatesAndLowercases) {
AccesstokenValidator validator(nullptr);
QString input = QStringLiteral(" AbC1239 ");
int pos = 10;

const auto state = validator.validate(input, pos);

EXPECT_EQ(state, QValidator::State::Acceptable);
EXPECT_EQ(input, QStringLiteral("abc123"));
EXPECT_EQ(pos, 6);
}

TEST(AccessTokenValidatorTest, ValidateReturnsInvalidForIllegalCharacter) {
AccesstokenValidator validator(nullptr);
QString input = QStringLiteral("ab-12");
int pos = 0;

const auto state = validator.validate(input, pos);

EXPECT_EQ(state, QValidator::State::Invalid);
EXPECT_EQ(input, QStringLiteral("ab-12"));
}

TEST(AccessTokenValidatorTest, ValidateAllowsEmptyInputAfterTrim) {
AccesstokenValidator validator(nullptr);
QString input = QStringLiteral(" ");
int pos = 3;

const auto state = validator.validate(input, pos);

EXPECT_EQ(state, QValidator::State::Acceptable);
EXPECT_TRUE(input.isEmpty());
}

TEST(AccessTokenValidatorTest, FixupUppercasesInput) {
AccesstokenValidator validator(nullptr);
QString input = QStringLiteral("ab12cd");

validator.fixup(input);

EXPECT_EQ(input, QStringLiteral("AB12CD"));
}

} // namespace
35 changes: 35 additions & 0 deletions src/app/views/friendly_error_code_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <gtest/gtest.h>

#include <app/views/friendly_error_code.h>

#include <ltproto/error_code.pb.h>

namespace {

QString friendlySuffix(const QString& message) {
constexpr auto kSeparator = "\n ";
const int idx = message.indexOf(kSeparator);
if (idx < 0) {
return message;
}

return message.mid(idx + 5);
}

TEST(FriendlyErrorCodeTest, KnownErrorCodeHasSpecificFriendlySuffix) {
const auto message = errorCode2FriendlyMessage(ltproto::ErrorCode::InvalidParameter);

EXPECT_TRUE(message.startsWith(QString("Error code: %1").arg(ltproto::ErrorCode::InvalidParameter)));
EXPECT_EQ(friendlySuffix(message), QStringLiteral("Invalid parameters"));
}

TEST(FriendlyErrorCodeTest, UnknownCodeFallsBackToUnknownMessage) {
constexpr int32_t kUnknownCode = 123456789;
const auto unknown_baseline = errorCode2FriendlyMessage(ltproto::ErrorCode::Unknown);
const auto message = errorCode2FriendlyMessage(kUnknownCode);

EXPECT_TRUE(message.startsWith(QString("Error code: %1").arg(kUnknownCode)));
EXPECT_EQ(friendlySuffix(message), friendlySuffix(unknown_baseline));
}

} // namespace
15 changes: 15 additions & 0 deletions src/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ target_link_libraries(lt_module_audio
)

set_code_analysis(lt_module_audio ${LT_ENABLE_CODE_ANALYSIS})

if (LT_ENABLE_TEST AND BUILD_TESTING)
add_executable(test_fake_audio_capturer
${CMAKE_CURRENT_SOURCE_DIR}/capturer/fake_audio_capturer_tests.cpp
)
target_link_libraries(test_fake_audio_capturer
GTest::gtest
GTest::gtest_main
protobuf::libprotobuf-lite
transport_api
lt_module_ltlib
lt_module_audio
)
add_test(NAME test_fake_audio_capturer COMMAND test_fake_audio_capturer)
endif()
31 changes: 31 additions & 0 deletions src/audio/capturer/fake_audio_capturer_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <gtest/gtest.h>

#include <audio/capturer/fake_audio_capturer.h>

TEST(FakeAudioCapturerTest, DefaultParametersAreInitialized) {
lt::audio::Capturer::Params params;
params.type = lt::AudioCodecType::PCM;
params.on_audio_data = [](const std::shared_ptr<google::protobuf::MessageLite>&) {};

lt::audio::FakeAudioCapturer capturer{params};

EXPECT_EQ(capturer.bytesPerFrame(), 8U);
EXPECT_EQ(capturer.framesPerSec(), 48000U);
EXPECT_EQ(capturer.channels(), 2U);
EXPECT_EQ(capturer.framesPer10ms(), 480U);
EXPECT_EQ(capturer.bytesPer10ms(), 3840U);
}

TEST(FakeAudioCapturerTest, InitPlatformSucceedsAndCaptureLoopIsNoop) {
lt::audio::Capturer::Params params;
params.type = lt::AudioCodecType::PCM;
params.on_audio_data = [](const std::shared_ptr<google::protobuf::MessageLite>&) {};

lt::audio::FakeAudioCapturer capturer{params};

EXPECT_TRUE(capturer.initPlatform());

int alive_call_count = 0;
capturer.captureLoop([&alive_call_count]() { ++alive_call_count; });
EXPECT_EQ(alive_call_count, 0);
}
12 changes: 12 additions & 0 deletions src/inputs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ if (LT_WINDOWS)
endif()

set_code_analysis(lt_module_inputs ${LT_ENABLE_CODE_ANALYSIS})

if (LT_ENABLE_TEST AND BUILD_TESTING)
add_executable(test_input_event
${CMAKE_CURRENT_SOURCE_DIR}/capturer/input_event_tests.cpp
)
target_link_libraries(test_input_event
GTest::gtest
GTest::gtest_main
lt_module_inputs
)
add_test(NAME test_input_event COMMAND test_input_event)
endif()
99 changes: 99 additions & 0 deletions src/inputs/capturer/input_event_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <gtest/gtest.h>

#include <inputs/capturer/input_event.h>

TEST(InputEventTest, KeyboardEventTypeAndPayload) {
const lt::input::KeyboardEvent source{42, true};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::Keyboard);
ASSERT_TRUE(std::holds_alternative<lt::input::KeyboardEvent>(event.ev));

const auto& payload = std::get<lt::input::KeyboardEvent>(event.ev);
EXPECT_EQ(payload.scan_code, 42);
EXPECT_TRUE(payload.is_pressed);
}

TEST(InputEventTest, MouseButtonEventTypeAndPayload) {
const lt::input::MouseButtonEvent source{lt::input::MouseButtonEvent::Button::Right, false, 11,
22, 1280, 720};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::MouseButton);
ASSERT_TRUE(std::holds_alternative<lt::input::MouseButtonEvent>(event.ev));

const auto& payload = std::get<lt::input::MouseButtonEvent>(event.ev);
EXPECT_EQ(payload.button, lt::input::MouseButtonEvent::Button::Right);
EXPECT_FALSE(payload.is_pressed);
EXPECT_EQ(payload.x, 11);
EXPECT_EQ(payload.y, 22);
EXPECT_EQ(payload.window_width, 1280U);
EXPECT_EQ(payload.window_height, 720U);
}

TEST(InputEventTest, MouseMoveEventTypeAndPayload) {
const lt::input::MouseMoveEvent source{100, 80, -2, 3, 1920, 1080};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::MouseMove);
ASSERT_TRUE(std::holds_alternative<lt::input::MouseMoveEvent>(event.ev));

const auto& payload = std::get<lt::input::MouseMoveEvent>(event.ev);
EXPECT_EQ(payload.x, 100);
EXPECT_EQ(payload.y, 80);
EXPECT_EQ(payload.delta_x, -2);
EXPECT_EQ(payload.delta_y, 3);
EXPECT_EQ(payload.window_width, 1920U);
EXPECT_EQ(payload.window_height, 1080U);
}

TEST(InputEventTest, MouseWheelEventTypeAndPayload) {
const lt::input::MouseWheelEvent source{-120};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::MouseWheel);
ASSERT_TRUE(std::holds_alternative<lt::input::MouseWheelEvent>(event.ev));

const auto& payload = std::get<lt::input::MouseWheelEvent>(event.ev);
EXPECT_EQ(payload.amount, -120);
}

TEST(InputEventTest, ControllerAddedRemovedEventTypeAndPayload) {
const lt::input::ControllerAddedRemovedEvent source{7, true};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::ControllerAddedRemoved);
ASSERT_TRUE(std::holds_alternative<lt::input::ControllerAddedRemovedEvent>(event.ev));

const auto& payload = std::get<lt::input::ControllerAddedRemovedEvent>(event.ev);
EXPECT_EQ(payload.index, 7U);
EXPECT_TRUE(payload.is_added);
}

TEST(InputEventTest, ControllerButtonEventTypeAndPayload) {
const lt::input::ControllerButtonEvent source{1, lt::input::ControllerButtonEvent::Button::A,
false};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::ControllerButton);
ASSERT_TRUE(std::holds_alternative<lt::input::ControllerButtonEvent>(event.ev));

const auto& payload = std::get<lt::input::ControllerButtonEvent>(event.ev);
EXPECT_EQ(payload.index, 1U);
EXPECT_EQ(payload.button, lt::input::ControllerButtonEvent::Button::A);
EXPECT_FALSE(payload.is_pressed);
}

TEST(InputEventTest, ControllerAxisEventTypeAndPayload) {
const lt::input::ControllerAxisEvent source{2, lt::input::ControllerAxisEvent::AxisType::RightThumbY,
-16384};
const lt::input::InputEvent event{source};

EXPECT_EQ(event.type, lt::input::InputEventType::ControllerAxis);
ASSERT_TRUE(std::holds_alternative<lt::input::ControllerAxisEvent>(event.ev));

const auto& payload = std::get<lt::input::ControllerAxisEvent>(event.ev);
EXPECT_EQ(payload.index, 2U);
EXPECT_EQ(payload.axis_type, lt::input::ControllerAxisEvent::AxisType::RightThumbY);
EXPECT_EQ(payload.value, -16384);
}
33 changes: 33 additions & 0 deletions src/ltlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ if (LT_ENABLE_TEST AND BUILD_TESTING)
set(LT_MODULE_LTLIB_TEST_PLAT_LIBS)
endif()

add_executable(test_reconnect_interval
${CMAKE_CURRENT_SOURCE_DIR}/reconnect_interval_tests.cpp
)
target_link_libraries(test_reconnect_interval
GTest::gtest
GTest::gtest_main
lt_module_ltlib
${LT_MODULE_LTLIB_TEST_PLAT_LIBS}
)
add_test(NAME test_reconnect_interval COMMAND test_reconnect_interval)

add_executable(test_transform
${CMAKE_CURRENT_SOURCE_DIR}/transform_tests.cpp
)
target_link_libraries(test_transform
GTest::gtest
GTest::gtest_main
lt_module_ltlib
${LT_MODULE_LTLIB_TEST_PLAT_LIBS}
)
add_test(NAME test_transform COMMAND test_transform)

add_executable(test_strings
${CMAKE_CURRENT_SOURCE_DIR}/strings_tests.cpp
)
target_link_libraries(test_strings
GTest::gtest
GTest::gtest_main
lt_module_ltlib
${LT_MODULE_LTLIB_TEST_PLAT_LIBS}
)
add_test(NAME test_strings COMMAND test_strings)

add_executable(test_settings
${CMAKE_CURRENT_SOURCE_DIR}/settings_tests.cpp
)
Expand Down
39 changes: 39 additions & 0 deletions src/ltlib/reconnect_interval_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <array>

#include <gtest/gtest.h>

#include <ltlib/reconnect_interval.h>

namespace {

TEST(ReconnectIntervalTest, FirstValueIsInitialBackoff) {
ltlib::ReconnectInterval reconnect;
EXPECT_EQ(reconnect.next(), 100);
}

TEST(ReconnectIntervalTest, SequenceSaturatesAtUpperBound) {
ltlib::ReconnectInterval reconnect;
constexpr std::array<int64_t, 8> kExpected{100, 500, 1000, 2000, 5000, 10000, 30000, 60000};

for (size_t i = 0; i < kExpected.size(); ++i) {
EXPECT_EQ(reconnect.next(), kExpected[i]);
}

for (int i = 0; i < 16; ++i) {
EXPECT_EQ(reconnect.next(), 60000);
}
}

TEST(ReconnectIntervalTest, ResetRestartsFromFirstValue) {
ltlib::ReconnectInterval reconnect;
(void)reconnect.next();
(void)reconnect.next();
(void)reconnect.next();

reconnect.reset();

EXPECT_EQ(reconnect.next(), 100);
EXPECT_EQ(reconnect.next(), 500);
}

} // namespace
Loading
Loading