On-screen keyboard widget for ebiten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.3 KiB

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{}
}