~eliasnaur/gio

ref: 872b4ba41be0d8b476d1cfd419d9bd9c94e2dc10 gio/op/clip/clip_test.go -rw-r--r-- 1.7 KiB
872b4ba4Pierre Curto op/clip: fix Path open state 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: Unlicense OR MIT

package clip_test

import (
	"image/color"
	"math"
	"testing"

	"gioui.org/f32"
	"gioui.org/gpu/headless"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
)

func TestPathOutline(t *testing.T) {
	t.Run("unclosed path", func(t *testing.T) {
		defer func() {
			if err := recover(); err == nil {
				t.Error("Outline of an open path didn't panic")
			}
		}()
		var p clip.Path
		p.Begin(new(op.Ops))
		p.Line(f32.Pt(10, 10))
		clip.Outline{Path: p.End()}.Op()
	})
	t.Run("closed path", func(t *testing.T) {
		defer func() {
			if err := recover(); err != nil {
				t.Error("Outline of a closed path did panic")
			}
		}()
		var p clip.Path
		p.Begin(new(op.Ops))
		p.MoveTo(f32.Pt(300, 200))
		p.LineTo(f32.Pt(150, 200))
		p.MoveTo(f32.Pt(150, 200))
		p.ArcTo(f32.Pt(300, 200), f32.Pt(300, 200), 3*math.Pi/4)
		p.LineTo(f32.Pt(300, 200))
		p.Close()
		clip.Outline{Path: p.End()}.Op()
	})
}

func TestPathBegin(t *testing.T) {
	ops := new(op.Ops)
	var p clip.Path
	p.Begin(ops)
	p.LineTo(f32.Point{10, 10})
	p.Close()
	stack := clip.Outline{Path: p.End()}.Op().Push(ops)
	paint.Fill(ops, color.NRGBA{A: 255})
	stack.Pop()
	w := newWindow(t, 100, 100)
	if w == nil {
		return
	}
	// The following should not panic.
	_ = w.Frame(ops)
}

func TestTransformChecks(t *testing.T) {
	defer func() {
		if err := recover(); err == nil {
			t.Error("cross-macro Pop didn't panic")
		}
	}()
	var ops op.Ops
	st := clip.Op{}.Push(&ops)
	op.Record(&ops)
	st.Pop()
}

func newWindow(t testing.TB, width, height int) *headless.Window {
	w, err := headless.NewWindow(width, height)
	if err != nil {
		t.Skipf("failed to create headless window, skipping: %v", err)
	}
	return w
}