Debian Codegraph @

tests/code/testcases/testx3dfields.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestX3DFields" -*-
2 {
3 Copyright 2017-2022 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 X3DFields unit. }
18 unit TestX3DFields;
19
20 interface
21
22 uses
23 Classes, SysUtils,
24 CastleTester, X3DFields, X3DTime;
25
26 type
27 TTestX3DFields = class(TCastleTestCase)
28 strict private
29 Counter: Integer;
30 procedure Check123(const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
31 published
32 procedure TestNotification;
33 end;
34
35 implementation
36
37 uses CastleSceneCore, X3DNodes;
38
39 procedure TTestX3DFields.Check123(const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
40 begin
41 AssertTrue(Value is TSFInt32);
42 AssertEquals(123, (Value as TSFInt32).Value);
43 Inc(Counter);
44 end;
45
46 procedure TTestX3DFields.TestNotification;
47 var
48 Scene: TCastleSceneCore;
49 Node: TX3DRootNode;
50 E: TSFInt32Event;
51 begin
52 { sending events requires a Scene and Node }
53 Scene := TCastleSceneCore.Create(nil);
54 try
55 Node := TX3DRootNode.Create;
56 Scene.Load(Node, true);
57
58 E := TSFInt32Event.Create(Node, 'my field', true);
59 try
60 E.AddNotification({$ifdef FPC}@{$endif}Check123);
61 Counter := 0;
62 E.Send(123);
63 AssertEquals(1, Counter);
64
65 { adding the same notification callback is OK,
66 and makes the callback called twice }
67 E.AddNotification({$ifdef FPC}@{$endif}Check123);
68 Counter := 0;
69 E.Send(123);
70 AssertEquals(2, Counter);
71
72 { removing the notification removes only 1 copy of it }
73 E.RemoveNotification({$ifdef FPC}@{$endif}Check123);
74 Counter := 0;
75 E.Send(123);
76 AssertEquals(1, Counter);
77
78 E.RemoveNotification({$ifdef FPC}@{$endif}Check123);
79 Counter := 0;
80 E.Send(123);
81 AssertEquals(0, Counter);
82 finally FreeAndNil(E) end;
83 finally FreeAndNil(Scene) end;
84 end;
85
86 initialization
87 RegisterTest(TTestX3DFields);
88 end.