IT
apps/winzik / tests / MobileKaraokeTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Audio\Player;use App\Core\WinZik;use App\Form\MobileForm;use App\Mobile\MobileView;use JumPHP\Gui\Pointer;use JumPHP\Testing\GameTestCase;/** * The karaoke on the whole screen of the phone (390 x 844 dp): one tap from the Lecteur, Portrait / Paysage (drawn turned a * quarter, its touches turned too), a tap on a line takes the song there, the finger scrolls the lyrics (a drag never * seeks) and the line sung is followed again. And the phone's keyboard closed by a touch outside the field. */final class MobileKaraokeTest extends GameTestCase{    private MobileForm $form;    private MobileView $view;    private WinZik $wz;    private function startPhone(array $params = []): void    {        $this->game->start(MobileForm::class, ['fresh' => true, 'phone' => true] + $params);        $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->wz->notice = '';        $this->game->run(3);    }    /** The song with lyrics (assets/music/winzik.lyr) playing at $at seconds. */    private function playTheSongWithLyrics(float $at = 17.0): void    {        $line = array_search('assets/music/winzik.mp3', $this->wz->playlist->items, true);        self::assertIsInt($line);        $this->wz->playIndex($line);        $this->wz->seek($at);        $this->game->wait(0.5);        self::assertNotNull($this->wz->lyrics);    }    private function tap(string $id, mixed $data = null, int $frames = 2): void    {        $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 pointer(float $x, float $y, string $what): void    {        $p = new Pointer();        [$p->x, $p->y] = [$x, $y];        match ($what) {            'down' => [$p->down, $p->held] = [true, true],            'move' => $p->held = true,            default => $p->up = true,        };        $this->form->handle($p);    }    /**     * A finger dragged along the lyrics, up the turned screen's vertical by $dp (dp: > 0 = towards the end), in steps.     * Upright: the screen's y; in landscape the lyrics' "up" is the screen's +x (the phone turned).     */    private function drag(mixed $line, float $dp, int $steps = 5): void    {        $at = $this->view->at('karaLine', $line);        self::assertNotNull($at);        $s = $this->view->screenRect()[2] / $this->view->W;        [$dx, $dy] = $this->view->turned() ? [$dp * $s, 0.0] : [0.0, -$dp * $s];        $this->pointer($at[0], $at[1], 'down');        for ($k = 1; $k <= $steps; $k++) {            $this->game->run(1);            $this->pointer($at[0] + $dx * $k / $steps, $at[1] + $dy * $k / $steps, 'move');        }        $this->pointer($at[0] + $dx, $at[1] + $dy, 'up');    }    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 testTheLecteurOpensTheKaraokeInOneTapAndBackClosesIt(): void    {        $this->startPhone();        $this->playTheSongWithLyrics();        // the button of the lyrics' card        $this->tap('karaokeOpen', null, 3);        self::assertSame('karaoke', $this->view->screen);        $this->form->key(Pointer::ESCAPE);        $this->game->run(3);        self::assertSame('player', $this->view->screen, 'the back button closes it');        // the card itself        $this->tap('lyricsBox', null, 3);        self::assertSame('karaoke', $this->view->screen);        $this->form->key(Pointer::ESCAPE);        $this->game->run(3);        // the mini player of the Playlist has its button too        $this->tap('tab', 'playlist', 3);        $this->tap('karaokeOpen', null, 3);        self::assertSame('karaoke', $this->view->screen);        $this->form->key(Pointer::ESCAPE);        $this->game->run(3);        self::assertSame('playlist', $this->view->screen, 'back where it came from');    }    public function testASongWithoutLyricsOffersToFindThem(): void    {        $this->startPhone();        $line = array_search('assets/music/partage.mp3', $this->wz->playlist->items, true);        self::assertIsInt($line);        $this->wz->playIndex($line);        // lyrics another test of the run may have given it (a .lrc imported, kept in var/lyr): gone        if ($this->wz->lyrics !== null && str_starts_with($this->wz->lyricsFile, rtrim($this->wz->storagePath('lyr'), '/'))) {            @unlink($this->wz->lyricsFile);            $this->wz->refreshLyrics();            $this->wz->loadLyrics('assets/music/partage.mp3');        }        $this->game->wait(0.3);        self::assertTrue($this->wz->lyrics === null || $this->wz->lyrics->isEmpty());        $this->tap('lyricsBox', null, 15);        self::assertSame('lyricsFind', $this->view->screen, 'the search of lyrics on the Internet');        self::assertSame('assets/music/partage.mp3', $this->view->lfFile);        $this->form->key(Pointer::ESCAPE);        $this->game->run(3);        self::assertSame('player', $this->view->screen);        // the mic of the top bar: the sheet to get lyrics        $this->tap('karaokeOpen', null, 15);        self::assertSame('lyrics', $this->view->sheet);    }    public function testATapOnALineSeeksThereAndPlays(): void    {        $this->startPhone();        $this->playTheSongWithLyrics();        $this->wz->pause();        $this->tap('karaokeOpen', null, 5);        $this->wz->notice = '';        $this->shot('mobile-karaoke-portrait');        $now = $this->wz->lyrics->indexAt($this->wz->player->position);        $target = $now + 2;        $this->tap('karaLine', $target, 2);        self::assertEqualsWithDelta($this->wz->lyrics->lines[$target][0] - 0.3, $this->wz->player->position, 0.3);        self::assertSame(Player::PLAYING, $this->wz->player->state, 'and it plays');        // the controls are still there        foreach (['play', 'lyShift', 'addBack', 'lyricsMenu', 'karaTurn', 'seek'] as $id) {            self::assertNotNull($this->view->at($id), "$id on the screen");        }    }    public function testThePortraitPaysageButtonTurnsTheKaraokeAndIsRemembered(): void    {        $this->startPhone();        $this->playTheSongWithLyrics();        $this->tap('karaokeOpen', null, 3);        self::assertFalse($this->view->turned());        $this->tap('karaTurn', null, 3);        self::assertTrue($this->view->karaokeLandscape());        self::assertTrue($this->view->turned(), 'drawn turned a quarter');        self::assertTrue((bool)$this->wz->ui['mobile']['karaokeLandscape'], 'remembered');        // its touches are turned: the top of the turned screen is the right edge of the phone, its right the bottom        [$x, $y] = $this->view->at('karaTurn');        [$sx, $sy, $sw, $sh] = $this->view->screenRect();        self::assertGreaterThan($sx + $sw * 0.75, $x, 'the top of the turned screen is at the right of the phone');        self::assertGreaterThan($sy + $sh * 0.6, $y, 'its right at the bottom of the phone');        $this->wz->notice = '';        $this->wz->pause();        $this->shot('mobile-karaoke-paysage');        // a tap on a line in landscape seeks too        $now = $this->wz->lyrics->indexAt($this->wz->player->position);        $this->tap('karaLine', $now + 1, 2);        self::assertEqualsWithDelta($this->wz->lyrics->lines[$now + 1][0] - 0.3, $this->wz->player->position, 0.3);        self::assertSame(Player::PLAYING, $this->wz->player->state);        // a drag in landscape scrolls without seeking        $pos = $this->wz->player->position;        $before = $this->view->karaScroll;        $this->drag($now + 1, 120);        self::assertTrue($this->view->karaFree);        self::assertGreaterThan($before + 1.5, $this->view->karaScroll, 'scrolled towards the end');        self::assertEqualsWithDelta($pos, $this->wz->player->position, 0.5, 'a drag never seeks');        // back to the Lecteur and in again: still in landscape; the button turns it upright        $this->form->key(Pointer::ESCAPE);        $this->game->run(3);        self::assertSame('player', $this->view->screen);        self::assertFalse($this->view->turned(), 'the Lecteur stays upright');        $this->tap('karaokeOpen', null, 3);        self::assertTrue($this->view->turned());        $this->tap('karaTurn', null, 3);        self::assertFalse($this->view->turned());        self::assertFalse($this->view->karaokeLandscape());    }    public function testTheFingerScrollsTheLyricsWithoutSeekingThenTheLineSungIsFollowedAgain(): void    {        $this->startPhone();        $this->playTheSongWithLyrics(9.0);        $this->tap('karaokeOpen', null, 5);        $now = $this->wz->lyrics->indexAt($this->wz->player->position);        $pos = $this->wz->player->position;        $this->drag($now, 260);        $afterDrag = $this->view->karaScroll;        self::assertTrue($this->view->karaFree, 'the line sung is no longer followed');        self::assertGreaterThan($now + 3, $afterDrag);        self::assertEqualsWithDelta($pos, $this->wz->player->position, 0.5, 'a drag never seeks');        $this->game->run(6);        self::assertGreaterThan($afterDrag + 0.3, $this->view->karaScroll, 'it goes on after the finger (a flick)');        $this->game->wait(0.5);        $this->wz->notice = '';        self::assertNotNull($this->view->at('karaFollow'), '« Revenir à la ligne chantée »');        $this->shot('mobile-karaoke-scroll');        // the chip: back to the line sung        $this->tap('karaFollow', null, 2);        self::assertFalse($this->view->karaFree);        $this->game->wait(1.0);        $sung = $this->wz->lyrics->indexAt($this->wz->player->position);        self::assertEqualsWithDelta($sung, $this->view->karaScroll, 0.2);        // scrolled up, then left alone: followed again after a few seconds        $this->drag($sung, -150);        self::assertTrue($this->view->karaFree);        self::assertLessThan($sung - 1, $this->view->karaScroll);        $this->game->wait(MobileView::KARAOKE_FOLLOW + 1.5);        self::assertFalse($this->view->karaFree, 'followed again');        $sung = $this->wz->lyrics->indexAt($this->wz->player->position);        self::assertEqualsWithDelta($sung, $this->view->karaScroll, 0.3);    }    public function testATouchOutsideTheFieldClosesTheKeyboardAndStillActs(): void    {        $this->startPhone(['screen' => 'playlist']);        // the search of the list: typed in, then a song tapped: the keyboard closes and the song plays        $this->tap('search', null, 2);        self::assertTrue($this->view->wantsText());        $this->form->type('winzik');        $this->game->run(2);        $this->tap('row', array_search('assets/music/winzik.mp3', $this->wz->playlist->items, true), 3);        self::assertFalse($this->view->wantsText(), 'the keyboard is closed');        self::assertSame('winzik', $this->view->search, 'what was typed stays');        self::assertSame(Player::PLAYING, $this->wz->player->state, 'the tap still played the song');        if (function_exists('SDL_IsTextInputActive')) {            self::assertFalse((bool)\SDL_IsTextInputActive(), 'SDL\'s text input stopped');        }        // a field of a page: a touch on the page around it closes the keyboard        $this->view->go('spotify');        $this->game->run(3);        $field = 'spotifyId';        $this->tap('field', $field, 2);        self::assertSame($field, $this->view->field);        self::assertTrue($this->view->wantsText());        $this->form->type('abc');        $this->game->run(1);        // a tap at the bottom of the page, beside the field        [$sx, $sy, $sw, $sh] = $this->view->screenRect();        $this->form->clickAt($sx + $sw / 2, $sy + $sh - 6);        $this->game->run(2);        self::assertSame('', $this->view->field, 'no field typed in');        self::assertFalse($this->view->wantsText());        self::assertSame('abc', $this->view->fields[$field]);        if (function_exists('SDL_IsTextInputActive')) {            self::assertFalse((bool)\SDL_IsTextInputActive());        }        // a drag of the page closes it too        $this->tap('field', $field, 2);        self::assertTrue($this->view->wantsText());        $at = $this->view->at('page');        self::assertNotNull($at);        $this->pointer($at[0], $at[1], 'down');        $this->pointer($at[0], $at[1] - 60, 'move');        $this->pointer($at[0], $at[1] - 60, 'up');        $this->game->run(2);        self::assertFalse($this->view->wantsText());    }}