Back to Repositories

Testing Terminal Control Sequence Implementation in Termux-App

This test suite validates DEC Private Mode Set (DECSET) and Reset (DECRST) functionality in the Termux terminal emulator. It ensures proper handling of terminal control sequences for cursor visibility, bracketed paste mode, and text wraparound behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of DECSET/DECRST terminal control sequences.

Key functionality tested includes:
  • Cursor visibility control (DECTCEM)
  • Bracketed paste mode handling
  • Text wraparound mode (DECAWM)
Edge cases covered include terminal reset behavior, escape character handling, and C1 control code processing.

Implementation Analysis

The testing approach uses JUnit framework with a custom TerminalTestCase base class for terminal emulation testing. The implementation follows a systematic pattern of setting up test conditions, executing terminal commands, and verifying expected behavior through assertions.

Framework-specific features utilized include custom assertion methods and terminal size configuration helpers.

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Custom terminal emulation test harness
  • Terminal size configuration utilities
  • Output capture and verification mechanisms
  • ANSI escape sequence handling

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear test organization and thorough verification.

Notable practices include:
  • Isolated test methods for specific functionality
  • Comprehensive state verification
  • Reset handling verification
  • Edge case coverage
  • Clear test method naming

termux/termux-app

terminal-emulator/src/test/java/com/termux/terminal/DecSetTest.java

            
package com.termux.terminal;

/**
 * <pre>
 * "CSI ? Pm h", DEC Private Mode Set (DECSET)
 * </pre>
 * <p/>
 * and
 * <p/>
 * <pre>
 * "CSI ? Pm l", DEC Private Mode Reset (DECRST)
 * </pre>
 * <p/>
 * controls various aspects of the terminal
 */
public class DecSetTest extends TerminalTestCase {

	/** DECSET 25, DECTCEM, controls visibility of the cursor. */
	public void testEnableDisableCursor() {
		withTerminalSized(3, 3);
		assertTrue("Initially the cursor should be enabled", mTerminal.isCursorEnabled());
		enterString("\033[?25l"); // Disable Cursor (DECTCEM).
		assertFalse(mTerminal.isCursorEnabled());
		enterString("\033[?25h"); // Enable Cursor (DECTCEM).
		assertTrue(mTerminal.isCursorEnabled());

		enterString("\033[?25l"); // Disable Cursor (DECTCEM), again.
		assertFalse(mTerminal.isCursorEnabled());
		mTerminal.reset();
		assertTrue("Resetting the terminal should enable the cursor", mTerminal.isCursorEnabled());

		enterString("\033[?25l");
		assertFalse(mTerminal.isCursorEnabled());
		enterString("\033c"); // RIS resetting should enabled cursor.
		assertTrue(mTerminal.isCursorEnabled());
	}

	/** DECSET 2004, controls bracketed paste mode. */
	public void testBracketedPasteMode() {
		withTerminalSized(3, 3);

		mTerminal.paste("a");
		assertEquals("Pasting 'a' should output 'a' when bracketed paste mode is disabled", "a", mOutput.getOutputAndClear());

		enterString("\033[?2004h"); // Enable bracketed paste mode.
		mTerminal.paste("a");
		assertEquals("Pasting when in bracketed paste mode should be bracketed", "\033[200~a\033[201~", mOutput.getOutputAndClear());

		enterString("\033[?2004l"); // Disable bracketed paste mode.
		mTerminal.paste("a");
		assertEquals("Pasting 'a' should output 'a' when bracketed paste mode is disabled", "a", mOutput.getOutputAndClear());

		enterString("\033[?2004h"); // Enable bracketed paste mode, again.
		mTerminal.paste("a");
		assertEquals("Pasting when in bracketed paste mode again should be bracketed", "\033[200~a\033[201~", mOutput.getOutputAndClear());

		mTerminal.paste("\033ab\033cd\033");
		assertEquals("Pasting an escape character should not input it", "\033[200~abcd\033[201~", mOutput.getOutputAndClear());
		mTerminal.paste("\u0081ab\u0081cd\u009F");
		assertEquals("Pasting C1 control codes should not input it", "\033[200~abcd\033[201~", mOutput.getOutputAndClear());

		mTerminal.reset();
		mTerminal.paste("a");
		assertEquals("Terminal reset() should disable bracketed paste mode", "a", mOutput.getOutputAndClear());
	}

	/** DECSET 7, DECAWM, controls wraparound mode. */
	public void testWrapAroundMode() {
		// Default with wraparound:
		withTerminalSized(3, 3).enterString("abcd").assertLinesAre("abc", "d  ", "   ");
		// With wraparound disabled:
		withTerminalSized(3, 3).enterString("\033[?7labcd").assertLinesAre("abd", "   ", "   ");
		enterString("efg").assertLinesAre("abg", "   ", "   ");
		// Re-enabling wraparound:
		enterString("\033[?7hhij").assertLinesAre("abh", "ij ", "   ");
	}

}