From 02d416515fd7a35a78da79439e426bd3a2ba0598 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 8 Jun 2026 22:17:40 -0400 Subject: [PATCH 1/2] refactor(sonar): fix cpp:S5817 --- src/common/include/display_device/logging.h | 2 +- src/common/logging.cpp | 2 +- .../include/fixtures/json_converter_test.h | 2 +- tests/unit/general/test_retry_scheduler.cpp | 2 +- .../unit/windows/test_settings_manager_apply.cpp | 16 ++++++++-------- .../windows/test_settings_manager_revert.cpp | 10 +++++----- tests/unit/windows/test_win_api_utils.cpp | 2 +- .../unit/windows/test_win_display_device_hdr.cpp | 2 +- .../windows/test_win_display_device_modes.cpp | 4 ++-- .../windows/test_win_display_device_primary.cpp | 2 +- .../windows/test_win_display_device_topology.cpp | 2 +- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/common/include/display_device/logging.h b/src/common/include/display_device/logging.h index d394db62..60fcbc3f 100644 --- a/src/common/include/display_device/logging.h +++ b/src/common/include/display_device/logging.h @@ -86,7 +86,7 @@ namespace display_device { * Logger::get().write(Logger::LogLevel::Info, "Hello World!"); * @examples_end */ - void write(LogLevel log_level, std::string value); + void write(LogLevel log_level, std::string value) const; /** * @brief A deleted copy constructor for singleton pattern. diff --git a/src/common/logging.cpp b/src/common/logging.cpp index cc51baa4..5e1aaa07 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -56,7 +56,7 @@ namespace display_device { m_custom_callback = std::move(callback); } - void Logger::write(const LogLevel log_level, std::string value) { + void Logger::write(const LogLevel log_level, std::string value) const { if (!isLogLevelEnabled(log_level)) { return; } diff --git a/tests/fixtures/include/fixtures/json_converter_test.h b/tests/fixtures/include/fixtures/json_converter_test.h index 5b85bd3b..c51a943c 100644 --- a/tests/fixtures/include/fixtures/json_converter_test.h +++ b/tests/fixtures/include/fixtures/json_converter_test.h @@ -7,7 +7,7 @@ class JsonConverterTest: public BaseTest { public: template - void executeTestCase(const T &input, const std::string &expected_string) { + void executeTestCase(const T &input, const std::string &expected_string) const { bool success {false}; const auto json_string {display_device::toJson(input, std::nullopt, &success)}; EXPECT_TRUE(success); diff --git a/tests/unit/general/test_retry_scheduler.cpp b/tests/unit/general/test_retry_scheduler.cpp index ad40847d..d11336aa 100644 --- a/tests/unit/general/test_retry_scheduler.cpp +++ b/tests/unit/general/test_retry_scheduler.cpp @@ -17,7 +17,7 @@ namespace { struct TestIface { std::vector m_durations; - void nonConstMethod() { /* noop */ } + void nonConstMethod() const { /* noop */ } void constMethod() const { /* noop */ } }; diff --git a/tests/unit/windows/test_settings_manager_apply.cpp b/tests/unit/windows/test_settings_manager_apply.cpp index 554835d1..0395a361 100644 --- a/tests/unit/windows/test_settings_manager_apply.cpp +++ b/tests/unit/windows/test_settings_manager_apply.cpp @@ -54,7 +54,7 @@ namespace { return *m_impl; } - void expectedDefaultCallsUntilTopologyPrep(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::ActiveTopology &topology = DEFAULT_CURRENT_TOPOLOGY, const std::optional &state = ut_consts::SDCS_EMPTY) { + void expectedDefaultCallsUntilTopologyPrep(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::ActiveTopology &topology = DEFAULT_CURRENT_TOPOLOGY, const std::optional &state = ut_consts::SDCS_EMPTY) const { EXPECT_CALL(*m_settings_persistence_api, load()) .Times(1) .WillOnce(Return(serializeState(state))) @@ -73,7 +73,7 @@ namespace { .RetiresOnSaturation(); } - void expectedIsCapturedCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const bool is_captured) { + void expectedIsCapturedCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const bool is_captured) const { EXPECT_CALL(*m_audio_context_api, isCaptured()) .Times(1) .WillOnce(Return(is_captured)) @@ -100,14 +100,14 @@ namespace { .RetiresOnSaturation(); } - void expectedDeviceEnumCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::EnumeratedDeviceList &devices = DEFAULT_DEVICES) { + void expectedDeviceEnumCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::EnumeratedDeviceList &devices = DEFAULT_DEVICES) const { EXPECT_CALL(*m_dd_api, enumAvailableDevices()) .Times(1) .WillOnce(Return(devices)) .RetiresOnSaturation(); } - void expectedIsTopologyTheSameCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::ActiveTopology &lhs, const display_device::ActiveTopology &rhs) { + void expectedIsTopologyTheSameCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const display_device::ActiveTopology &lhs, const display_device::ActiveTopology &rhs) const { EXPECT_CALL(*m_dd_api, isTopologyTheSame(lhs, rhs)) .Times(1) .WillOnce(Return(lhs == rhs)) @@ -141,7 +141,7 @@ namespace { } } - void expectedIsPrimaryCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::string &device_id, const bool success = true) { + void expectedIsPrimaryCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::string &device_id, const bool success = true) const { EXPECT_CALL(*m_dd_api, isPrimary(device_id)) .Times(1) .WillOnce(Return(success)) @@ -169,7 +169,7 @@ namespace { .RetiresOnSaturation(); } - void expectedGetCurrentDisplayModesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::set &devices, const display_device::DeviceDisplayModeMap &modes) { + void expectedGetCurrentDisplayModesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::set &devices, const display_device::DeviceDisplayModeMap &modes) const { EXPECT_CALL(*m_dd_api, getCurrentDisplayModes(devices)) .Times(1) .WillOnce(Return(modes)) @@ -190,7 +190,7 @@ namespace { .RetiresOnSaturation(); } - void expectedGetCurrentHdrStatesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::set &devices, const display_device::HdrStateMap &states) { + void expectedGetCurrentHdrStatesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::set &devices, const display_device::HdrStateMap &states) const { EXPECT_CALL(*m_dd_api, getCurrentHdrStates(devices)) .Times(1) .WillOnce(Return(states)) @@ -204,7 +204,7 @@ namespace { .RetiresOnSaturation(); } - void expectedHdrWorkaroundCalls(InSequence &sequence /* To ensure that sequence is created outside this scope */) { + void expectedHdrWorkaroundCalls(InSequence &sequence /* To ensure that sequence is created outside this scope */) const { // Using the "failure" path, to keep it simple EXPECT_CALL(*m_dd_api, getCurrentTopology()) .Times(1) diff --git a/tests/unit/windows/test_settings_manager_revert.cpp b/tests/unit/windows/test_settings_manager_revert.cpp index 855e5701..437ed7c0 100644 --- a/tests/unit/windows/test_settings_manager_revert.cpp +++ b/tests/unit/windows/test_settings_manager_revert.cpp @@ -59,7 +59,7 @@ namespace { expectedDefaultCallsUntilModifiedSettingsNoPersistence(sequence); } - void expectedDefaultCallsUntilModifiedSettingsNoPersistence(InSequence & /* To ensure that sequence is created outside this scope */) { + void expectedDefaultCallsUntilModifiedSettingsNoPersistence(InSequence & /* To ensure that sequence is created outside this scope */) const { EXPECT_CALL(*m_dd_api, isApiAccessAvailable()) .Times(1) .WillOnce(Return(true)) @@ -89,7 +89,7 @@ namespace { .RetiresOnSaturation(); } - void expectedDefaultHdrStateGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) { + void expectedDefaultHdrStateGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) const { EXPECT_CALL(*m_dd_api, getCurrentHdrStates(display_device::win_utils::flattenTopology(ut_consts::SDCS_FULL->m_modified.m_topology))) .Times(1) .WillOnce(Return(CURRENT_MODIFIED_HDR_STATES)) @@ -103,7 +103,7 @@ namespace { .RetiresOnSaturation(); } - void expectedDefaultDisplayModeGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) { + void expectedDefaultDisplayModeGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) const { EXPECT_CALL(*m_dd_api, getCurrentDisplayModes(display_device::win_utils::flattenTopology(ut_consts::SDCS_FULL->m_modified.m_topology))) .Times(1) .WillOnce(Return(CURRENT_MODIFIED_DISPLAY_MODES)) @@ -117,7 +117,7 @@ namespace { .RetiresOnSaturation(); } - void expectedDefaultPrimaryDeviceGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) { + void expectedDefaultPrimaryDeviceGuardInitCall(InSequence & /* To ensure that sequence is created outside this scope */) const { EXPECT_CALL(*m_dd_api, isPrimary("DeviceId1")) .Times(1) .WillOnce(Return(false)) @@ -231,7 +231,7 @@ namespace { .RetiresOnSaturation(); } - void expectedHdrWorkaroundCalls(InSequence &sequence /* To ensure that sequence is created outside this scope */) { + void expectedHdrWorkaroundCalls(InSequence &sequence /* To ensure that sequence is created outside this scope */) const { // Using the "failure" path, to keep it simple EXPECT_CALL(*m_dd_api, getCurrentTopology()) .Times(1) diff --git a/tests/unit/windows/test_win_api_utils.cpp b/tests/unit/windows/test_win_api_utils.cpp index ecda1173..3f4d6a47 100644 --- a/tests/unit/windows/test_win_api_utils.cpp +++ b/tests/unit/windows/test_win_api_utils.cpp @@ -17,7 +17,7 @@ namespace { // Test fixture(s) for this file class WinApiUtilsMocked: public BaseTest { public: - void setupExpectCallForValidPaths(int number_of_calls, InSequence & /* To ensure that sequence is created outside this scope */) { + void setupExpectCallForValidPaths(int number_of_calls, InSequence & /* To ensure that sequence is created outside this scope */) const { for (int i = 1; i <= number_of_calls; ++i) { EXPECT_CALL(m_layer, getMonitorDevicePath(_)) .Times(1) diff --git a/tests/unit/windows/test_win_display_device_hdr.cpp b/tests/unit/windows/test_win_display_device_hdr.cpp index 7113aed8..7db2d874 100644 --- a/tests/unit/windows/test_win_display_device_hdr.cpp +++ b/tests/unit/windows/test_win_display_device_hdr.cpp @@ -30,7 +30,7 @@ namespace { class WinDisplayDeviceHdrMocked: public BaseTest { public: - void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) { + void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) const { for (int i = 1; i <= id_number; ++i) { EXPECT_CALL(*m_layer, getMonitorDevicePath(_)) .Times(1) diff --git a/tests/unit/windows/test_win_display_device_modes.cpp b/tests/unit/windows/test_win_display_device_modes.cpp index 9b105e31..37f6063d 100644 --- a/tests/unit/windows/test_win_display_device_modes.cpp +++ b/tests/unit/windows/test_win_display_device_modes.cpp @@ -32,7 +32,7 @@ namespace { class WinDisplayDeviceModesMocked: public BaseTest { public: - void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) { + void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) const { for (int i = 1; i <= id_number; ++i) { EXPECT_CALL(*m_layer, getMonitorDevicePath(_)) .Times(1) @@ -60,7 +60,7 @@ namespace { } } - void setupExpectedGetAllDeviceIdsCall(InSequence & /* To ensure that sequence is created outside this scope */, const std::set &entries = {1, 2, 3, 4}) { + void setupExpectedGetAllDeviceIdsCall(InSequence & /* To ensure that sequence is created outside this scope */, const std::set &entries = {1, 2, 3, 4}) const { EXPECT_CALL(*m_layer, queryDisplayConfig(display_device::QueryType::Active)) .Times(1) .WillOnce(Return(ut_consts::PAM_4_ACTIVE_WITH_2_DUPLICATES)) diff --git a/tests/unit/windows/test_win_display_device_primary.cpp b/tests/unit/windows/test_win_display_device_primary.cpp index 622cb62b..8545bc15 100644 --- a/tests/unit/windows/test_win_display_device_primary.cpp +++ b/tests/unit/windows/test_win_display_device_primary.cpp @@ -31,7 +31,7 @@ namespace { class WinDisplayDevicePrimaryMocked: public BaseTest { public: - void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) { + void setupExpectedGetActivePathCall(int id_number, InSequence & /* To ensure that sequence is created outside this scope */) const { for (int i = 1; i <= id_number; ++i) { EXPECT_CALL(*m_layer, getMonitorDevicePath(_)) .Times(1) diff --git a/tests/unit/windows/test_win_display_device_topology.cpp b/tests/unit/windows/test_win_display_device_topology.cpp index afc56fe7..9cb9d1c1 100644 --- a/tests/unit/windows/test_win_display_device_topology.cpp +++ b/tests/unit/windows/test_win_display_device_topology.cpp @@ -31,7 +31,7 @@ namespace { class WinDisplayDeviceTopologyMocked: public BaseTest { public: - void setupExpectCallFor3ActivePathsAndModes(const display_device::QueryType query_type, InSequence & /* To ensure that sequence is created outside this scope */) { + void setupExpectCallFor3ActivePathsAndModes(const display_device::QueryType query_type, InSequence & /* To ensure that sequence is created outside this scope */) const { EXPECT_CALL(*m_layer, queryDisplayConfig(query_type)) .Times(1) .WillOnce(Return(ut_consts::PAM_3_ACTIVE)) From 6d66d209f627a8055ec1fd7e9be301f843cf9e29 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 8 Jun 2026 22:29:10 -0400 Subject: [PATCH 2/2] Fix const correctness in unit tests Adjust const qualifiers in tests: mark helper expectation methods as const in windows tests to match their usage, and modify nonConstMethod in the retry scheduler test to be non-const and clear m_durations (so it mutates state as intended). Affected files: tests/unit/general/test_retry_scheduler.cpp, tests/unit/windows/test_settings_manager_revert.cpp, tests/unit/windows/test_win_display_device_modes.cpp. --- tests/unit/general/test_retry_scheduler.cpp | 4 +++- tests/unit/windows/test_settings_manager_revert.cpp | 2 +- tests/unit/windows/test_win_display_device_modes.cpp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/unit/general/test_retry_scheduler.cpp b/tests/unit/general/test_retry_scheduler.cpp index d11336aa..e2fb7ceb 100644 --- a/tests/unit/general/test_retry_scheduler.cpp +++ b/tests/unit/general/test_retry_scheduler.cpp @@ -17,7 +17,9 @@ namespace { struct TestIface { std::vector m_durations; - void nonConstMethod() const { /* noop */ } + void nonConstMethod() { + m_durations.clear(); + } void constMethod() const { /* noop */ } }; diff --git a/tests/unit/windows/test_settings_manager_revert.cpp b/tests/unit/windows/test_settings_manager_revert.cpp index 437ed7c0..9b520fe8 100644 --- a/tests/unit/windows/test_settings_manager_revert.cpp +++ b/tests/unit/windows/test_settings_manager_revert.cpp @@ -51,7 +51,7 @@ namespace { return *m_impl; } - void expectedDefaultCallsUntilModifiedSettings(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::optional &state = ut_consts::SDCS_FULL) { + void expectedDefaultCallsUntilModifiedSettings(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::optional &state = ut_consts::SDCS_FULL) const { EXPECT_CALL(*m_settings_persistence_api, load()) .Times(1) .WillOnce(Return(serializeState(state))) diff --git a/tests/unit/windows/test_win_display_device_modes.cpp b/tests/unit/windows/test_win_display_device_modes.cpp index 37f6063d..30d4a4c9 100644 --- a/tests/unit/windows/test_win_display_device_modes.cpp +++ b/tests/unit/windows/test_win_display_device_modes.cpp @@ -49,7 +49,7 @@ namespace { } } - void setupExpectedGetCurrentDisplayModesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::optional &pam = ut_consts::PAM_4_ACTIVE_WITH_2_DUPLICATES) { + void setupExpectedGetCurrentDisplayModesCall(InSequence &sequence /* To ensure that sequence is created outside this scope */, const std::optional &pam = ut_consts::PAM_4_ACTIVE_WITH_2_DUPLICATES) const { EXPECT_CALL(*m_layer, queryDisplayConfig(display_device::QueryType::Active)) .Times(1) .WillOnce(Return(pam))