-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
file_test.go
311 lines (258 loc) · 9.41 KB
/
file_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package dialog
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"fyne.io/fyne"
"fyne.io/fyne/layout"
"fyne.io/fyne/storage"
"fyne.io/fyne/test"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
// comparePaths compares if two file paths point to the same thing, and calls
// t.Fatalf() if there is an error in performing the comparison.
//
// Returns true of both paths point to the same thing.
//
// You should use this if you need to compare file paths, since it explicitly
// normalizes the paths to a stable canonical form. It also nicely
// abstracts out the requisite error handling.
//
// You should only call this function on paths that you expect to be valid.
func comparePaths(t *testing.T, p1, p2 string) bool {
a1, err := filepath.Abs(p1)
if err != nil {
t.Fatalf("Failed to normalize path '%s'", p1)
}
a2, err := filepath.Abs(p2)
if err != nil {
t.Fatalf("Failed to normalize path '%s'", p2)
}
return a1 == a2
}
func TestEffectiveStartingDir(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Skipf("os.UserHomeDir) failed, cannot run this test on this system, error was '%s'", err)
}
dialog := &FileDialog{}
// test that we get $HOME when running with the default struct values
res := dialog.effectiveStartingDir()
expect := home
if !comparePaths(t, res, expect) {
t.Errorf("Expected effectiveStartingDir() to be '%s', but it was '%s'",
expect, res)
}
}
func TestFileDialogResize(t *testing.T) {
win := test.NewWindow(widget.NewLabel("Content"))
win.Resize(fyne.NewSize(600, 400))
file := NewFileOpen(func(file fyne.URIReadCloser, err error) {}, win)
file.SetFilter(storage.NewExtensionFileFilter([]string{".png"}))
//Mimic the fileopen dialog
d := &fileDialog{file: file}
open := widget.NewButton("open", func() {})
ui := fyne.NewContainerWithLayout(layout.NewBorderLayout(nil, nil, nil, open), open)
originalSize := ui.MinSize().Add(fyne.NewSize(fileIconCellWidth*2+theme.Padding()*4,
(fileIconSize+fileTextSize)+theme.Padding()*4))
d.win = widget.NewModalPopUp(ui, file.parent.Canvas())
d.win.Resize(originalSize)
file.dialog = d
//Test resize - normal size scenario
size := fyne.NewSize(200, 180) //normal size to fit (600,400)
file.Resize(size)
expectedWidth := 200
assert.Equal(t, expectedWidth, file.dialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight := 180
assert.Equal(t, expectedHeight, file.dialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - normal size scenario again
size = fyne.NewSize(300, 280) //normal size to fit (600,400)
file.Resize(size)
expectedWidth = 300
assert.Equal(t, expectedWidth, file.dialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight = 280
assert.Equal(t, expectedHeight, file.dialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - greater than max size scenario
size = fyne.NewSize(800, 600)
file.Resize(size)
expectedWidth = 600 //since win width only 600
assert.Equal(t, expectedWidth, file.dialog.win.Size().Width) //max, also work
assert.Equal(t, expectedWidth, file.dialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight = 400 //since win heigh only 400
assert.Equal(t, expectedHeight, file.dialog.win.Size().Height) //max, also work
assert.Equal(t, expectedHeight, file.dialog.win.Content.Size().Height+theme.Padding()*2)
//Test again - extreme small size
size = fyne.NewSize(1, 1)
file.Resize(size)
expectedWidth = file.dialog.win.Content.MinSize().Width
assert.Equal(t, expectedWidth, file.dialog.win.Content.Size().Width)
expectedHeight = file.dialog.win.Content.MinSize().Height
assert.Equal(t, expectedHeight, file.dialog.win.Content.Size().Height)
}
func TestShowFileOpen(t *testing.T) {
var chosen fyne.URIReadCloser
var openErr error
win := test.NewWindow(widget.NewLabel("Content"))
ShowFileOpen(func(file fyne.URIReadCloser, err error) {
chosen = file
openErr = err
}, win)
popup := win.Canvas().Overlays().Top().(*widget.PopUp)
defer win.Canvas().Overlays().Remove(popup)
assert.NotNil(t, popup)
ui := popup.Content.(*fyne.Container)
title := ui.Objects[1].(*widget.Label)
assert.Equal(t, "Open File", title.Text)
nameLabel := ui.Objects[2].(*fyne.Container).Objects[1].(*widget.ScrollContainer).Content.(*widget.Label)
buttons := ui.Objects[2].(*fyne.Container).Objects[0].(*widget.Box)
open := buttons.Children[1].(*widget.Button)
files := ui.Objects[3].(*fyne.Container).Objects[1].(*widget.ScrollContainer).Content.(*fyne.Container)
assert.Greater(t, len(files.Objects), 0)
fileName := files.Objects[0].(*fileDialogItem).name
assert.Equal(t, "(Parent)", fileName)
assert.True(t, open.Disabled())
var target *fileDialogItem
for _, icon := range files.Objects {
if icon.(*fileDialogItem).dir == false {
target = icon.(*fileDialogItem)
}
}
if target == nil {
log.Println("Could not find a file in the default directory to tap :(")
return
}
// This will only execute if we have a file in the home path.
// Until we have a way to set the directory of an open file dialog.
test.Tap(target)
assert.Equal(t, filepath.Base(target.path), nameLabel.Text)
assert.False(t, open.Disabled())
test.Tap(open)
assert.Nil(t, win.Canvas().Overlays().Top())
assert.Nil(t, openErr)
assert.Equal(t, "file://"+target.path, chosen.URI().String())
err := chosen.Close()
assert.Nil(t, err)
}
func TestShowFileSave(t *testing.T) {
var chosen fyne.URIWriteCloser
var saveErr error
win := test.NewWindow(widget.NewLabel("Content"))
ShowFileSave(func(file fyne.URIWriteCloser, err error) {
chosen = file
saveErr = err
}, win)
popup := win.Canvas().Overlays().Top().(*widget.PopUp)
defer win.Canvas().Overlays().Remove(popup)
assert.NotNil(t, popup)
ui := popup.Content.(*fyne.Container)
title := ui.Objects[1].(*widget.Label)
assert.Equal(t, "Save File", title.Text)
nameEntry := ui.Objects[2].(*fyne.Container).Objects[1].(*widget.ScrollContainer).Content.(*widget.Entry)
buttons := ui.Objects[2].(*fyne.Container).Objects[0].(*widget.Box)
save := buttons.Children[1].(*widget.Button)
files := ui.Objects[3].(*fyne.Container).Objects[1].(*widget.ScrollContainer).Content.(*fyne.Container)
assert.Greater(t, len(files.Objects), 0)
fileName := files.Objects[0].(*fileDialogItem).name
assert.Equal(t, "(Parent)", fileName)
assert.True(t, save.Disabled())
var target *fileDialogItem
for _, icon := range files.Objects {
if icon.(*fileDialogItem).dir == false {
target = icon.(*fileDialogItem)
}
}
if target == nil {
log.Println("Could not find a file in the default directory to tap :(")
return
}
// This will only execute if we have a file in the home path.
// Until we have a way to set the directory of an open file dialog.
test.Tap(target)
assert.Equal(t, filepath.Base(target.path), nameEntry.Text)
assert.False(t, save.Disabled())
// we are about to overwrite, a warning will show
test.Tap(save)
confirmUI := win.Canvas().Overlays().Top().(*widget.PopUp)
assert.NotEqual(t, confirmUI, popup)
confirmUI.Hide()
// give the file a unique name and it will callback fine
test.Type(nameEntry, "v2_")
test.Tap(save)
assert.Nil(t, win.Canvas().Overlays().Top())
assert.Nil(t, saveErr)
expectedPath := filepath.Join(filepath.Dir(target.path), "v2_"+filepath.Base(target.path))
assert.Equal(t, "file://"+expectedPath, chosen.URI().String())
err := chosen.Close()
assert.Nil(t, err)
err = os.Remove(expectedPath)
assert.Nil(t, err)
}
func TestFileFilters(t *testing.T) {
win := test.NewWindow(widget.NewLabel("Content"))
f := NewFileOpen(func(file fyne.URIReadCloser, err error) {
}, win)
f.SetFilter(storage.NewExtensionFileFilter([]string{".png"}))
f.Show()
workingDir, err := os.Getwd()
if err != nil {
fyne.LogError("Could not get current working directory", err)
t.FailNow()
}
testDataDir := filepath.Join(workingDir, "testdata")
f.dialog.setDirectory(testDataDir)
count := 0
for _, icon := range f.dialog.files.Objects {
if icon.(*fileDialogItem).dir == false {
uri := storage.NewURI("file://" + icon.(*fileDialogItem).path)
assert.Equal(t, uri.Extension(), ".png")
count++
}
}
assert.Equal(t, 3, count)
f.SetFilter(storage.NewMimeTypeFileFilter([]string{"image/jpeg"}))
count = 0
for _, icon := range f.dialog.files.Objects {
if icon.(*fileDialogItem).dir == false {
uri := storage.NewURI("file://" + icon.(*fileDialogItem).path)
assert.Equal(t, uri.MimeType(), "image/jpeg")
count++
}
}
assert.Equal(t, 1, count)
f.SetFilter(storage.NewMimeTypeFileFilter([]string{"image/*"}))
count = 0
for _, icon := range f.dialog.files.Objects {
if icon.(*fileDialogItem).dir == false {
uri := storage.NewURI("file://" + icon.(*fileDialogItem).path)
mimeType := strings.Split(uri.MimeType(), "/")[0]
assert.Equal(t, mimeType, "image")
count++
}
}
assert.Equal(t, 4, count)
}
func TestFileFavorites(t *testing.T) {
win := test.NewWindow(widget.NewLabel("Content"))
dlg := NewFileOpen(func(reader fyne.URIReadCloser, err error) {
assert.Nil(t, err)
assert.Nil(t, reader)
}, win)
dlg.Show()
favorites := dlg.dialog.loadFavorites()
places := dlg.dialog.loadPlaces()
assert.Equal(t, 3+len(places), len(favorites))
curDir := ""
for _, f := range favorites {
btn := f.(*widget.Button)
t.Log(fmt.Sprint("Changing from ", curDir, " to ", btn.Text))
test.Tap(btn)
assert.NotEqual(t, curDir, dlg.dialog.dir)
curDir = dlg.dialog.dir
}
test.Tap(dlg.dialog.dismiss)
}