| 1 |
package warpc_test |
| 2 |
|
| 3 |
import ( |
| •4 |
"runtime" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
qt "github.com/frankban/quicktest" |
| 9 |
"github.com/gohugoio/hugo/hugolib" |
| •10 |
"github.com/gohugoio/hugo/resources/images/imagetesting" |
| 11 |
) |
| 12 |
|
| 13 |
func TestAvifBasic(t *testing.T) { |
| •14 |
if imagetesting.SkipGoldenTests { |
| 15 |
t.Skip("Skip golden test on this architecture") |
| 16 |
} |
| 17 |
|
| 18 |
files := ` |
| 19 |
-- hugo.toml -- |
| 20 |
-- assets/sunset.avif -- |
| 21 |
sourcefilename: ../../resources/testdata/bep/dock-75-hdr.avif |
| 22 |
-- layouts/home.html -- |
| 23 |
{{ $image := resources.Get "sunset.avif" }} |
| 24 |
Width/Height: {{ $image.Width }}/{{ $image.Height }}| |
| 25 |
Decode avif: {{ $jpeg := $image.Process "jpeg" }}|{{ $jpeg.RelPermalink }}| |
| 26 |
Encode avif from JPEG: {{ $avif := $jpeg.Process "avif" }}|{{ $avif.RelPermalink }}| |
| 27 |
Encode avif from avif: {{ $avif := $image.Process "avif" }}|{{ $avif.RelPermalink }}| |
| 28 |
` |
| 29 |
|
| 30 |
b, err := hugolib.TestE(t, files) |
| 31 |
b.Assert(err, qt.IsNil) |
| 32 |
b.AssertFileContent("public/index.html", "Width/Height: 1024/683") |
| 33 |
} |
| 34 |
|
| 35 |
// giphy.avif is an animated AVIF transcoded from giphy.gif (14 frames @ 200ms, |
| 36 |
// infinite loop) via avifenc. Verifies that decode preserves animation, the |
| 37 |
// pipeline runs every frame through resize, the GIF encoder writes a multi-frame |
| 38 |
// GIF, and the libavif "repetitionCount" → Go "LoopCount" inversion (libavif |
| 39 |
// -1 = infinite, image/gif 0 = infinite) is handled at the AVIF decoder boundary. |
| 40 |
func TestAvifAnimatedToGif(t *testing.T) { |
| •41 |
if imagetesting.SkipGoldenTests { |
| 42 |
t.Skip("Skip golden test on this architecture") |
| 43 |
} |
| 44 |
|
| 45 |
files := ` |
| 46 |
-- hugo.toml -- |
| 47 |
disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "404"] |
| 48 |
-- assets/giphy.avif -- |
| 49 |
sourcefilename: ../../resources/testdata/giphy.avif |
| 50 |
-- layouts/home.html -- |
| 51 |
{{ $img := resources.Get "giphy.avif" }} |
| 52 |
{{ $gif := $img.Resize "x60 gif" }} |
| 53 |
{{ $gif.Publish }} |
| 54 |
gif:{{ $gif.RelPermalink }} |
| 55 |
` |
| 56 |
|
| 57 |
b := hugolib.Test(t, files) |
| 58 |
rel := b.FileContent("public/index.html") |
| 59 |
|
| 60 |
var gifFile string |
| 61 |
for line := range strings.SplitSeq(rel, "\n") { |
| 62 |
if rest, ok := strings.CutPrefix(line, "gif:"); ok { |
| 63 |
gifFile = "public" + rest |
| 64 |
break |
| 65 |
} |
| 66 |
} |
| 67 |
b.Assert(gifFile, qt.Not(qt.Equals), "") |
| 68 |
|
| 69 |
durations := make([]int, 14) |
| 70 |
for i := range durations { |
| 71 |
durations[i] = 200 |
| 72 |
} |
| 73 |
b.ImageHelper(gifFile). |
| 74 |
AssertFormat("gif"). |
| 75 |
AssertIsAnimated(true). |
| 76 |
AssertLoopCount(0). |
| 77 |
AssertFrameDurations(durations) |
| 78 |
} |
| 79 |
|
| 80 |
// See issue 14987. |
| 81 |
func TestAvifEncodeHintSubsampling(t *testing.T) { |
| •82 |
if runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "arm64" { |
| 83 |
t.Skipf("Skipping test on %v due to dithering variations.", runtime.GOARCH) |
| 84 |
} |
| 85 |
files := ` |
| 86 |
-- hugo.toml -- |
| 87 |
-- assets/logo.png -- |
| 88 |
sourcefilename: ../../resources/testdata/gohugoio24.png |
| 89 |
-- layouts/home.html -- |
| 90 |
{{ $img := resources.Get "logo.png" }} |
| 91 |
default:{{ ($img.Process "avif photo").RelPermalink }}| |
| 92 |
photo:{{ ($img.Process "avif photo").RelPermalink }}| |
| 93 |
text:{{ ($img.Process "avif text").RelPermalink }}| |
| 94 |
` |
| 95 |
|
| 96 |
b := hugolib.Test(t, files) |
| 97 |
b.AssertFileContent("public/index.html", |
| 98 |
` |
| 99 |
default:/logo_hu_72e150ce03376cfd.avif| |
| 100 |
photo:/logo_hu_72e150ce03376cfd.avif| |
| 101 |
text:/logo_hu_9de8f282912bc925.avif| |
| 102 |
`) |
| 103 |
} |
| 104 |
|
| 105 |
// See issue 14985. |
| 106 |
func TestAvifEncodeOutOfMemory(t *testing.T) { |
| 107 |
files := ` |
| 108 |
-- assets/gopher.png -- |
| 109 |
sourcefilename: ../../resources/testdata/bw-gopher.png |
| 110 |
-- layouts/home.html -- |
| 111 |
{{ $img := resources.Get "gopher.png" }} |
| 112 |
{{ $r := try ($img.Resize "3000x3000 avif") }} |
| 113 |
{{ with $r.Err }}BigErr: {{ . }}|{{ else }}BigOK|{{ end }} |
| 114 |
{{ $small := $img.Resize "32x32 avif" }} |
| 115 |
SmallAfter: {{ $small.RelPermalink }}| |
| 116 |
` |
| 117 |
|
| 118 |
b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) { |
| 119 |
c.WarpcMemory = 8 |
| 120 |
})) |
| 121 |
|
| 122 |
// The big resize must fail gracefully (caught by try) ... |
| 123 |
b.AssertFileContent("public/index.html", |
| 124 |
"BigErr:", |
| 125 |
"out of memory allocating", |
| 126 |
"for blob data", |
| 127 |
// ... and the dispatcher must still process images afterwards. |
| 128 |
"SmallAfter: /gopher_", |
| 129 |
) |
| 130 |
} |