| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleSoundEngine" -*- |
| 2 |
{ |
| 3 |
Copyright 2017-2021 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test CastleSoundEngine unit. } |
| 18 |
unit TestCastleSoundEngine; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleSoundEngine = class(TCastleTestCase) |
| 27 |
published |
| 28 |
procedure TestLoadBufferException; |
| 29 |
procedure TestNotPcmEncodingWarning; |
| 30 |
procedure TestImportancePriority; |
| 31 |
procedure TestSoundFromDataUri; |
| 32 |
end; |
| 33 |
|
| 34 |
implementation |
| 35 |
|
| 36 |
uses CastleFilesUtils, CastleSoundEngine, CastleApplicationProperties, CastleDownload, |
| 37 |
CastleLog; |
| 38 |
|
| 39 |
procedure TTestCastleSoundEngine.TestLoadBufferException; |
| 40 |
var |
| 41 |
Sound: TCastleSound; |
| 42 |
begin |
| 43 |
// we want "Sound.Url := " on invalid file to cause exception, not warning |
| 44 |
ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 45 |
try |
| 46 |
Sound := TCastleSound.Create(nil); |
| 47 |
try |
| 48 |
try |
| 49 |
Sound.Url := 'castle-data:/sound/non-existing.wav'; |
| 50 |
if not SoundEngine.IsContextOpenSuccess then |
| 51 |
Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything') |
| 52 |
else |
| 53 |
Fail('Should have raised ESoundFileError 1'); |
| 54 |
except on Exception do ; end; |
| 55 |
|
| 56 |
try |
| 57 |
Sound.Url := 'castle-data:/sound/non-existing.ogg'; |
| 58 |
if not SoundEngine.IsContextOpenSuccess then |
| 59 |
Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything') |
| 60 |
else |
| 61 |
Fail('Should have raised ESoundFileError 2'); |
| 62 |
except on Exception do ; end; |
| 63 |
|
| 64 |
try |
| 65 |
Sound.Url := 'castle-data:/sound/invalid.wav'; |
| 66 |
if not SoundEngine.IsContextOpenSuccess then |
| 67 |
Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything') |
| 68 |
else |
| 69 |
Fail('Should have raised ESoundFileError 3'); |
| 70 |
except on Exception do ; end; |
| 71 |
|
| 72 |
try |
| 73 |
Sound.Url := 'castle-data:/sound/invalid.ogg'; |
| 74 |
if not SoundEngine.IsContextOpenSuccess then |
| 75 |
Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything') |
| 76 |
else |
| 77 |
Fail('Should have raised ESoundFileError 4'); |
| 78 |
except on Exception do ; end; |
| 79 |
finally |
| 80 |
FreeAndNil(Sound); |
| 81 |
end; |
| 82 |
finally |
| 83 |
ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 84 |
end; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TTestCastleSoundEngine.TestNotPcmEncodingWarning; |
| 88 |
begin |
| 89 |
ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 90 |
try |
| 91 |
if SoundEngine.IsContextOpenSuccess then |
| 92 |
begin |
| 93 |
try |
| 94 |
SoundEngine.RepositoryURL := 'castle-data:/sound/not_pcm_encoding/index.xml'; |
| 95 |
Fail('Should have raised EWavLoadError'); |
| 96 |
except |
| 97 |
on E: Exception do |
| 98 |
begin |
| 99 |
if Pos('Only uncompressed (PCM) WAV files are supported', E.Message) > 0 then |
| 100 |
begin |
| 101 |
// good, we expect this |
| 102 |
end else |
| 103 |
raise; |
| 104 |
end; |
| 105 |
end; |
| 106 |
end else |
| 107 |
Writeln('Sound backend cannot be initialized, TestNotPcmEncodingWarning doesn''t really do anything'); |
| 108 |
finally |
| 109 |
ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 110 |
end; |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TTestCastleSoundEngine.TestImportancePriority; |
| 114 |
var |
| 115 |
Params: TPlaySoundParameters; |
| 116 |
begin |
| 117 |
Params := TPlaySoundParameters.Create; |
| 118 |
try |
| 119 |
AssertSameValue(0.5, Params.Priority); |
| 120 |
finally FreeAndNil(Params) end; |
| 121 |
end; |
| 122 |
|
| 123 |
procedure TTestCastleSoundEngine.TestSoundFromDataUri; |
| 124 |
var |
| 125 |
Sound: TCastleSound; |
| 126 |
Stream: TStream; |
| 127 |
begin |
| 128 |
if not SoundEngine.IsContextOpenSuccess then |
| 129 |
begin |
| 130 |
Writeln('Sound backend cannot be initialized, TestSoundFromDataUri ignored'); |
| 131 |
Exit; |
| 132 |
end; |
| 133 |
|
| 134 |
Sound := TCastleSound.Create(nil); |
| 135 |
try |
| 136 |
Stream := Download('castle-data:/game/alien_sudden_pain.wav', []); |
| 137 |
try |
| 138 |
Sound.LoadFromStream(Stream, 'audio/x-wav'); |
| 139 |
finally FreeAndNil(Stream) end; |
| 140 |
AssertSameValue(1.46, Sound.Duration, 0.01); |
| 141 |
finally FreeAndNil(Sound) end; |
| 142 |
|
| 143 |
Sound := TCastleSound.Create(nil); |
| 144 |
try |
| 145 |
Stream := Download('castle-data:/game/alien_sudden_pain.wav', []); |
| 146 |
try |
| 147 |
Sound.LoadFromStream(Stream, 'audio/wav'); |
| 148 |
finally FreeAndNil(Stream) end; |
| 149 |
AssertSameValue(1.46, Sound.Duration, 0.01); |
| 150 |
finally FreeAndNil(Sound) end; |
| 151 |
end; |
| 152 |
|
| 153 |
initialization |
| 154 |
RegisterTest(TTestCastleSoundEngine); |
| 155 |
end. |