audio: Use dummy hooks from tests

This commit is contained in:
Hajime Hoshi 2019-04-28 19:35:59 +09:00
parent 81549a3523
commit 70a225fd7e
4 changed files with 60 additions and 25 deletions

View File

@ -40,7 +40,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
) )
@ -112,15 +111,16 @@ func CurrentContext() *Context {
func (c *Context) loop() { func (c *Context) loop() {
suspendCh := make(chan struct{}, 1) suspendCh := make(chan struct{}, 1)
resumeCh := make(chan struct{}, 1) resumeCh := make(chan struct{}, 1)
hooks.OnSuspendAudio(func() { h := getHook()
h.OnSuspendAudio(func() {
suspendCh <- struct{}{} suspendCh <- struct{}{}
}) })
hooks.OnResumeAudio(func() { h.OnResumeAudio(func() {
resumeCh <- struct{}{} resumeCh <- struct{}{}
}) })
var once sync.Once var once sync.Once
hooks.AppendHookOnBeforeUpdate(func() error { h.AppendHookOnBeforeUpdate(func() error {
once.Do(func() { once.Do(func() {
close(c.initCh) close(c.initCh)
}) })

View File

@ -15,34 +15,13 @@
package audio_test package audio_test
import ( import (
"errors"
"os"
"runtime" "runtime"
"testing" "testing"
"time" "time"
"github.com/hajimehoshi/ebiten"
. "github.com/hajimehoshi/ebiten/audio" . "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/internal/testflock"
) )
func TestMain(m *testing.M) {
testflock.Lock()
defer testflock.Unlock()
code := 0
// Run an Ebiten process so that audio is available.
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
}
var context *Context var context *Context
func init() { func init() {
@ -76,6 +55,9 @@ func TestGC(t *testing.T) {
if want := 0; got == want { if want := 0; got == want {
return return
} }
if err := UpdateForTesting(); err != nil {
t.Error(err)
}
// 200[ms] should be enough all the bytes are consumed. // 200[ms] should be enough all the bytes are consumed.
// TODO: This is a darty hack. Would it be possible to use virtual time? // TODO: This is a darty hack. Would it be possible to use virtual time?
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)

View File

@ -19,6 +19,8 @@ import (
"sync" "sync"
"github.com/hajimehoshi/oto" "github.com/hajimehoshi/oto"
"github.com/hajimehoshi/ebiten/internal/hooks"
) )
type context interface { type context interface {
@ -100,3 +102,32 @@ func newContext(sampleRate int, initCh <-chan struct{}) context {
initCh: initCh, initCh: initCh,
} }
} }
type hook interface {
OnSuspendAudio(f func())
OnResumeAudio(f func())
AppendHookOnBeforeUpdate(f func() error)
}
var hookForTesting hook
func getHook() hook {
if hookForTesting != nil {
return hookForTesting
}
return &hookImpl{}
}
type hookImpl struct{}
func (h *hookImpl) OnSuspendAudio(f func()) {
hooks.OnSuspendAudio(f)
}
func (h *hookImpl) OnResumeAudio(f func()) {
hooks.OnResumeAudio(f)
}
func (h *hookImpl) AppendHookOnBeforeUpdate(f func() error) {
hooks.AppendHookOnBeforeUpdate(f)
}

View File

@ -42,3 +42,25 @@ func (p *dummyPlayer) Close() error {
func init() { func init() {
contextForTesting = &dummyContext{} contextForTesting = &dummyContext{}
} }
type dummyHook struct {
update func() error
}
func (h *dummyHook) OnSuspendAudio(f func()) {
}
func (h *dummyHook) OnResumeAudio(f func()) {
}
func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
h.update = f
}
func init() {
hookForTesting = &dummyHook{}
}
func UpdateForTesting() error {
return hookForTesting.(*dummyHook).update()
}