github.com/utopiagio/gio@v0.0.8/widget/selectable_test.go (about) 1 package widget 2 3 import ( 4 "fmt" 5 "image" 6 "testing" 7 8 "github.com/utopiagio/gio/font" 9 "github.com/utopiagio/gio/font/gofont" 10 "github.com/utopiagio/gio/io/input" 11 "github.com/utopiagio/gio/io/key" 12 "github.com/utopiagio/gio/layout" 13 "github.com/utopiagio/gio/op" 14 "github.com/utopiagio/gio/text" 15 "github.com/utopiagio/gio/unit" 16 ) 17 18 func TestSelectableZeroValue(t *testing.T) { 19 var s Selectable 20 if s.Text() != "" { 21 t.Errorf("expected zero value to have no text, got %q", s.Text()) 22 } 23 if start, end := s.Selection(); start != 0 || end != 0 { 24 t.Errorf("expected start=0, end=0, got start=%d, end=%d", start, end) 25 } 26 if selected := s.SelectedText(); selected != "" { 27 t.Errorf("expected selected text to be \"\", got %q", selected) 28 } 29 s.SetCaret(5, 5) 30 if start, end := s.Selection(); start != 0 || end != 0 { 31 t.Errorf("expected start=0, end=0, got start=%d, end=%d", start, end) 32 } 33 } 34 35 // Verify that an existing selection is dismissed when you press arrow keys. 36 func TestSelectableMove(t *testing.T) { 37 r := new(input.Router) 38 gtx := layout.Context{ 39 Ops: new(op.Ops), 40 Locale: english, 41 Source: r.Source(), 42 } 43 cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) 44 fnt := font.Font{} 45 fontSize := unit.Sp(10) 46 47 str := `0123456789` 48 49 // Layout once to populate e.lines and get focus. 50 s := new(Selectable) 51 52 gtx.Execute(key.FocusCmd{Tag: s}) 53 s.SetText(str) 54 // Set up selection so the Selectable filters for all 4 directional keys. 55 s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{}) 56 r.Frame(gtx.Ops) 57 s.SetCaret(3, 6) 58 s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{}) 59 r.Frame(gtx.Ops) 60 s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{}) 61 r.Frame(gtx.Ops) 62 63 for _, keyName := range []key.Name{key.NameLeftArrow, key.NameRightArrow, key.NameUpArrow, key.NameDownArrow} { 64 // Select 345 65 s.SetCaret(3, 6) 66 if start, end := s.Selection(); start != 3 || end != 6 { 67 t.Errorf("expected start=%d, end=%d, got start=%d, end=%d", 3, 6, start, end) 68 } 69 if expected, got := "345", s.SelectedText(); expected != got { 70 t.Errorf("KeyName %s, expected %q, got %q", keyName, expected, got) 71 } 72 73 // Press the key 74 r.Queue(key.Event{State: key.Press, Name: keyName}) 75 s.SetText(str) 76 s.Layout(gtx, cache, fnt, fontSize, op.CallOp{}, op.CallOp{}) 77 r.Frame(gtx.Ops) 78 79 if expected, got := "", s.SelectedText(); expected != got { 80 t.Errorf("KeyName %s, expected %q, got %q", keyName, expected, got) 81 } 82 } 83 } 84 85 func TestSelectableConfigurations(t *testing.T) { 86 gtx := layout.Context{ 87 Ops: new(op.Ops), 88 Constraints: layout.Exact(image.Pt(300, 300)), 89 Locale: english, 90 } 91 cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) 92 fontSize := unit.Sp(10) 93 font := font.Font{} 94 sentence := "\n\n\n\n\n\n\n\n\n\n\n\nthe quick brown fox jumps over the lazy dog" 95 96 for _, alignment := range []text.Alignment{text.Start, text.Middle, text.End} { 97 for _, zeroMin := range []bool{true, false} { 98 t.Run(fmt.Sprintf("Alignment: %v ZeroMinConstraint: %v", alignment, zeroMin), func(t *testing.T) { 99 defer func() { 100 if err := recover(); err != nil { 101 t.Error(err) 102 } 103 }() 104 if zeroMin { 105 gtx.Constraints.Min = image.Point{} 106 } else { 107 gtx.Constraints.Min = gtx.Constraints.Max 108 } 109 s := new(Selectable) 110 s.Alignment = alignment 111 s.SetText(sentence) 112 interactiveDims := s.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) 113 staticDims := Label{Alignment: alignment}.Layout(gtx, cache, font, fontSize, sentence, op.CallOp{}) 114 115 if interactiveDims != staticDims { 116 t.Errorf("expected consistent dimensions, static returned %#+v, interactive returned %#+v", staticDims, interactiveDims) 117 } 118 }) 119 } 120 } 121 }