RU
apps/winzik / tests / MobileTest.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 phone app (Android), held upright: its own screens, used with a finger (390 x 844 dp in the middle of the view). */final class MobileTest 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);    }    /** A tap on something of the screen (its id, its data). */    private function tap(string $id, mixed $data = null, int $frames = 2): void    {        $at = $this->view->at($id, $data);        self::assertNotNull($at, "$id is on the screen (" . implode(', ', $this->view->touchable()) . ')');        $this->form->clickAt($at[0], $at[1]);        $this->game->run($frames);    }    private function down(float $x, float $y): void    {        $p = new Pointer();        [$p->x, $p->y, $p->down, $p->held] = [$x, $y, true, true];        $this->form->handle($p);    }    private function move(float $x, float $y): void    {        $p = new Pointer();        [$p->x, $p->y, $p->held] = [$x, $y, true];        $this->form->handle($p);    }    private function up(float $x, float $y): void    {        $p = new Pointer();        [$p->x, $p->y, $p->up] = [$x, $y, true];        $this->form->handle($p);    }    /** A picture of the phone's screen alone: var/screenshots/tests/<name>.png */    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 testThePlayerPlaysAndTheBarSeeksUnderTheFinger(): void    {        $this->startPhone();        self::assertSame('player', $this->view->screen);        self::assertLessThan($this->view->H, $this->view->W, 'upright');        $this->tap('play');        self::assertSame(Player::PLAYING, $this->wz->player->state);        // the finger on the bar at its middle, slid to three quarters, let go        $length = $this->wz->player->duration();        self::assertGreaterThan(0, $length);        [$x0, $y0, $w] = $this->view->screenRect();        $at = $this->view->at('seek');        $scale = $w / $this->view->W;        $left = $x0 + 28 * $scale;        $width = ($this->view->W - 56) * $scale;        $this->down($left + $width * 0.5, $at[1]);        $this->move($left + $width * 0.75, $at[1]);        $this->up($left + $width * 0.75, $at[1]);        $this->game->run(1);        self::assertEqualsWithDelta($length * 0.75, $this->wz->player->position, $length * 0.03);        // next, previous (back to the start first, like every player)        $first = $this->wz->playlist->active;        $this->tap('next');        self::assertNotSame($first, $this->wz->playlist->active);        $this->tap('prev');        self::assertSame($first, $this->wz->playlist->active);        $this->tap('shuffle');        self::assertTrue($this->wz->playlist->shuffle);        $this->tap('shuffle');        $this->tap('repeat');        self::assertTrue($this->view->repeat());    }    public function testThePlayerShowsTheLyricsOfTheSong(): void    {        $this->startPhone();        $line = array_search('assets/music/winzik.mp3', $this->wz->playlist->items, true);        self::assertIsInt($line);        $this->wz->playIndex($line);        $this->wz->seek(17.0);        $this->game->wait(1);        self::assertNotNull($this->wz->lyrics);        $this->wz->notice = '';        $this->shot('mobile-lecteur');    }    public function testRepeatPlaysTheSongAgainAtItsEnd(): void    {        $this->startPhone();        $this->tap('play');        $this->tap('repeat');        $line = $this->wz->playlist->active;        $this->wz->seek($this->wz->player->duration() - 0.3);        $this->game->wait(1.5);        self::assertSame($line, $this->wz->playlist->active);        self::assertSame(Player::PLAYING, $this->wz->player->state);        self::assertLessThan(3, $this->wz->player->position);    }    public function testATapInThePlaylistPlaysTheSong(): void    {        $this->startPhone();        $this->tap('tab', 'playlist');        self::assertSame('playlist', $this->view->screen);        $this->tap('row', 1);        self::assertSame(1, $this->wz->playlist->active);        self::assertSame(Player::PLAYING, $this->wz->player->state);        $this->wz->rate((string)$this->wz->playlist->items[1], 4);        $this->wz->notice = '';        $this->game->wait(0.5);        $this->shot('mobile-playlist');        // the mini player: pause, then back to the player        $this->tap('miniPlay');        self::assertSame(Player::PAUSED, $this->wz->player->state);        $this->tap('mini');        self::assertSame('player', $this->view->screen);    }    public function testALongPressOpensTheActionSheetAndRetirerRemovesTheSong(): void    {        $this->startPhone(['screen' => 'playlist']);        $count = $this->wz->playlist->count();        $removed = $this->wz->playlist->items[1];        $at = $this->view->at('row', 1);        $this->down($at[0], $at[1]);        $this->game->run(35);     // held still more than 0.45 s        self::assertSame('actions', $this->view->sheet);        self::assertSame(1, $this->view->sheetLine);        $this->up($at[0], $at[1]);        $this->game->wait(0.4);        self::assertSame('actions', $this->view->sheet, 'lifting the finger keeps the sheet open');        $this->shot('mobile-actions');        $this->tap('star', 5);        self::assertSame(5, $this->wz->stats()->rating($this->wz->path($removed)));        $this->tap('act', 'remove', 20);        self::assertSame($count - 1, $this->wz->playlist->count());        self::assertNotContains($removed, $this->wz->playlist->items);        self::assertSame('', $this->view->sheet);    }    public function testTheActionSheetMovesAndQueuesSongs(): void    {        $this->startPhone(['screen' => 'playlist', 'play' => 0]);        $items = $this->wz->playlist->items;        $last = count($items) - 1;        $this->tap('rowMore', $last, 15);        self::assertSame('actions', $this->view->sheet);        $this->tap('act', 'next', 20);        self::assertSame($items[$last], $this->wz->playlist->items[1], 'Lire ensuite: right after the song playing');        $this->tap('rowMore', 1, 15);        $this->tap('act', 'down');        self::assertSame($items[$last], $this->wz->playlist->items[2]);        $this->tap('act', 'info', 15);        self::assertSame('info', $this->view->sheet);        $this->shot('mobile-infos');    }    public function testTheBackButtonClosesTheSheetThenGoesBackToThePlayer(): void    {        $this->startPhone(['screen' => 'playlist']);        $this->tap('rowMore', 0, 15);        self::assertSame('actions', $this->view->sheet);        $this->form->key(Pointer::ESCAPE);        $this->game->wait(0.4);        self::assertSame('', $this->view->sheet);        self::assertSame('playlist', $this->view->screen);        $this->form->key(Pointer::ESCAPE);        $this->game->run(1);        self::assertSame('player', $this->view->screen);        // the veil around a sheet closes it too        $this->tap('tab', 'more', 15);        self::assertSame('more', $this->view->sheet);        [$x, $y] = $this->view->screenRect();        $this->form->clickAt($x + 20, $y + 40);        $this->game->wait(0.4);        self::assertSame('', $this->view->sheet);    }    public function testThePlusMenuOpensTheEqualizerTheThemesAndTheOptions(): void    {        $this->startPhone(['play' => 0]);        $this->tap('tab', 'more', 15);        self::assertSame('more', $this->view->sheet);        $this->shot('mobile-plus');        $this->tap('more', 'eq', 15);        self::assertSame('eq', $this->view->sheet);        $this->tap('eqPreset', 'Rock');        self::assertSame(array_map('floatval', \App\Model\Equalizer::PRESETS['Rock']), $this->wz->eq->bands);        $this->shot('mobile-egaliseur');        $this->form->key(Pointer::ESCAPE);        $this->game->wait(0.4);        $this->tap('tab', 'more', 15);        $this->tap('goto', 'options', 15);        self::assertSame('options', $this->view->screen);        $before = (bool)($this->wz->options['skipBlanks'] ?? true);        $this->tap('opt', 'skipBlanks');        self::assertSame(!$before, (bool)$this->wz->options['skipBlanks']);        $this->shot('mobile-options');        $this->form->key(Pointer::ESCAPE);        $this->game->wait(0.4);        $this->tap('tab', 'more', 15);        $this->tap('more', 'about', 15);        self::assertSame('about', $this->view->sheet);        $this->shot('mobile-apropos');        $this->form->key(Pointer::ESCAPE);        $this->game->wait(0.4);        $this->tap('tab', 'more', 15);        $this->tap('more', 'theme', 15);        $this->shot('mobile-themes');        $this->tap('theme', 'nuit');        self::assertSame('nuit', $this->view->themeId());        $this->form->key(Pointer::ESCAPE);        $this->game->wait(0.5);        $this->wz->notice = '';        $this->shot('mobile-lecteur-nuit');    }    public function testTheListScrollsWithTheFingerAndGoesOnAfterAFlick(): void    {        $this->startPhone(['screen' => 'playlist']);        $songs = $this->wz->playlist->items;        for ($i = 0; $i < 8; $i++) {            foreach ($songs as $song) {                $this->wz->playlist->add($song);            }        }        $this->game->run(2);        $at = $this->view->at('row', 3);        $this->down($at[0], $at[1]);        for ($k = 1; $k <= 4; $k++) {            $this->game->run(1);            $this->move($at[0], $at[1] - $k * 60);        }        $this->up($at[0], $at[1] - 240);        $afterDrag = $this->view->scroll['list'];        self::assertGreaterThan(80, $afterDrag);        self::assertNotSame(Player::PLAYING, $this->wz->player->state, 'a drag is not a tap');        $this->game->run(10);        self::assertGreaterThan($afterDrag + 20, $this->view->scroll['list'], 'the list goes on after the finger');        self::assertSame('', $this->view->sheet, 'a drag is not a long press');        $this->shot('mobile-playlist-defilee');    }    public function testSearchFiltersTheList(): void    {        $this->startPhone(['screen' => 'playlist']);        $this->tap('search');        self::assertTrue($this->view->searching);        self::assertTrue($this->view->wantsText(), 'the keyboard of the phone opens');        $this->form->type('partage');        $this->game->run(1);        $lines = $this->view->shownLines();        self::assertCount(1, $lines);        self::assertStringContainsString('partage', (string)$this->wz->playlist->items[$lines[0]]);        $this->form->key(Pointer::ESCAPE);        self::assertFalse($this->view->searching);        $this->shot('mobile-recherche');        $this->tap('searchClear');        self::assertCount($this->wz->playlist->count(), $this->view->shownLines());    }    public function testAnEmptyListSaysHowToAddMusicAndAFolderIsAdded(): void    {        $this->startPhone(['screen' => 'playlist']);        $this->wz->removeLines(array_keys($this->wz->playlist->items));        $this->wz->notice = '';        $this->game->run(2);        $this->shot('mobile-playlist-vide');        $this->tap('emptyAdd');        self::assertSame('add', $this->view->screen);        $demo = $this->wz->path('assets/music');        $this->tap('place', $demo);        self::assertSame($demo, $this->view->folder);        $this->shot('mobile-ajouter');        $this->tap('addDir', $demo);        self::assertGreaterThanOrEqual(3, $this->wz->playlist->count());        $this->form->key(Pointer::ESCAPE);        $this->game->run(1);        self::assertSame('playlist', $this->view->screen);    }}