Skip to content

Commit

Permalink
Merge pull request #5198 from andydotxyz/fix/moretests
Browse files Browse the repository at this point in the history
Plopping in some tests in packages lacking completely or low hanging fruit
  • Loading branch information
andydotxyz authored Oct 16, 2024
2 parents 4875e35 + 43d7e39 commit bfa7219
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (a *fyneApp) Icon() fyne.Resource {
return a.icon
}

if a.Metadata().Icon == nil || len(a.Metadata().Icon.Content()) == 0 {
return nil
}
return a.Metadata().Icon
}

Expand Down
2 changes: 1 addition & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestFyneApp_UniqueID(t *testing.T) {
func TestFyneApp_SetIcon(t *testing.T) {
app := NewWithID("io.fyne.test")

metaIcon := &fyne.StaticResource{StaticName: "Metadata"}
metaIcon := &fyne.StaticResource{StaticName: "Metadata", StaticContent: []byte("?PNG...")}
SetMetadata(fyne.AppMetadata{
Icon: metaIcon,
})
Expand Down
34 changes: 34 additions & 0 deletions app/icon_cache_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app

import (
_ "embed"
"os"
"testing"

"fyne.io/fyne/v2"
"github.com/stretchr/testify/assert"
)

//go:embed testdata/fyne.png
var iconData []byte

func TestCachedIcon_PATH(t *testing.T) {
SetMetadata(fyne.AppMetadata{})
a := &fyneApp{uniqueID: "icontest"}
assert.Equal(t, "", a.cachedIconPath())

a.SetIcon(fyne.NewStaticResource("dummy", iconData))
path := a.cachedIconPath()
if path == "" {
t.Error("cache path not constructed")
return
} else {
defer os.Remove(path)
}

info, err := os.Stat(path)
assert.Nil(t, err)
assert.Equal(t, "icon.png", info.Name())

assert.Nil(t, err)
}
7 changes: 1 addition & 6 deletions app/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ func convertLists(values map[string]any) {
floats[i] = item.(float64)
}
values[k] = floats
case int:
ints := make([]int, len(items))
for i, item := range items {
ints[i] = item.(int)
}
values[k] = ints
//case int: // json has no int!
case string:
strings := make([]string, len(items))
for i, item := range items {
Expand Down
15 changes: 15 additions & 0 deletions app/preferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestPreferences_Save(t *testing.T) {
val["keyFloatList"] = []float64{1.1, 2.2, 3.3}
val["keyBool"] = true
val["keyBoolList"] = []bool{true, false, true}
val["keyEmptyList"] = []string{}
})

path := p.storagePath()
Expand Down Expand Up @@ -97,4 +98,18 @@ func TestPreferences_Load(t *testing.T) {
assert.Equal(t, []float64{1.1, 2.2, 3.3}, p.FloatList("keyFloatList"))
assert.Equal(t, true, p.Bool("keyBool"))
assert.Equal(t, []bool{true, false, true}, p.BoolList("keyBoolList"))
assert.Equal(t, 0, len(p.StringList("keyEmptyList")))
}

func TestPreferences_EmptyLoad(t *testing.T) {
p := newPreferences(&fyneApp{uniqueID: ""})

count := 0
p.ReadValues(func(v map[string]any) {
for range v {
count++
}
})

assert.Zero(t, count)
}
Binary file added app/testdata/fyne.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion app/testdata/preferences.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"keyFloat": 3.5,
"keyFloatList": [1.1, 2.2, 3.3],
"keyBool": true,
"keyBoolList": [true, false, true]
"keyBoolList": [true, false, true],
"keyEmptyList": []
}
2 changes: 1 addition & 1 deletion lang/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func SystemLocale() fyne.Locale {
if err != nil {
fyne.LogError("Failed to look up user locale", err)
}
if loc == "" {
if len(loc) < 2 {
loc = "en"
}

Expand Down
24 changes: 24 additions & 0 deletions lang/locale_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lang

import (
"testing"

"github.com/jeandeaual/go-locale"
"github.com/stretchr/testify/assert"
)

func TestSystemLocale(t *testing.T) {
info, err := locale.GetLocale()
if err != nil {
// something not testable
t.Log("Unable to run locale test because", err)
return
}

if len(info) < 2 {
info = "en_US"
}

loc := SystemLocale()
assert.Equal(t, info[:2], loc.String()[:2])
}
12 changes: 10 additions & 2 deletions tools/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ import (
"fyne.io/fyne/v2/theme"
)

func imageToPlayground(img image.Image) {
func encodeImage(img image.Image) (string, error) {
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}

func imageToPlayground(img image.Image) {
enc, err := encodeImage(img)
if err != nil {
fyne.LogError("Failed to encode image", err)
return
}

enc := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println("IMAGE:" + enc)
}

Expand Down
31 changes: 31 additions & 0 deletions tools/playground/playground_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package playground

import (
"encoding/base64"
"image/color"
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/software"
"fyne.io/fyne/v2/internal/test"
"fyne.io/fyne/v2/theme"

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

func TestRender(t *testing.T) {
obj := canvas.NewRectangle(color.Black)
obj.SetMinSize(fyne.NewSquareSize(10))

img := software.Render(obj, test.DarkTheme(theme.DefaultTheme()))
assert.NotNil(t, img)

enc, err := encodeImage(img)
assert.Nil(t, err)
assert.Equal(t, "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAFElEQVR4nGJiwAtGpbECQAAAAP//DogAFaNSFa8AAAAASUVORK5CYII=", enc)

bytes, err := base64.StdEncoding.DecodeString(enc)
assert.Nil(t, err)
assert.Equal(t, "PNG", string(bytes)[1:4])
}

0 comments on commit bfa7219

Please sign in to comment.