Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter focus events that came from the API #13260

Merged
7 commits merged into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions src/terminal/adapter/ut_adapter/inputTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Microsoft::Console::VirtualTerminal::InputTest
static void s_TerminalInputTestNullCallback(_In_ std::deque<std::unique_ptr<IInputEvent>>& inEvents);

TEST_METHOD(TerminalInputTests);
TEST_METHOD(TestFocusEvents);
TEST_METHOD(TerminalInputModifierKeyTests);
TEST_METHOD(TerminalInputNullKeyTests);
TEST_METHOD(DifferentModifiersTest);
Expand Down Expand Up @@ -300,10 +301,62 @@ void InputTest::TerminalInputTests()
inputEvent = IInputEvent::Create(irUnhandled);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify MENU_EVENT was NOT handled.");

Log::Comment(L"Testing FOCUS_EVENT");
irUnhandled.EventType = FOCUS_EVENT;
inputEvent = IInputEvent::Create(irUnhandled);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FOCUS_EVENT was NOT handled.");
Log::Comment(L"Testing FOCUS_EVENTs is handled by TestFocusEvents");
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}

void InputTest::TestFocusEvents()
{
// GH#12900, #13238
// Focus events that come in from the API should never be translated to VT sequences.
// We're relying on the fact that the INPUT_RECORD version of the ctor is only called by the API
const auto pInput = new TerminalInput(s_TerminalInputTestCallback);

INPUT_RECORD irTest = { 0 };
irTest.EventType = FOCUS_EVENT;

{
irTest.Event.FocusEvent.bSetFocus = false;
auto inputEvent = IInputEvent::Create(irTest);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FOCUS_EVENT from API was NOT handled.");
}
{
irTest.Event.FocusEvent.bSetFocus = true;
auto inputEvent = IInputEvent::Create(irTest);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FOCUS_EVENT from API was NOT handled.");
}
{
auto inputEvent = std::make_unique<FocusEvent>(false);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FocusEvent from any other source was NOT handled.");
}
{
auto inputEvent = std::make_unique<FocusEvent>(true);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FocusEvent from any other source was NOT handled.");
}

Log::Comment(L"Enable focus event handling");

pInput->SetInputMode(TerminalInput::Mode::FocusEvent, true);

{
irTest.Event.FocusEvent.bSetFocus = false;
auto inputEvent = IInputEvent::Create(irTest);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FOCUS_EVENT from API was NOT handled.");
}
{
irTest.Event.FocusEvent.bSetFocus = true;
auto inputEvent = IInputEvent::Create(irTest);
VERIFY_ARE_EQUAL(false, pInput->HandleKey(inputEvent.get()), L"Verify FOCUS_EVENT from API was NOT handled.");
}
{
s_expectedInput = L"\x1b[O";
auto inputEvent = std::make_unique<FocusEvent>(false);
VERIFY_ARE_EQUAL(true, pInput->HandleKey(inputEvent.get()), L"Verify FocusEvent from any other source was handled.");
}
{
s_expectedInput = L"\x1b[I";
auto inputEvent = std::make_unique<FocusEvent>(true);
VERIFY_ARE_EQUAL(true, pInput->HandleKey(inputEvent.get()), L"Verify FocusEvent from any other source was handled.");
}
}

void InputTest::TerminalInputModifierKeyTests()
Expand Down
2 changes: 1 addition & 1 deletion src/terminal/input/terminalInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ bool TerminalInput::HandleKey(const IInputEvent* const pInEvent)
// GH#13238 - Filter out focus events that came from the API.
if (focusEvent.CameFromApi())
{
return true;
return false;
}

return HandleFocus(focusEvent.GetFocus());
Expand Down
2 changes: 1 addition & 1 deletion src/types/inc/IInputEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class FocusEvent : public IInputEvent

private:
bool _focus;
const bool _cameFromApi;
bool _cameFromApi;

#ifdef UNIT_TESTING
friend std::wostream& operator<<(std::wostream& stream, const FocusEvent* const pFocusEvent);
Expand Down