Repository: fyne-io/fyne
File: widget/entry_internal_test.go (Go)

Code before:
func TestEntry_CallbackLocking(t *testing.T) {
	e := &Entry{}
	called := 0
	e.OnChanged = func(_ string) {
		called++ // Just to not have an empty critical section.
	}

	_ = e.Theme()
	test.Type(e, "abc123")
	e.selectAll()
	e.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
	assert.Equal(t, 7, called)
}

func TestEntry_IconContentSizeAndPlacement(t *testing.T) {
	entry := NewEntry()
	entry.Wrapping = fyne.TextWrapOff
	entry.Scroll = fyne.ScrollNone
	icon := theme.MailComposeIcon()
	entry.SetIcon(icon)
	entry.SetText("SomeText")
	r := test.TempWidgetRenderer(t, entry)
	r.Layout(entry.MinSize())

	var iconObj *canvas.Image
	for _, obj := range r.Objects() {
		if img, ok := obj.(*canvas.Image); ok && img.Resource == icon {
			iconObj = img
			break
		}
	}

	// Icon should be at the left, with correct size
	assert.NotNil(t, iconObj)
	assert.Equal(t, theme.IconInlineSize(), iconObj.Size().Width)
	assert.Equal(t, theme.IconInlineSize(), iconObj.Size().Height)
	assert.Equal(t, fyne.NewPos(theme.InnerPadding(), theme.InnerPadding()), iconObj.Position())
	// Content should be positioned after the icon, with correct padding
	assert.Equal(t, fyne.NewPos(theme.InnerPadding()+theme.LineSpacing()+theme.IconInlineSize(), theme.InputBorderSize()), entry.content.Position())
}

func TestEntry_MouseClickAndDragOutsideText(t *testing.T) {
	entry := NewEntry()
	entry.SetText("A\nB\n")

	testCharSize := theme.TextSize()
	pos := fyne.NewPos(testCharSize, testCharSize*4) // tap below rows
	ev := &fyne.PointEvent{Position: pos}

	me := &desktop.MouseEvent{PointEvent: *ev, Button: desktop.MouseButtonPrimary}
	entry.MouseDown(me)

Review comment (suggestion):
You can cast r to a `*entryRenderer` and then access `.icon` directly instead of looping like this to find it.

(I'm mostly just trying to find something that can be changed for the PR to get the checks to run again :D but this (casting to the known concrete render type) is generally the pattern used in tests elsewhere)

Code after:
func TestEntry_CallbackLocking(t *testing.T) {
	e := &Entry{}
	called := 0
	e.OnChanged = func(_ string) {
		called++ // Just to not have an empty critical section.
	}

	_ = e.Theme()
	test.Type(e, "abc123")
	e.selectAll()
	e.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
	assert.Equal(t, 7, called)
}

func TestEntry_ContentSizeAndPlacementWithIcon(t *testing.T) {
	entry := NewEntry()
	entry.SetIcon(theme.MailComposeIcon())
	entry.SetText("SomeText")
	renderer := entry.CreateRenderer()
	contentPos := fyne.NewPos(theme.LineSpacing()+theme.IconInlineSize(), theme.InputBorderSize())

	renderer.Layout(entry.MinSize())
	// Scrollable content should be positioned after the icon, with correct padding
	assert.Equal(t, contentPos, entry.scroll.Position())

	entry.Wrapping = fyne.TextWrapOff
	entry.Scroll = fyne.ScrollNone
	renderer.Layout(entry.MinSize())
	// Content should be positioned after the icon, with correct padding
	assert.Equal(t, contentPos, entry.content.Position())
}

func TestEntry_MouseClickAndDragOutsideText(t *testing.T) {
	entry := NewEntry()
	entry.SetText("A\nB\n")

	testCharSize := theme.TextSize()
	pos := fyne.NewPos(testCharSize, testCharSize*4) // tap below rows
	ev := &fyne.PointEvent{Position: pos}

	me := &desktop.MouseEvent{PointEvent: *ev, Button: desktop.MouseButtonPrimary}
	entry.MouseDown(me)
	de := &fyne.DragEvent{PointEvent: *ev, Dragged: fyne.NewDelta(1, 0)}
	entry.Dragged(de)
	entry.MouseUp(me)
	assert.False(t, entry.sel.selecting)
}

func TestEntry_MouseDownOnSelect(t *testing.T) {
	entry := NewEntry()
	entry.SetText("Ahnj\nBuki\n")