summaryrefslogtreecommitdiffstats
path: root/api/emoji_test.go
diff options
context:
space:
mode:
authorIraquitan Cordeiro Filho <iraquitanfilho@gmail.com>2016-11-21 23:00:13 -0300
committerenahum <nahumhbl@gmail.com>2016-11-21 23:00:13 -0300
commit48d64f3f68bc861eda3732457b5d24d238cacc53 (patch)
tree0f710ba1787dc60f4f20748b4d783bee47aa2211 /api/emoji_test.go
parent9b7e2e50e3fd0a8b81a3cddc0f7240b894778c65 (diff)
downloadchat-48d64f3f68bc861eda3732457b5d24d238cacc53.tar.gz
chat-48d64f3f68bc861eda3732457b5d24d238cacc53.tar.bz2
chat-48d64f3f68bc861eda3732457b5d24d238cacc53.zip
PLT-4277: Allow larger custom emojis by resizing (#4447)
Add function to resize image using resize.Thumbnail. Add function to resize gif using previous function. Add function to convert image.Image to image.Palleted. Add logic to identify image type and resize them if they are larger than MaxEmojiHeight or MaxEmojiWidth. Also increase MaxEmojiFileSize. * fix: Add github.com/nfnt to vendor * fix: Fix max file size and if logic in resizeEmoji * test: Fix and add new tests for new resize feature * fix: Fix and update translations to fit new feature * fix: Add requested changes
Diffstat (limited to 'api/emoji_test.go')
-rw-r--r--api/emoji_test.go59
1 files changed, 54 insertions, 5 deletions
diff --git a/api/emoji_test.go b/api/emoji_test.go
index fb23cc439..efe4fd363 100644
--- a/api/emoji_test.go
+++ b/api/emoji_test.go
@@ -177,8 +177,8 @@ func TestCreateEmoji(t *testing.T) {
CreatorId: th.BasicUser.Id,
Name: model.NewId(),
}
- if _, err := Client.CreateEmoji(emoji, createTestGif(t, 1000, 10), "image.gif"); err == nil {
- t.Fatal("shouldn't be able to create an emoji that's too wide")
+ if _, err := Client.CreateEmoji(emoji, createTestGif(t, 1000, 10), "image.gif"); err != nil {
+ t.Fatal("should be able to create an emoji that's too wide by resizing it")
}
// try to create an emoji that's too tall
@@ -186,8 +186,8 @@ func TestCreateEmoji(t *testing.T) {
CreatorId: th.BasicUser.Id,
Name: model.NewId(),
}
- if _, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 1000), "image.gif"); err == nil {
- t.Fatal("shouldn't be able to create an emoji that's too tall")
+ if _, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 1000), "image.gif"); err != nil {
+ t.Fatal("should be able to create an emoji that's too tall by resizing it")
}
// try to create an emoji that's too large
@@ -195,7 +195,7 @@ func TestCreateEmoji(t *testing.T) {
CreatorId: th.BasicUser.Id,
Name: model.NewId(),
}
- if _, err := Client.CreateEmoji(emoji, createTestAnimatedGif(t, 100, 100, 4000), "image.gif"); err == nil {
+ if _, err := Client.CreateEmoji(emoji, createTestAnimatedGif(t, 100, 100, 10000), "image.gif"); err == nil {
t.Fatal("shouldn't be able to create an emoji that's too large")
}
@@ -424,3 +424,52 @@ func TestGetEmojiImage(t *testing.T) {
t.Fatal("should've failed to get image for deleted emoji")
}
}
+
+func TestResizeEmoji(t *testing.T) {
+ // try to resize a jpeg image within MaxEmojiWidth and MaxEmojiHeight
+ small_img_data := createTestJpeg(t, MaxEmojiWidth, MaxEmojiHeight)
+ if small_img, _, err := image.Decode(bytes.NewReader(small_img_data)); err != nil {
+ t.Fatal("failed to decode jpeg bytes to image.Image")
+ } else {
+ resized_img := resizeEmoji(small_img, small_img.Bounds().Dx(), small_img.Bounds().Dy())
+ if resized_img.Bounds().Dx() > MaxEmojiWidth || resized_img.Bounds().Dy() > MaxEmojiHeight {
+ t.Fatal("resized jpeg width and height should not be greater than MaxEmojiWidth or MaxEmojiHeight")
+ }
+ if resized_img != small_img {
+ t.Fatal("should've returned small_img itself")
+ }
+ }
+ // try to resize a jpeg image
+ jpeg_data := createTestJpeg(t, 256, 256)
+ if jpeg_img, _, err := image.Decode(bytes.NewReader(jpeg_data)); err != nil {
+ t.Fatal("failed to decode jpeg bytes to image.Image")
+ } else {
+ resized_jpeg := resizeEmoji(jpeg_img, jpeg_img.Bounds().Dx(), jpeg_img.Bounds().Dy())
+ if resized_jpeg.Bounds().Dx() > MaxEmojiWidth || resized_jpeg.Bounds().Dy() > MaxEmojiHeight {
+ t.Fatal("resized jpeg width and height should not be greater than MaxEmojiWidth or MaxEmojiHeight")
+ }
+ }
+ // try to resize a png image
+ png_data := createTestJpeg(t, 256, 256)
+ if png_img, _, err := image.Decode(bytes.NewReader(png_data)); err != nil {
+ t.Fatal("failed to decode png bytes to image.Image")
+ } else {
+ resized_png := resizeEmoji(png_img, png_img.Bounds().Dx(), png_img.Bounds().Dy())
+ if resized_png.Bounds().Dx() > MaxEmojiWidth || resized_png.Bounds().Dy() > MaxEmojiHeight {
+ t.Fatal("resized png width and height should not be greater than MaxEmojiWidth or MaxEmojiHeight")
+ }
+ }
+ // try to resize an animated gif
+ gif_data := createTestAnimatedGif(t, 256, 256, 10)
+ if gif_img, err := gif.DecodeAll(bytes.NewReader(gif_data)); err != nil {
+ t.Fatal("failed to decode gif bytes to gif.GIF")
+ } else {
+ resized_gif := resizeEmojiGif(gif_img)
+ if resized_gif.Config.Width > MaxEmojiWidth || resized_gif.Config.Height > MaxEmojiHeight {
+ t.Fatal("resized gif width and height should not be greater than MaxEmojiWidth or MaxEmojiHeight")
+ }
+ if len(resized_gif.Image) != len(gif_img.Image) {
+ t.Fatal("resized gif should have the same number of frames as original gif")
+ }
+ }
+}