github.com/status-im/status-go@v1.1.0/images/decode_test.go (about)

     1  package images
     2  
     3  import (
     4  	"errors"
     5  	"image"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  const (
    14  	path = "../_assets/tests/"
    15  )
    16  
    17  func TestDecode(t *testing.T) {
    18  
    19  	cs := []struct {
    20  		Filepath string
    21  		Error    bool
    22  		Nil      bool
    23  		Bounds   image.Rectangle
    24  	}{
    25  		{
    26  			"elephant.jpg",
    27  			false,
    28  			false,
    29  			image.Rectangle{
    30  				Min: image.Point{X: 0, Y: 0},
    31  				Max: image.Point{X: 80, Y: 80},
    32  			},
    33  		},
    34  		{
    35  			"status.png",
    36  			false,
    37  			false,
    38  			image.Rectangle{
    39  				Min: image.Point{X: 0, Y: 0},
    40  				Max: image.Point{X: 256, Y: 256},
    41  			},
    42  		},
    43  		{
    44  			"spin.gif",
    45  			false,
    46  			false,
    47  			image.Rectangle{
    48  				Min: image.Point{X: 0, Y: 0},
    49  				Max: image.Point{X: 256, Y: 256},
    50  			},
    51  		},
    52  		{
    53  			"rose.webp",
    54  			false,
    55  			false,
    56  			image.Rectangle{
    57  				Min: image.Point{X: 0, Y: 0},
    58  				Max: image.Point{X: 400, Y: 301},
    59  			},
    60  		},
    61  		{
    62  			"test.aac",
    63  			true,
    64  			true,
    65  			image.Rectangle{},
    66  		},
    67  	}
    68  
    69  	for _, c := range cs {
    70  		img, err := Decode(path + c.Filepath)
    71  
    72  		if c.Error {
    73  			require.Error(t, err)
    74  		} else {
    75  			require.NoError(t, err)
    76  		}
    77  
    78  		if c.Nil {
    79  			require.Nil(t, img)
    80  			continue
    81  		} else {
    82  			require.NotNil(t, img)
    83  		}
    84  
    85  		require.Exactly(t, c.Bounds, img.Bounds())
    86  	}
    87  }
    88  
    89  func TestDecodeFromURL(t *testing.T) {
    90  	s := httptest.NewServer(http.FileServer(http.Dir(path)))
    91  	defer s.Close()
    92  
    93  	cs := []struct {
    94  		Filepath string
    95  		Nil      bool
    96  		Bounds   image.Rectangle
    97  	}{
    98  		{
    99  			s.URL + "/2x1.png",
   100  			false,
   101  			image.Rectangle{
   102  				Min: image.Point{X: 0, Y: 0},
   103  				Max: image.Point{X: 2, Y: 1},
   104  			},
   105  		},
   106  		{
   107  			s.URL + "/1.jpg",
   108  			false,
   109  			image.Rectangle{
   110  				Min: image.Point{X: 0, Y: 0},
   111  				Max: image.Point{X: 1, Y: 1},
   112  			},
   113  		},
   114  		{
   115  			s.URL + "/1.gif",
   116  			false,
   117  			image.Rectangle{
   118  				Min: image.Point{X: 0, Y: 0},
   119  				Max: image.Point{X: 1, Y: 1},
   120  			},
   121  		},
   122  		{
   123  			s.URL + "/1.webp",
   124  			false,
   125  			image.Rectangle{
   126  				Min: image.Point{X: 0, Y: 0},
   127  				Max: image.Point{X: 1, Y: 1},
   128  			},
   129  		},
   130  		{
   131  			s.URL + "/1.webp",
   132  			true,
   133  			image.Rectangle{
   134  				Min: image.Point{X: 0, Y: 0},
   135  				Max: image.Point{X: 10, Y: 10},
   136  			},
   137  		},
   138  	}
   139  
   140  	for _, c := range cs {
   141  		img, err := DecodeFromURL(c.Filepath)
   142  
   143  		if c.Nil {
   144  			require.Nil(t, err)
   145  		} else {
   146  			require.NoError(t, err)
   147  			require.Exactly(t, c.Bounds, img.Bounds())
   148  		}
   149  
   150  	}
   151  }
   152  
   153  func TestDecodeFromURL_WithErrors(t *testing.T) {
   154  	s := httptest.NewServer(http.FileServer(http.Dir(path)))
   155  	defer s.Close()
   156  
   157  	_, err := DecodeFromURL("https://doesnt-exist.com")
   158  	require.Error(t, err)
   159  }
   160  
   161  func TestGetType(t *testing.T) {
   162  	cs := []struct {
   163  		Buf   []byte
   164  		Value ImageType
   165  	}{
   166  		{testJpegBytes, JPEG},
   167  		{testPngBytes, PNG},
   168  		{testGifBytes, GIF},
   169  		{testWebpBytes, WEBP},
   170  		{testAacBytes, UNKNOWN},
   171  	}
   172  
   173  	for _, c := range cs {
   174  		require.Exactly(t, c.Value, GetType(c.Buf))
   175  	}
   176  }
   177  
   178  func TestGetMimeType(t *testing.T) {
   179  	cs := []struct {
   180  		Buf   []byte
   181  		Value string
   182  		Error error
   183  	}{
   184  		{testJpegBytes, "jpeg", nil},
   185  		{testPngBytes, "png", nil},
   186  		{testGifBytes, "gif", nil},
   187  		{testWebpBytes, "webp", nil},
   188  		{testAacBytes, "", errors.New("image format not supported")},
   189  	}
   190  
   191  	for _, c := range cs {
   192  		mt, err := GetMimeType(c.Buf)
   193  		if c.Error == nil {
   194  			require.NoError(t, err)
   195  		} else {
   196  			require.EqualError(t, err, c.Error.Error())
   197  		}
   198  
   199  		require.Exactly(t, c.Value, mt)
   200  	}
   201  }