From d45ea7a3cd5769fe37c12977d43f35a543edaeee Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Mon, 22 Aug 2022 15:54:00 +1200 Subject: [PATCH] Update libtsm to not erase screen on palette change Fixes #57. Fixes #58. --- addons/godot_xterm/native/thirdparty/libtsm | 2 +- test/integration/terminal.test.gd | 136 ++++++++++++++++++++ test/scenes/theme.gd | 33 +++++ test/scenes/theme.tscn | 8 ++ 4 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 test/scenes/theme.gd create mode 100644 test/scenes/theme.tscn diff --git a/addons/godot_xterm/native/thirdparty/libtsm b/addons/godot_xterm/native/thirdparty/libtsm index 308bc9d..0c2d956 160000 --- a/addons/godot_xterm/native/thirdparty/libtsm +++ b/addons/godot_xterm/native/thirdparty/libtsm @@ -1 +1 @@ -Subproject commit 308bc9dc51819c1da0b4690e9a0654d28e83fc3c +Subproject commit 0c2d956b3110b72d35951dbf38ef6ed71e4bda9a diff --git a/test/integration/terminal.test.gd b/test/integration/terminal.test.gd index 37c3809..0217350 100644 --- a/test/integration/terminal.test.gd +++ b/test/integration/terminal.test.gd @@ -37,3 +37,139 @@ func test_writing_random_data_to_terminal_does_not_crash_application(): add_child_autofree(preload("res://test/scenes/write_random.tscn").instance()) yield(yield_frames(5, "Writing random data to terminal"), YIELD) assert_true(true, "Expected no crash when writing random data to terminal.") + + +class TestTheme: + extends "res://addons/gut/test.gd" + + const Terminal := preload("res://addons/godot_xterm/terminal.gd") + const TestScene := preload("../scenes/theme.tscn") + + const default_theme := preload("res://addons/godot_xterm/themes/default.tres") + const alt_theme := preload("res://addons/godot_xterm/themes/default_light.tres") + + const COLORS := [ + "Black", + "Red", + "Green", + "Yellow", + "Blue", + "Magenta", + "Cyan", + "Light Grey", + "Dark Grey", + "Light Red", + "Light Green", + "Light Yellow", + "Light Blue", + "Light Magenta", + "Light Cyan", + "White", + ] + + var terminal: Terminal + + # Current themes don't work with Godot 3.5. So skip tests on this version. + static func _version_gt_3_4() -> bool: + var version := Engine.get_version_info() + return version.major > 3 or (version.major == 3 and version.minor > 4) + + func _get_pixelv(src: Vector2) -> Color: + var screen := get_tree().root.get_texture().get_data() + screen.lock() + screen.flip_y() + var pixel := screen.get_pixelv(src) + screen.unlock() + return pixel + + func _check_colors(theme: Theme): + var cell_size := Vector2( + int(terminal.rect_size.x / terminal.get_cols()), + int(terminal.rect_size.y / terminal.get_rows()) + ) + var src := cell_size / 2 + + for i in range(16): + var color_name = COLORS[i] + var expected_color = "#" + theme.get_color(color_name, "Terminal").to_html(false) + var actual_color = "#" + _get_pixelv(src).to_html(false) + assert_eq( + actual_color, + expected_color, + "Expected color '%s' to be displayed for '%s'." % [expected_color, color_name] + ) + src += Vector2(cell_size.x, 0) + + func before_each(): + terminal = autofree(TestScene.instance()) + yield(yield_frames(1), YIELD) + + func test_terminal_display_colors_from_default_theme(): + if _version_gt_3_4(): + return + terminal.theme = null + add_child(terminal) + yield(yield_to(terminal, "theme_changed", 5), YIELD) + _check_colors(default_theme) + + func test_terminal_displays_colors_from_theme(): + if _version_gt_3_4(): + return + terminal.theme = alt_theme + add_child(terminal) + yield(yield_to(terminal, "theme_changed", 5), YIELD) + _check_colors(alt_theme) + + func test_visible_characters_still_displayed_after_resize_with_default_theme(): + if _version_gt_3_4(): + return + terminal.theme = null + add_child(terminal) + yield(yield_frames(1), YIELD) + OS.window_size += Vector2(1, 0) + yield(yield_to(terminal, "size_changed", 5), YIELD) + _check_colors(default_theme) + + func test_visible_characters_still_displayed_after_resize_with_custom_theme(): + if _version_gt_3_4(): + return + # Issue 57: https://github.com/lihop/godot-xterm/issues/57 + terminal.theme = alt_theme + add_child(terminal) + yield(yield_to(terminal, "theme_changed", 5), YIELD) + OS.window_size += Vector2(1, 0) + yield(yield_to(terminal, "size_changed", 5), YIELD) + _check_colors(alt_theme) + + func test_updates_colors_after_theme_set(): + if _version_gt_3_4(): + return + # Issue 58: https://github.com/lihop/godot-xterm/issues/58 + terminal.theme = null + add_child(terminal) + yield(yield_frames(1), YIELD) + terminal.theme = alt_theme + yield(yield_to(terminal, "theme_changed", 50), YIELD) + _check_colors(alt_theme) + + func test_updates_colors_after_theme_unset(): + if _version_gt_3_4(): + return + # Issue 58: https://github.com/lihop/godot-xterm/issues/58 + terminal.theme = alt_theme + add_child(terminal) + yield(yield_to(terminal, "theme_changed", 5), YIELD) + terminal.theme = null + yield(yield_to(terminal, "theme_changed", 5), YIELD) + _check_colors(default_theme) + + func test_updates_colors_after_theme_changed(): + if _version_gt_3_4(): + return + # Issue 58: https://github.com/lihop/godot-xterm/issues/58 + terminal.theme = alt_theme + add_child(terminal) + yield(yield_to(terminal, "theme_changed", 5), YIELD) + terminal.theme = default_theme + yield(yield_to(terminal, "theme_changed", 5), YIELD) + _check_colors(default_theme) diff --git a/test/scenes/theme.gd b/test/scenes/theme.gd new file mode 100644 index 0000000..14a2a38 --- /dev/null +++ b/test/scenes/theme.gd @@ -0,0 +1,33 @@ +tool +extends "res://addons/godot_xterm/terminal.gd" + +signal theme_changed + + +func _ready(): + # Print every background color. + for i in range(8): + write("\u001b[4%dm " % i) # Regular. + for i in range(8): + write("\u001b[10%dm " % i) # Bright. + write("\u001b[0m") # Reset. + + # Print every foreground color. + for i in range(8): + write("\u001b[3%dm█" % i) # Regular. + for i in range(8): + write("\u001b[9%dm█" % i) # Bright. + write("\u001b[0m") # Reset. + + # Print every font. + write("L\u001b[0m") # Regular. + write("\u001b[1mL\u001b[0m") # Bold. + write("\u001b[3mL\u001b[0m") # Italic. + write("\u001b[1m\u001b[3mL\u001b[0m") # Bold Italic. + + +func _notification(what): + ._notification(what) + match what: + NOTIFICATION_THEME_CHANGED: + call_deferred("emit_signal", "theme_changed") diff --git a/test/scenes/theme.tscn b/test/scenes/theme.tscn new file mode 100644 index 0000000..079e8ed --- /dev/null +++ b/test/scenes/theme.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://test/scenes/theme.gd" type="Script" id=1] + +[node name="Terminal" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 )