PL
apps/winzik / tests / PlaylistKeysTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Gui\PlaylistWindow;use App\Model\Playlist;use JumPHP\Gui\Pointer;/** The play list with the keyboard and the mouse: arrows, pages, Entrée, Suppr, Ctrl+A, the search typed, the hints. */final class PlaylistKeysTest extends PlayerTestCase{    private function list(int $songs = 60): PlaylistWindow    {        $this->startPlayer();        $list = $this->desk->playlist;        self::assertNotNull($list);        $this->wz->playlist->clear();        // real songs (a song that cannot be read is skipped): the four demo songs again and again        $demo = ['assets/music/winzik.mp3', 'assets/music/archettes-city.ogg', 'assets/music/chaine-hifi.wav', 'assets/music/partage.mp3'];        for ($i = 0; $i < $songs; $i++) {            $this->wz->playlist->add($demo[$i % 4]);        }        $this->game->run(2);        return $list;    }    /** @param list<int> $keys */    private function press(PlaylistWindow $list, array $keys, bool $ctrl = false, bool $shift = false, array $text = []): bool    {        $p = new Pointer();        $p->keys = $keys;        $p->ctrl = $ctrl;        $p->shift = $shift;        $p->text = $text;        $used = $list->keys($p);        $this->game->run(1);        return $used;    }    public function testTheArrowsPagesHomeAndEndMoveTheLineOfTheKeyboardAndTheListFollows(): void    {        $list = $this->list();        $this->wz->playIndex(0);        self::assertTrue($this->press($list, [Pointer::DOWN]));        self::assertSame(0, $list->cursor, 'the first arrow: the line of the keyboard on the song playing');        $this->press($list, [Pointer::DOWN]);        self::assertSame(1, $list->cursor);        $this->press($list, [Pointer::PAGEDOWN]);        self::assertSame(1 + $list->rows() - 1, $list->cursor);        $this->press($list, [Pointer::END]);        self::assertSame(59, $list->cursor);        self::assertSame(60 - $list->rows(), $list->top(), 'the last line shown');        $this->press($list, [Pointer::HOME]);        self::assertSame(0, $list->cursor);        self::assertSame(0, $list->top());        $this->press($list, [Pointer::UP]);        self::assertSame(0, $list->cursor, 'no line above the first');        $this->game->shot('playlist-keyboard');    }    public function testEnterPlaysTheLineOfTheKeyboardAndDeleteRemovesIt(): void    {        $list = $this->list();        $this->wz->playIndex(0);        $this->press($list, [Pointer::DOWN, Pointer::DOWN, Pointer::DOWN, Pointer::DOWN]);        $this->press($list, [Pointer::RETURN]);        self::assertSame(3, $this->wz->playlist->active);        $this->press($list, [Pointer::DOWN, Pointer::DELETE]);        self::assertSame(59, $this->wz->playlist->count());        self::assertSame(4, $list->cursor, 'the keyboard stays on the same place');        self::assertSame(3, $this->wz->playlist->active, 'the song playing still plays');    }    public function testCtrlAChoosesEveryLineThenDeleteEmptiesTheListAndTheHintShows(): void    {        $list = $this->list(12);        self::assertTrue($this->press($list, [ord('a')], ctrl: true));        self::assertTrue($list->editing(), 'the edit mode, where chosen lines show');        self::assertSame(12, $list->chosenCount());        $this->press($list, [Pointer::ESCAPE]);        self::assertSame(0, $list->chosenCount(), 'Échap lets them go');        $this->press($list, [ord('a')], ctrl: true);        $this->press($list, [Pointer::DELETE]);        self::assertSame(0, $this->wz->playlist->count());        $this->game->run(2);        $this->game->shot('playlist-empty');    }    public function testShiftAndTheArrowsChooseLinesInTheEditMode(): void    {        $list = $this->list();        $this->clickControl($list, 'edit');        $this->press($list, [Pointer::HOME]);        $this->press($list, [Pointer::DOWN, Pointer::DOWN], shift: true);        self::assertSame([0, 1, 2], array_keys($list->selection));    }    public function testCtrlAndAnArrowMoveTheSongItself(): void    {        $list = $this->list();        $items = $this->wz->playlist->items;        $this->press($list, [Pointer::HOME]);        $this->press($list, [Pointer::DOWN], ctrl: true);        self::assertSame([$items[1], $items[0]], array_slice($this->wz->playlist->items, 0, 2));        self::assertSame(1, $list->cursor, 'the keyboard goes with the song');    }    public function testALetterTypedStartsTheSearchAndTheArrowsGoThroughTheSongsFound(): void    {        $list = $this->list();        self::assertTrue($this->press($list, [], text: ['p', 'a', 'r', 't', 'a', 'g', 'e']));        self::assertTrue($list->searching);        self::assertSame('partage', $list->filter);        self::assertSame(15, $list->lineCount(), 'one line in four: 3, 7, 11...');        $this->press($list, [Pointer::DOWN, Pointer::DOWN]);        self::assertTrue($list->searchKey(Pointer::RETURN, ''));        self::assertSame(7, $this->wz->playlist->active, 'the second song found: the first arrow on 3, the second on 7');        $list->searchKey(null, 'z');        self::assertSame(0, $list->lineCount());        $this->game->run(2);        $this->game->shot('playlist-nothing-found');    }    public function testCtrlJAndAClickOnTheCountShowTheSongPlaying(): void    {        $list = $this->list();        $this->wz->playIndex(45);        $list->setTop(0);        $this->press($list, [ord('j')], ctrl: true);        self::assertSame(45, $list->cursor);        self::assertTrue($list->top() <= 45 && 45 < $list->top() + $list->rows());        $list->setTop(0);        $this->clickControl($list, 'status');        self::assertTrue($list->top() <= 45 && 45 < $list->top() + $list->rows(), 'the count of the foot does it too');    }    public function testADoubleClickInTheEditModePlaysTheLine(): void    {        $list = $this->list();        $this->clickControl($list, 'edit');        [$x, $y] = $this->at($list, 120, PlaylistWindow::LIST_TOP + 2 * PlaylistWindow::ROW_HEIGHT + 4);        $this->form->clickAt($x, $y);        $this->game->run(1);        self::assertSame([2], array_keys($list->selection));        $this->form->clickAt($x, $y, true);       // the second click of the double click        $this->game->run(1);        self::assertSame(2, $this->wz->playlist->active, 'the double click played it');        self::assertSame([], $list->selection, 'its two clicks chose it and let it go: as before the double click');    }    public function testHasardNeverPlaysTheSameSongTwiceInARowAndPreviousGoesBackToTheSongsPlayed(): void    {        $list = new Playlist(['a.mp3', 'b.mp3', 'c.mp3', 'd.mp3', 'e.mp3']);        $list->shuffle = true;        // the worst generator: always the same number; without the line playing, it can never come again        $list->setRandom(fn (int $max) => 0);        $played = [$list->current()];        for ($i = 0; $i < 6; $i++) {            $played[] = $list->next();            self::assertNotSame($played[$i], $played[$i + 1], 'never the same song twice in a row');        }        $list->setRandom(fn (int $max) => $max);        $list->select(4);                            // a line clicked: it counts as played too        self::assertSame($played[6], $list->previous(), 'Précédent: the song played before the click');        self::assertSame($played[5], $list->previous());        self::assertSame($played[4], $list->previous());        // the lines move and go: the history follows its songs        $list = new Playlist(['a.mp3', 'b.mp3', 'c.mp3', 'd.mp3', 'e.mp3']);        $list->shuffle = true;        $list->select(1);        $list->select(3);        $list->select(4);                            // played: a, b, d, then e        $list->move(1, 4);                           // b to the end        $list->remove([0]);                          // a goes        self::assertSame('d.mp3', $list->previous());        self::assertSame('b.mp3', $list->previous(), 'b, where it went');        self::assertSame([], $list->history(), 'a was removed: nothing before');    }}