terminal/src/cascadia/ut_app/ColorHelperTests.cpp
Carlos Zamora 2608e94822
Introduce TerminalSettingsModel project (#7667)
Introduces a new TerminalSettingsModel (TSM) project. This project is
responsible for (de)serializing and exposing Windows Terminal's settings
as WinRT objects.

## References
#885: TSM epic
#1564: Settings UI is dependent on this for data binding and settings access
#6904: TSM Spec

In the process of ripping out TSM from TerminalApp, a few other changes
were made to make this possible:
1. AppLogic's `ApplicationDisplayName` and `ApplicationVersion` was
   moved to `CascadiaSettings`
   - These are defined as static functions. They also no longer check if
     `AppLogic::Current()` is nullptr.
2. `enum LaunchMode` was moved from TerminalApp to TSM
3. `AzureConnectionType` and `TelnetConnectionType` were moved from the
   profile generators to their respective TerminalConnections
4. CascadiaSettings' `SettingsPath` and `DefaultSettingsPath` are
   exposed as `hstring` instead of `std::filesystem::path`
5. `Command::ExpandCommands()` was exposed via the IDL
   - This required some of the warnings to be saved to an `IVector`
     instead of `std::vector`, among some other small changes.
6. The localization resources had to be split into two halves.
   - Resource file linked in init.cpp. Verified at runtime thanks to the
     StaticResourceLoader.
7. Added constructors to some `ActionArgs`
8. Utils.h/cpp were moved to `cascadia/inc`. `JsonKey()` was moved to
   `JsonUtils`. Both TermApp and TSM need access to Utils.h/cpp.

A large amount of work includes moving to the new namespace
(`TerminalApp` --> `Microsoft::Terminal::Settings::Model`).

Fixing the tests had its own complications. Testing required us to split
up TSM into a DLL and LIB, similar to TermApp. Discussion on creating a
non-local test variant can be found in #7743.

Closes #885
2020-10-06 09:56:59 -07:00

128 lines
4.8 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "../TerminalSettingsModel/Profile.h"
#include "../TerminalApp/ColorHelper.h"
using namespace Microsoft::Console;
using namespace winrt::TerminalApp;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
using namespace WEX::Common;
namespace TerminalAppUnitTests
{
class ColorHelperTests
{
BEGIN_TEST_CLASS(ColorHelperTests)
TEST_CLASS_PROPERTY(L"ActivationContext", L"TerminalApp.Unit.Tests.manifest")
END_TEST_CLASS()
TEST_METHOD(ConvertRgbToHsl);
TEST_METHOD(ConvertHslToRgb);
TEST_METHOD(LuminanceTests);
};
void ColorHelperTests::ConvertHslToRgb()
{
auto red = winrt::Windows::UI::Colors::Red();
auto redHsl = ColorHelper::RgbToHsl(red);
VERIFY_ARE_EQUAL(0.f, redHsl.H);
VERIFY_ARE_EQUAL(1.f, redHsl.S);
VERIFY_ARE_EQUAL(0.5f, redHsl.L);
auto green = winrt::Windows::UI::Colors::Lime();
auto greenHsl = ColorHelper::RgbToHsl(green);
VERIFY_ARE_EQUAL(120.f, greenHsl.H);
VERIFY_ARE_EQUAL(1.f, greenHsl.S);
VERIFY_ARE_EQUAL(0.5f, greenHsl.L);
auto blue = winrt::Windows::UI::Colors::Blue();
auto blueHsl = ColorHelper::RgbToHsl(blue);
VERIFY_ARE_EQUAL(240.f, blueHsl.H);
VERIFY_ARE_EQUAL(1.f, blueHsl.S);
VERIFY_ARE_EQUAL(0.5f, blueHsl.L);
auto darkTurquoise = winrt::Windows::UI::Colors::DarkTurquoise();
auto darkTurquoiseHsl = ColorHelper::RgbToHsl(darkTurquoise);
VERIFY_ARE_EQUAL(181.f, darkTurquoiseHsl.H);
VERIFY_ARE_EQUAL(1.f, darkTurquoiseHsl.S);
VERIFY_ARE_EQUAL(0.41f, darkTurquoiseHsl.L);
auto darkViolet = winrt::Windows::UI::Colors::DarkViolet();
auto darkVioletHsl = ColorHelper::RgbToHsl(darkViolet);
VERIFY_ARE_EQUAL(282.f, darkVioletHsl.H);
VERIFY_ARE_EQUAL(1.f, darkVioletHsl.S);
VERIFY_ARE_EQUAL(0.414f, darkVioletHsl.L);
auto white = winrt::Windows::UI::Colors::White();
auto whiteHsl = ColorHelper::RgbToHsl(white);
VERIFY_ARE_EQUAL(0.f, whiteHsl.H);
VERIFY_ARE_EQUAL(0.f, whiteHsl.S);
VERIFY_ARE_EQUAL(1.f, whiteHsl.L);
auto black = winrt::Windows::UI::Colors::Black();
auto blackHsl = ColorHelper::RgbToHsl(black);
VERIFY_ARE_EQUAL(0.f, blackHsl.H);
VERIFY_ARE_EQUAL(0.f, blackHsl.S);
VERIFY_ARE_EQUAL(0.f, blackHsl.L);
}
void ColorHelperTests::ConvertRgbToHsl()
{
auto redHsl = HSL{ 0.f, 100.f, 50.f };
auto red = ColorHelper::HslToRgb(redHsl);
VERIFY_ARE_EQUAL(255, red.R);
VERIFY_ARE_EQUAL(0, red.G);
VERIFY_ARE_EQUAL(0, red.B);
auto greenHsl = HSL{ 120.f, 100.f, 50.f };
auto green = ColorHelper::HslToRgb(greenHsl);
VERIFY_ARE_EQUAL(0, green.R);
VERIFY_ARE_EQUAL(255, green.G);
VERIFY_ARE_EQUAL(0, green.B);
auto blueHsl = HSL{ 240.f, 100.f, 50.f };
auto blue = ColorHelper::HslToRgb(blueHsl);
VERIFY_ARE_EQUAL(0, blue.R);
VERIFY_ARE_EQUAL(0, blue.G);
VERIFY_ARE_EQUAL(255, blue.B);
auto darkTurquoiseHsl = HSL{ 181.f, 100.f, 41.f };
auto darkTurquoise = ColorHelper::HslToRgb(darkTurquoiseHsl);
VERIFY_ARE_EQUAL(0, darkTurquoise.R);
VERIFY_ARE_EQUAL(206, darkTurquoise.G);
VERIFY_ARE_EQUAL(209, darkTurquoise.B);
auto darkVioletHsl = HSL{ 282.f, 100.f, 41.4f };
auto darkViolet = ColorHelper::HslToRgb(darkVioletHsl);
VERIFY_ARE_EQUAL(148, darkViolet.R);
VERIFY_ARE_EQUAL(0, darkViolet.G);
VERIFY_ARE_EQUAL(211, darkViolet.B);
auto whiteHsl = HSL{ 360.f, 100.f, 100.f };
auto white = ColorHelper::HslToRgb(whiteHsl);
VERIFY_ARE_EQUAL(255, white.R);
VERIFY_ARE_EQUAL(255, white.G);
VERIFY_ARE_EQUAL(255, white.B);
auto blackHsl = HSL{ 0.f, 0.f, 0.f };
auto black = ColorHelper::HslToRgb(blackHsl);
VERIFY_ARE_EQUAL(0, black.R);
VERIFY_ARE_EQUAL(0, black.G);
VERIFY_ARE_EQUAL(0, black.B);
}
void ColorHelperTests::LuminanceTests()
{
auto darkTurquoiseLuminance = ColorHelper::GetLuminance(winrt::Windows::UI::Colors::DarkTurquoise());
VERIFY_ARE_EQUAL(48.75f, darkTurquoiseLuminance * 100);
auto darkVioletLuminance = ColorHelper::GetLuminance(winrt::Windows::UI::Colors::DarkViolet());
VERIFY_ARE_EQUAL(11.f, darkVioletLuminance * 100);
auto magentaLuminance = ColorHelper::GetLuminance(winrt::Windows::UI::Colors::Magenta());
VERIFY_ARE_EQUAL(28.48f, magentaLuminance * 100);
}
}