audio_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package audio_test
  15. import (
  16. "bytes"
  17. "io"
  18. "runtime"
  19. "testing"
  20. "time"
  21. "github.com/hajimehoshi/ebiten/v2/audio"
  22. )
  23. var context *audio.Context
  24. func setup() {
  25. context = audio.NewContext(44100)
  26. }
  27. func teardown() {
  28. audio.ResetContextForTesting()
  29. context = nil
  30. }
  31. // Issue #746
  32. func TestGC(t *testing.T) {
  33. setup()
  34. defer teardown()
  35. p, _ := context.NewPlayer(bytes.NewReader(make([]byte, 4)))
  36. got := audio.PlayersCountForTesting()
  37. if want := 0; got != want {
  38. t.Errorf("PlayersCountForTesting(): got: %d, want: %d", got, want)
  39. }
  40. p.Play()
  41. got = audio.PlayersCountForTesting()
  42. if want := 1; got != want {
  43. t.Errorf("PlayersCountForTesting() after Play: got: %d, want: %d", got, want)
  44. }
  45. runtime.KeepAlive(p)
  46. p = nil
  47. runtime.GC()
  48. for i := 0; i < 10; i++ {
  49. got = audio.PlayersCountForTesting()
  50. if want := 0; got == want {
  51. return
  52. }
  53. if err := audio.UpdateForTesting(); err != nil {
  54. t.Error(err)
  55. }
  56. // 200[ms] should be enough all the bytes are consumed.
  57. // TODO: This is a dirty hack. Would it be possible to use virtual time?
  58. time.Sleep(200 * time.Millisecond)
  59. }
  60. t.Errorf("time out")
  61. }
  62. // Issue #853
  63. func TestSameSourcePlayers(t *testing.T) {
  64. setup()
  65. defer teardown()
  66. src := bytes.NewReader(make([]byte, 4))
  67. p0, err := context.NewPlayer(src)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. p1, err := context.NewPlayer(src)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. // As the player does not play yet, error doesn't happen.
  76. if err := audio.UpdateForTesting(); err != nil {
  77. t.Error(err)
  78. }
  79. p0.Play()
  80. p1.Play()
  81. if err := audio.UpdateForTesting(); err == nil {
  82. t.Errorf("got: nil, want: an error")
  83. }
  84. }
  85. func TestPauseBeforeInit(t *testing.T) {
  86. setup()
  87. defer teardown()
  88. src := bytes.NewReader(make([]byte, 4))
  89. p, err := context.NewPlayer(src)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. p.Play()
  94. p.Pause()
  95. p.Play()
  96. if err := audio.UpdateForTesting(); err != nil {
  97. t.Error(err)
  98. }
  99. }
  100. type emptySource struct{}
  101. func (emptySource) Read(buf []byte) (int, error) {
  102. return len(buf), nil
  103. }
  104. func TestNonSeekableSource(t *testing.T) {
  105. if runtime.GOOS == "js" {
  106. t.Skip("infinite steams in tests cannot be treated well on browsers")
  107. }
  108. setup()
  109. defer teardown()
  110. p, err := context.NewPlayer(emptySource{})
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. p.Play()
  115. p.Pause()
  116. if err := audio.UpdateForTesting(); err != nil {
  117. t.Error(err)
  118. }
  119. }
  120. type uncomparableSource []int
  121. func (uncomparableSource) Read(buf []byte) (int, error) {
  122. return 0, io.EOF
  123. }
  124. // Issue #3039
  125. func TestUncomparableSource(t *testing.T) {
  126. setup()
  127. defer teardown()
  128. p, err := context.NewPlayer(uncomparableSource{})
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. p.Play()
  133. p.Pause()
  134. if err := audio.UpdateForTesting(); err != nil {
  135. t.Error(err)
  136. }
  137. }