winzik/engine/tests/Native/BrowserPasteTest.php
David Nurdin 10c0cd7673 Paste in the browser, for real: Ctrl+V and the menu's Paste fill the text field
Checked in headless Chrome on the browser build: before, nothing was pasted.
- SDL cancels every key it reads, Ctrl+V included, and a cancelled Ctrl+V has no paste event: jumphp-device.js keeps
  that key's default and lends the focus to a hidden text area for the paste, then gives it back (the key still reaches
  SDL, so the field knows a paste was asked)
- Native::provider() made a new BrowserProvider at each call: the text the page sent was kept by one and lost for the
  next, which then asked the (blocked) clipboard API and got nothing; one provider per page now, listening from the start
- the page answers PHP's paste request with the text of a paste event it just saw
- BrowserPasteTest: the event before or after the field asks, one event pasted once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UZqwpHZaasov261cZ96aQf
2026-11-10 23:53:58 +01:00

59 lines
2 KiB
PHP

<?php
namespace JumPHP\Tests\Native;
use JumPHP\Bridge\JsBridge;
use JumPHP\Native\BrowserProvider;
use PHPUnit\Framework\TestCase;
final class BrowserPasteTest extends TestCase
{
private function bridge(): JsBridge
{
return new class extends JsBridge {
/** @var list<array{string, array}> */
public array $sent = [];
public function __construct()
{
}
public function send(string $type, array $data = []): bool
{
$this->sent[] = [$type, $data];
return true;
}
};
}
public function testACtrlVWhosePasteEventCameFirstGivesItsText(): void
{
$bridge = $this->bridge();
$provider = new BrowserProvider($bridge);
$bridge->emit('paste', [['text' => "Secret123\n"]]); // the page's event, before the text field asks
self::assertSame("Secret123\n", $provider->paste()->result());
self::assertSame([], $bridge->sent, 'no clipboard API asked: the event had the text');
}
public function testACtrlVWhosePasteEventComesAfterWaitsForIt(): void
{
$bridge = $this->bridge();
$provider = new BrowserProvider($bridge);
$task = $provider->paste();
self::assertFalse($task->isDone());
self::assertSame(['device', ['do' => 'paste']], $bridge->sent[0]);
$bridge->emit('paste', [['text' => 'Secret123']]);
self::assertSame('Secret123', $task->result());
$bridge->emit('paste', [['text' => null]]); // the clipboard API's late answer changes nothing
self::assertSame('Secret123', $task->result());
}
public function testOnePasteEventIsPastedOnce(): void
{
$bridge = $this->bridge();
$provider = new BrowserProvider($bridge);
$bridge->emit('paste', [['text' => 'abc']]);
self::assertSame('abc', $provider->paste()->result());
self::assertFalse($provider->paste()->isDone());
}
}