github.com/Seikaijyu/gio@v0.0.1/widget/dnd_test.go (about)

     1  package widget
     2  
     3  import (
     4  	"image"
     5  	"testing"
     6  
     7  	"github.com/Seikaijyu/gio/f32"
     8  	"github.com/Seikaijyu/gio/io/pointer"
     9  	"github.com/Seikaijyu/gio/io/router"
    10  	"github.com/Seikaijyu/gio/io/transfer"
    11  	"github.com/Seikaijyu/gio/layout"
    12  	"github.com/Seikaijyu/gio/op"
    13  	"github.com/Seikaijyu/gio/op/clip"
    14  )
    15  
    16  func TestDraggable(t *testing.T) {
    17  	var r router.Router
    18  	gtx := layout.Context{
    19  		Constraints: layout.Exact(image.Pt(100, 100)),
    20  		Queue:       &r,
    21  		Ops:         new(op.Ops),
    22  	}
    23  
    24  	drag := &Draggable{
    25  		Type: "file",
    26  	}
    27  	defer pointer.PassOp{}.Push(gtx.Ops).Pop()
    28  	dims := drag.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
    29  		return layout.Dimensions{Size: gtx.Constraints.Min}
    30  	}, nil)
    31  	stack := clip.Rect{Max: dims.Size}.Push(gtx.Ops)
    32  	transfer.TargetOp{
    33  		Tag:  drag,
    34  		Type: drag.Type,
    35  	}.Add(gtx.Ops)
    36  	stack.Pop()
    37  
    38  	r.Frame(gtx.Ops)
    39  	r.Queue(
    40  		pointer.Event{
    41  			Position: f32.Pt(10, 10),
    42  			Kind:     pointer.Press,
    43  		},
    44  		pointer.Event{
    45  			Position: f32.Pt(20, 10),
    46  			Kind:     pointer.Move,
    47  		},
    48  		pointer.Event{
    49  			Position: f32.Pt(20, 10),
    50  			Kind:     pointer.Release,
    51  		},
    52  	)
    53  	ofr := &offer{data: "hello"}
    54  	drag.Offer(gtx.Ops, "file", ofr)
    55  	r.Frame(gtx.Ops)
    56  
    57  	evs := r.Events(drag)
    58  	if len(evs) != 1 {
    59  		t.Fatalf("expected 1 event, got %d", len(evs))
    60  	}
    61  	ev := evs[0].(transfer.DataEvent)
    62  	ev.Open = nil
    63  	if got, want := ev.Type, "file"; got != want {
    64  		t.Errorf("expected %v; got %v", got, want)
    65  	}
    66  	if ofr.closed {
    67  		t.Error("offer closed prematurely")
    68  	}
    69  	r.Frame(gtx.Ops)
    70  	if !ofr.closed {
    71  		t.Error("offer was not closed")
    72  	}
    73  }
    74  
    75  // offer satisfies io.ReadCloser for use in data transfers.
    76  type offer struct {
    77  	data   string
    78  	closed bool
    79  }
    80  
    81  func (*offer) Read([]byte) (int, error) { return 0, nil }
    82  
    83  func (o *offer) Close() error {
    84  	o.closed = true
    85  	return nil
    86  }