mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
Some refactoring of FancyZones::IsInterestingWindow and added Unit Tests (#1521)
* Some refactoring of FancyZones::IsInterestingWindow and added UnitTests
This commit is contained in:
committed by
GitHub
parent
04027b9c32
commit
02857d1b7f
@@ -91,10 +91,11 @@
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>RuntimeObject.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>RuntimeObject.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="UnitTestsCommon.cpp" />
|
||||
<ClCompile Include="UnitTestsVersionHelper.cpp" />
|
||||
<ClCompile Include="pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
<ClCompile Include="UnitTestsVersionHelper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UnitTestsCommon.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="pch.h">
|
||||
|
||||
57
src/common/UnitTests-CommonLib/UnitTestsCommon.cpp
Normal file
57
src/common/UnitTests-CommonLib/UnitTestsCommon.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "pch.h"
|
||||
#include "common.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace UnitTestsCommon
|
||||
{
|
||||
TEST_CLASS (CommonUtils)
|
||||
{
|
||||
std::vector<std::wstring> what_global{
|
||||
L"TELEGRAM",
|
||||
L"SUBLIME TEXT",
|
||||
L"PROGRAM",
|
||||
L"TEXT",
|
||||
};
|
||||
|
||||
TEST_METHOD (FindAppNameInPathTest1)
|
||||
{
|
||||
std::wstring where(L"C:\\USERS\\GUEST\\APPDATA\\ROAMING\\TELEGRAM DESKTOP\\TELEGRAM.EXE");
|
||||
bool ans = find_app_name_in_path(where, what_global);
|
||||
Assert::IsTrue(ans);
|
||||
}
|
||||
TEST_METHOD (FindAppNameInPathTest2)
|
||||
{
|
||||
std::vector<std::wstring> what{
|
||||
L"NOTEPAD",
|
||||
};
|
||||
std::wstring where(L"C:\\PROGRAM FILES\\NOTEPAD++\\NOTEPAD++.EXE");
|
||||
bool ans = find_app_name_in_path(where, what);
|
||||
Assert::IsTrue(ans);
|
||||
}
|
||||
TEST_METHOD (FindAppNameInPathTest3)
|
||||
{
|
||||
std::vector<std::wstring> what{
|
||||
L"NOTEPAD++.EXE",
|
||||
};
|
||||
std::wstring where(L"C:\\PROGRAM FILES\\NOTEPAD++\\NOTEPAD++.EXE");
|
||||
bool ans = find_app_name_in_path(where, what);
|
||||
Assert::IsTrue(ans);
|
||||
}
|
||||
TEST_METHOD (FindAppNameInPathTest4)
|
||||
{
|
||||
std::wstring where(L"C:\\PROGRAM FILES\\SUBLIME TEXT 3\\SUBLIME_TEXT.EXE");
|
||||
bool ans = find_app_name_in_path(where, what_global);
|
||||
Assert::IsFalse(ans);
|
||||
}
|
||||
TEST_METHOD (FindAppNameInPathTest5)
|
||||
{
|
||||
std::vector<std::wstring> what{
|
||||
L"NOTEPAD.EXE",
|
||||
};
|
||||
std::wstring where(L"C:\\PROGRAM FILES\\NOTEPAD++\\NOTEPAD++.EXE");
|
||||
bool ans = find_app_name_in_path(where, what);
|
||||
Assert::IsFalse(ans);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "version.h"
|
||||
|
||||
#pragma comment(lib, "advapi32.lib")
|
||||
#pragma comment(lib, "shlwapi.lib")
|
||||
|
||||
namespace localized_strings
|
||||
{
|
||||
@@ -715,3 +716,18 @@ bool check_user_is_admin()
|
||||
freeMemory(pSID, pGroupInfo);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool find_app_name_in_path(const std::wstring& where, const std::vector<std::wstring>& what)
|
||||
{
|
||||
for (const auto& row : what)
|
||||
{
|
||||
const auto pos = where.rfind(row);
|
||||
const auto last_slash = where.rfind('\\');
|
||||
//Check that row occurs in where, and its last occurrence contains in itself the first character after the last backslash.
|
||||
if (pos != std::wstring::npos && pos <= last_slash + 1 && pos + row.length() > last_slash)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
// Returns RECT with positions of the minmize/maximize buttons of the given window.
|
||||
// Does not always work, since some apps draw custom toolbars.
|
||||
@@ -77,6 +78,9 @@ bool run_same_elevation(const std::wstring& file, const std::wstring& params);
|
||||
// Returns true if the current process is running from administrator account
|
||||
bool check_user_is_admin();
|
||||
|
||||
//Returns true when one or more strings from vector found in string
|
||||
bool find_app_name_in_path(const std::wstring& where, const std::vector<std::wstring>& what);
|
||||
|
||||
// Get the executable path or module name for modern apps
|
||||
std::wstring get_process_path(DWORD pid) noexcept;
|
||||
// Get the executable path or module name for modern apps
|
||||
|
||||
@@ -706,12 +706,10 @@ bool FancyZones::IsInterestingWindow(HWND window) noexcept
|
||||
CharUpperBuffW(filtered.process_path.data(), (DWORD)filtered.process_path.length());
|
||||
if (m_settings)
|
||||
{
|
||||
for (const auto& excluded : m_settings->GetSettings()->excludedAppsArray)
|
||||
const auto& excludedAppsArray = m_settings->GetSettings()->excludedAppsArray;
|
||||
if (find_app_name_in_path(filtered.process_path, excludedAppsArray))
|
||||
{
|
||||
if (filtered.process_path.find(excluded) != std::wstring::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user