diff --git a/src/cascadia/TerminalCore/TerminalApi.cpp b/src/cascadia/TerminalCore/TerminalApi.cpp index 7d526151a42..b54a22581f5 100644 --- a/src/cascadia/TerminalCore/TerminalApi.cpp +++ b/src/cascadia/TerminalCore/TerminalApi.cpp @@ -382,15 +382,18 @@ bool Terminal::SetWindowTitle(std::wstring_view title) // - true iff we successfully updated the color table entry. bool Terminal::SetColorTableEntry(const size_t tableIndex, const COLORREF dwColor) { - if (tableIndex > _colorTable.size()) + try + { + _colorTable.at(tableIndex) = dwColor; + + // Repaint everything - the colors might have changed + _buffer->GetRenderTarget().TriggerRedrawAll(); + return true; + } + catch (std::out_of_range&) { return false; } - _colorTable.at(tableIndex) = dwColor; - - // Repaint everything - the colors might have changed - _buffer->GetRenderTarget().TriggerRedrawAll(); - return true; } // Method Description: diff --git a/src/cascadia/UnitTests_TerminalCore/TerminalApiTest.cpp b/src/cascadia/UnitTests_TerminalCore/TerminalApiTest.cpp new file mode 100644 index 00000000000..a4c08504b71 --- /dev/null +++ b/src/cascadia/UnitTests_TerminalCore/TerminalApiTest.cpp @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "precomp.h" +#include + +#include "../cascadia/TerminalCore/Terminal.hpp" +#include "MockTermSettings.h" +#include "../renderer/inc/DummyRenderTarget.hpp" +#include "consoletaeftemplates.hpp" + +using namespace winrt::Microsoft::Terminal::Settings; +using namespace Microsoft::Terminal::Core; + +namespace TerminalCoreUnitTests +{ +#define WCS(x) WCSHELPER(x) +#define WCSHELPER(x) L#x + + class TerminalApiTest + { + TEST_CLASS(TerminalApiTest); + + TEST_METHOD(SetColorTableEntry) + { + Terminal term; + DummyRenderTarget emptyRT; + term.Create({ 100, 100 }, 0, emptyRT); + + auto settings = winrt::make(100, 100, 100); + term.UpdateSettings(settings); + + VERIFY_IS_TRUE(term.SetColorTableEntry(0, 100)); + VERIFY_IS_TRUE(term.SetColorTableEntry(128, 100)); + VERIFY_IS_TRUE(term.SetColorTableEntry(255, 100)); + + VERIFY_IS_FALSE(term.SetColorTableEntry(256, 100)); + VERIFY_IS_FALSE(term.SetColorTableEntry(512, 100)); + } + }; +} diff --git a/src/cascadia/UnitTests_TerminalCore/UnitTests.vcxproj b/src/cascadia/UnitTests_TerminalCore/UnitTests.vcxproj index d4911ce43d6..feec2c0c73a 100644 --- a/src/cascadia/UnitTests_TerminalCore/UnitTests.vcxproj +++ b/src/cascadia/UnitTests_TerminalCore/UnitTests.vcxproj @@ -6,12 +6,10 @@ TerminalCoreUnitTests UnitTests_TerminalCore Terminal.Core.Unit.Tests - DynamicLibrary + DynamicLibrary - - @@ -19,6 +17,7 @@ Create + @@ -56,4 +55,4 @@ - + \ No newline at end of file