-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnd_test.go
86 lines (77 loc) · 1.65 KB
/
dnd_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
package widget
import (
"image"
"testing"
"gioui.org/f32"
"gioui.org/io/pointer"
"gioui.org/io/router"
"gioui.org/io/transfer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
)
func TestDraggable(t *testing.T) {
var r router.Router
gtx := layout.Context{
Constraints: layout.Exact(image.Pt(100, 100)),
Queue: &r,
Ops: new(op.Ops),
}
drag := &Draggable{
Type: "file",
}
defer pointer.PassOp{}.Push(gtx.Ops).Pop()
dims := drag.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: gtx.Constraints.Min}
}, nil)
stack := clip.Rect{Max: dims.Size}.Push(gtx.Ops)
transfer.TargetOp{
Tag: drag,
Type: drag.Type,
}.Add(gtx.Ops)
stack.Pop()
r.Frame(gtx.Ops)
r.Queue(
pointer.Event{
Position: f32.Pt(10, 10),
Type: pointer.Press,
},
pointer.Event{
Position: f32.Pt(20, 10),
Type: pointer.Move,
},
pointer.Event{
Position: f32.Pt(20, 10),
Type: pointer.Release,
},
)
ofr := &offer{data: "hello"}
drag.Offer(gtx.Ops, "file", ofr)
r.Frame(gtx.Ops)
evs := r.Events(drag)
if len(evs) != 1 {
t.Fatalf("expected 1 event, got %d", len(evs))
}
ev := evs[0].(transfer.DataEvent)
ev.Open = nil
if got, want := ev.Type, "file"; got != want {
t.Errorf("expected %v; got %v", got, want)
}
if ofr.closed {
t.Error("offer closed prematurely")
}
r.Frame(gtx.Ops)
if !ofr.closed {
t.Error("offer was not closed")
}
}
// offer satisfies io.ReadCloser for use in data transfers.
type offer struct {
data string
closed bool
}
func (*offer) Read([]byte) (int, error) { return 0, nil }
func (o *offer) Close() error {
o.closed = true
return nil
}