github.com/utopiagio/gio@v0.0.8/widget/material/list_test.go (about)

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