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

     1  package material_test
     2  
     3  import (
     4  	"image"
     5  	"testing"
     6  
     7  	"gioui.org/layout"
     8  	"gioui.org/op"
     9  	"gioui.org/unit"
    10  	"gioui.org/widget"
    11  	"gioui.org/widget/material"
    12  )
    13  
    14  func TestListAnchorStrategies(t *testing.T) {
    15  	gtx := layout.Context{
    16  		Ops: new(op.Ops),
    17  		Metric: unit.Metric{
    18  			PxPerDp: 1,
    19  			PxPerSp: 1,
    20  		},
    21  		Constraints: layout.Exact(image.Point{
    22  			X: 500,
    23  			Y: 500,
    24  		}),
    25  	}
    26  	gtx.Constraints.Min = image.Point{}
    27  
    28  	var spaceConstraints layout.Constraints
    29  	space := func(gtx layout.Context, index int) layout.Dimensions {
    30  		spaceConstraints = gtx.Constraints
    31  		if spaceConstraints.Min.X < 0 || spaceConstraints.Min.Y < 0 ||
    32  			spaceConstraints.Max.X < 0 || spaceConstraints.Max.Y < 0 {
    33  			t.Errorf("invalid constraints at index %d: %#+v", index, spaceConstraints)
    34  		}
    35  		return layout.Dimensions{Size: image.Point{
    36  			X: gtx.Constraints.Max.X,
    37  			Y: gtx.Dp(20),
    38  		}}
    39  	}
    40  
    41  	var list widget.List
    42  	list.Axis = layout.Vertical
    43  	elements := 100
    44  	th := material.NewTheme()
    45  	materialList := material.List(th, &list)
    46  	indicatorWidth := gtx.Dp(materialList.Width())
    47  
    48  	materialList.AnchorStrategy = material.Occupy
    49  	occupyDims := materialList.Layout(gtx, elements, space)
    50  	occupyConstraints := spaceConstraints
    51  
    52  	materialList.AnchorStrategy = material.Overlay
    53  	overlayDims := materialList.Layout(gtx, elements, space)
    54  	overlayConstraints := spaceConstraints
    55  
    56  	// Both anchor strategies should use all space available if their elements do.
    57  	if occupyDims != overlayDims {
    58  		t.Errorf("expected occupy dims (%v) to be equal to overlay dims (%v)", occupyDims, overlayDims)
    59  	}
    60  	// The overlay strategy should not reserve any space for the scroll indicator,
    61  	// so the constraints that it presents to its elements should be larger than
    62  	// those presented by the occupy strategy.
    63  	if overlayConstraints.Max.X != occupyConstraints.Max.X+indicatorWidth {
    64  		t.Errorf("overlay max width (%d) != occupy max width (%d) + indicator width (%d)",
    65  			overlayConstraints.Max.X, occupyConstraints.Max.X, indicatorWidth)
    66  	}
    67  }