2024-02-18 08:26:32 +01:00
|
|
|
# SPDX-FileCopyrightText: 2024 Leroy Hopson <godot-xterm@leroy.nix.nz>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
class_name RenderingTest extends GodotXtermTest
|
2024-02-18 08:26:32 +01:00
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
|
|
|
|
func get_described_class():
|
|
|
|
return Terminal
|
2024-02-18 08:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Return the color in the center of the given cell.
|
|
|
|
func pick_cell_color(cell := Vector2i(0, 0)) -> Color:
|
2024-03-03 03:34:18 +01:00
|
|
|
var cell_size = subject.get_cell_size()
|
2024-02-18 08:26:32 +01:00
|
|
|
var pixelv = Vector2(cell) * cell_size + (cell_size / 2)
|
2024-03-03 10:25:14 +01:00
|
|
|
return get_viewport().get_texture().get_image().get_pixelv(pixelv)
|
2024-02-18 08:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
func before_each():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject = described_class.new()
|
|
|
|
subject.add_theme_font_override("normal_font", preload("res://themes/fonts/regular.tres"))
|
|
|
|
subject.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
|
|
watch_signals(subject)
|
|
|
|
call_deferred("add_child_autofree", subject)
|
|
|
|
await wait_for_signal(subject.ready, 5)
|
2024-02-18 08:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestRendering:
|
|
|
|
extends RenderingTest
|
|
|
|
|
2024-03-03 10:25:14 +01:00
|
|
|
# Fill the terminal with colored blocks.
|
|
|
|
func fill_color(color = Color.BLACK, rows: int = subject.get_rows()):
|
|
|
|
subject.write("\u001b[38;2;%d;%d;%dm" % [color.r8, color.g8, color.b8])
|
|
|
|
subject.write("█".repeat(subject.get_cols() * rows))
|
|
|
|
|
|
|
|
func test_render():
|
|
|
|
fill_color(Color.BLUE)
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
|
|
|
await wait_frames(15)
|
|
|
|
var cell_color = pick_cell_color()
|
|
|
|
assert_eq(cell_color, Color.BLUE)
|
|
|
|
|
2024-02-18 08:26:32 +01:00
|
|
|
func test_update():
|
2024-03-03 10:25:14 +01:00
|
|
|
fill_color(Color.RED)
|
2024-02-18 08:26:32 +01:00
|
|
|
await get_tree().physics_frame
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.queue_redraw()
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
2024-02-24 04:58:43 +01:00
|
|
|
await wait_frames(15)
|
2024-02-18 08:26:32 +01:00
|
|
|
var cell_color = pick_cell_color(Vector2i(0, 0))
|
|
|
|
assert_eq(cell_color, Color.RED)
|
2024-02-25 08:25:32 +01:00
|
|
|
|
2024-03-03 10:25:14 +01:00
|
|
|
func test_clear_clears_all_but_the_first_row():
|
|
|
|
await wait_frames(15)
|
|
|
|
var cell = Vector2i(0, 1) # Pick a cell not on the first row.
|
|
|
|
var original_color = pick_cell_color(cell)
|
|
|
|
call_deferred("fill_color", Color.CYAN)
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
|
|
|
await wait_frames(15)
|
|
|
|
subject.clear()
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
|
|
|
await wait_frames(15)
|
|
|
|
var cell_color = pick_cell_color(cell)
|
|
|
|
assert_eq(cell_color, original_color)
|
|
|
|
|
|
|
|
func test_clear_keeps_the_last_row():
|
|
|
|
fill_color(Color.GREEN)
|
|
|
|
fill_color(Color.ORANGE, 1)
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
|
|
|
await wait_frames(15)
|
|
|
|
subject.clear()
|
|
|
|
await wait_for_signal(subject.draw, 3)
|
|
|
|
await wait_frames(15)
|
|
|
|
var cell_color = pick_cell_color(Vector2i(0, 0))
|
|
|
|
assert_eq(cell_color, Color.ORANGE)
|
|
|
|
|
2024-02-25 08:25:32 +01:00
|
|
|
|
|
|
|
class TestKeyPressed:
|
|
|
|
extends RenderingTest
|
|
|
|
|
|
|
|
var input_event: InputEventKey
|
|
|
|
|
|
|
|
func before_each():
|
|
|
|
await super.before_each()
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.grab_focus()
|
2024-02-25 08:25:32 +01:00
|
|
|
|
|
|
|
input_event = InputEventKey.new()
|
|
|
|
input_event.pressed = true
|
|
|
|
Input.call_deferred("parse_input_event", input_event)
|
|
|
|
|
|
|
|
func test_key_pressed_emitted_on_key_input():
|
|
|
|
input_event.keycode = KEY_A
|
|
|
|
input_event.unicode = "a".unicode_at(0)
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
await wait_for_signal(subject.key_pressed, 1)
|
|
|
|
assert_signal_emitted(subject, "key_pressed")
|
2024-02-25 08:25:32 +01:00
|
|
|
|
2024-02-25 10:06:58 +01:00
|
|
|
func test_key_pressed_emitted_only_once_per_key_input():
|
|
|
|
input_event.keycode = KEY_B
|
|
|
|
input_event.unicode = "b".unicode_at(0)
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
await wait_for_signal(subject.key_pressed, 1)
|
|
|
|
assert_signal_emit_count(subject, "key_pressed", 1)
|
2024-02-25 10:06:58 +01:00
|
|
|
|
2024-02-25 08:25:32 +01:00
|
|
|
func test_key_pressed_emits_interpreted_key_input_as_first_param():
|
|
|
|
input_event.keycode = KEY_UP
|
|
|
|
input_event.unicode = 0
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
await wait_for_signal(subject.key_pressed, 1)
|
2024-02-25 08:25:32 +01:00
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
var signal_parameters = get_signal_parameters(subject, "key_pressed", 0)
|
2024-02-25 08:25:32 +01:00
|
|
|
assert_eq(signal_parameters[0], "\u001b[A")
|
|
|
|
|
|
|
|
func test_key_pressed_emits_original_input_event_as_second_param():
|
|
|
|
input_event.keycode = KEY_L
|
|
|
|
input_event.unicode = "l".unicode_at(0)
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
await wait_for_signal(subject.key_pressed, 1)
|
2024-02-25 08:25:32 +01:00
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
var signal_parameters = get_signal_parameters(subject, "key_pressed", 0)
|
2024-02-25 08:25:32 +01:00
|
|
|
assert_eq(signal_parameters[1], input_event)
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
func test_key_pressed_not_emitted_when_writing_to_subject():
|
|
|
|
subject.write("a")
|
2024-02-25 08:25:32 +01:00
|
|
|
await wait_frames(1)
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_signal_emit_count(subject, "key_pressed", 0)
|
2024-02-25 08:25:32 +01:00
|
|
|
|
|
|
|
func test_key_pressed_not_emitted_by_other_input_type():
|
|
|
|
var mouse_input = InputEventMouseButton.new()
|
|
|
|
mouse_input.button_index = MOUSE_BUTTON_LEFT
|
|
|
|
mouse_input.pressed = true
|
|
|
|
Input.call_deferred("parse_input_event", mouse_input)
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
await wait_for_signal(subject.gui_input, 1)
|
|
|
|
assert_signal_emit_count(subject, "key_pressed", 0)
|