Skip to content

Commit

Permalink
Add tests for tree as well
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jul 27, 2023
1 parent fdcb428 commit dff3583
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
52 changes: 51 additions & 1 deletion widget/gridwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,61 @@ package widget
import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestGridWrap_Focus(t *testing.T) {
defer test.NewApp()
list := createGridWrap(100)
window := test.NewWindow(list)
defer window.Close()
window.Resize(list.MinSize().Max(fyne.NewSize(150, 200)))

canvas := window.Canvas().(test.WindowlessCanvas)
assert.Nil(t, canvas.Focused())

canvas.FocusNext()
assert.NotNil(t, canvas.Focused())
assert.Equal(t, 0, canvas.Focused().(*GridWrap).currentFocus)

children := list.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).children
assert.True(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.False(t, children[6].(*gridWrapItem).hovered)
assert.False(t, children[7].(*gridWrapItem).hovered)

list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown})
assert.False(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.True(t, children[6].(*gridWrapItem).hovered)
assert.False(t, children[7].(*gridWrapItem).hovered)

list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyRight})
assert.False(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.False(t, children[6].(*gridWrapItem).hovered)
assert.True(t, children[7].(*gridWrapItem).hovered)

list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyLeft})
assert.False(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.True(t, children[6].(*gridWrapItem).hovered)
assert.False(t, children[7].(*gridWrapItem).hovered)

list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp})
assert.True(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.False(t, children[6].(*gridWrapItem).hovered)
assert.False(t, children[7].(*gridWrapItem).hovered)

canvas.Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
assert.True(t, children[0].(*gridWrapItem).selected)
}

func TestGridWrap_New(t *testing.T) {
g := createGridWrap(1000)
template := NewIcon(theme.AccountIcon())
Expand Down
8 changes: 8 additions & 0 deletions widget/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func (t *Tree) IsBranchOpen(uid TreeNodeID) bool {
//
// Implements: fyne.Focusable
func (t *Tree) FocusGained() {
if t.currentFocus == "" {
if childUIDs := t.ChildUIDs; childUIDs != nil {
if ids := childUIDs(""); len(ids) > 0 {
t.currentFocus = ids[0]
}
}
}

t.focused = true
t.ScrollTo(t.currentFocus)
t.Refresh() // TODO RefreshItem(t.currentFocus)
Expand Down
33 changes: 33 additions & 0 deletions widget/tree_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ func TestTree(t *testing.T) {
})
}

func TestTree_Focus(t *testing.T) {
var treeData = map[string][]string{
"": {"foo", "bar"},
"foo": {"foobar", "barbar"},
}
tree := NewTreeWithStrings(treeData)
window := test.NewWindow(tree)
defer window.Close()
window.Resize(tree.MinSize().Max(fyne.NewSize(150, 200)))

canvas := window.Canvas().(test.WindowlessCanvas)
assert.Nil(t, canvas.Focused())

canvas.FocusNext()
assert.NotNil(t, canvas.Focused())
assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus)

tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown})
assert.Equal(t, "bar", canvas.Focused().(*Tree).currentFocus)

tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp})
assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus)

tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyRight})
assert.Equal(t, "foobar", canvas.Focused().(*Tree).currentFocus)

tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyLeft})
assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus)

canvas.Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
assert.Equal(t, "foo", tree.selected[0])
}

func TestTree_Indentation(t *testing.T) {
data := make(map[string][]string)
tree := NewTreeWithStrings(data)
Expand Down

0 comments on commit dff3583

Please sign in to comment.