Skip to content

Commit

Permalink
Some refactoring of FancyZones::IsInterestingWindow and added Unit Te…
Browse files Browse the repository at this point in the history
…sts (#1521)

* Some refactoring of FancyZones::IsInterestingWindow and added UnitTests
  • Loading branch information
yevhenii44-zz committed Mar 16, 2020
1 parent 04027b9 commit 02857d1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/common/UnitTests-CommonLib/UnitTests-CommonLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
57 changes: 57 additions & 0 deletions src/common/UnitTests-CommonLib/UnitTestsCommon.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
};
}
16 changes: 16 additions & 0 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "version.h"

#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "shlwapi.lib")

namespace localized_strings
{
Expand Down Expand Up @@ -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 changes: 4 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 02857d1

Please sign in to comment.