-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcard_test.go
144 lines (128 loc) · 3.55 KB
/
card_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package widget_test
import (
"image/color"
"testing"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/stretchr/testify/assert"
)
func TestCard_SetImage(t *testing.T) {
c := widget.NewCard("Title", "sub", widget.NewLabel("Content"))
r := test.TempWidgetRenderer(t, c)
assert.Equal(t, 4, len(r.Objects())) // the 3 above plus shadow
c.SetImage(canvas.NewImageFromResource(theme.ComputerIcon()))
assert.Equal(t, 5, len(r.Objects()))
}
func TestCard_SetContent(t *testing.T) {
c := widget.NewCard("Title", "sub", widget.NewLabel("Content"))
r := test.TempWidgetRenderer(t, c)
assert.Equal(t, 4, len(r.Objects())) // the 3 above plus shadow
newContent := widget.NewLabel("New")
c.SetContent(newContent)
assert.Equal(t, 4, len(r.Objects()))
assert.Equal(t, newContent, r.Objects()[3])
}
func TestCard_Layout(t *testing.T) {
test.NewApp()
for name, tt := range map[string]struct {
title, subtitle string
icon *canvas.Image
content fyne.CanvasObject
}{
"title": {
title: "Title",
subtitle: "",
icon: nil,
content: nil,
},
"subtitle": {
title: "",
subtitle: "Subtitle",
icon: nil,
content: nil,
},
"titles": {
title: "Title",
subtitle: "Subtitle",
icon: nil,
content: nil,
},
"titles_image": {
title: "Title",
subtitle: "Subtitle",
icon: canvas.NewImageFromResource(theme.ComputerIcon()),
content: nil,
},
"just_image": {
title: "",
subtitle: "",
icon: canvas.NewImageFromResource(theme.ComputerIcon()),
content: nil,
},
"just_content": {
title: "",
subtitle: "",
icon: nil,
content: newContentRect(),
},
"title_content": {
title: "Hello",
subtitle: "",
icon: nil,
content: newContentRect(),
},
"image_content": {
title: "",
subtitle: "",
icon: canvas.NewImageFromResource(theme.ComputerIcon()),
content: newContentRect(),
},
"all_items": {
title: "Longer title",
subtitle: "subtitle with length",
icon: canvas.NewImageFromResource(theme.ComputerIcon()),
content: newContentRect(),
},
} {
t.Run(name, func(t *testing.T) {
card := &widget.Card{
Title: tt.title,
Subtitle: tt.subtitle,
Image: tt.icon,
Content: tt.content,
}
window := test.NewTempWindow(t, card)
size := card.MinSize().Max(fyne.NewSize(80, 0)) // give a little width for image only tests
window.Resize(size.Add(fyne.NewSize(theme.InnerPadding(), theme.InnerPadding())))
if tt.content != nil {
assert.Equal(t, float32(10), tt.content.Size().Height)
}
test.AssertRendersToMarkup(t, "card/layout_"+name+".xml", window.Canvas())
})
}
}
func TestCard_MinSize(t *testing.T) {
content := widget.NewLabel("simple")
card := &widget.Card{Content: content}
inner := card.MinSize().Subtract(fyne.NewSize(theme.InnerPadding()+theme.Padding(), theme.InnerPadding()+theme.Padding())) // shadow + content pad
assert.Equal(t, content.MinSize(), inner)
}
func TestCard_Refresh(t *testing.T) {
text := widget.NewLabel("Test")
card := widget.NewCard("", "", text)
w := test.NewTempWindow(t, card)
test.AssertRendersToMarkup(t, "card/content_label.xml", w.Canvas())
text.Text = "Changed"
card.Refresh()
test.AssertRendersToMarkup(t, "card/content_label_changed.xml", w.Canvas())
}
func newContentRect() *canvas.Rectangle {
rect := canvas.NewRectangle(color.Gray{0x66})
rect.StrokeColor = color.Black
rect.StrokeWidth = 2
rect.SetMinSize(fyne.NewSize(10, 10))
return rect
}