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