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

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget
     4  
     5  import (
     6  	"image"
     7  	"testing"
     8  
     9  	"gioui.org/f32"
    10  	"gioui.org/layout"
    11  )
    12  
    13  func TestFit(t *testing.T) {
    14  	type test struct {
    15  		Dims   image.Point
    16  		Scale  f32.Point
    17  		Result image.Point
    18  	}
    19  
    20  	fittests := [...][]test{
    21  		Unscaled: {
    22  			{
    23  				Dims:   image.Point{0, 0},
    24  				Scale:  f32.Point{X: 1, Y: 1},
    25  				Result: image.Point{X: 0, Y: 0},
    26  			}, {
    27  				Dims:   image.Point{50, 25},
    28  				Scale:  f32.Point{X: 1, Y: 1},
    29  				Result: image.Point{X: 50, Y: 25},
    30  			}, {
    31  				Dims:   image.Point{50, 200},
    32  				Scale:  f32.Point{X: 1, Y: 1},
    33  				Result: image.Point{X: 50, Y: 100},
    34  			}},
    35  		Contain: {
    36  			{
    37  				Dims:   image.Point{50, 25},
    38  				Scale:  f32.Point{X: 2, Y: 2},
    39  				Result: image.Point{X: 100, Y: 50},
    40  			}, {
    41  				Dims:   image.Point{50, 200},
    42  				Scale:  f32.Point{X: 0.5, Y: 0.5},
    43  				Result: image.Point{X: 25, Y: 100},
    44  			}},
    45  		Cover: {
    46  			{
    47  				Dims:   image.Point{50, 25},
    48  				Scale:  f32.Point{X: 4, Y: 4},
    49  				Result: image.Point{X: 100, Y: 100},
    50  			}, {
    51  				Dims:   image.Point{50, 200},
    52  				Scale:  f32.Point{X: 2, Y: 2},
    53  				Result: image.Point{X: 100, Y: 100},
    54  			}},
    55  		ScaleDown: {
    56  			{
    57  				Dims:   image.Point{50, 25},
    58  				Scale:  f32.Point{X: 1, Y: 1},
    59  				Result: image.Point{X: 50, Y: 25},
    60  			}, {
    61  				Dims:   image.Point{50, 200},
    62  				Scale:  f32.Point{X: 0.5, Y: 0.5},
    63  				Result: image.Point{X: 25, Y: 100},
    64  			}},
    65  		Fill: {
    66  			{
    67  				Dims:   image.Point{50, 25},
    68  				Scale:  f32.Point{X: 2, Y: 4},
    69  				Result: image.Point{X: 100, Y: 100},
    70  			}, {
    71  				Dims:   image.Point{50, 200},
    72  				Scale:  f32.Point{X: 2, Y: 0.5},
    73  				Result: image.Point{X: 100, Y: 100},
    74  			}},
    75  	}
    76  
    77  	for fit, tests := range fittests {
    78  		fit := Fit(fit)
    79  		for i, test := range tests {
    80  			cs := layout.Constraints{
    81  				Max: image.Point{X: 100, Y: 100},
    82  			}
    83  			result, trans := fit.scale(cs, layout.NW, layout.Dimensions{Size: test.Dims})
    84  			sx, _, _, _, sy, _ := trans.Elems()
    85  			if scale := f32.Pt(sx, sy); scale != test.Scale {
    86  				t.Errorf("got scale %v expected %v", scale, test.Scale)
    87  			}
    88  
    89  			if result.Size != test.Result {
    90  				t.Errorf("fit %v, #%v: expected %#v, got %#v", fit, i, test.Result, result.Size)
    91  			}
    92  		}
    93  	}
    94  }