github.com/Seikaijyu/gio@v0.0.1/op/clip/clip_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package clip_test
     4  
     5  import (
     6  	"image/color"
     7  	"math"
     8  	"testing"
     9  
    10  	"github.com/Seikaijyu/gio/f32"
    11  	"github.com/Seikaijyu/gio/gpu/headless"
    12  	"github.com/Seikaijyu/gio/op"
    13  	"github.com/Seikaijyu/gio/op/clip"
    14  	"github.com/Seikaijyu/gio/op/paint"
    15  )
    16  
    17  func TestPathOutline(t *testing.T) {
    18  	t.Run("closed path", func(t *testing.T) {
    19  		defer func() {
    20  			if err := recover(); err != nil {
    21  				t.Error("Outline of a closed path did panic")
    22  			}
    23  		}()
    24  		var p clip.Path
    25  		p.Begin(new(op.Ops))
    26  		p.MoveTo(f32.Pt(300, 200))
    27  		p.LineTo(f32.Pt(150, 200))
    28  		p.MoveTo(f32.Pt(150, 200))
    29  		p.ArcTo(f32.Pt(300, 200), f32.Pt(300, 200), 3*math.Pi/4)
    30  		p.LineTo(f32.Pt(300, 200))
    31  		p.Close()
    32  		clip.Outline{Path: p.End()}.Op()
    33  	})
    34  }
    35  
    36  func TestPathBegin(t *testing.T) {
    37  	ops := new(op.Ops)
    38  	var p clip.Path
    39  	p.Begin(ops)
    40  	p.LineTo(f32.Pt(10, 10))
    41  	p.Close()
    42  	stack := clip.Outline{Path: p.End()}.Op().Push(ops)
    43  	paint.Fill(ops, color.NRGBA{A: 255})
    44  	stack.Pop()
    45  	w := newWindow(t, 100, 100)
    46  	if w == nil {
    47  		return
    48  	}
    49  	// The following should not panic.
    50  	_ = w.Frame(ops)
    51  }
    52  
    53  func TestTransformChecks(t *testing.T) {
    54  	defer func() {
    55  		if err := recover(); err == nil {
    56  			t.Error("cross-macro Pop didn't panic")
    57  		}
    58  	}()
    59  	var ops op.Ops
    60  	st := clip.Op{}.Push(&ops)
    61  	op.Record(&ops)
    62  	st.Pop()
    63  }
    64  
    65  func TestEmptyPath(t *testing.T) {
    66  	var ops op.Ops
    67  	p := clip.Path{}
    68  	p.Begin(&ops)
    69  	defer clip.Stroke{
    70  		Path:  p.End(),
    71  		Width: 3,
    72  	}.Op().Push(&ops).Pop()
    73  }
    74  
    75  func newWindow(t testing.TB, width, height int) *headless.Window {
    76  	w, err := headless.NewWindow(width, height)
    77  	if err != nil {
    78  		t.Skipf("failed to create headless window, skipping: %v", err)
    79  	}
    80  	return w
    81  }