This repository has been archived on 2024-01-17. You can view files and clone it, but cannot push or open issues or pull requests.
kibodo/keyboard_test.go

89 lines
1.3 KiB
Go

package kibodo
import (
"runtime"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
// TODO test presses registered
func TestKeyboard_Draw(t *testing.T) {
k, _ := newTestKeyboard()
// Warm caches
k.drawBackground()
}
func BenchmarkKeyboard_Draw(b *testing.B) {
k, _ := newTestKeyboard()
// Warm caches
k.drawBackground()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
k.drawBackground()
}
}
func BenchmarkKeyboard_Press(b *testing.B) {
go func() {
time.Sleep(2 * time.Second)
k, _ := newTestKeyboard()
// Warm caches
k.drawBackground()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
k.drawBackground()
k.keys[0][0].pressed = true
k.drawBackground()
k.keys[0][0].pressed = false
}
}()
runtime.LockOSThread()
err := ebiten.RunGame(NewDummyGame())
if err != nil {
b.Error(err)
}
}
func newTestKeyboard() (*Keyboard, chan *Input) {
ch := make(chan *Input, 10)
k := NewKeyboard()
k.SetRect(0, 0, 300, 100)
k.Show(ch)
return k, ch
}
type DummyGame struct {
ready bool
}
func (d *DummyGame) Update() error {
return nil
}
func (d *DummyGame) Draw(screen *ebiten.Image) {
d.ready = true
}
func (d *DummyGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return outsideWidth, outsideHeight
}
func NewDummyGame() *DummyGame {
return &DummyGame{}
}