JA
apps/winzik / tests / MobileFollowTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\MobileForm;use App\Mobile\MobileView;use App\Core\WinZik;use JumPHP\Gui\Pointer;use JumPHP\Testing\GameTestCase;/** * The Playlist tab of the phone follows the song playing; a list scrolled by the finger is left alone a moment; the round * button « où ça joue » (a target over the mini player, only while the song playing is out of view) glides back to it. */final class MobileFollowTest extends GameTestCase{    private MobileForm $form;    private MobileView $view;    private WinZik $wz;    private function startPhone(int $times = 12): void    {        $this->game->start(MobileForm::class, ['fresh' => true, 'phone' => true, 'screen' => 'playlist']);        $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 = '';        $songs = $this->wz->playlist->items;        for ($i = 1; $i < $times; $i++) {            foreach ($songs as $song) {                $this->wz->playlist->add($song);            }        }        $this->wz->playIndex(0);        $this->game->run(30);    }    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);    }    /** The finger drags the list up by $dy view pixels, slowly (no flick). */    private function drag(float $dy): void    {        $at = $this->view->at('row', 1) ?? $this->view->at('list');        self::assertNotNull($at);        $p = new Pointer();        [$p->x, $p->y, $p->down, $p->held] = [$at[0], $at[1], true, true];        $this->form->handle($p);        for ($k = 1; $k <= 8; $k++) {            $this->game->run(3);            $m = new Pointer();            [$m->x, $m->y, $m->held] = [$at[0], $at[1] - $dy * $k / 8, true];            $this->form->handle($m);        }        $this->game->run(20);      // still: no flick        $u = new Pointer();        [$u->x, $u->y, $u->up] = [$at[0], $at[1] - $dy, true];        $this->form->handle($u);        $this->game->run(100);     // what the finger flicked comes to rest    }    /** The row playing is whole on screen. */    private function playingShown(): bool    {        return $this->view->at('row', $this->wz->playlist->active) !== null;    }    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 testASongChangedFarAwayGlidesTheListToIt(): void    {        $this->startPhone();        self::assertSame(0.0, $this->view->scroll['list']);        self::assertNull($this->view->at('locate'), 'the song playing in view: no button');        $far = $this->wz->playlist->count() - 6;        $this->wz->playIndex($far);        $this->game->run(2);        self::assertNotNull($this->view->listGlide, 'it glides');        $this->game->run(60);        self::assertTrue($this->playingShown(), 'the row playing on screen');        self::assertEqualsWithDelta($this->view->playingScroll(), $this->view->scroll['list'], 1.0, 'in the middle');        // next: it goes on following        $this->tap('miniNext', null, 60);        self::assertTrue($this->playingShown());        $this->shot('mobile-playlist-suit');    }    public function testAListScrolledByTheFingerIsLeftAloneAndTheButtonBringsItBack(): void    {        $this->startPhone();        $this->drag(500);        $scrolled = $this->view->scroll['list'];        self::assertGreaterThan(100, $scrolled);        self::assertTrue($this->view->listHeld());        $this->wz->playIndex(1);            // the song playing goes up, out of view        $this->game->run(30);        self::assertEqualsWithDelta($scrolled, $this->view->scroll['list'], 0.5, 'held by the finger: not yanked');        self::assertFalse($this->playingShown());        self::assertNotNull($this->view->at('locate'), 'the button « où ça joue » shows');        $this->shot('mobile-playlist-ou-ca-joue');        $this->tap('locate', null, 3);        self::assertGreaterThan(0, $this->view->flash, 'the row glows');        $this->game->run(4);        $this->shot('mobile-playlist-retrouve');        $this->game->run(50);        self::assertTrue($this->playingShown(), 'the button: back to the row playing');        self::assertNull($this->view->at('locate'), 'and it goes away');        self::assertFalse($this->view->listHeld());    }    public function testTheFollowComesBackAfterAMomentWithoutTheFinger(): void    {        $this->startPhone();        $this->drag(500);        $this->wz->playIndex(2);        $this->game->run(10);        self::assertFalse($this->playingShown());        $this->game->wait(MobileView::FOLLOW_AFTER + 1.5);        self::assertTrue($this->playingShown(), 'the finger gone a while: the list follows again');    }    public function testTheSearchFollowsItsSongsAndASongItHidesMovesNothing(): void    {        $this->startPhone(20);        $this->tap('search');        $this->form->type('partage');        $this->game->run(2);        $lines = $this->view->shownLines();        self::assertGreaterThan(12, count($lines));        $this->wz->playIndex($lines[count($lines) - 2]);        $this->game->run(60);        self::assertTrue($this->playingShown(), 'its row of the search is shown');        $scroll = $this->view->scroll['list'];        $hidden = array_values(array_diff(array_keys($this->wz->playlist->items), $lines))[0];        $this->wz->playIndex($hidden);        $this->game->run(30);        self::assertSame($scroll, $this->view->scroll['list'], 'a song the search hides: the list stays');        self::assertNull($this->view->at('locate'));    }}