KO
apps/winzik / tests / ShortcutsTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\FilesForm;use App\Gui\Desktop;use App\Media\SystemVolume;use JumPHP\Gui\Pointer;/** The keyboard of WinZik: the keys of the "Plugins Clavier", the ones that always work, the computer's volume. */final class ShortcutsTest extends PlayerTestCase{    /** @param list<int> $keys @param list<string> $text */    private function press(array $keys = [], array $text = [], bool $ctrl = false, bool $shift = false): bool    {        $p = new Pointer();        [$p->keys, $p->text, $p->ctrl, $p->shift] = [$keys, $text, $ctrl, $shift];        return $this->desk->keys($p);    }    public function testNPlaysTheNextSongAndShiftArrowsJumpThirtySeconds(): void    {        $this->startPlayer();        $this->wz->playIndex(0);        $this->game->run(2);        self::assertTrue($this->press(text: ['n']));        self::assertSame(1, $this->wz->playlist->active);        // a song long enough for the jumps (the demo songs last half a minute)        foreach ($this->wz->playlist->items as $i => $song) {            if ($this->wz->describe($song)[1]->duration > 32) {                $this->wz->playIndex($i);                break;            }        }        $this->game->run(2);        self::assertGreaterThan(32, $this->wz->player->duration());        $this->wz->seek(1);        self::assertTrue($this->press([Pointer::RIGHT], shift: true));        self::assertEqualsWithDelta(31, $this->wz->player->position, 0.5, 'Shift: 30 s');        self::assertTrue($this->press([Pointer::LEFT]));        self::assertEqualsWithDelta(26, $this->wz->player->position, 0.5, 'without Shift: 5 s');        self::assertTrue($this->press([Pointer::LEFT], shift: true));        self::assertEqualsWithDelta(0, $this->wz->player->position, 0.5, 'never before the start');    }    public function testTheFiguresGoToTenthsOfTheSong(): void    {        $this->startPlayer();        $this->wz->playIndex(0);        $this->game->run(2);        $length = $this->wz->player->duration();        self::assertGreaterThan(0, $length);        $this->press(text: ['5']);        self::assertEqualsWithDelta($length * 0.5, $this->wz->player->position, 0.5);        $this->press(text: ['0']);        self::assertEqualsWithDelta(0, $this->wz->player->position, 0.5);        $this->press(text: ['9']);        self::assertEqualsWithDelta($length * 0.9, $this->wz->player->position, 0.5);    }    public function testCtrlOOpensTheFilesAndCtrlQQuitsWithTheEffect(): void    {        $this->startPlayer();        self::assertTrue($this->press([ord('o')], ctrl: true));        $this->game->run(2);        $files = $this->game->scene();        self::assertInstanceOf(FilesForm::class, $files, 'the window to add files');        $files->close();        $this->game->run(2);        self::assertTrue($this->press([ord('q')], ctrl: true));        self::assertGreaterThan(0.0, (float)$this->game->peek('closing', $this->desk), 'the windows roll up, then WinZik ends');    }    public function testDeleteRemovesTheLinesChosenEvenWithThePointerElsewhere(): void    {        $this->startPlayer();        $list = $this->desk->playlist;        self::assertNotNull($list);        $before = $this->wz->playlist->items;        $list->selection = [1 => true, 2 => true];        self::assertTrue($this->press([Pointer::DELETE]));        self::assertSame([$before[0], $before[3]], $this->wz->playlist->items);    }    public function testSpaceAndTheMediaKeysWorkWithoutThePlugin(): void    {        $this->startPlayer();        $this->wz->usePlugin('');        self::assertTrue($this->press(text: [' ']));        self::assertSame('playing', $this->wz->player->state, 'Space: play');        self::assertTrue($this->press([Desktop::KEY_AUDIOPLAY]));        self::assertSame('paused', $this->wz->player->state, 'the play / pause key');        self::assertTrue($this->press([Desktop::KEY_AUDIONEXT]));        self::assertSame(1, $this->wz->playlist->active, 'the next song key');        self::assertTrue($this->press([Desktop::KEY_AUDIOPREV]));        self::assertSame(0, $this->wz->playlist->active, 'the previous song key');        self::assertTrue($this->press([Desktop::KEY_AUDIOSTOP]));        self::assertSame('stopped', $this->wz->player->state, 'the stop key');        $volume = $this->wz->player->volume;        self::assertTrue($this->press([Desktop::KEY_AUDIOMUTE]));        self::assertSame(0, $this->wz->player->volume, 'muted');        self::assertTrue($this->press([Desktop::KEY_AUDIOMUTE]));        self::assertSame($volume, $this->wz->player->volume, 'the sound back');        // the other keys are the plug-in's        self::assertFalse($this->press(text: ['b']));        self::assertSame(0, $this->wz->playlist->active);    }    public function testTheArrowsSetTheComputersVolumeWhenTheSliderIsOnIt(): void    {        $this->startPlayer();        $own = $this->wz->player->volume;        $system = new SystemVolume($this->wz->storagePath('tmp/logs'));        $system->volume = 40;        $this->wz->systemVolume = $system;        $this->wz->volumeMode = 'system';        $this->press([Pointer::UP]);        self::assertSame(45, $system->volume, 'the computer louder');        $this->press(text: ['-', '-']);        self::assertSame(35, $system->volume);        self::assertSame($own, $this->wz->player->volume, "WinZik's own volume untouched");        $this->wz->volumeMode = 'winzik';        $this->wz->systemVolume = null;     // no program run to set the computer's volume    }    public function testF11PutsTheFilmOnTheWholeScreen(): void    {        $this->startPlayer();        $video = $this->desk->video;        self::assertNotNull($video);        self::assertFalse($this->press([Desktop::KEY_F11]), 'no film: nothing');        $this->wz->player->file = $this->wz->storagePath('test/clip.mp4');     // a film playing        self::assertTrue($this->press([Desktop::KEY_F11]));        self::assertTrue($video->full());        self::assertTrue($this->press([Pointer::ESCAPE]));        self::assertFalse($video->full(), 'Escape: back');        $this->wz->player->file = null;    }}