github.com/Seikaijyu/gio@v0.0.1/widget/material/list_test.go (about)

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