gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/widget/widget_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget_test
     4  
     5  import (
     6  	"image"
     7  	"testing"
     8  
     9  	"gioui.org/f32"
    10  	"gioui.org/io/input"
    11  	"gioui.org/io/pointer"
    12  	"gioui.org/io/semantic"
    13  	"gioui.org/layout"
    14  	"gioui.org/op"
    15  	"gioui.org/widget"
    16  )
    17  
    18  func TestBool(t *testing.T) {
    19  	var (
    20  		r input.Router
    21  		b widget.Bool
    22  	)
    23  	gtx := layout.Context{
    24  		Ops:    new(op.Ops),
    25  		Source: r.Source(),
    26  	}
    27  	layout := func() {
    28  		b.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
    29  			semantic.CheckBox.Add(gtx.Ops)
    30  			semantic.DescriptionOp("description").Add(gtx.Ops)
    31  			return layout.Dimensions{Size: image.Pt(100, 100)}
    32  		})
    33  	}
    34  	layout()
    35  	r.Frame(gtx.Ops)
    36  	r.Queue(
    37  		pointer.Event{
    38  			Source:   pointer.Touch,
    39  			Kind:     pointer.Press,
    40  			Position: f32.Pt(50, 50),
    41  		},
    42  		pointer.Event{
    43  			Source:   pointer.Touch,
    44  			Kind:     pointer.Release,
    45  			Position: f32.Pt(50, 50),
    46  		},
    47  	)
    48  	gtx.Reset()
    49  	layout()
    50  	r.Frame(gtx.Ops)
    51  	tree := r.AppendSemantics(nil)
    52  	n := tree[0].Children[0].Desc
    53  	if n.Description != "description" {
    54  		t.Errorf("unexpected semantic description: %s", n.Description)
    55  	}
    56  	if n.Class != semantic.CheckBox {
    57  		t.Errorf("unexpected semantic class: %v", n.Class)
    58  	}
    59  	if !b.Value || !n.Selected {
    60  		t.Error("click did not select")
    61  	}
    62  }