mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.24k stars 243 forks source link

How to write tests for Lanterna GUI's (blocking issue) #486

Closed AObuchow closed 4 years ago

AObuchow commented 4 years ago

I'm trying to write unit tests for my app which uses Lanterna for the GUI.

I'm able to emulate keystroke's using AbstractWindow.handleInput(KeyStroke). However there are cases where the GUI will block until another keystroke is given (eg. when a dialog is shown or when a text input box is activated) and I'm unable to send another keystroke until the GUI has finished blocking.

Is there a suggested way to test Lanterna GUI's by sending synthetic keystrokes? I tried running the handleInput in a CompletableFuture but it doesn't seem to have an effect (no keystroke is read).

Maybe https://github.com/mabe02/lanterna/issues/158 would help?

Thanks so much :)

avl42 commented 4 years ago

You will need to spoof, rather than circumvent the input system.

that means, you'll need to replace the Terminal by one that reads input from your testdriver rather than from a real (or swing) terminal.

Andrew O. notifications@github.com schrieb am Do., 28. Mai 2020 14:45:

I'm trying to write unit tests for my app which uses Lanterna for the GUI.

I'm able to emulate keystroke's using AbstractWindow.handleInput(KeyStroke). However there are cases where the GUI will block until another keystroke is given (eg. when a dialog is shown or when a text input box is activated) and I'm unable to send another keystroke until the GUI has finished blocking.

Is there a suggested way to test Lanterna GUI's by sending synthetic keystrokes? I tried running the handleInput in a CompletableFuture but it doesn't seem to have an effect (no keystroke is read).

Maybe #158 https://github.com/mabe02/lanterna/issues/158 would help?

Thanks so much :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mabe02/lanterna/issues/486, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIDBMURIQX2IJ3EJEEITX3RTZMFNANCNFSM4NNBV4KQ .

AObuchow commented 4 years ago

Thanks for the quick reply.

Is there an example of such terminal (eg. in Lanterna's test suite)? Or is there an existing implementation of a Terminal that would be best to reference when implementing this terminal.?

avl42 commented 4 years ago

By a little stretch you could consider the SwingTerminals as "spoof" Terminals, in that the SwingTerminals implement their input and output differently than by I/O'ing with a "tty".

Essentially, you'd start a new implementation of class Terminal (or ExtendedTerminal, if you also want to emulate Mouse and more), and then your IDE ought to generate empty methods that you'd have to implement.

Some of those you might just leave empty, like (enter-&exitPrivateMode) but you'll likely need to implement pollInput and readInput (from super-interface InputProvider), and you'll have to find a way how to get the input from your test-suite.

For output, you can leave empty almost all the methods, if your test-suite instead inspects the TerminalScreen to verify the output.

It's not trivial, but no rocket science, either.

On Thu, May 28, 2020 at 4:10 PM Andrew O. notifications@github.com wrote:

Thanks for the quick reply.

Is there an example of such terminal (eg. in Lanterna's test suite)? Or is there an existing implementation of a Terminal that would be best to reference when implementing this terminal.?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

AObuchow commented 4 years ago

Awesome, thank you for the tips :)

I'll report if I have any trouble.

Maybe if I do a nice enough implementation, it could be useful upstream for other users to easily test their Lanterna GUI :D

mabe02 commented 4 years ago

Are you ok with this answer, can I close the issue?

AObuchow commented 4 years ago

@mabe02 yes we can close it. If I have further questions on the topic I'll post them here.