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

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget
     4  
     5  import (
     6  	"image"
     7  	"testing"
     8  
     9  	"github.com/utopiagio/gio/layout"
    10  	"github.com/utopiagio/gio/op"
    11  	"github.com/utopiagio/gio/op/paint"
    12  )
    13  
    14  func TestImageScale(t *testing.T) {
    15  	var ops op.Ops
    16  	gtx := layout.Context{
    17  		Ops: &ops,
    18  		Constraints: layout.Constraints{
    19  			Max: image.Pt(50, 50),
    20  		},
    21  	}
    22  	imgSize := image.Pt(10, 10)
    23  	img := image.NewNRGBA(image.Rectangle{Max: imgSize})
    24  	imgOp := paint.NewImageOp(img)
    25  
    26  	// Ensure the default scales correctly.
    27  	dims := Image{Src: imgOp}.Layout(gtx)
    28  	expectedSize := imgSize
    29  	expectedSize.X = int(float32(expectedSize.X))
    30  	expectedSize.Y = int(float32(expectedSize.Y))
    31  	if dims.Size != expectedSize {
    32  		t.Fatalf("non-scaled image is wrong size, expected %v, got %v", expectedSize, dims.Size)
    33  	}
    34  
    35  	// Ensure scaling the image via the Scale field works.
    36  	currentScale := float32(0.5)
    37  	dims = Image{Src: imgOp, Scale: float32(currentScale)}.Layout(gtx)
    38  	expectedSize = imgSize
    39  	expectedSize.X = int(float32(expectedSize.X) * currentScale)
    40  	expectedSize.Y = int(float32(expectedSize.Y) * currentScale)
    41  	if dims.Size != expectedSize {
    42  		t.Fatalf(".5 scale image is wrong size, expected %v, got %v", expectedSize, dims.Size)
    43  	}
    44  
    45  	// Ensure the image responds to changes in DPI.
    46  	currentScale = float32(1)
    47  	gtx.Metric.PxPerDp = 2
    48  	dims = Image{Src: imgOp, Scale: float32(currentScale)}.Layout(gtx)
    49  	expectedSize = imgSize
    50  	expectedSize.X = int(float32(expectedSize.X) * currentScale * gtx.Metric.PxPerDp)
    51  	expectedSize.Y = int(float32(expectedSize.Y) * currentScale * gtx.Metric.PxPerDp)
    52  	if dims.Size != expectedSize {
    53  		t.Fatalf("HiDPI non-scaled image is wrong size, expected %v, got %v", expectedSize, dims.Size)
    54  	}
    55  
    56  	// Ensure scaling the image responds to changes in DPI.
    57  	currentScale = float32(.5)
    58  	gtx.Metric.PxPerDp = 2
    59  	dims = Image{Src: imgOp, Scale: float32(currentScale)}.Layout(gtx)
    60  	expectedSize = imgSize
    61  	expectedSize.X = int(float32(expectedSize.X) * currentScale * gtx.Metric.PxPerDp)
    62  	expectedSize.Y = int(float32(expectedSize.Y) * currentScale * gtx.Metric.PxPerDp)
    63  	if dims.Size != expectedSize {
    64  		t.Fatalf("HiDPI .5 scale image is wrong size, expected %v, got %v", expectedSize, dims.Size)
    65  	}
    66  }