winzik/engine/tests/Gui/FocusKeyboardTest.php
David Nurdin 07a73b561b Gui : un tap hors d'un champ de texte enlève le focus et le clavier du téléphone
- un appui hors de tout widget (autour d'un dialogue, vide) lâche aussi le focus
- Échap / retour Android avec le clavier ouvert ferme d'abord le clavier
  (sur téléphone / écran tactile ; Form::$softKeyboard pour les tests)
- clavier fermé par le système (retour Android : SDL arrête la saisie
  lui-même) : le champ lâche le focus au lieu de relancer la saisie
- état de saisie SDL partagé entre formulaires (statique), textInputOn()
  observable pour les tests ; tests FocusKeyboardTest

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JkYTqrBXzYFUXiSrqYBdP3
2026-11-20 14:02:24 +01:00

113 lines
4.1 KiB
PHP

<?php
namespace JumPHP\Tests\Gui;
use JumPHP\Gui\Button;
use JumPHP\Gui\Form;
use JumPHP\Gui\Pointer;
use PHPUnit\Framework\TestCase;
final class KeyboardForm extends Form
{
/** @var list<string> */
public array $calls = [];
public function onOkClick(Button $sender): void
{
$this->calls[] = 'ok';
}
public function onCancelClick(Button $sender): void
{
$this->calls[] = 'cancel';
}
}
/** A tap away from a text field takes its focus and the phone's on-screen keyboard away; Escape / back closes the keyboard first. */
final class FocusKeyboardTest extends TestCase
{
protected function tearDown(): void
{
Form::$softKeyboard = null;
}
private function form(): KeyboardForm
{
$form = new KeyboardForm();
$form->build(['padding' => 0, 'gap' => 10, 'children' => [
['type' => 'textbox', 'id' => 'name'],
['type' => 'textbox', 'id' => 'notes', 'multiline' => true, 'rows' => 3],
['type' => 'label', 'id' => 'hint', 'text' => 'Hint'],
['type' => 'button', 'id' => 'ok', 'text' => 'OK', 'events' => ['click' => 'onOkClick']],
['type' => 'button', 'id' => 'cancel', 'text' => 'Cancel', 'cancel' => true, 'events' => ['click' => 'onCancelClick']],
]]);
$form->layoutTo(800, 600);
return $form;
}
public function testATapOnAButtonTakesTheKeyboardAwayAndStillClicks(): void
{
Form::$softKeyboard = true;
$form = $this->form();
$form->click($form->widget('name'));
self::assertSame($form->widget('name'), $form->focused());
self::assertTrue($form->textInputOn(), 'a text field with the focus: the keyboard is shown');
$form->click($form->widget('ok'));
self::assertFalse($form->widget('name')->focused);
self::assertFalse($form->textInputOn(), 'text input stopped: the keyboard is gone');
self::assertSame(['ok'], $form->calls, 'the tap still reached the button');
}
public function testATapOnALabelOrOutsideEveryWidgetTakesTheKeyboardAway(): void
{
Form::$softKeyboard = true;
$form = $this->form();
$form->click($form->widget('notes'));
self::assertTrue($form->textInputOn(), 'a multi-line field shows the keyboard too');
$form->click($form->widget('hint'));
self::assertNull($form->focused());
self::assertFalse($form->textInputOn());
$form->click($form->widget('name'));
self::assertTrue($form->textInputOn());
$form->clickAt(900, 700); // outside the form's widgets
self::assertNull($form->focused());
self::assertFalse($form->textInputOn());
}
public function testATapFromOneFieldToAnotherKeepsTheKeyboard(): void
{
Form::$softKeyboard = true;
$form = $this->form();
$form->click($form->widget('name'));
$form->click($form->widget('notes'));
self::assertSame($form->widget('notes'), $form->focused());
self::assertTrue($form->textInputOn());
}
public function testEscapeOrBackWithTheKeyboardOpenClosesTheKeyboardFirst(): void
{
Form::$softKeyboard = true;
$form = $this->form();
$form->click($form->widget('name'));
$form->key(Pointer::ESCAPE);
self::assertNull($form->focused());
self::assertFalse($form->textInputOn());
self::assertSame([], $form->calls, 'the dialog is not cancelled by the back that closed the keyboard');
$form->key(Pointer::ESCAPE);
self::assertSame(['cancel'], $form->calls, 'the next back cancels');
}
public function testOnADesktopEscapeStaysForTheFieldAndAClickOnAButtonStillWorks(): void
{
Form::$softKeyboard = false;
$form = $this->form();
$form->click($form->widget('name'));
$form->key(Pointer::ESCAPE);
self::assertSame($form->widget('name'), $form->focused(), 'no on-screen keyboard to close');
$form->click($form->widget('ok'));
self::assertSame(['ok'], $form->calls);
self::assertFalse($form->textInputOn());
}
}