package ui import ( "fmt" "slices" "testing" "github.com/egoist/mygo/internal/platform" ) // drag presses at from, moves in steps to to, and releases there. func drag(tt *Tester, fromX, fromY, toX, toY float32) { tt.Press(fromX, fromY) for k := 1; k <= 4; k++ { f := float32(k) / 4 tt.Move(fromX+(toX-fromX)*f, fromY+(toY-fromY)*f) } tt.Release(toX, toY) } func TestDragAndDrop(t *testing.T) { var dropped []string var over bool clicks, numbers := 0, 0 tt := coreNewTester(func(c *context) { coreColumn(c).Gap(20).Padding(20).Children(func() { src := coreBox(c).Size(100, 40).Label("Source").Drag("hello") if src.Clicked() { clicks++ } target := coreBox(c).Size(200, 100).Label("Target") if v, ok := coreDrop[string](target); ok { dropped = append(dropped, v) } _, over = coreDragOver[string](target) // Inside the target, an element taking numbers, not text. inner := coreBox(c).Size(50, 30).Label("Numbers") if _, ok := coreDrop[int](inner); ok { numbers++ } }) }, 400, 400) src, _ := tt.Find("Source") target, _ := tt.Find("Target") inner, _ := tt.Find("Numbers") // A press that hardly moves is a click. drag(tt, src.X+10, src.Y+10, src.X+12, src.Y+11) if clicks != 1 || len(dropped) != 0 { t.Fatalf("a press moving 2 DIPs: %d clicks, dropped %q", clicks, dropped) } // Dragged over the target, then dropped on it: no click. tt.Press(src.X+10, src.Y+10) tt.Move(src.X+30, src.Y+30) tt.Move(target.X+20, target.Y+20) if !over { t.Fatal("the target does not see the value over it") } tt.Release(target.X+20, target.Y+20) if !slices.Equal(dropped, []string{"hello"}) || clicks != 1 || over { t.Fatalf("dropped %q, %d clicks, over %v", dropped, clicks, over) } // The element taking numbers does not take text. drag(tt, src.X+10, src.Y+10, inner.X+10, inner.Y+10) if numbers != 0 { t.Errorf("text dropped on an element taking numbers") } // Escape gives up the drag. tt.Press(src.X+10, src.Y+10) tt.Move(target.X+20, target.Y+20) tt.Image() // the copy following the pointer paints tt.Key(0, KeyEscape) tt.Release(target.X+20, target.Y+20) if len(dropped) != 1 || clicks != 1 { t.Errorf("after Escape: dropped %q, %d clicks", dropped, clicks) } } func TestListReorder(t *testing.T) { items := []string{"a", "b", "c", "d", "e", "f"} var moves [][]int sel := -1 var chosen Selection[string] s := ListState{ Selected: &sel, Selection: &chosen, Key: func(i int) any { return items[i] }, Reorder: func(rows []int, to int) { moves = append(moves, append(slices.Clone(rows), to)) moved := make([]string, 0, len(rows)) for _, r := range rows { moved = append(moved, items[r]) } var rest []string at := 0 for i, it := range items { if i == to { at = len(rest) } if !slices.Contains(rows, i) { rest = append(rest, it) } } if to == len(items) { at = len(rest) } items = slices.Insert(rest, at, moved...) }, } tt := coreNewTester(func(c *context) { coreList(c, &s, len(items), func(i int) { coreText(c, items[i]).Padding(8) }).Grow(1) }, 300, 400) b, _ := tt.Find("b") d, _ := tt.Find("d") // b below d's middle: before e. drag(tt, b.X, b.Y+b.H/2, d.X, d.Y+d.H) if len(moves) != 1 || !slices.Equal(moves[0], []int{1, 4}) || !slices.Equal(items, []string{"a", "c", "d", "b", "e", "f"}) { t.Fatalf("moves %v, items %q", moves, items) } // The rows chosen move together, the badge counting them. tt.Click("c") tt.ClickWith(Cmd, "e") e, _ := tt.Find("e") a, _ := tt.Find("a") tt.Press(e.X, e.Y+e.H/2) tt.Move(e.X, e.Y) tt.Move(a.X, a.Y+2) if d := tt.rt.drag; d == nil || d.count != 2 { t.Errorf("the drag of two rows is %+v", d) } tt.Release(a.X, a.Y+2) if len(moves) != 2 || !slices.Equal(moves[1], []int{1, 4, 0}) || !slices.Equal(items, []string{"c", "e", "a", "d", "b", "f"}) { t.Errorf("moves %v, items %q", moves, items) } // Dropped where they are, nothing moves. f, _ := tt.Find("f") drag(tt, f.X, f.Y+f.H/2, f.X, f.Y+f.H/2+6) if len(moves) != 2 { t.Errorf("a row dropped on itself moved: %v", moves) } // They go where they dropped, though the pointer moved on before the // next frame. f, _ = tt.Find("f") c, _ := tt.Find("c") tt.Press(f.X, f.Y+f.H/2) tt.Move(f.X, f.Y) tt.Move(c.X, c.Y+2) tt.rt.event(platform.SurfaceEvent{Kind: platform.PointerUp, X: float64(c.X), Y: float64(c.Y + 2)}) tt.rt.event(platform.SurfaceEvent{Kind: platform.PointerMove, X: float64(f.X), Y: float64(f.Y + f.H)}) tt.settle() if len(moves) != 3 || !slices.Equal(moves[2], []int{5, 0}) { t.Errorf("moves %v, items %q", moves, items) } } // A list's rows dragged out with DragRows drag the value it makes of them, // for targets inside the list or around it to take: the list takes none // of it, so it neither reorders nor shows a place between its rows. func TestListDragRows(t *testing.T) { items := []string{"a", "b", "c", "d", "e", "f"} sel := -1 var chosen Selection[string] var asked [][]int s := ListState{ Selected: &sel, Selection: &chosen, Key: func(i int) any { return items[i] }, DragRows: func(rows []int) any { asked = append(asked, slices.Clone(rows)) var names []string for _, r := range rows { if items[r] == "f" { return nil // f stays } names = append(names, items[r]) } return names }, } var intoD, around, trash [][]string overAround := false tt := coreNewTester(func(c *context) { coreColumn(c).Fill().Children(func() { wrap := coreColumn(c).Height(300).Children(func() { coreList(c, &s, len(items), func(i int) { row := coreRow(c).Padding(8).Children(func() { coreText(c, items[i]) }) if items[i] == "d" { if v, ok := coreDrop[[]string](row); ok { intoD = append(intoD, v) } } }).Grow(1) }) if v, ok := coreDrop[[]string](wrap); ok { around = append(around, v) } _, overAround = coreDragOver[[]string](wrap) bin := coreBox(c).Size(200, 60).Label("Trash") if v, ok := coreDrop[[]string](bin); ok { trash = append(trash, v) } }) }, 300, 400) find := func(label string) Rect { r, ok := tt.Find(label) if !ok { t.Fatalf("no %q", label) } return Rect{float32(r.X), float32(r.Y), float32(r.W), float32(r.H)} } b, d, bin := find("b"), find("d"), find("Trash") drag(tt, b.X, b.Y+b.H/2, bin.X+20, bin.Y+20) if len(trash) != 1 || !slices.Equal(trash[0], []string{"b"}) { t.Fatalf("dropped on the trash: %q", trash) } // On a row taking them, inside the list. drag(tt, b.X, b.Y+b.H/2, d.X, d.Y+d.H/2) if len(intoD) != 1 || !slices.Equal(intoD[0], []string{"b"}) { t.Fatalf("dropped on d: %q", intoD) } // Past the rows, the list takes nothing: the element around it does, // and the rows stay as they are. f := find("f") tt.Press(b.X, b.Y+b.H/2) tt.Move(b.X, b.Y+b.H/2+10) tt.Move(f.X, f.Y+f.H+40) if !overAround { t.Error("the list took rows dragged past its own") } tt.Release(f.X, f.Y+f.H+40) if len(around) != 1 || !slices.Equal(items, []string{"a", "b", "c", "d", "e", "f"}) { t.Fatalf("around %q, items %q", around, items) } // The rows chosen drag together, the badge counting them. tt.Click("c") tt.ClickWith(Cmd, "e") e := find("e") tt.Press(e.X, e.Y+e.H/2) tt.Move(e.X, e.Y+e.H/2+10) tt.Move(bin.X+20, bin.Y+20) if d := tt.rt.drag; d == nil || d.count != 2 { t.Errorf("the drag of two rows is %+v", d) } tt.Release(bin.X+20, bin.Y+20) if len(trash) != 2 || !slices.Equal(trash[1], []string{"c", "e"}) { t.Fatalf("dropped on the trash: %q, rows asked for %v", trash, asked) } // Rows DragRows makes nothing of do not drag. drag(tt, f.X, f.Y+f.H/2, bin.X+20, bin.Y+20) if len(trash) != 2 { t.Errorf("f dragged: %q", trash) } } func TestListDragRowsAndReorder(t *testing.T) { s := ListState{Reorder: func([]int, int) {}, DragRows: func([]int) any { return nil }} defer func() { if recover() == nil { t.Error("a list both reordered and dragged its rows out") } }() tt := coreNewTester(func(c *context) { coreList(c, &s, 1, func(int) { coreText(c, "a") }) }, 100, 100) tt.Frame() } // A press on a control taking clicks alone, inside an element that drags // a value, drags that element once it moves, and is no click; a press that // does not move clicks the control. Controls taking the pointer's moves, // as a text input, keep them. func TestDragAroundControls(t *testing.T) { items := []string{"a", "b", "c", "d"} var moves [][]int clicked := "" s := ListState{Reorder: func(rows []int, to int) { moves = append(moves, append(slices.Clone(rows), to)) }} var dropped []string text := "" tt := coreNewTester(func(c *context) { coreColumn(c).Fill().Children(func() { coreList(c, &s, len(items), func(i int) { b := coreButtonBase(c).Padding(8).Children(func() { coreText(c, items[i]) }) if b.Clicked() { clicked += items[i] } }).Height(200) coreRow(c).Gap(8).Padding(8).Drag("card").Children(func() { coreButton(c, "Open") coreTextInput(c, &text).Label("Note").Width(120) }) target := coreBox(c).Size(200, 60).Label("Target") if v, ok := coreDrop[string](target); ok { dropped = append(dropped, v) } }) }, 300, 400) b, _ := tt.Find("b") d, _ := tt.Find("d") // b's button, below d's middle: before the end. drag(tt, b.X, b.Y+b.H/2, d.X, d.Y+d.H) if len(moves) != 1 || !slices.Equal(moves[0], []int{1, 4}) || clicked != "" { t.Fatalf("moves %v, clicked %q", moves, clicked) } // A press that does not move clicks the button. if err := tt.Click("a"); err != nil { t.Fatal(err) } if clicked != "a" || len(moves) != 1 { t.Errorf("clicked %q, moves %v", clicked, moves) } // A button in an element with Drag drags it. open, _ := tt.Find("Open") target, _ := tt.Find("Target") drag(tt, open.X+4, open.Y+4, target.X+20, target.Y+20) if !slices.Equal(dropped, []string{"card"}) { t.Errorf("dropped %q", dropped) } // A text input keeps the pointer's moves, to choose text. note, _ := tt.Find("Note") drag(tt, note.X+4, note.Y+4, target.X+20, target.Y+20) if len(dropped) != 1 { t.Errorf("a text input dragged its row: %q", dropped) } } func TestGridReorder(t *testing.T) { var moves [][]int sel := -1 s := GridState{Selected: &sel, Reorder: func(items []int, to int) { moves = append(moves, append(slices.Clone(items), to)) }} tt := coreNewTester(func(c *context) { coreGridView(c, &s, 12, 100, 60, func(i int) { coreTextf(c, "Item %d", i) }).Grow(1) }, 448, 400) from, _ := tt.Find("Item 0") to, _ := tt.Find("Item 2") // Item 0 on the right half of item 2: before item 3. drag(tt, from.X, from.Y, to.X+to.W+20, to.Y) if len(moves) != 1 || !slices.Equal(moves[0], []int{0, 3}) { t.Fatalf("moves %v", moves) } // On the left half of an item: before it. five, _ := tt.Find("Item 5") drag(tt, to.X, to.Y, five.X-20, five.Y) if len(moves) != 2 || !slices.Equal(moves[1], []int{2, 5}) { t.Errorf("moves %v", fmt.Sprint(moves)) } // Where they dropped, though the pointer moved on before the next // frame. tt.Press(from.X, from.Y) tt.Move(from.X+20, from.Y) tt.Move(to.X+to.W+20, to.Y) tt.rt.event(platform.SurfaceEvent{Kind: platform.PointerUp, X: float64(to.X + to.W + 20), Y: float64(to.Y)}) tt.rt.event(platform.SurfaceEvent{Kind: platform.PointerMove, X: float64(five.X), Y: float64(five.Y + 80)}) tt.settle() if len(moves) != 3 || !slices.Equal(moves[2], []int{0, 3}) { t.Errorf("moves %v", fmt.Sprint(moves)) } } // A value let go over no element taking it, as past the window's edge, // tells its source where, once; one given up with Escape does not. func TestDragMissed(t *testing.T) { var missed []Point tt := coreNewTester(func(c *context) { coreColumn(c).Gap(20).Padding(20).Children(func() { src := coreBox(c).Size(100, 40).Label("Source").Drag("hello") if at, ok := src.DragMissed(); ok { missed = append(missed, at) } if _, ok := coreDrop[string](coreBox(c).Size(200, 100).Label("Target")); ok { t.Error("the target took a value let go elsewhere") } }) }, 400, 400) src, _ := tt.Find("Source") drag(tt, src.X+10, src.Y+10, 450, -30) tt.Frame() if !slices.Equal(missed, []Point{{450, -30}}) { t.Fatalf("let go past the edge: missed %v", missed) } missed = nil tt.Press(src.X+10, src.Y+10) tt.Move(src.X+40, src.Y+40) tt.Move(450, 200) tt.Key(0, KeyEscape) tt.Release(450, 200) tt.Frame() if len(missed) != 0 { t.Fatalf("given up with Escape: missed %v", missed) } } // A source the frame builds no more, as one in a view that went away // while it was dragged, goes on: its copy follows the pointer, and it // drops where it is let go of. func TestDragSourceGone(t *testing.T) { var dropped []string shown := true tt := coreNewTester(func(c *context) { coreColumn(c).Gap(20).Padding(20).Children(func() { if shown { coreBox(c).Size(100, 40).Label("Source").Background(RGB(255, 0, 0)).Drag("hello") } else { coreBox(c).Size(100, 40).Label("Other") } target := coreBox(c).Size(200, 100).Label("Target") if v, ok := coreDrop[string](target); ok { dropped = append(dropped, v) } }) }, 400, 400) src, _ := tt.Find("Source") target, _ := tt.Find("Target") tt.Press(src.X+10, src.Y+10) tt.Move(src.X+30, src.Y+30) shown = false tt.Frame() if _, ok := tt.Find("Source"); ok { t.Fatal("the source is still built") } tt.Move(target.X+20, target.Y+20) if c := tt.Image().RGBAAt(int(target.X+40), int(target.Y+20)); c.R < 150 || c.G > 100 { t.Errorf("no copy of the source under the pointer: %v", c) } tt.Release(target.X+20, target.Y+20) if !slices.Equal(dropped, []string{"hello"}) { t.Fatalf("dropped %q", dropped) } // It is forgotten once let go of. tt.Frame() if c := tt.Image().RGBAAt(int(target.X+40), int(target.Y+20)); c.R > 150 && c.G < 100 { t.Errorf("the copy stays after the drop: %v", c) } } // An element taking the value built under a pointer that stands still, as // in a view that just showed, is the one the value is over, and takes it. func TestDragTargetBuiltUnderPointer(t *testing.T) { var dropped []string var over, shown bool tt := coreNewTester(func(c *context) { coreColumn(c).Gap(20).Padding(20).Children(func() { coreBox(c).Size(100, 40).Label("Source").Drag("hello") if shown { target := coreBox(c).Size(200, 100).Label("Target") if v, ok := coreDrop[string](target); ok { dropped = append(dropped, v) } _, over = coreDragOver[string](target) } }) }, 400, 400) src, _ := tt.Find("Source") x, y := src.X+20, src.Y+src.H+60 tt.Press(src.X+10, src.Y+10) tt.Move(src.X+30, src.Y+30) tt.Move(x, y) shown = true tt.Frame() tt.Frame() if !over { t.Fatal("the target built under the pointer does not see the value") } tt.Release(x, y) if !slices.Equal(dropped, []string{"hello"}) { t.Fatalf("dropped %q", dropped) } }