From a753f2ab4ab7234ca544ec47babcc552a1ee59db Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Wed, 1 Apr 2020 12:18:35 +0200 Subject: [PATCH 1/3] #1821 Support device names with underscores (such as 'Default_Monitor') in JSONHelpers.isValidDeviceId --- src/modules/fancyzones/lib/JsonHelpers.cpp | 25 +++++++++++++++++++ .../tests/UnitTests/JsonHelpers.Tests.cpp | 8 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index c6194365e1ba..dced3e2b2c57 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 (!std::getline(wss, temp, L'#')) + { + return false; + } + + 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 01f1669e3431..21f3e9b0860b 100644 --- a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp +++ b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp @@ -84,6 +84,12 @@ namespace FancyZonesUnitTests Assert::IsTrue(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}"; @@ -1011,7 +1017,7 @@ namespace FancyZonesUnitTests for (int i = 0; i < 10; i++) { json::JsonObject obj = json::JsonObject::Parse(m_defaultCustomDeviceStr); - obj.SetNamedValue(L"device-id", json::JsonValue::CreateStringValue(std::to_wstring(i) + L"_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}")); + obj.SetNamedValue(L"device-id", json::JsonValue::CreateStringValue(std::to_wstring(i) + L"#_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}")); devices.Append(obj); } From 5c0a226f8cc984a8ee73b3921595361bbd615c7c Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Wed, 1 Apr 2020 13:02:20 +0200 Subject: [PATCH 2/3] Make support for '#' in device names optional --- src/modules/fancyzones/lib/JsonHelpers.cpp | 22 +++++++++---------- .../tests/UnitTests/JsonHelpers.Tests.cpp | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index dced3e2b2c57..eba75334f082 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -67,20 +67,20 @@ namespace JSONHelpers */ // Step 1: parse the name until the #, then to the '_' - if (!std::getline(wss, temp, L'#')) + if (str.find(L'#') != std::string::npos) { - return false; - } - - monitorName = temp; + std::getline(wss, temp, L'#'); - if (!std::getline(wss, temp, L'_')) - { - return false; - } + monitorName = temp; - monitorName += L"#" + temp; - parts.push_back(monitorName); + 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'_')) diff --git a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp index 21f3e9b0860b..e0271f6837a5 100644 --- a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp +++ b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp @@ -1017,7 +1017,7 @@ namespace FancyZonesUnitTests for (int i = 0; i < 10; i++) { json::JsonObject obj = json::JsonObject::Parse(m_defaultCustomDeviceStr); - obj.SetNamedValue(L"device-id", json::JsonValue::CreateStringValue(std::to_wstring(i) + L"#_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}")); + obj.SetNamedValue(L"device-id", json::JsonValue::CreateStringValue(std::to_wstring(i) + L"_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}")); devices.Append(obj); } From 2b081860301630905ff16b5c762f7d8e46538fbc Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Thu, 2 Apr 2020 18:14:26 +0200 Subject: [PATCH 3/3] Add more unit tests for isValidDeviceId --- .../fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp index e0271f6837a5..78fc684d1a40 100644 --- a/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp +++ b/src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp @@ -84,6 +84,18 @@ 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}";