@@ -178,6 +178,8 @@ class ScreenBufferTests
178178 TEST_METHOD (HardResetBuffer);
179179
180180 TEST_METHOD (RestoreDownAltBufferWithTerminalScrolling);
181+
182+ TEST_METHOD (ClearAlternateBuffer);
181183};
182184
183185void ScreenBufferTests::SingleAlternateBufferCreationTest ()
@@ -4219,6 +4221,8 @@ void ScreenBufferTests::RestoreDownAltBufferWithTerminalScrolling()
42194221 VERIFY_ARE_EQUAL (0 , altBuffer._viewport .Top ());
42204222 VERIFY_ARE_EQUAL (altBuffer._viewport .BottomInclusive (), altBuffer._virtualBottom );
42214223
4224+ auto useMain = wil::scope_exit ([&] { altBuffer.UseMainScreenBuffer (); });
4225+
42224226 const COORD originalSize = originalView.Dimensions ();
42234227 const COORD doubledSize = { originalSize.X * 2 , originalSize.Y * 2 };
42244228
@@ -4255,3 +4259,91 @@ void ScreenBufferTests::RestoreDownAltBufferWithTerminalScrolling()
42554259 VERIFY_ARE_EQUAL (altBuffer._viewport .BottomInclusive (), altBuffer._virtualBottom );
42564260 }
42574261}
4262+
4263+ void ScreenBufferTests::ClearAlternateBuffer ()
4264+ {
4265+ // This is a test for microsoft/terminal#1189. Refer to that issue for more
4266+ // context
4267+
4268+ CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals ().getConsoleInformation ();
4269+ auto & g = ServiceLocator::LocateGlobals ();
4270+ gci.LockConsole (); // Lock must be taken to manipulate buffer.
4271+ auto unlock = wil::scope_exit ([&] { gci.UnlockConsole (); });
4272+
4273+ auto & siMain = gci.GetActiveOutputBuffer ();
4274+ auto WriteText = [&](TextBuffer& tbi) {
4275+ // Write text to buffer
4276+ auto & stateMachine = siMain.GetStateMachine ();
4277+ auto & cursor = tbi.GetCursor ();
4278+ stateMachine.ProcessString (L" foo\n foo" );
4279+ VERIFY_ARE_EQUAL (cursor.GetPosition ().X , 3 );
4280+ VERIFY_ARE_EQUAL (cursor.GetPosition ().Y , 1 );
4281+ };
4282+
4283+ auto VerifyText = [&](TextBuffer& tbi) {
4284+ // Verify written text in buffer
4285+ {
4286+ auto iter00 = tbi.GetCellDataAt ({ 0 , 0 });
4287+ auto iter10 = tbi.GetCellDataAt ({ 1 , 0 });
4288+ auto iter20 = tbi.GetCellDataAt ({ 2 , 0 });
4289+ auto iter30 = tbi.GetCellDataAt ({ 3 , 0 });
4290+ auto iter01 = tbi.GetCellDataAt ({ 0 , 1 });
4291+ auto iter02 = tbi.GetCellDataAt ({ 1 , 1 });
4292+ auto iter03 = tbi.GetCellDataAt ({ 2 , 1 });
4293+ VERIFY_ARE_EQUAL (L" f" , iter00->Chars ());
4294+ VERIFY_ARE_EQUAL (L" o" , iter10->Chars ());
4295+ VERIFY_ARE_EQUAL (L" o" , iter20->Chars ());
4296+ VERIFY_ARE_EQUAL (L" \x20 " , iter30->Chars ());
4297+ VERIFY_ARE_EQUAL (L" f" , iter01->Chars ());
4298+ VERIFY_ARE_EQUAL (L" o" , iter02->Chars ());
4299+ VERIFY_ARE_EQUAL (L" o" , iter03->Chars ());
4300+ }
4301+ };
4302+
4303+ WriteText (siMain.GetTextBuffer ());
4304+ VerifyText (siMain.GetTextBuffer ());
4305+
4306+ Log::Comment (L" Create an alternate buffer" );
4307+ if (VERIFY_IS_TRUE (NT_SUCCESS (siMain.UseAlternateScreenBuffer ())))
4308+ {
4309+ VERIFY_IS_NOT_NULL (siMain._psiAlternateBuffer );
4310+ auto & altBuffer = *siMain._psiAlternateBuffer ;
4311+ VERIFY_ARE_EQUAL (0 , altBuffer._viewport .Top ());
4312+ VERIFY_ARE_EQUAL (altBuffer._viewport .BottomInclusive (), altBuffer._virtualBottom );
4313+
4314+ auto useMain = wil::scope_exit ([&] { altBuffer.UseMainScreenBuffer (); });
4315+
4316+ WriteText (altBuffer.GetTextBuffer ());
4317+ VerifyText (altBuffer.GetTextBuffer ());
4318+
4319+ #pragma region Test ScrollConsoleScreenBufferWImpl()
4320+ // Clear text of alt buffer (same params as in CMD)
4321+ VERIFY_SUCCEEDED (g.api .ScrollConsoleScreenBufferWImpl (siMain,
4322+ { 0 , 0 , 120 , 9001 },
4323+ { 0 , -9001 },
4324+ std::nullopt ,
4325+ L' ' ,
4326+ 7 ));
4327+
4328+ // Verify text is now gone
4329+ VERIFY_ARE_EQUAL (L" " , altBuffer.GetTextBuffer ().GetCellDataAt ({ 0 , 0 })->Chars ());
4330+ #pragma endregion
4331+
4332+ #pragma region Test SetConsoleCursorPositionImpl()
4333+ // Reset cursor position as we do with CLS command (same params as in CMD)
4334+ VERIFY_SUCCEEDED (g.api .SetConsoleCursorPositionImpl (siMain, { 0 }));
4335+
4336+ // Verify state of alt buffer
4337+ auto & altBufferCursor = altBuffer.GetTextBuffer ().GetCursor ();
4338+ VERIFY_ARE_EQUAL (altBufferCursor.GetPosition ().X , 0 );
4339+ VERIFY_ARE_EQUAL (altBufferCursor.GetPosition ().Y , 0 );
4340+ #pragma endregion
4341+ }
4342+
4343+ // Verify state of main buffer is untouched
4344+ auto & cursor = siMain.GetTextBuffer ().GetCursor ();
4345+ VERIFY_ARE_EQUAL (cursor.GetPosition ().X , 3 );
4346+ VERIFY_ARE_EQUAL (cursor.GetPosition ().Y , 1 );
4347+
4348+ VerifyText (siMain.GetTextBuffer ());
4349+ }
0 commit comments