Skip to content

Commit

Permalink
Unit tests update (microsoft#19)
Browse files Browse the repository at this point in the history
* check device existence in map
* updated ZoneSet tests
* updated JsonHelpers tests
  • Loading branch information
SeraphimaZykova authored Feb 6, 2020
1 parent 5864942 commit 75ba40a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/modules/fancyzones/lib/JsonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ namespace JSONHelpers

bool FancyZonesData::ParseCustomZoneSetFromTmpFile(std::wstring_view tmpFilePath, const std::wstring& deviceId)
{
if (deviceInfoMap.at(deviceId).activeZoneSet.type != JSONHelpers::ZoneSetLayoutType::Custom)
const auto device = deviceInfoMap.find(deviceId);
if (device == deviceInfoMap.end() || device->second.activeZoneSet.type != JSONHelpers::ZoneSetLayoutType::Custom)
{
return false;
}

bool res = true;
if (std::filesystem::exists(tmpFilePath))
{
Expand Down
34 changes: 21 additions & 13 deletions src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ namespace FancyZonesUnitTests
const json::JsonObject m_defaultCustomDeviceObj = json::JsonObject::Parse(m_defaultCustomDeviceStr);

HINSTANCE m_hInst{};
FancyZonesData& m_fzData = FancyZonesDataInstance();

void compareJsonArrays(const json::JsonArray& expected, const json::JsonArray& actual)
{
Expand All @@ -775,6 +776,7 @@ namespace FancyZonesUnitTests
TEST_METHOD_INITIALIZE(Init)
{
m_hInst = (HINSTANCE)GetModuleHandleW(nullptr);
m_fzData = FancyZonesData();
}

public:
Expand Down Expand Up @@ -1305,9 +1307,20 @@ namespace FancyZonesUnitTests
compareJsonArrays(expected, actual);
}

#if 0
TEST_METHOD(CustomZoneSetsReadTemp)
{
//prepare device data
const std::wstring deviceId = L"default_device_id";

{
DeviceInfoJSON deviceInfo{ deviceId, DeviceInfoData{ ZoneSetData{ L"uuid", ZoneSetLayoutType::Custom }, true, 16, 3 } };
const std::wstring deviceInfoPath = m_fzData.GetPersistFancyZonesJSONPath() + L".device_info_tmp";
m_fzData.SerializeDeviceInfoToTmpFile(deviceInfo, deviceInfoPath);

m_fzData.ParseDeviceInfoFromTmpFile(deviceInfoPath);
std::filesystem::remove(deviceInfoPath);
}

const std::wstring uuid = L"uuid";
const GridLayoutInfo grid(GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 1,
Expand All @@ -1319,9 +1332,8 @@ namespace FancyZonesUnitTests

FancyZonesData data;
const std::wstring path = data.GetPersistFancyZonesJSONPath() + L".test_tmp";
json::to_file(path, CustomZoneSetJSON::ToJson(expected));

data.ParseCustomZoneSetFromTmpFile(path);
json::to_file(path, CustomZoneSetJSON::ToJson(expected));
m_fzData.ParseCustomZoneSetFromTmpFile(path, deviceId);

bool actualFileExists = std::filesystem::exists(path);
if (actualFileExists)
Expand All @@ -1330,7 +1342,7 @@ namespace FancyZonesUnitTests
}
Assert::IsFalse(actualFileExists);

auto devices = data.GetCustomZoneSetsMap();
auto devices = m_fzData.GetCustomZoneSetsMap();
Assert::AreEqual((size_t)1, devices.size());

auto actual = devices.find(uuid)->second;
Expand All @@ -1341,20 +1353,16 @@ namespace FancyZonesUnitTests
Assert::AreEqual(expectedGrid.rows(), actualGrid.rows());
Assert::AreEqual(expectedGrid.columns(), actualGrid.columns());
}
#endif

#if 0
TEST_METHOD(CustomZoneSetsReadTempUnexsisted)
{
FancyZonesData data;

const std::wstring path = data.GetPersistFancyZonesJSONPath() + L".test_tmp";
const std::wstring path = m_fzData.GetPersistFancyZonesJSONPath() + L".test_tmp";
const std::wstring deviceId = L"default_device_id";

data.ParseCustomZoneSetFromTmpFile(path);
auto devices = data.GetDeviceInfoMap();
m_fzData.ParseCustomZoneSetFromTmpFile(path, deviceId);
auto devices = m_fzData.GetDeviceInfoMap();
Assert::AreEqual((size_t)0, devices.size());
}
#endif

TEST_METHOD(SetActiveZoneSet)
{
Expand Down
44 changes: 34 additions & 10 deletions src/modules/fancyzones/tests/UnitTests/ZoneSet.Spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,22 +1089,35 @@ namespace FancyZonesUnitTests
Assert::IsFalse(result);
}
}
#if 0

TEST_METHOD(CustomZoneFromValidCanvasLayoutInfo)
{
using namespace JSONHelpers;

const std::wstring uuid = L"uuid";
//prepare device data
{
const std::wstring zoneUuid = L"default_device_id";
DeviceInfoJSON deviceInfo{ zoneUuid, DeviceInfoData{ ZoneSetData{ L"uuid", ZoneSetLayoutType::Custom }, true, 16, 3 } };
const std::wstring deviceInfoPath = FancyZonesDataInstance().GetPersistFancyZonesJSONPath() + L".device_info_tmp";
FancyZonesDataInstance().SerializeDeviceInfoToTmpFile(deviceInfo, deviceInfoPath);

FancyZonesDataInstance().ParseDeviceInfoFromTmpFile(deviceInfoPath);
std::filesystem::remove(deviceInfoPath);
}

//prepare expected data
wil::unique_cotaskmem_string uuid;
Assert::AreEqual(S_OK, StringFromCLSID(m_id, &uuid));
const CanvasLayoutInfo info{ 123, 321, { CanvasLayoutInfo::Rect{ 0, 0, 100, 100 }, CanvasLayoutInfo::Rect{ 50, 50, 150, 150 } } };
CustomZoneSetJSON expected{ uuid, CustomZoneSetData{ L"name", CustomLayoutType::Canvas, info } };
CustomZoneSetJSON expected{ uuid.get(), CustomZoneSetData{ L"name", CustomLayoutType::Canvas, info } };
json::to_file(m_path, CustomZoneSetJSON::ToJson(expected));
Assert::IsTrue(std::filesystem::exists(m_path));
FancyZonesDataInstance().ParseCustomZoneSetFromTmpFile(m_path, L"default_device_id");

//test
const int spacing = 10;
const auto zoneCount = info.zones.size();

ZoneSetConfig m_config = ZoneSetConfig(m_id, TZoneSetLayoutType::Custom, m_monitor, m_resolutionKey);

for (const auto& monitorInfo : m_popularMonitors)
{
auto set = MakeZoneSet(m_config);
Expand All @@ -1113,23 +1126,35 @@ namespace FancyZonesUnitTests
checkZones(set, zoneCount, monitorInfo);
}
}
#endif

#if 0
TEST_METHOD(CustomZoneFromValidGridFullLayoutInfo)
{
using namespace JSONHelpers;

const std::wstring uuid = L"uuid";
//prepare device data
{
const std::wstring zoneUuid = L"default_device_id";
DeviceInfoJSON deviceInfo{ zoneUuid, DeviceInfoData{ ZoneSetData{ L"uuid", ZoneSetLayoutType::Custom }, true, 16, 3 } };
const std::wstring deviceInfoPath = FancyZonesDataInstance().GetPersistFancyZonesJSONPath() + L".device_info_tmp";
FancyZonesDataInstance().SerializeDeviceInfoToTmpFile(deviceInfo, deviceInfoPath);

FancyZonesDataInstance().ParseDeviceInfoFromTmpFile(deviceInfoPath);
std::filesystem::remove(deviceInfoPath);
}

//prepare expected data
wil::unique_cotaskmem_string uuid;
Assert::AreEqual(S_OK, StringFromCLSID(m_id, &uuid));
const GridLayoutInfo grid(GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 1,
.columns = 3,
.rowsPercents = { 10000 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 } } }));
CustomZoneSetJSON expected{ uuid, CustomZoneSetData{ L"name", CustomLayoutType::Grid, grid } };
CustomZoneSetJSON expected{ uuid.get(), CustomZoneSetData{ L"name", CustomLayoutType::Grid, grid } };
json::to_file(m_path, CustomZoneSetJSON::ToJson(expected));
Assert::IsTrue(std::filesystem::exists(m_path));
FancyZonesDataInstance().ParseCustomZoneSetFromTmpFile(m_path, L"default_device_id");

const int spacing = 10;
const int zoneCount = grid.rows() * grid.columns();
Expand All @@ -1144,7 +1169,6 @@ namespace FancyZonesUnitTests
checkZones(set, zoneCount, monitorInfo);
}
}
#endif

TEST_METHOD(CustomZoneFromValidGridMinimalLayoutInfo)
{
Expand Down

0 comments on commit 75ba40a

Please sign in to comment.