ebiten/audio/export_test.go

162 lines
2.9 KiB
Go
Raw Normal View History

// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audio
2019-01-26 16:31:55 +01:00
import (
"io"
"io/ioutil"
"sync"
2019-01-26 16:31:55 +01:00
)
2019-01-26 16:31:55 +01:00
type (
dummyWriterPlayerDriver struct{}
dummyWriterPlayer struct{}
2019-01-26 16:31:55 +01:00
)
func (d *dummyWriterPlayerDriver) NewPlayer() io.WriteCloser {
return &dummyWriterPlayer{}
2019-01-26 16:31:55 +01:00
}
func (d *dummyWriterPlayerDriver) Close() error {
2019-01-26 16:31:55 +01:00
return nil
}
func (p *dummyWriterPlayer) Write(b []byte) (int, error) {
return len(b), nil
}
func (p *dummyWriterPlayer) Close() error {
return nil
}
func init() {
writerDriverForTesting = &dummyWriterPlayerDriver{}
}
type (
dummyReaderPlayerDriver struct{}
dummyReaderPlayer struct {
r io.Reader
playing bool
volume float64
m sync.Mutex
}
)
func (d *dummyReaderPlayerDriver) NewPlayer(r io.Reader) readerDriverPlayer {
return &dummyReaderPlayer{
r: r,
volume: 1,
}
}
func (d *dummyReaderPlayerDriver) Close() error {
return nil
}
func (p *dummyReaderPlayer) Pause() {
p.m.Lock()
p.playing = false
p.m.Unlock()
}
func (p *dummyReaderPlayer) Play() {
p.m.Lock()
p.playing = true
p.m.Unlock()
go func() {
if _, err := ioutil.ReadAll(p.r); err != nil {
panic(err)
}
p.m.Lock()
p.playing = false
p.m.Unlock()
}()
}
func (p *dummyReaderPlayer) IsPlaying() bool {
p.m.Lock()
defer p.m.Unlock()
return p.playing
}
func (p *dummyReaderPlayer) Reset() {
p.m.Lock()
defer p.m.Unlock()
p.playing = false
}
func (p *dummyReaderPlayer) Volume() float64 {
return p.volume
}
func (p *dummyReaderPlayer) SetVolume(volume float64) {
p.volume = volume
}
func (p *dummyReaderPlayer) UnplayedBufferSize() int64 {
return 0
}
func (p *dummyReaderPlayer) Close() error {
p.m.Lock()
defer p.m.Unlock()
p.playing = false
return nil
}
func init() {
readerDriverForTesting = &dummyReaderPlayerDriver{}
}
2019-04-28 12:35:59 +02:00
type dummyHook struct {
updates []func() error
2019-04-28 12:35:59 +02:00
}
func (h *dummyHook) OnSuspendAudio(f func()) {
}
func (h *dummyHook) OnResumeAudio(f func()) {
}
func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
h.updates = append(h.updates, f)
2019-04-28 12:35:59 +02:00
}
func init() {
hookForTesting = &dummyHook{}
}
func UpdateForTesting() error {
for _, f := range hookForTesting.(*dummyHook).updates {
if err := f(); err != nil {
return err
}
}
return nil
2019-04-28 12:35:59 +02:00
}
2021-01-06 17:37:29 +01:00
func PlayersNumForTesting() int {
c := CurrentContext()
c.m.Lock()
n := len(c.players)
c.m.Unlock()
return n
}
func ResetContextForTesting() {
2021-01-06 17:37:29 +01:00
theContext = nil
}