IT
apps/winzik / tests / MobileCastTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Cast\Cast;use App\Cast\TvServer;use App\Core\WinZik;use App\Form\MobileForm;use App\Mobile\MobileView;use JumPHP\Testing\GameTestCase;use Tests\Cast\FakeRenderer;/** * Plus > « Caster sur la TV » on the phone: the TVs of the Wi-Fi (a fake DLNA TV), a tap casts the song, the WinZik * screen in a TV's browser shows its address and its QR code, Arrêter. Pictures var/screenshots/tests/mobile-cast-*.png. */final class MobileCastTest extends GameTestCase{    private MobileForm $form;    private MobileView $view;    private WinZik $wz;    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 startPhone(): void    {        $this->game->start(MobileForm::class, ['fresh' => true, 'phone' => true]);        $form = $this->game->scene();        self::assertInstanceOf(MobileForm::class, $form);        $this->form = $form;        $view = $form->widget('view');        self::assertInstanceOf(MobileView::class, $view);        $this->view = $view;        $this->wz = $form->winzik();        $this->wz->playlist->shuffle = false;        $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);        $this->cast->discovery()->targets = ['ssdp' => '127.0.0.1:' . $this->tv->udpPort, 'mdns' => '127.0.0.1:9'];        $this->wz->useCast($this->cast);        $this->wz->notice = '';        $this->game->run(3);    }    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 tap(string $id, mixed $data = null, int $frames = 2): void    {        $key = 'page:' . $this->view->screen;        for ($i = 0; $i < 30 && $this->view->at($id, $data) === null && $this->view->isPage(); $i++) {            $this->view->scroll[$key] = ($this->view->scroll[$key] ?? 0.0) + 120;            $this->game->run(1);        }        $at = $this->view->at($id, $data);        self::assertNotNull($at, "$id " . json_encode($data) . ' is on the screen (' . implode(', ', $this->view->touchable()) . ')');        $this->form->clickAt($at[0], $at[1]);        $this->game->run($frames);    }    private function shot(string $name): void    {        $this->game->run(1);        $file = $this->game->shot($name);        $image = @imagecreatefrompng($file);        if ($image === false) {            return;        }        [$x, $y, $w, $h] = $this->view->screenRect();        $crop = imagecrop($image, ['x' => (int)$x, 'y' => (int)$y, 'width' => (int)round($w), 'height' => (int)round($h)]);        if ($crop !== false) {            imagepng($crop, $file);        }    }    public function testThePlusMenuCastsTheSongToATvOfTheWifiThenShowsTheTvPageWithItsQrCode(): void    {        $this->startPhone();        $this->wz->playIndex(0);        $this->tap('tab', 'more', 12);        $this->tap('goto', 'cast', 12);        self::assertSame('cast', $this->view->screen);        $this->frames(fn () => count($this->cast->devices()) === 1);        $this->shot('mobile-cast-devices');        $this->tap('castMode', 'interface');        self::assertSame('interface', $this->view->castMode);        $this->tap('castMode', 'stream');        $this->tap('castDevice', 'dlna:uuid:fake-tv-0001');        $this->frames(fn () => $this->tv->state === 'PLAYING' && $this->cast->state() === 'casting');        self::assertTrue($this->wz->player->silent);        $this->view->scroll['page:cast'] = 0.0;        $this->shot('mobile-cast-casting');        // the WinZik screen in a TV's browser        $this->tap('castBrowser', null, 3);        self::assertSame('browser', $this->cast->how());        self::assertSame('interface', $this->view->castMode);        self::assertSame(0.0, $this->view->scroll['page:cast']);        $this->shot('mobile-cast-tv-page');        $this->tap('castStop', null, 3);        self::assertSame('off', $this->cast->state());        self::assertFalse($this->wz->player->silent);        // back: the Lecteur again        $this->view->back();        $this->game->run(2);        self::assertSame('player', $this->view->screen);    }}