Debug scenarios
When using AppCUI and needing to test the interface, it is recommended to write unit tests using the .debug_script(...) builder method. This method allows one to write a succession of system events (mouse clicks, keys being pressed, etc.) and validate whether the output is the expected one. This succession of commands is considered an event script, formed from multiple commands, each command written on a line. A command can have parameters. You can also use // to comment out a command.
General format for a script
{ ...settings...}
Command-1(param1,param2,param3)
Command-2()
// comment
Each command has a specfic form (Class.Verb) where the Class is the name of the class that implements the command, and the Verb is the name of the method that implements the command.
Currently, the following classes are supported:
Mouse- for mouse related commands (moving, dragging, clicking, double clicking, scrolling)Keyboard- for keyboard related commands (pressing keys, typing text, setting modifiers)Screen- for screen related commands (painting, repainting and asserting the virtual screen)Cursor- for cursor related commands (asserting the cursor position)Terminal- for terminal related commands (resizing the terminal)Clipboard- for clipboard related commands (setting the clipboard text)
Every command that has a format of Class.Assert evaluates the current state of the system and checks if it is as expected. If not, the script will panic (unless the assert.policy setting says otherwise - see the Settings section).
A script can also start with an optional settings block ({ ... }) that configures the script as a whole (see the Settings section).
Remarks:
.debug_script(...)will panic if the script is incorrect (a command is not valid, the number of parameters is incorrect, etc.).- associate
.size(...)with any.debug_script(...)to specify the simulated terminal size AppCUIallows only one instance at a time (this is done via a mutex object). If you have multiple unit tests and you try to run them with thecargo testcommand, you might get an error as cargo might try to use multiple threads to do this, and it is likely that one thread might try to start anAppCUIapplication while another one is already running on another thread. The solution in this case is to run the tests using a single thread:
cargo test -- --test-threads=1
Mouse related commands
Mouse related commands are a set of commands that simulate various mouse events
| Command | Purpose |
|---|---|
Mouse.Press(x,y,button) | simulates an event where the mouse button is being pressed while the mouse is located at a specific position on screen. The parameters x and y are a screen position, while the parameter button is one of left, right or center |
Mouse.Release(x,y,button) | simulates the release of the mouse buttons while the mouse is located at a specific screen position. The parameters x and y are a screen position, while the parameter button is one of left, right or center |
Mouse.Click(x,y,button) | simulates a click (hold and release). It is equivalent to - Mouse.Press(x,y,button) - Mouse.Release(x,y) |
Mouse.DoubleClick(x,y,button) | simulates a double-click (for a specific button) |
Mouse.Move(x,y) | simulates the movement of the mouse to coordinates (x,y). No mouse buttons are being pressed. |
Mouse.Drag(x1,y1,x2,y2) | simulates the movement of a mouse from (x1,y1) to (x2,y2) while the left button is being pressed |
Mouse.Scroll(x,y,direction,[times]) | simulates the mouse wheel being rotated in a direction (one of up, down, left, right) a number of times. The times parameter is optional and must be greater than 0 if present. |
Keyboard related commands
| Command | Purpose |
|---|---|
Keyboard.Press(key,[times]) | where the key parameter can be a key name or any combination of a control key and a regular key, such as- Z (for pressing the Z key)- Enter (for pressing the Enter key)- Alt+T (Alt + T combination)- Ctrl+Alt+F1 (Ctrl+Alt+F1 keys). The times parameter can be omitted. If present, it has to be at least 1 |
Keyboard.TypeText(text) | where the text parameter is text that is being typed.Example: Keyboard.TypeText('Hello world') will trigger the following keys to be pressed: H, e, l, l, o, Space, w, o, r, l, and d. |
Keyboard.SetModifiers(modifiers) | Sets the active keyboard modifiers. The modifiers parameter is a combination of Alt, Ctrl and Shift separated by +, or None to clear them all. Each call replaces the previous state. For example: Keyboard.SetModifiers(Alt+Ctrl) makes Alt and Ctrl the only active modifiers. |
Usually the key parameter can have several forms:
keymodifier-1+keymodifier-1+modifier-2+keymodifier-1+modifier-2+modifier-3+key
where the list of all keys supported by this command is:
- F-commands (
F1toF12) - Letters (
AtoZ) - with upper case - Numbers (
0to9) - Arrows (
Up,Down,Left,Right) - Navigation keys (
PageUp,PageDown,Home,End) - Deletion and Insertions (
Delete,Backspace,Insert) - White-spaces (
Space,Tab) - Other (
Enter,Escape)
and the list of modifiers consists of Shift, Ctrl, and Alt.
Screen related commands
| Command | Purpose |
|---|---|
Screen.Assert(hash) | checks if the hash computed over the current virtual screen is as expected. If not, it will panic. This is useful for unit testing. |
Screen.Paint([state_name]) | paints the current virtual screen to the current screen using ANSI codes and colors. This command also computes a hash over the current virtual screen and prints it. The optional parameter state_name is a name that can be used to reflect the current execution state. This is useful if multiple Screen.Paint commands are being used and you need to differentiate between them. |
Screen.Repaint() | requests a repaint of the current virtual screen without printing it. Unlike Screen.Paint, it is not ignored when screen.visible is false (mostly used for benchmarking). |
Cursor related commands
| Command | Purpose |
|---|---|
Cursor.Assert(x,y) | checks if the cursor (caret) is at a specific position. The parameters x and y are a screen position (not the coordinates relative to a control) |
Cursor.Assert(hidden) | checks if the cursor (caret) is hidden (not visible). |
System events
| Command | Purpose |
|---|---|
Terminal.Resize(width,height) | simulates a resize of the virtual terminal to the size represented by width and height parameters |
Clipboard commands
| Command | Purpose |
|---|---|
Clipboard.SetText(text) | sets a new text into a simulated clipboard. That text will be available to all controls if they want to paste it |
Clipboard.Clear() | clears the text from the simulated clipboard. |
Clipboard.Assert(text) | checks whether the clipboard contains specific text. This method is used to validate whether the Copy/Cut to clipboard command from a control worked properly. |
Settings
A script can start with an optional settings block, written between { and }. The block configures the script as a whole: it is read once, before any command runs, and its entries are settings, not commands.
{
screen.visible = true // true | false (default: true)
screen.output = ansi // ansi | text (default: ansi)
assert.policy = panic // panic | print | ignore (default: panic)
}
Screen.Paint('initial state')
Screen.Assert(0xB1471A30B30F5C6C)
| Setting | Values | Default | Description |
|---|---|---|---|
screen.visible | true, false | true | When false, the Screen.Paint commands are ignored and nothing is printed. Screen.Repaint() and the assertion commands still repaint the virtual screen. |
screen.output | ansi, text | ansi | ansi prints the screen using ANSI colors. text prints plain text, without any escape sequences (including in the [Error] lines). It has no effect if screen.visible is false. |
assert.policy | panic, print, ignore | panic | What happens when a Screen.Assert, Cursor.Assert or Clipboard.Assert fails. panic panics. print prints an [Error] line and the script continues. ignore skips all assertion work (no hash is computed and nothing is checked), which is useful for benchmarks. |
Rules:
- The block is optional. If it exists, it has to be the first element of the script (only spaces, tabs and new lines can appear before the
{). - Entries (
key = value) are separated by commas, by new lines, or both. A trailing comma is allowed. - Comment lines (
//,#and;) are allowed inside the block. - Keys are lowercase and have the form
group.name. Values are not case sensitive. - If the script is built with the
format!macro, the braces of the block have to be escaped as{{and}}.
Example
Let’s consider a scenario where we want to test if moving a window with a mouse works as expected. For this we will create a test function, with the following code:
#[test]
fn check_if_window_can_be_moved() {
let script = "
Screen.Paint('initial state')
Screen.Assert(0xB1471A30B30F5C6C)
Mouse.Drag(30,3,35,5)
Screen.Paint('window was moved')
Screen.Assert(0x419533D4BBEFE538)
";
App::new()
.size(Size::new(60, 10))
.debug_script(script)
.window(|| Window::new("Title", layout!("a:c,w:20,h:5"), window::Flags::None))
.run()
.unwrap();
}
Let’s break the event script in pieces and see exactly what is supposed to happen:
Screen.Paint('initial state')- this will print the virtual screen. It should look like the following (but with colors):
+=================================================================+
| Name : initial state |
| Hash : 0xB1471A30B30F5C6C |
| Cursor: Hidden |
| --------------------------------------------------------------- |
| 11111111112222222222333333333344444444445555555555 |
| 012345678901234567890123456789012345678901234567890123456789 |
| --------------------------------------------------------------- |
| 0 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 2 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 3 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╔════ Title ════[x]╗▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 4 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 5 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 6 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 7 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╚══════════════════╝▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 8 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 9 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| --------------------------------------------------------------- |
We can inspect whether the position of the window is correct. We can also see the hash computed for the entire virtual screen: 0xB1471A30B30F5C6C (this can help us perform further checks).
-
Screen.Assert(0xB1471A30B30F5C6C)- this computes the hash for the entire virtual screen and then checks it against the expected one. The usual scenario here is that we first apply aScreen.Paintcommand, validate it, and then write theScreen.Assertcommand with the hash obtained from theScreen.Paintcommand. This way, if something changes in the logic or code of the program, the new hash will be different. If the hash for the virtual screen is not as expected, the application will panic. If used in a test, this behavior will fail the test. -
Mouse.Drag(30,3,35,5)this command does the following:- moves the mouse to the (30,3) coordinate (over the title of the window)
- click and hold the left mouse button
- moves the mouse to a new position (35,5) (since we hold the mouse button, we expect the window to move as well)
- releases the left mouse button
-
Screen.Paint('window was moved')now we should see something like the following. Notice that indeed, the window was moved to a new position. We also have a new hash for the virtual screen:0x419533D4BBEFE538
+================================================================+
| Name : window was moved |
| Hash : 0x419533D4BBEFE538 |
| Cursor: Hidden |
| -------------------------------------------------------------- |
| 11111111112222222222333333333344444444445555555555 |
| 012345678901234567890123456789012345678901234567890123456789 |
| -------------------------------------------------------------- |
| 0 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 2 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 3 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 4 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 5 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╔════ Title ════[x]╗▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 6 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 7 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 8 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║ ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| 9 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒╚══════════════════╝▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
| -------------------------------------------------------------- |
Screen.Assert(0x419533D4BBEFE538)- finally we check the new hash to see if it matches the one we expect.
Remark: Using unit tests (while it works with the Screen.Paint command activated) might look strange on the actual screen (especially if all you need is to validate an example). As such, it is best that after an example such as the previous one has been validated, you add a settings block at the beginning of the script: { screen.visible = false }. This will not change the logic of the script; instead, it will not print anything on the screen. The final test function should then look like this:
use appcui::prelude::*;
#[test]
fn check_if_window_can_be_moved() {
let script = "
{ screen.visible = false }
Screen.Paint('initial state')
Screen.Assert(0xB1471A30B30F5C6C)
Mouse.Drag(30,3,35,5)
Screen.Paint('window was moved')
Screen.Assert(0x419533D4BBEFE538)
";
App::new()
.size(Size::new(60, 10))
.debug_script(script)
.window(|| Window::new("Title", layout!("a:c,w:20,h:5"), window::Flags::None))
.run()
.unwrap();
}
and its execution should produce an output similar to the next one:
running 1 test
test check_if_window_can_be_moved ... ok