...
1 package kibodo
2
3 import (
4 "bytes"
5 "runtime"
6 "testing"
7 "time"
8
9 "github.com/hajimehoshi/ebiten/v2"
10 "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
11 "github.com/hajimehoshi/ebiten/v2/text/v2"
12 )
13
14
15
16 func TestKeyboard_Draw(t *testing.T) {
17 k := newTestKeyboard()
18
19
20 k.drawBackground()
21 }
22
23 func BenchmarkKeyboard_Draw(b *testing.B) {
24 k := newTestKeyboard()
25
26
27 k.drawBackground()
28
29 b.ReportAllocs()
30 b.ResetTimer()
31 for i := 0; i < b.N; i++ {
32 k.drawBackground()
33 }
34 }
35
36 func BenchmarkKeyboard_Press(b *testing.B) {
37 go func() {
38 time.Sleep(2 * time.Second)
39
40 k := newTestKeyboard()
41
42
43 k.drawBackground()
44
45 b.ReportAllocs()
46 b.ResetTimer()
47 for i := 0; i < b.N; i++ {
48 k.drawBackground()
49 k.keys[0][0].pressed = true
50 k.drawBackground()
51 k.keys[0][0].pressed = false
52 }
53 }()
54
55 runtime.LockOSThread()
56
57 err := ebiten.RunGame(NewDummyGame())
58 if err != nil {
59 b.Error(err)
60 }
61 }
62
63 func newTestKeyboard() *Keyboard {
64 k := NewKeyboard(defaultFont())
65 k.SetRect(0, 0, 300, 100)
66
67 return k
68 }
69
70 type DummyGame struct {
71 ready bool
72 }
73
74 func (d *DummyGame) Update() error {
75 return nil
76 }
77
78 func (d *DummyGame) Draw(screen *ebiten.Image) {
79 d.ready = true
80 }
81
82 func (d *DummyGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
83 return outsideWidth, outsideHeight
84 }
85
86 func NewDummyGame() *DummyGame {
87 return &DummyGame{}
88 }
89
90 func defaultFont() *text.GoTextFaceSource {
91 source, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
92 if err != nil {
93 panic(err)
94 }
95 return source
96 }
97
View as plain text