From 96fbc968ef5fe7ea57121cddcd58b9f54cf3e746 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Thu, 2 Apr 2020 18:51:36 +0200 Subject: [PATCH] Support device names with underscores (such as 'Default_Monitor') in JSONHelpers.isValidDeviceId (#1826) * #1821 Support device names with underscores (such as 'Default_Monitor') in JSONHelpers.isValidDeviceId * Make support for '#' in device names optional * Add more unit tests for isValidDeviceId --- src/modules/fancyzones/lib/JsonHelpers.cpp | 25 +++++++++++++++++++ .../tests/UnitTests/JsonHelpers.Tests.cpp | 18 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index c6194365e1..eba75334f0 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -55,9 +55,34 @@ namespace JSONHelpers bool isValidDeviceId(const std::wstring& str) { + std::wstring monitorName; std::wstring temp; std::vector parts; std::wstringstream wss(str); + + /* + Important fix for device info that contains a '_' in the name: + 1. first search for '#' + 2. Then split the remaining string by '_' + */ + + // Step 1: parse the name until the #, then to the '_' + if (str.find(L'#') != std::string::npos) + { + std::getline(wss, temp, L'#'); + + monitorName = temp; + + if (!std::getline(wss, temp, L'_')) + { + return false; + } + + monitorName += L"#" + temp; + parts.push_back(monitorName); + } + + // Step 2: parse the rest of the id while (std::getline(wss, temp, L'_')) { parts.push_back(temp); diff --git a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp index 01f1669e34..78fc684d1a 100644 --- a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp +++ b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp @@ -84,6 +84,24 @@ namespace FancyZonesUnitTests Assert::IsTrue(isValidDeviceId(deviceId)); } + TEST_METHOD (DeviceIdWithoutHashInName) + { + const auto deviceId = L"LOCALDISPLAY_5120_1440_{00000000-0000-0000-0000-000000000000}"; + Assert::IsTrue(isValidDeviceId(deviceId)); + } + + TEST_METHOD (DeviceIdWithoutHashInNameButWithUnderscores) + { + const auto deviceId = L"LOCAL_DISPLAY_5120_1440_{00000000-0000-0000-0000-000000000000}"; + Assert::IsFalse(isValidDeviceId(deviceId)); + } + + TEST_METHOD (DeviceIdWithUnderscoresInName) + { + const auto deviceId = L"Default_Monitor#1&1f0c3c2f&0&UID256_5120_1440_{00000000-0000-0000-0000-000000000000}"; + Assert::IsTrue(isValidDeviceId(deviceId)); + } + TEST_METHOD (DeviceIdInvalidFormat) { const auto deviceId = L"_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}";