package render
import (
"context"
"fmt"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
)
// FixturePage is the page the installers and verify-installed render to
// show the renderer works (spec section 3, "Pin and provisioning"): one
// block of fixtureColour at fixtureBlock on fixtureBackground, static,
// with nothing to load, so every render of it paints the same pixels.
const FixturePage = `
Renderer check
`
// What FixturePage paints at the desktop viewport (CSS pixels are
// device pixels there).
var (
fixtureBlock = image.Rect(100, 100, 300, 300)
fixtureColour = color.RGBA{0x1e, 0x6b, 0x52, 0xff}
fixtureBackground = color.RGBA{0xff, 0xff, 0xff, 0xff}
)
// CheckFixture renders FixturePage at the desktop viewport with cfg's
// renderer (cfg.Site is replaced by the fixture's own site) and checks
// the capture: no error code, one page, and the sampled pixels the
// page's. The site and output directories are made in scratch, a
// directory only this user can write, and removed again. A tree that
// does not verify is the launch's *release.RendererError.
func CheckFixture(ctx context.Context, cfg LaunchConfig, scratch string) error {
dir, err := os.MkdirTemp(scratch, ".fixture-")
if err != nil {
return err
}
defer os.RemoveAll(dir)
site, out := filepath.Join(dir, "site"), filepath.Join(dir, "out")
if err = os.Mkdir(site, 0o700); err != nil {
return err
}
if err = os.WriteFile(filepath.Join(site, "index.html"), []byte(FixturePage), 0o600); err != nil {
return err
}
if err = os.Mkdir(out, 0o700); err != nil {
return err
}
// Mkdir's mode follows the umask: Render wants its output private.
if err = os.Chmod(out, 0o700); err != nil {
return err
}
cfg.Site = site
report, err := Render(ctx, cfg, out, []Page{{Path: "/", Viewport: Desktop}})
if err != nil {
return err
}
if report.Error != "" || len(report.Pages) != 1 || report.Pages[0].Capture != "page-1.png" {
return fmt.Errorf("render: the fixture page rendered as %+v", report)
}
f, err := os.Open(filepath.Join(out, report.Pages[0].Capture))
if err != nil {
return err
}
defer f.Close()
img, err := png.Decode(f)
if err != nil {
return fmt.Errorf("render: the fixture capture: %w", err)
}
return checkFixtureCapture(img)
}
// checkFixtureCapture samples a capture of FixturePage: the viewport's
// size, the block's colour at its corners and centre, the background's
// just outside it and far from it.
func checkFixtureCapture(img image.Image) error {
w, h, _ := Desktop.Size()
if b := img.Bounds(); b != image.Rect(0, 0, w, h) {
return fmt.Errorf("render: the fixture capture is %v, not %dx%d", b, w, h)
}
in := fixtureBlock
samples := map[image.Point]color.RGBA{
in.Min: fixtureColour, {in.Max.X - 1, in.Max.Y - 1}: fixtureColour, {(in.Min.X + in.Max.X) / 2, (in.Min.Y + in.Max.Y) / 2}: fixtureColour,
{in.Min.X - 1, in.Min.Y}: fixtureBackground, {in.Max.X, in.Max.Y - 1}: fixtureBackground, {w - 1, h - 1}: fixtureBackground,
}
for p, want := range samples {
if got := color.RGBAModel.Convert(img.At(p.X, p.Y)).(color.RGBA); got != want {
return fmt.Errorf("render: the fixture capture is not the fixture page: %v at %v, want %v", got, p, want)
}
}
return nil
}