1 package messeji
2
3 import (
4 "embed"
5 "fmt"
6 "image"
7 "log"
8 "sync"
9 "testing"
10
11 "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
12 "golang.org/x/image/font"
13 "golang.org/x/image/font/opentype"
14 )
15
16
17 var testDataFS embed.FS
18
19 var testTextField *TextField
20
21 func TestWrapContent(t *testing.T) {
22 testCases := []struct {
23 long bool
24 wordWrap bool
25
26 }{
27 {false, false},
28 {false, true},
29 {true, false},
30 {true, true},
31 }
32
33 tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
34 if err != nil {
35 log.Fatal(err)
36 }
37
38 const size = 24
39 const dpi = 72
40 face, err := opentype.NewFace(tt, &opentype.FaceOptions{
41 Size: size,
42 DPI: dpi,
43 Hinting: font.HintingFull,
44 })
45 if err != nil {
46 log.Fatal(err)
47 }
48
49 testRect := image.Rect(0, 0, 200, 400)
50
51 for _, c := range testCases {
52 var name string
53 if !c.long {
54 name = "short"
55 } else {
56 name = "long"
57 }
58
59 content, err := testDataFS.ReadFile(fmt.Sprintf("testdata/%s.txt", name))
60 if err != nil {
61 t.Errorf("failed to open testdata: %s", err)
62 }
63
64 if !c.wordWrap {
65 name += "/wrapChar"
66 } else {
67 name += "/wrapWord"
68 }
69
70 t.Run(name, func(t *testing.T) {
71 textField := NewTextField(face, &sync.Mutex{})
72 testTextField = textField
73 textField.SetRect(testRect)
74 textField.SetWordWrap(c.wordWrap)
75 textField.Write(content)
76 textField.bufferModified()
77 })
78 }
79 }
80
81 func BenchmarkWrapContent(b *testing.B) {
82 testCases := []struct {
83 long bool
84 wordWrap bool
85
86 }{
87 {false, false},
88 {false, true},
89 {true, false},
90 {true, true},
91 }
92
93 tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
94 if err != nil {
95 log.Fatal(err)
96 }
97
98 const size = 24
99 const dpi = 72
100 face, err := opentype.NewFace(tt, &opentype.FaceOptions{
101 Size: size,
102 DPI: dpi,
103 Hinting: font.HintingFull,
104 })
105 if err != nil {
106 log.Fatal(err)
107 }
108
109 testRect := image.Rect(0, 0, 200, 400)
110
111 for _, c := range testCases {
112 var name string
113 if !c.long {
114 name = "short"
115 } else {
116 name = "long"
117 }
118
119 content, err := testDataFS.ReadFile(fmt.Sprintf("testdata/%s.txt", name))
120 if err != nil {
121 b.Errorf("failed to open testdata: %s", err)
122 }
123
124 if !c.wordWrap {
125 name += "/wrapChar"
126 } else {
127 name += "/wrapWord"
128 }
129
130 textField := NewTextField(face, &sync.Mutex{})
131 testTextField = textField
132 textField.SetRect(testRect)
133 textField.SetWordWrap(c.wordWrap)
134
135 b.Run(name, func(b *testing.B) {
136 for i := 0; i < b.N; i++ {
137 textField.SetText("")
138 textField.Write(content)
139 textField.bufferModified()
140 }
141 })
142 }
143 }
144
View as plain text