PL
apps/winzik / tests / DialogsErgonomicsTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\FilesForm;use App\Form\MainForm;use App\Form\MarketForm;use App\Form\OptionsForm;use App\Form\SpotifyForm;use App\Form\TagForm;use App\Form\YouTubeForm;use App\Model\Id3Tag;use JumPHP\Gui\Form;use JumPHP\Gui\Pointer;/** The dialogs as the user meets them: Entrée validates, Échap cancels, the focus is where one types, clear errors. */final class DialogsErgonomicsTest extends PlayerTestCase{    private function top(): Form    {        $scene = $this->game->scene();        self::assertInstanceOf(Form::class, $scene);        return $scene;    }    /** A key pressed for real (through the form's handle(), as the engine does each frame). */    private function press(Form $form, int $key): void    {        $p = new Pointer();        [$p->x, $p->y] = [-1, -1];        $p->keys[] = $key;        $form->handle($p);        $this->game->run(1);    }    /** A copy of the demo song with its tag, playing. */    private function playCopy(): string    {        $copy = $this->wz->storagePath('music/essai.mp3');        @mkdir(dirname($copy), 0777, true);        self::assertTrue(copy($this->wz->path('assets/music/winzik.mp3'), $copy), 'never the demo song itself');        $this->wz->addFiles([$copy]);        $this->wz->playIndex($this->wz->playlist->count() - 1);        self::assertSame($copy, $this->wz->path((string)$this->wz->playlist->current()));        return $copy;    }    public function testEntreeInAFieldOfTheTagWritesItAndClosesIt(): void    {        $this->startPlayer();        $copy = $this->playCopy();        $this->form->openTags();        $this->game->run(1);        $tags = $this->top();        self::assertInstanceOf(TagForm::class, $tags);        self::assertSame($tags->widget('songTitle'), $tags->focused(), 'the title gets the focus: type at once');        $tags->key(Pointer::END);        $tags->type(" (live)\n");        $this->game->run(1);        self::assertInstanceOf(MainForm::class, $this->game->scene());        self::assertSame('WinZik (live)', Id3Tag::read($copy)->title);    }    public function testEchapInAFieldOfTheTagCancelsWithoutWriting(): void    {        $this->startPlayer();        $copy = $this->playCopy();        $this->form->openTags();        $this->game->run(1);        $tags = $this->top();        $tags->key(Pointer::END);        $tags->type(' (oups)');        $this->press($tags, Pointer::ESCAPE);        self::assertInstanceOf(MainForm::class, $this->game->scene(), 'Échap closes it from the text field too');        self::assertSame('WinZik', Id3Tag::read($copy)->title);    }    public function testAWrongYearOrMarksSayWhatToDoAndKeepTheTagOpen(): void    {        $this->startPlayer();        $copy = $this->playCopy();        $this->form->openTags();        $this->game->run(1);        $tags = $this->top();        $tags->widget('year')->props['text'] = '01';        $tags->click($tags->widget('ok'));        $this->game->run(1);        self::assertInstanceOf(TagForm::class, $this->game->scene());        self::assertStringContainsString('4 chiffres', (string)$tags->widget('error')->props['text']);        self::assertSame($tags->widget('year'), $tags->focused());        $tags->widget('year')->props['text'] = '2002';        $tags->widget('markStart')->props['text'] = '30';        $tags->widget('markEnd')->props['text'] = '10';        $tags->click($tags->widget('ok'));        $this->game->run(1);        self::assertStringContainsString('après le début', (string)$tags->widget('error')->props['text']);        $this->game->shot('dlg-tag-error');        $tags->widget('markEnd')->props['text'] = '';        $tags->click($tags->widget('ok'));        $this->game->run(1);        self::assertInstanceOf(MainForm::class, $this->game->scene());        self::assertSame('2002', Id3Tag::read($copy)->year);    }    public function testTheOptionsRefuseAMissingLyricsFolderAndEchapCancels(): void    {        $this->startPlayer();        $this->form->openOptions();        $this->game->run(1);        $options = $this->top();        self::assertInstanceOf(OptionsForm::class, $options);        $options->click($options->widget('lyricsDir'));        $options->type('/pas/de/dossier/ici' . "\n");        $this->game->run(1);        self::assertInstanceOf(OptionsForm::class, $this->game->scene(), 'Entrée: Ok, refused');        self::assertStringContainsString("Ce répertoire n'existe pas", (string)$options->widget('lyricsInfo')->props['text']);        $this->game->shot('dlg-options-error');        $this->press($options, Pointer::ESCAPE);        self::assertInstanceOf(MainForm::class, $this->game->scene());        self::assertSame('', (string)($this->wz->options['lyricsDir'] ?? ''), 'nothing saved');    }    public function testTheFilesOpenOnTheSongsAndEntreeAddsTheOneChosen(): void    {        $this->startPlayer();        $before = $this->wz->playlist->count();        $this->form->openFiles();        $this->game->run(1);        $files = $this->top();        self::assertInstanceOf(FilesForm::class, $files);        self::assertSame($files->widget('files'), $files->focused());        $files->key(Pointer::DOWN);        $files->key(Pointer::RETURN);        $this->game->run(1);        self::assertSame($before + 1, $this->wz->playlist->count());        $files->click($files->widget('path'));        $files->key(Pointer::END);        $files->type("/nulle-part\n");        self::assertStringContainsString("Ce dossier n'existe pas", (string)$files->widget('status')->props['text']);        $this->press($files, Pointer::ESCAPE);        self::assertInstanceOf(MainForm::class, $this->game->scene());    }    public function testYoutubeWithoutItsToolsSaysWhereItWorksAndOffersOnlyFermer(): void    {        $this->startPlayer();        $this->form->openYoutube();        $this->game->run(3);        $yt = $this->top();        self::assertInstanceOf(YouTubeForm::class, $yt);        foreach (['query', 'find', 'get', 'prev', 'next', 'remove', 'clearAll', 'clear'] as $id) {            self::assertFalse($yt->widget($id)->isEnabled(), $id . ' cannot be used');        }        self::assertStringContainsString('installez-le', (string)$yt->widget('status')->props['text']);        self::assertNotEmpty($yt->widget('source')->props['items'], 'the sources are named, not "Choose…"');        $this->game->shot('dlg-youtube');        $this->press($yt, Pointer::ESCAPE);        self::assertInstanceOf(MainForm::class, $this->game->scene());    }    public function testSpotifyWithoutClientIdLeadsToTheOptions(): void    {        $this->startPlayer();        $this->form->openSpotify();        $this->game->run(2);        $spotify = $this->top();        self::assertInstanceOf(SpotifyForm::class, $spotify);        self::assertTrue($spotify->widget('options')->props['visible']);        self::assertFalse($spotify->widget('connect')->props['visible'], 'no Se connecter that cannot work');        self::assertSame($spotify->widget('options'), $spotify->focused());        $this->game->shot('dlg-spotify');        $this->press($spotify, Pointer::RETURN);        $this->game->run(1);        self::assertInstanceOf(OptionsForm::class, $this->game->scene());    }    public function testTheMarketplaceWithoutNetworkLeavesOnlyFermer(): void    {        $this->startPlayer();        $this->form->openMarket('skin');        $this->game->run(2);        $market = $this->top();        self::assertInstanceOf(MarketForm::class, $market);        foreach (['search', 'find', 'install', 'share'] as $id) {            self::assertFalse($market->widget($id)->isEnabled(), $id . ' cannot be used');        }        self::assertStringContainsString('Vérifiez la connexion', (string)$market->widget('status')->props['text']);        $this->game->shot('dlg-market');        $this->press($market, Pointer::ESCAPE);        self::assertInstanceOf(MainForm::class, $this->game->scene());    }    public function testEveryDialogIsShotAsItOpens(): void    {        $this->startPlayer();        foreach (['openOptions', 'openFiles', 'openTags'] as $open) {            $this->form->$open();            $this->game->run(3);            $this->game->shot('dlg-' . $open);            $this->top()->close();            $this->game->run(1);        }    }}