IT
apps/winzik / tests / SplashPhoneTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\MainForm;use App\Scene\SplashScene;use JumPHP\Testing\GameTestCase;/** * The splash of 2001 on the phone app held upright (screens of 1080 x 2400 and 720 x 1280, real pixels): the picture * on the whole screen, turned a quarter, as big as the screen allows; it stays its time, a tap skips it after a second. */final class SplashPhoneTest extends GameTestCase{    protected function tearDown(): void    {        $this->resize(900, 860);        parent::tearDown();    }    /** The headless surface has one size: the view stops following it and is made the phone's size. */    private function resize(int $width, int $height): void    {        $config = $this->game->engine->config;        $values = new \ReflectionProperty($config, 'values');        $all = $values->getValue($config);        $all['view'] = $width === 900 && $height === 860 ? 'window' : 'fixed';        $values->setValue($config, $all);        $this->game->engine->sdl->getRenderer()->resizeView($width, $height);    }    /** @return array{0: int, 1: int, 2: int, 3: int} the box of the pixels that are not the teal background */    private static function pictureBox(\GdImage $img): array    {        [$w, $h] = [imagesx($img), imagesy($img)];        $box = [$w, $h, -1, -1];        for ($y = 0; $y < $h; $y += 4) {            for ($x = 0; $x < $w; $x += 4) {                $c = imagecolorat($img, $x, $y);                [$r, $g, $b] = [($c >> 16) & 255, ($c >> 8) & 255, $c & 255];                if (abs($r - 0) + abs($g - 128) + abs($b - 128) > 30) {                    $box = [min($box[0], $x), min($box[1], $y), max($box[2], $x), max($box[3], $y)];                }            }        }        return $box;    }    /**     * The headless shot is the test window (the view letterboxed in it, black around): cut to the view (its edges are the     * teal of the splash) and brought to the phone's size; written back as the shot.     */    private static function phoneScreen(string $file, int $w, int $h): \GdImage    {        $shot = imagecreatefrompng($file);        [$sw, $sh] = [imagesx($shot), imagesy($shot)];        $k = min($sw / $w, $sh / $h);        [$vw, $vh] = [(int)round($w * $k), (int)round($h * $k)];        $img = imagecreatetruecolor($w, $h);        imagecopyresampled($img, $shot, 0, 0, intdiv($sw - $vw, 2), intdiv($sh - $vh, 2), $w, $h, $vw, $vh);        imagepng($img, $file);        return $img;    }    /** @return list<array{0: int, 1: int}> */    public static function screens(): array    {        return [[1080, 2400], [720, 1280]];    }    public function testTheSplashFillsThePhoneScreenTurnedAQuarter(): void    {        foreach (self::screens() as [$w, $h]) {            $this->resize($w, $h);            $this->game->start(SplashScene::class, ['phone' => true]);            $splash = $this->game->scene();            self::assertInstanceOf(SplashScene::class, $splash);            self::assertTrue($splash->phone());            $this->game->wait(1.2);            $file = $this->game->shot("splash-phone-{$w}x{$h}");            $img = self::phoneScreen($file, $w, $h);            [$x1, $y1, $x2, $y2] = self::pictureBox($img);            $pw = $x2 - $x1;            $ph = $y2 - $y1;            // turned: taller than wide, in the proportions of the picture, the whole height of the screen            self::assertGreaterThan($pw * 2.5, $ph, "{$w}x{$h}: the picture is turned ($pw x $ph)");            self::assertGreaterThan($h * 0.95, $ph, "{$w}x{$h}: as tall as the screen");            self::assertGreaterThan($w * 0.5, $pw, "{$w}x{$h}: more than half the width (a 16:9 screen is wider than the picture turned)");            self::assertEqualsWithDelta(SplashScene::PICTURE_W / SplashScene::PICTURE_H, $ph / $pw, 0.15, 'its proportions kept');            // the yellow lines (what is loaded, the version) are on it, turned too: along the left edge of the picture            $yellow = 0;            for ($y = $y1; $y < $y2; $y += 2) {                for ($x = $x1; $x < $x1 + $pw * 0.15; $x++) {                    $c = imagecolorat($img, $x, $y);                    if ((($c >> 16) & 255) > 200 && (($c >> 8) & 255) > 200 && ($c & 255) < 80) {                        $yellow++;                    }                }            }            self::assertGreaterThan(200, $yellow, "{$w}x{$h}: the yellow texts are drawn");        }    }    public function testItStaysItsTimeATapSkipsItOnlyAfterASecond(): void    {        $this->resize(1080, 2400);        $this->game->start(SplashScene::class, ['phone' => true]);        $mouse = $this->game->engine->input->getMouse();        $this->game->wait(0.5);        $mouse->down(1);        $this->game->run(1);        $mouse->up(1);        self::assertInstanceOf(SplashScene::class, $this->game->scene(), 'a tap at once does not skip the picture');        $this->game->wait(1.8);        self::assertInstanceOf(SplashScene::class, $this->game->scene(), 'still there after the 2.2 s of the computer');        $this->game->wait(2.4);        self::assertInstanceOf(SplashScene::class, $this->game->scene(), 'still there while the jingle of 4.9 s plays');        $this->game->wait(0.6);        self::assertSame(SplashScene::main(), $this->game->scene()::class, 'then the interface chosen');        $this->game->start(SplashScene::class, ['phone' => true]);        $this->game->wait(1.2);        $mouse->down(1);        $this->game->run(1);        $mouse->up(1);        $this->game->wait(0.6);        self::assertInstanceOf(MainForm::class, $this->game->scene(), 'a tap after a second goes on');    }}