|
| 1 | +package filters |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "github.com/zalando-stups/skrop/filters/imagefiltertest" |
| 7 | + "github.com/zalando/skipper/filters" |
| 8 | +) |
| 9 | + |
| 10 | +func TestNewCropByFocalPoint(t *testing.T) { |
| 11 | + name := NewCropByFocalPoint().Name() |
| 12 | + assert.Equal(t, "cropByFocalPoint", name) |
| 13 | +} |
| 14 | + |
| 15 | +func TestCropByFocalPoint_Name(t *testing.T) { |
| 16 | + c := cropByFocalPoint{} |
| 17 | + assert.Equal(t, "cropByFocalPoint", c.Name()) |
| 18 | +} |
| 19 | + |
| 20 | +func TestCropByFocalPoint_CreateOptions(t *testing.T) { |
| 21 | + c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} |
| 22 | + image := imagefiltertest.LandscapeImage() |
| 23 | + fc := createDefaultContext(t, "doesnotmatter.com") |
| 24 | + fc.FParams = make(map[string]string) |
| 25 | + fc.FParams["focalPointX"] = "500"; |
| 26 | + fc.FParams["focalPointY"] = "334"; |
| 27 | + |
| 28 | + options, _ := c.CreateOptions(buildParameters(fc, image)) |
| 29 | + |
| 30 | + assert.Equal(t, 445, options.AreaWidth) |
| 31 | + assert.Equal(t, 668, options.AreaHeight) |
| 32 | + assert.Equal(t, 0, options.Top) |
| 33 | + assert.Equal(t, 278, options.Left) |
| 34 | + |
| 35 | + c = cropByFocalPoint{targetX: 0.5, targetY: 0.25, aspectRatio: 1.5} |
| 36 | + image = imagefiltertest.LandscapeImage() |
| 37 | + fc = createDefaultContext(t, "doesnotmatter.com") |
| 38 | + fc.FParams = make(map[string]string) |
| 39 | + fc.FParams["focalPointX"] = "500"; |
| 40 | + fc.FParams["focalPointY"] = "334"; |
| 41 | + |
| 42 | + options, _ = c.CreateOptions(buildParameters(fc, image)) |
| 43 | + |
| 44 | + assert.Equal(t, 296, options.AreaWidth) |
| 45 | + assert.Equal(t, 445, options.AreaHeight) |
| 46 | + assert.Equal(t, 223, options.Top) |
| 47 | + assert.Equal(t, 352, options.Left) |
| 48 | +} |
| 49 | + |
| 50 | +func TestCropByFocalPoint_CreateOptions_MissingPathParam(t *testing.T) { |
| 51 | + c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} |
| 52 | + image := imagefiltertest.LandscapeImage() |
| 53 | + fc := createDefaultContext(t, "doesnotmatter.com") |
| 54 | + fc.FParams = make(map[string]string) |
| 55 | + fc.FParams["focalPointY"] = "334"; |
| 56 | + |
| 57 | + options, err := c.CreateOptions(buildParameters(fc, image)) |
| 58 | + |
| 59 | + assert.Nil(t, options) |
| 60 | + assert.Equal(t, filters.ErrInvalidFilterParameters, err) |
| 61 | + |
| 62 | + fc = createDefaultContext(t, "doesnotmatter.com") |
| 63 | + fc.FParams = make(map[string]string) |
| 64 | + fc.FParams["focalPointX"] = "334"; |
| 65 | + |
| 66 | + options, err = c.CreateOptions(buildParameters(fc, image)) |
| 67 | + |
| 68 | + assert.Nil(t, options) |
| 69 | + assert.Equal(t, filters.ErrInvalidFilterParameters, err) |
| 70 | +} |
| 71 | + |
| 72 | +func TestCropByFocalPoint_CreateOptions_InvalidPathParam(t *testing.T) { |
| 73 | + c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} |
| 74 | + image := imagefiltertest.LandscapeImage() |
| 75 | + fc := createDefaultContext(t, "doesnotmatter.com") |
| 76 | + fc.FParams = make(map[string]string) |
| 77 | + fc.FParams["focalPointX"] = "xyz"; |
| 78 | + fc.FParams["focalPointY"] = "abc"; |
| 79 | + |
| 80 | + options, err := c.CreateOptions(buildParameters(fc, image)) |
| 81 | + |
| 82 | + assert.Nil(t, options) |
| 83 | + assert.NotNil(t, err) |
| 84 | + |
| 85 | + fc.FParams["focalPointX"] = "100"; |
| 86 | + fc.FParams["focalPointY"] = "abc"; |
| 87 | + |
| 88 | + options, err = c.CreateOptions(buildParameters(fc, image)) |
| 89 | + |
| 90 | + assert.Nil(t, options) |
| 91 | + assert.NotNil(t, err) |
| 92 | +} |
| 93 | + |
| 94 | +func TestCropByFocalPoint_CanBeMerged(t *testing.T) { |
| 95 | + ea := transformFromQueryParams{} |
| 96 | + assert.Equal(t, false, ea.CanBeMerged(nil, nil)) |
| 97 | +} |
| 98 | + |
| 99 | +func TestCropByFocalPoint_CreateFilter(t *testing.T) { |
| 100 | + imagefiltertest.TestCreate(t, NewCropByFocalPoint, []imagefiltertest.CreateTestItem{{ |
| 101 | + Msg: "less than 3 args", |
| 102 | + Args: nil, |
| 103 | + Err: true, |
| 104 | + }, { |
| 105 | + Msg: "invalid targetX", |
| 106 | + Args: []interface{}{"xyz", 0.5, 1.5}, |
| 107 | + Err: true, |
| 108 | + }, { |
| 109 | + Msg: "invalid targetY", |
| 110 | + Args: []interface{}{0.5, "abc", 1.5}, |
| 111 | + Err: true, |
| 112 | + }, { |
| 113 | + Msg: "invalid aspectRatio", |
| 114 | + Args: []interface{}{0.5, 0.5, "qwerty"}, |
| 115 | + Err: true, |
| 116 | + }, { |
| 117 | + Msg: "3 args", |
| 118 | + Args: []interface{}{0.5, 0.5, 1.5}, |
| 119 | + Err: false, |
| 120 | + }, { |
| 121 | + Msg: "more than 3 args", |
| 122 | + Args: []interface{}{0.5, 0.5, 1.5, 1.0}, |
| 123 | + Err: true, |
| 124 | + }}) |
| 125 | +} |
0 commit comments