status-go/images/encode_test.go

103 lines
1.6 KiB
Go
Raw Permalink Normal View History

package images
import (
"bytes"
2020-10-22 15:59:01 +00:00
"errors"
"testing"
"github.com/stretchr/testify/require"
)
2020-10-09 11:17:04 +00:00
func TestEncode(t *testing.T) {
cs := []struct {
FileName string
RenderSize int
}{
{
"elephant.jpg",
1447,
},
{
"rose.webp",
11119,
},
{
"spin.gif",
2263,
},
{
"status.png",
5834,
},
}
options := EncodeConfig{
Quality: 70,
}
for _, c := range cs {
2020-10-09 11:17:04 +00:00
img, err := Decode(path + c.FileName)
require.NoError(t, err)
bb := bytes.NewBuffer([]byte{})
err = Encode(bb, img, options)
require.NoError(t, err)
require.Exactly(t, c.RenderSize, bb.Len())
}
}
2020-10-22 15:59:01 +00:00
func TestEncodeToBestSize(t *testing.T) {
cs := []struct {
FileName string
RenderSize int
2020-10-27 14:42:42 +00:00
Error error
2020-10-22 15:59:01 +00:00
}{
{
"elephant.jpg",
1467,
nil,
},
{
"rose.webp",
8513,
errors.New("image size after processing exceeds max, expected < '5632', received < '8513'"),
2020-10-22 15:59:01 +00:00
},
{
"spin.gif",
2407,
nil,
},
{
"status.png",
4725,
nil,
},
}
for _, c := range cs {
img, err := Decode(path + c.FileName)
require.NoError(t, err)
bb := bytes.NewBuffer([]byte{})
err = EncodeToBestSize(bb, img, ResizeDimensions[0])
require.Exactly(t, c.RenderSize, bb.Len())
if c.Error != nil {
require.EqualError(t, err, c.Error.Error())
} else {
require.NoError(t, err)
}
}
}
func TestCompressToFileLimits(t *testing.T) {
img, err := Decode(path + "IMG_1205.HEIC.jpg")
require.NoError(t, err)
bb := bytes.NewBuffer([]byte{})
err = CompressToFileLimits(bb, img, FileSizeLimits{50000, 350000})
require.NoError(t, err)
require.Equal(t, 291645, bb.Len())
}