Files
terminal/src/cascadia/UnitTests_Control/ControlCoreTests.cpp
Mike Griese 8910a16fd0 Split TermControl into a Core, Interactivity, and Control layer (#9820)
## Summary of the Pull Request

Brace yourselves, it's finally here. This PR does the dirty work of splitting the monolithic `TermControl` into three components. These components are: 

* `ControlCore`: This encapsulates the `Terminal` instance, the `DxEngine` and `Renderer`, and the `Connection`. This is intended to everything that someone might need to stand up a terminal instance in a control, but without any regard for how the UX works.
* `ControlInteractivity`: This is a wrapper for the `ControlCore`, which holds the logic for things like double-click, right click copy/paste, selection, etc. This is intended to be a UI framework-independent abstraction. The methods this layer exposes can be called the same from both the WinUI TermControl and the WPF control.
* `TermControl`: This is the UWP control. It's got a Core and Interactivity inside it, which it uses for the actual logic of the terminal itself. TermControl's main responsibility is now 

By splitting into smaller pieces, it will enable us to
* write unit tests for the `Core` and `Interactivity` bits, which we desparately need
* Combine `ControlCore` and `ControlInteractivity` in an out-of-proc core process in the future, to enable tab tearout.

However, we're not doing that work quite yet. There's still lots of work to be done to enable that, thought this is likely the biggest portion.

Ideally, this would just be methods moved wholesale from one file to another. Unfortunately, there are a bunch of cases where that didn't work as well as expected. Especially when trying to better enforce the boundary between the classes. 

We've got a couple tests here that I've added. These are partially examples, and partially things I ran into while implementing this. A bunch of things from #7001 can go in now that we have this.

This PR is gonna be a huge pain to review - 38 files with 3,730 additions and 1,661 deletions is nothing to scoff at. It will also conflict 100% with anything that's targeting `TermControl`. I'm hoping we can review this over the course of the next week and just be done with it, and leave plenty of runway for 1.9 bugs in post.

## References

* In pursuit of #1256
* Proc Model: #5000
* https://github.com/microsoft/terminal/projects/5

## PR Checklist
* [x] Closes #6842
* [x] Closes https://github.com/microsoft/terminal/projects/5#card-50760249
* [x] Closes https://github.com/microsoft/terminal/projects/5#card-50760258
* [x] I work here
* [x] Tests added/passed
* [n/a] Requires documentation to be updated

## Detailed Description of the Pull Request / Additional comments

* I don't love the names `ControlCore` and `ControlInteractivity`. Open to other names.
* I added a `ICoreState` interface for "properties that come from the `ControlCore`, but consumers of the `TermControl` need to know". In the future, these will all need to be handled specially, because they might involve an RPC call to retrieve the info from the core (or cache it) in the window process.
* I've added more `EventArgs` to make more events proper `TypedEvent`s.
* I've changed how the TerminalApp layer requests updated TaskbarProgress state. It doesn't need to pump TermControl to raise a new event anymore.
* ~~Something that snuck into this branch in the very long history is the switch to `DCompositionCreateSurfaceHandle` for the `DxEngine`. @miniksa wrote this originally in 30b8335, I'm just finally committing it here. We'll need that in the future for the out-of-proc stuff.~~
  * I reverted this in c113b65d9. We can revert _that_ commit when we want to come back to it.
* I've changed the acrylic handler a decent amount. But added tests!
* All the `ThrottledFunc` things are left in `TermControl`. Some might be able to move down into core/interactivity, but once we figure out how to use a different kind of Dispatcher (because a UI thread won't necessarily exist for those components).
* I've undoubtably messed up the merging of the locking around the appearance config stuff recently

## Validation Steps Performed

I've got a rolling list in https://github.com/microsoft/terminal/issues/6842#issuecomment-810990460 that I'm updating as I go.
2021-04-27 15:50:45 +00:00

201 lines
7.3 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "../TerminalControl/EventArgs.h"
#include "../TerminalControl/ControlCore.h"
#include "MockControlSettings.h"
#include "MockConnection.h"
using namespace Microsoft::Console;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
using namespace WEX::Common;
using namespace winrt;
using namespace winrt::Microsoft::Terminal;
namespace ControlUnitTests
{
class ControlCoreTests
{
BEGIN_TEST_CLASS(ControlCoreTests)
TEST_CLASS_PROPERTY(L"TestTimeout", L"0:0:10") // 10s timeout
END_TEST_CLASS()
TEST_METHOD(ComPtrSettings);
TEST_METHOD(InstantiateCore);
TEST_METHOD(TestInitialize);
TEST_METHOD(TestAdjustAcrylic);
TEST_METHOD(TestFreeAfterClose);
TEST_METHOD(TestFontInitializedInCtor);
TEST_CLASS_SETUP(ModuleSetup)
{
winrt::init_apartment(winrt::apartment_type::single_threaded);
return true;
}
TEST_CLASS_CLEANUP(ClassCleanup)
{
winrt::uninit_apartment();
return true;
}
std::tuple<winrt::com_ptr<MockControlSettings>, winrt::com_ptr<MockConnection>> _createSettingsAndConnection()
{
Log::Comment(L"Create settings object");
auto settings = winrt::make_self<MockControlSettings>();
VERIFY_IS_NOT_NULL(settings);
Log::Comment(L"Create connection object");
auto conn = winrt::make_self<MockConnection>();
VERIFY_IS_NOT_NULL(conn);
return { settings, conn };
}
};
void ControlCoreTests::ComPtrSettings()
{
Log::Comment(L"Just make sure we can instantiate a settings obj in a com_ptr");
auto settings = winrt::make_self<MockControlSettings>();
Log::Comment(L"Verify literally any setting, it doesn't matter");
VERIFY_ARE_EQUAL(DEFAULT_FOREGROUND, settings->DefaultForeground());
}
void ControlCoreTests::InstantiateCore()
{
auto [settings, conn] = _createSettingsAndConnection();
Log::Comment(L"Create ControlCore object");
auto core = winrt::make_self<Control::implementation::ControlCore>(*settings, *conn);
VERIFY_IS_NOT_NULL(core);
}
void ControlCoreTests::TestInitialize()
{
auto [settings, conn] = _createSettingsAndConnection();
Log::Comment(L"Create ControlCore object");
auto core = winrt::make_self<Control::implementation::ControlCore>(*settings, *conn);
VERIFY_IS_NOT_NULL(core);
VERIFY_IS_FALSE(core->_initializedTerminal);
// "Consolas" ends up with an actual size of 9x21 at 96DPI. So
// let's just arbitrarily start with a 270x420px (30x20 chars) window
core->Initialize(270, 420, 1.0);
VERIFY_IS_TRUE(core->_initializedTerminal);
VERIFY_ARE_EQUAL(30, core->_terminal->GetViewport().Width());
}
void ControlCoreTests::TestAdjustAcrylic()
{
auto [settings, conn] = _createSettingsAndConnection();
settings->UseAcrylic(true);
settings->TintOpacity(0.5f);
Log::Comment(L"Create ControlCore object");
auto core = winrt::make_self<Control::implementation::ControlCore>(*settings, *conn);
VERIFY_IS_NOT_NULL(core);
// A callback to make sure that we're raising TransparencyChanged events
double expectedOpacity = 0.5;
auto opacityCallback = [&](auto&&, Control::TransparencyChangedEventArgs args) mutable {
VERIFY_ARE_EQUAL(expectedOpacity, args.Opacity());
VERIFY_ARE_EQUAL(expectedOpacity, settings->TintOpacity());
VERIFY_ARE_EQUAL(expectedOpacity, core->_settings.TintOpacity());
if (expectedOpacity < 1.0)
{
VERIFY_IS_TRUE(settings->UseAcrylic());
VERIFY_IS_TRUE(core->_settings.UseAcrylic());
}
VERIFY_ARE_EQUAL(expectedOpacity < 1.0, settings->UseAcrylic());
VERIFY_ARE_EQUAL(expectedOpacity < 1.0, core->_settings.UseAcrylic());
};
core->TransparencyChanged(opacityCallback);
VERIFY_IS_FALSE(core->_initializedTerminal);
// "Cascadia Mono" ends up with an actual size of 9x19 at 96DPI. So
// let's just arbitrarily start with a 270x380px (30x20 chars) window
core->Initialize(270, 380, 1.0);
VERIFY_IS_TRUE(core->_initializedTerminal);
Log::Comment(L"Increasing opacity till fully opaque");
expectedOpacity += 0.1; // = 0.6;
core->AdjustOpacity(0.1);
expectedOpacity += 0.1; // = 0.7;
core->AdjustOpacity(0.1);
expectedOpacity += 0.1; // = 0.8;
core->AdjustOpacity(0.1);
expectedOpacity += 0.1; // = 0.9;
core->AdjustOpacity(0.1);
expectedOpacity += 0.1; // = 1.0;
// cast to float because floating point numbers are mean
VERIFY_ARE_EQUAL(1.0f, base::saturated_cast<float>(expectedOpacity));
core->AdjustOpacity(0.1);
Log::Comment(L"Increasing opacity more doesn't actually change it to be >1.0");
expectedOpacity = 1.0;
core->AdjustOpacity(0.1);
Log::Comment(L"Decrease opacity");
expectedOpacity -= 0.25; // = 0.75;
core->AdjustOpacity(-0.25);
expectedOpacity -= 0.25; // = 0.5;
core->AdjustOpacity(-0.25);
expectedOpacity -= 0.25; // = 0.25;
core->AdjustOpacity(-0.25);
expectedOpacity -= 0.25; // = 0.05;
// cast to float because floating point numbers are mean
VERIFY_ARE_EQUAL(0.0f, base::saturated_cast<float>(expectedOpacity));
core->AdjustOpacity(-0.25);
Log::Comment(L"Decreasing opacity more doesn't actually change it to be < 0");
expectedOpacity = 0.0;
core->AdjustOpacity(-0.25);
}
void ControlCoreTests::TestFreeAfterClose()
{
{
auto [settings, conn] = _createSettingsAndConnection();
Log::Comment(L"Create ControlCore object");
auto core = winrt::make_self<Control::implementation::ControlCore>(*settings, *conn);
VERIFY_IS_NOT_NULL(core);
Log::Comment(L"Close the Core, like a TermControl would");
core->Close();
}
VERIFY_IS_TRUE(true, L"Make sure that the test didn't crash when the core when out of scope");
}
void ControlCoreTests::TestFontInitializedInCtor()
{
// This is to catch a dumb programming mistake I made while working on
// the core/control split. We want the font initialized in the ctor,
// before we even get to Core::Initialize.
auto [settings, conn] = _createSettingsAndConnection();
// Make sure to use something dumb like "Impact" as a font name here so
// that you don't default to Cascadia*
settings->FontFace(L"Impact");
Log::Comment(L"Create ControlCore object");
auto core = winrt::make_self<Control::implementation::ControlCore>(*settings, *conn);
VERIFY_IS_NOT_NULL(core);
VERIFY_ARE_EQUAL(L"Impact", std::wstring_view{ core->_actualFont.GetFaceName() });
}
}