Skip to content

Commit

Permalink
Migrate some TextInput and CodeInput tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthaTi committed Jan 16, 2025
1 parent 1f9e130 commit 82da3c4
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/nodes/code_input.d
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,60 @@ unittest {

}

@("CodeInput.paste creates a history entry")
unittest {

auto input = codeInput(.useSpaces(2));
auto clipboard = clipboardChain();
auto root = chain(clipboard, input);

root.draw();
clipboard.value = "World";
input.push(" Hello,");
input.runInputAction!(FluidInputAction.breakLine);
input.paste();
input.push("!");
assert(input.value == " Hello,\n World!");

// Undo the exclamation mark
input.undo();
assert(input.value == " Hello,\n World");

// Undo moves before pasting
input.undo();
assert(input.value == " Hello,\n ");
assert(input.valueBeforeCaret == input.value);

// Next undo moves before line break
input.undo();
assert(input.value == " Hello,");

// Next undo clears all changes
input.undo();
assert(input.value == "");

// No change
input.undo();
assert(input.value == "");

// It can all be redone
input.redo();
assert(input.value == " Hello,");
assert(input.valueBeforeCaret == input.value);
input.redo();
assert(input.value == " Hello,\n ");
assert(input.valueBeforeCaret == input.value);
input.redo();
assert(input.value == " Hello,\n World");
assert(input.valueBeforeCaret == input.value);
input.redo();
assert(input.value == " Hello,\n World!");
assert(input.valueBeforeCaret == input.value);
input.redo();
assert(input.value == " Hello,\n World!");

}

@("CodeInput.paste creates a history entry (single line)")
unittest {

Expand Down
160 changes: 160 additions & 0 deletions tests/nodes/text_input.d
Original file line number Diff line number Diff line change
Expand Up @@ -1587,3 +1587,163 @@ unittest {
assert(input.value == content);

}

@("TextInput: Mouse selections works correctly across lines")
unittest {

import std.math : isClose;

auto theme = nullTheme.derive(
rule!TextInput(
Rule.selectionBackgroundColor = color("#02a"),
Rule.fontSize = 14.pt,
),
);
auto input = textInput(.multiline, theme);
auto focus = focusChain();
auto hover = hoverChain();
auto root = chain(focus, hover, input);

input.value = "Line one\nLine two\n\nLine four";
focus.currentFocus = input;
root.draw();

auto lineHeight = input.style.getTypeface.lineHeight;

// Move the caret to second line
input.caretIndex = "Line one\nLin".length;
input.updateCaretPosition();

const middle = input.caretPosition;
const top = middle - Vector2(0, lineHeight);
const blank = middle + Vector2(0, lineHeight);
const bottom = middle + Vector2(0, lineHeight * 2);

// Press in the middle and drag to the top
hover.point(middle)
.then((a) {
a.press(false);
return a.move(top);
})

// Check results; move to bottom
.then((a) {
a.press(false);
assert(input.selectedValue == "e one\nLin");
assert(input.selectionStart > input.selectionEnd);
return a.move(bottom);
})

// Now move to the blank line
.then((a) {
a.press(false);
assert(input.selectedValue == "e two\n\nLin");
assert(input.selectionStart < input.selectionEnd);
return a.move(blank);
})
.then((a) {
a.press(true);
assert(input.selectedValue == "e two\n");
assert(input.selectionStart < input.selectionEnd);
})
.runWhileDrawing(root);

}

version (TODO)
unittest {

{
// Double click
io.mousePosition = middle;
root._lastClick = SysTime.init;

foreach (i; 0..2) {

io.nextFrame();
io.release();
root.draw();

io.nextFrame();
io.press();
root.draw();

}

assert(root.selectedValue == "Line");
assert(root.selectionStart < root.selectionEnd);

// Move it to top row
io.nextFrame();
io.mousePosition = top;
root.draw();

assert(root.selectedValue == "Line one\nLine");
assert(root.selectionStart > root.selectionEnd);

// Move it to bottom row
io.nextFrame();
io.mousePosition = bottom;
root.draw();

assert(root.selectedValue == "Line two\n\nLine");
assert(root.selectionStart < root.selectionEnd);

// And to the blank line
io.nextFrame();
io.mousePosition = blank;
root.draw();

assert(root.selectedValue == "Line two\n");
assert(root.selectionStart < root.selectionEnd);

}

{

// Triple
io.mousePosition = middle;
root._lastClick = SysTime.init;

foreach (i; 0..3) {

io.nextFrame();
io.release();
root.draw();

io.nextFrame();
io.press();
root.draw();

}

assert(root.selectedValue == "Line two");
assert(root.selectionStart < root.selectionEnd);

// Move it to top row
io.nextFrame();
io.mousePosition = top;
root.draw();

assert(root.selectedValue == "Line one\nLine two");
assert(root.selectionStart > root.selectionEnd);

// Move it to bottom row
io.nextFrame();
io.mousePosition = bottom;
root.draw();

assert(root.selectedValue == "Line two\n\nLine four");
assert(root.selectionStart < root.selectionEnd);

// And to the blank line
io.nextFrame();
io.mousePosition = blank;
root.draw();

assert(root.selectedValue == "Line two\n");
assert(root.selectionStart < root.selectionEnd);

}

}

0 comments on commit 82da3c4

Please sign in to comment.