github.com/utopiagio/gio@v0.0.8/layout/layout_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package layout
     4  
     5  import (
     6  	"image"
     7  	"testing"
     8  
     9  	"github.com/utopiagio/gio/op"
    10  )
    11  
    12  func TestStack(t *testing.T) {
    13  	gtx := Context{
    14  		Ops: new(op.Ops),
    15  		Constraints: Constraints{
    16  			Max: image.Pt(100, 100),
    17  		},
    18  	}
    19  	exp := image.Point{X: 60, Y: 70}
    20  	dims := Stack{Alignment: Center}.Layout(gtx,
    21  		Expanded(func(gtx Context) Dimensions {
    22  			return Dimensions{Size: exp}
    23  		}),
    24  		Stacked(func(gtx Context) Dimensions {
    25  			return Dimensions{Size: image.Point{X: 50, Y: 50}}
    26  		}),
    27  	)
    28  	if got := dims.Size; got != exp {
    29  		t.Errorf("Stack ignored Expanded size, got %v expected %v", got, exp)
    30  	}
    31  }
    32  
    33  func TestFlex(t *testing.T) {
    34  	gtx := Context{
    35  		Ops: new(op.Ops),
    36  		Constraints: Constraints{
    37  			Min: image.Pt(100, 100),
    38  			Max: image.Pt(100, 100),
    39  		},
    40  	}
    41  	dims := Flex{}.Layout(gtx)
    42  	if got := dims.Size; got != gtx.Constraints.Min {
    43  		t.Errorf("Flex ignored minimum constraints, got %v expected %v", got, gtx.Constraints.Min)
    44  	}
    45  }
    46  
    47  func TestDirection(t *testing.T) {
    48  	max := image.Pt(100, 100)
    49  	for _, tc := range []struct {
    50  		dir Direction
    51  		exp image.Point
    52  	}{
    53  		{N, image.Pt(max.X, 0)},
    54  		{S, image.Pt(max.X, 0)},
    55  		{E, image.Pt(0, max.Y)},
    56  		{W, image.Pt(0, max.Y)},
    57  		{NW, image.Pt(0, 0)},
    58  		{NE, image.Pt(0, 0)},
    59  		{SE, image.Pt(0, 0)},
    60  		{SW, image.Pt(0, 0)},
    61  		{Center, image.Pt(0, 0)},
    62  	} {
    63  		t.Run(tc.dir.String(), func(t *testing.T) {
    64  			gtx := Context{
    65  				Ops:         new(op.Ops),
    66  				Constraints: Exact(max),
    67  			}
    68  			var min image.Point
    69  			tc.dir.Layout(gtx, func(gtx Context) Dimensions {
    70  				min = gtx.Constraints.Min
    71  				return Dimensions{}
    72  			})
    73  			if got, exp := min, tc.exp; got != exp {
    74  				t.Errorf("got %v; expected %v", got, exp)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestConstraints(t *testing.T) {
    81  	type testcase struct {
    82  		name     string
    83  		in       Constraints
    84  		subMax   image.Point
    85  		addMin   image.Point
    86  		expected Constraints
    87  	}
    88  	for _, tc := range []testcase{
    89  		{
    90  			name:     "no-op",
    91  			in:       Constraints{Max: image.Pt(100, 100)},
    92  			expected: Constraints{Max: image.Pt(100, 100)},
    93  		},
    94  		{
    95  			name:     "shrink max",
    96  			in:       Constraints{Max: image.Pt(100, 100)},
    97  			subMax:   image.Pt(25, 25),
    98  			expected: Constraints{Max: image.Pt(75, 75)},
    99  		},
   100  		{
   101  			name:     "shrink max below min",
   102  			in:       Constraints{Max: image.Pt(100, 100), Min: image.Pt(50, 50)},
   103  			subMax:   image.Pt(75, 75),
   104  			expected: Constraints{Max: image.Pt(25, 25), Min: image.Pt(25, 25)},
   105  		},
   106  		{
   107  			name:     "shrink max below zero",
   108  			in:       Constraints{Max: image.Pt(100, 100), Min: image.Pt(50, 50)},
   109  			subMax:   image.Pt(125, 125),
   110  			expected: Constraints{Max: image.Pt(0, 0), Min: image.Pt(0, 0)},
   111  		},
   112  		{
   113  			name:     "enlarge min",
   114  			in:       Constraints{Max: image.Pt(100, 100)},
   115  			addMin:   image.Pt(25, 25),
   116  			expected: Constraints{Max: image.Pt(100, 100), Min: image.Pt(25, 25)},
   117  		},
   118  		{
   119  			name:     "enlarge min beyond max",
   120  			in:       Constraints{Max: image.Pt(100, 100)},
   121  			addMin:   image.Pt(125, 125),
   122  			expected: Constraints{Max: image.Pt(100, 100), Min: image.Pt(100, 100)},
   123  		},
   124  		{
   125  			name:     "decrease min below zero",
   126  			in:       Constraints{Max: image.Pt(100, 100), Min: image.Pt(50, 50)},
   127  			addMin:   image.Pt(-125, -125),
   128  			expected: Constraints{Max: image.Pt(100, 100), Min: image.Pt(0, 0)},
   129  		},
   130  	} {
   131  		t.Run(tc.name, func(t *testing.T) {
   132  			start := tc.in
   133  			if tc.subMax != (image.Point{}) {
   134  				start = start.SubMax(tc.subMax)
   135  			}
   136  			if tc.addMin != (image.Point{}) {
   137  				start = start.AddMin(tc.addMin)
   138  			}
   139  			if start != tc.expected {
   140  				t.Errorf("expected %#+v, got %#+v", tc.expected, start)
   141  			}
   142  		})
   143  	}
   144  }