Skip to content

Commit

Permalink
internal/test: allow images to be +/- 1 RGB value per pixel
Browse files Browse the repository at this point in the history
This provides robustness when the rendering engine
behaves (ever so) slightly differently.

Fixes fyne-io#4573 (I think, I can't be entirely sure
because my tests still fail because of fyne-io#4572)

While we're here, let gofumpt add an octal marker to 0o755.
  • Loading branch information
josharian committed Jan 28, 2024
1 parent caf50f6 commit 5056eac
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions internal/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -44,13 +43,38 @@ func AssertImageMatches(t *testing.T, masterFilename string, img image.Image, ms
if len(msgAndArgs) > 0 {
msg = fmt.Sprintf(msgAndArgs[0].(string)+"\n", msgAndArgs[1:]...)
}
if !assert.Equal(t, masterPix, capturePix, "%sImage did not match master. Actual image written to file://%s.", msg, failedPath) {
if !pixCloseEnough(masterPix, capturePix) {
t.Logf("%sImage did not match master. Actual image written to file://%s.", msg, failedPath)
require.NoError(t, writeImage(failedPath, img))
return false
}
return true
}

// pixCloseEnough reports whether a and b vary by at most 1 at any given offset.
func pixCloseEnough(a, b []uint8) bool {
if len(a) != len(b) {
return false
}

for i, v := range a {
w := b[i]
if v == w {
continue
}
// Allow a delta of 1 for rendering variation.
delta := int(v) - int(w) // use int to avoid overflow
if delta < 0 {
delta *= -1
}
if delta > 1 {
return false
}
}

return true
}

// NewCheckedImage returns a new black/white checked image with the specified size
// and the specified amount of horizontal and vertical tiles.
func NewCheckedImage(w, h, hTiles, vTiles int) image.Image {
Expand Down Expand Up @@ -83,7 +107,7 @@ func pixelsForImage(t *testing.T, img image.Image) []uint8 {
}

func writeImage(path string, img image.Image) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
f, err := os.Create(path)
Expand Down

0 comments on commit 5056eac

Please sign in to comment.