File: testcastlesoundengine.pas

package info (click to toggle)
castle-game-engine 7.0~alpha.3%2Bdfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 969,476 kB
  • sloc: pascal: 911,523; javascript: 28,186; cpp: 14,157; xml: 9,939; ansic: 9,229; java: 3,653; objc: 2,737; sh: 1,214; makefile: 657; php: 65; lisp: 21; ruby: 8
file content (155 lines) | stat: -rw-r--r-- 4,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// -*- compile-command: "./test_single_testcase.sh TTestCastleSoundEngine" -*-
{
  Copyright 2017-2021 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Test CastleSoundEngine unit. }
unit TestCastleSoundEngine;

interface

uses
  Classes, SysUtils, CastleTester;

type
  TTestCastleSoundEngine = class(TCastleTestCase)
  published
    procedure TestLoadBufferException;
    procedure TestNotPcmEncodingWarning;
    procedure TestImportancePriority;
    procedure TestSoundFromDataUri;
  end;

implementation

uses CastleFilesUtils, CastleSoundEngine, CastleApplicationProperties, CastleDownload,
  CastleLog;

procedure TTestCastleSoundEngine.TestLoadBufferException;
var
  Sound: TCastleSound;
begin
  // we want "Sound.Url := " on invalid file to cause exception, not warning
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Sound := TCastleSound.Create(nil);
    try
      try
        Sound.Url := 'castle-data:/sound/non-existing.wav';
        if not SoundEngine.IsContextOpenSuccess then
          Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything')
        else
          Fail('Should have raised ESoundFileError 1');
      except on Exception do ; end;

      try
        Sound.Url := 'castle-data:/sound/non-existing.ogg';
        if not SoundEngine.IsContextOpenSuccess then
          Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything')
        else
          Fail('Should have raised ESoundFileError 2');
      except on Exception do ; end;

      try
        Sound.Url := 'castle-data:/sound/invalid.wav';
        if not SoundEngine.IsContextOpenSuccess then
          Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything')
        else
          Fail('Should have raised ESoundFileError 3');
      except on Exception do ; end;

      try
        Sound.Url := 'castle-data:/sound/invalid.ogg';
        if not SoundEngine.IsContextOpenSuccess then
          Writeln('Sound backend cannot be initialized, TestLoadBufferException doesn''t really do anything')
        else
          Fail('Should have raised ESoundFileError 4');
      except on Exception do ; end;
    finally
      FreeAndNil(Sound);
    end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestCastleSoundEngine.TestNotPcmEncodingWarning;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    if SoundEngine.IsContextOpenSuccess then
    begin
      try
        SoundEngine.RepositoryURL := 'castle-data:/sound/not_pcm_encoding/index.xml';
        Fail('Should have raised EWavLoadError');
      except
        on E: Exception do
        begin
          if Pos('Only uncompressed (PCM) WAV files are supported', E.Message) > 0 then
          begin
            // good, we expect this
          end else
            raise;
        end;
      end;
    end else
      Writeln('Sound backend cannot be initialized, TestNotPcmEncodingWarning doesn''t really do anything');
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestCastleSoundEngine.TestImportancePriority;
var
  Params: TPlaySoundParameters;
begin
  Params := TPlaySoundParameters.Create;
  try
    AssertSameValue(0.5, Params.Priority);
  finally FreeAndNil(Params) end;
end;

procedure TTestCastleSoundEngine.TestSoundFromDataUri;
var
  Sound: TCastleSound;
  Stream: TStream;
begin
  if not SoundEngine.IsContextOpenSuccess then
  begin
    Writeln('Sound backend cannot be initialized, TestSoundFromDataUri ignored');
    Exit;
  end;

  Sound := TCastleSound.Create(nil);
  try
    Stream := Download('castle-data:/game/alien_sudden_pain.wav', []);
    try
      Sound.LoadFromStream(Stream, 'audio/x-wav');
    finally FreeAndNil(Stream) end;
    AssertSameValue(1.46, Sound.Duration, 0.01);
  finally FreeAndNil(Sound) end;

  Sound := TCastleSound.Create(nil);
  try
    Stream := Download('castle-data:/game/alien_sudden_pain.wav', []);
    try
      Sound.LoadFromStream(Stream, 'audio/wav');
    finally FreeAndNil(Stream) end;
    AssertSameValue(1.46, Sound.Duration, 0.01);
  finally FreeAndNil(Sound) end;
end;

initialization
  RegisterTest(TTestCastleSoundEngine);
end.