winzik/apps/winzik/tests/CastUiTest.php

117 lines
4.5 KiB
PHP

<?php
namespace Tests;
use App\Cast\Cast;
use App\Cast\TvServer;
use App\Form\CastForm;
use App\Form\MainForm;
use JumPHP\Gui\Form;
use Tests\Cast\FakeRenderer;
/**
* « Caster sur la TV… » in WinZik's windows: the right click menu opens the cast window, which finds the (fake) TV of
* the network, casts the song to it, opens the WinZik TV page with its address and QR code, and stops.
* Pictures var/screenshots/tests/cast-*.png.
*/
final class CastUiTest extends PlayerTestCase
{
private FakeRenderer $tv;
private Cast $cast;
protected function tearDown(): void
{
if (isset($this->tv)) {
$this->tv->close();
}
if (isset($this->wz)) {
$this->wz->useCast(null);
}
parent::tearDown();
}
private function startWithTv(): void
{
$this->startPlayer();
$this->tv = new FakeRenderer('TV du salon');
$server = new TvServer();
$server->addresses = ['127.0.0.1'];
@unlink($this->wz->storagePath('cast-test/cast.json'));
$this->cast = new Cast($this->wz, $this->wz->storagePath('cast-test/cast.json'), $server);
// the search goes to the fake TV only (never to the network of the computer running the tests)
$this->cast->discovery()->targets = ['ssdp' => '127.0.0.1:' . $this->tv->udpPort, 'mdns' => '127.0.0.1:9'];
$this->wz->useCast($this->cast);
}
private function frames(\Closure $until, int $max = 1500): void
{
for ($i = 0; $i < $max; $i++) {
$this->tv->poll();
$this->game->run(1);
if ($until()) {
return;
}
usleep(2000);
}
self::fail('not reached after ' . $max . ' frames');
}
private function top(): Form
{
$scene = $this->game->scene();
self::assertInstanceOf(Form::class, $scene);
return $scene;
}
public function testTheRightClickMenuCastsTheSongToATvOfTheNetworkThenShowsTheTvPage(): void
{
$this->startWithTv();
$this->wz->playIndex(0);
[$x, $y] = $this->at($this->desk->main, 60, 20);
$this->rightClick($x, $y);
self::assertContains('Caster sur la TV…', $this->desk->menuLabels());
$menu = $this->desk->menu();
self::assertNotNull($menu);
$at = $menu->rowCenter('Caster sur la TV…');
self::assertNotNull($at);
$this->form->clickAt($this->desk->x + $at[0] * $this->desk->scale, $this->desk->y + $at[1] * $this->desk->scale);
$this->game->run(2);
$form = $this->top();
self::assertInstanceOf(CastForm::class, $form);
$this->frames(fn () => $form->deviceIds === ['dlna:uuid:fake-tv-0001']);
self::assertSame(0, $form->devices->selected, 'the only TV found is chosen');
self::assertSame(['TV du salon (DLNA)'], $form->devices->items);
$this->game->shot('cast-window');
$form->click($form->connect);
$this->frames(fn () => $this->tv->state === 'PLAYING');
$this->frames(fn () => $this->cast->state() === 'casting');
self::assertStringContainsString('Diffusé sur TV du salon (streaming DLNA)', $form->now->text);
self::assertTrue($this->wz->player->silent);
$this->game->shot('cast-window-casting');
// the menu says it casts, and stops it
$form->click($form->quit);
$this->game->run(2);
self::assertInstanceOf(MainForm::class, $this->game->scene());
$this->rightClick($x, $y);
self::assertContains('Arrêter la diffusion sur la TV', $this->desk->menuLabels());
$this->desk->close();
$this->game->run(1);
// the WinZik screen in the TV's browser: its address and its QR code
$this->form->engineOrNull()?->scenes->push(CastForm::class);
$this->game->run(2);
$form = $this->top();
self::assertInstanceOf(CastForm::class, $form);
$form->click($form->browser);
$this->game->run(3);
self::assertTrue((bool)$form->tvPanel->props['visible']);
self::assertMatchesRegularExpression('#^http://127\.0\.0\.1:\d+/tv/[a-z2-9]{8}$#', $form->tvUrl->text);
self::assertFileExists((string)$form->qr->src);
self::assertTrue($form->modeInterface->checked);
$this->game->shot('cast-window-tv-page');
$form->click($form->disconnect);
$this->game->run(2);
self::assertSame('off', $this->cast->state());
self::assertFalse($this->wz->player->silent);
self::assertFalse((bool)$form->tvPanel->props['visible']);
}
}