| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "image" |
| 5 | "image/color" |
| 6 | "image/draw" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // fixtureImage is a capture as the fixture page paints it, with the |
| 12 | // block in colour c. |
| 13 | func fixtureImage(w, h int, c color.Color) *image.RGBA { |
| 14 | img := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 15 | draw.Draw(img, img.Bounds(), image.NewUniform(fixtureBackground), image.Point{}, draw.Src) |
| 16 | draw.Draw(img, fixtureBlock, image.NewUniform(c), image.Point{}, draw.Src) |
| 17 | return img |
| 18 | } |
| 19 | |
| 20 | // A capture passes only as the fixture page paints it at the desktop |
| 21 | // viewport: the block in its colour, the rest in the background. |
| 22 | func TestFixtureCaptureIsSampled(t *testing.T) { |
| 23 | w, h, _ := Desktop.Size() |
| 24 | if err := checkFixtureCapture(fixtureImage(w, h, fixtureColour)); err != nil { |
| 25 | t.Fatalf("the fixture's own capture refused: %v", err) |
| 26 | } |
| 27 | blank := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 28 | draw.Draw(blank, blank.Bounds(), image.NewUniform(fixtureBackground), image.Point{}, draw.Src) |
| 29 | for name, img := range map[string]image.Image{ |
| 30 | "no block": blank, |
| 31 | "other colour": fixtureImage(w, h, color.RGBA{0x1e, 0x6b, 0x53, 0xff}), |
| 32 | "all block": fixtureImage(w, h, fixtureColour).SubImage(fixtureBlock), |
| 33 | "smaller": fixtureImage(w-1, h, fixtureColour), |
| 34 | "larger": fixtureImage(w, h+1, fixtureColour), |
| 35 | } { |
| 36 | if checkFixtureCapture(img) == nil { |
| 37 | t.Errorf("a capture with %s passed", name) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // The page is static: no script, no external load, nothing that could |
| 43 | // make two renders differ. |
| 44 | func TestFixturePageIsStatic(t *testing.T) { |
| 45 | for _, banned := range []string{"<script", "http:", "https:", "@import", "url(", "<img", "<link"} { |
| 46 | if strings.Contains(strings.ToLower(FixturePage), banned) { |
| 47 | t.Errorf("the fixture page has %q", banned) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |