File: testcastlescenecore.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 (1273 lines) | stat: -rw-r--r-- 40,945 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
// -*- compile-command: "./test_single_testcase.sh TTestSceneCore" -*-
{
  Copyright 2020-2023 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 CastleSceneCore unit. }
unit TestCastleSceneCore;

interface

uses
  Classes, SysUtils,
  CastleTester, CastleSceneCore, X3DNodes;

type
  TTestSceneCore = class(TCastleTestCase)
  strict private
    SearchingForDescription: string;
    WarningFlag: Boolean;
    function SearchingForDescriptionCallback(Node: TX3DNode): Pointer;
    procedure NodeMultipleTimesWarning(const Category, S: string);
    procedure OnWarningFlag(const Category, S: string);
    { Load scene and expect a warning during loading.
      Raises error if no warnings occurred. }
    procedure LoadSceneRequireWarning(const Scene: TCastleSceneCore; const Url: String);
  published
    procedure TestBorderManifoldEdges;
    procedure TestIterator;
    { $define ITERATOR_SPEED_TEST}
    {$ifdef ITERATOR_SPEED_TEST}
    procedure TestIteratorSpeed;
    {$endif ITERATOR_SPEED_TEST}
    procedure TestFind;
    procedure TestViewpointBillboardTricky;
    procedure TestManifold;
    procedure TestMultipleScenesOneNodeIncorrect;
    procedure TestMultipleScenesOneNodeCorrect;
    procedure TestSpineUtf8Names;
    procedure TestAnimsInSwitch;
    procedure TestLoadGzipped;
    procedure TestStarlingAndAnchors;
    procedure TestCocos;
    procedure TestImageAsNode;
    procedure TestPlayStopAnim;
    procedure TestFontStyleChanges;
    procedure TestUnassociate;
    procedure TestUnassociate2;
    procedure TestUnassociate3;
    procedure TestUnassociateChangeMaterialOneLeft;
    procedure TestUnassociateChangeMaterialSharedAppearance;
    procedure TestUnassociateChangeAppearance;
    procedure TestUnassociateAbstractPrimitive;
    procedure TestUnassociateAbstractPrimitiveSimplified;
    procedure TestNonGenericNode;
    {$ifdef GENERIC_METHODS}
    procedure TestGenericNode;
    {$endif}
    procedure TestNonGenericNodeAndFindNodeOptions;
    procedure TestGeometryNodesInShape;
    procedure TestExposedTransforms;
    //procedure TestInternalNodesReadOnly;
  end;

implementation

uses X3DLoad, CastleVectors, CastleShapes,
  CastleTimeUtils, CastleStringUtils, X3DFields, CastleViewport, CastleBoxes,
  CastleFilesUtils, CastleScene, CastleTransform, CastleApplicationProperties,
  CastleUriUtils, CastleColors;

procedure TTestSceneCore.TestBorderManifoldEdges;
var
  Scene: TCastleSceneCore;
  Manifold, Border: Cardinal;
begin
  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.Load('castle-data:/model_manifold.wrl');
    Scene.EdgesCount(Manifold, Border);
    AssertEquals(0, Border);
  finally FreeAndNil(Scene) end;
end;

{$ifdef ITERATOR_SPEED_TEST}
procedure TTestSceneCore.TestIteratorSpeed;

  procedure CheckIteratorSpeed(const FileName: string;
    const TestCount: Integer = 1000);
  var
    Scene: TCastleSceneCore;
    List: TShapesList;
    SI: TShapeTreeIterator;
    OnlyActive: boolean;
    I: Integer;
    Test: Integer;
  begin
    Writeln('CheckIteratorSpeed on ', FileName);

    Scene := TCastleSceneCore.Create(nil);
    try
      Scene.Load(FileName);
      for OnlyActive := false to true do
      begin
        ProcessTimerBegin;
        for Test := 0 to TestCount - 1 do
        begin
          List := TShapesList.Create(Scene.Shapes, OnlyActive);
          for I := 0 to List.Count - 1 do
            { Just do anything that requires access to List[I] }
            PointerToStr(List[I].Geometry);
          FreeAndNil(List);
        end;
        Writeln('TShapesList traverse: ', ProcessTimerEnd:1:2);

        ProcessTimerBegin;
        for Test := 0 to TestCount - 1 do
        begin
          SI := TShapeTreeIterator.Create(Scene.Shapes, OnlyActive);
          while SI.GetNext do
            PointerToStr(SI.Current.Geometry);
          FreeAndNil(SI);
        end;
        Writeln('TShapeTreeIterator: ', ProcessTimerEnd:1:2);

      end;
    finally FreeAndNil(Scene) end;
  end;

begin
  CheckIteratorSpeed('../../demo_models/x3d/deranged_house_final.x3dv');
  CheckIteratorSpeed('../../demo_models/x3d/anchor_test.x3dv');
  CheckIteratorSpeed('../../demo_models/x3d/switches_and_transforms.x3dv');
  CheckIteratorSpeed('../../demo_models/x3d/key_sensor.x3dv');

  CheckIteratorSpeed('castle-data:/switches_and_transforms_2.x3dv');
  CheckIteratorSpeed('castle-data:/key_sensor_2.x3dv');

  CheckIteratorSpeed('/home/michalis/sources/rrtankticks2/rrtankticks3/rrtt.wrl',
    10); { smaller TestCount, as it's quite slow }
end;
{$endif ITERATOR_SPEED_TEST}

procedure TTestSceneCore.TestIterator;

  procedure CheckIterator(const FileName: string);
  var
    Scene: TCastleSceneCore;
    List: TShapeList;
    SI: TShapeTreeIterator;
    OnlyActive: boolean;
    I: Integer;
  begin
    Scene := TCastleSceneCore.Create(nil);
    try
      Scene.Load(FileName);
      for OnlyActive := false to true do
      begin
        { Compare the simple iterator implementation (that just calls
          Traverse and gathers results to the list) with actual sophisticated
          implementation in TShapeTreeIterator. }
        List := TShapeList.Create(Scene.Shapes, OnlyActive);
        SI := TShapeTreeIterator.Create(Scene.Shapes, OnlyActive);
        for I := 0 to List.Count - 1 do
        begin
          AssertTrue('SI.GetNext', SI.GetNext);
          AssertTrue('SI.Current', SI.Current = List[I]);
        end;
        AssertTrue('not SI.GetNext', not SI.GetNext);

//        writeln('done for ', FileName, ' active: ', OnlyActive, ' count is ', List.Count);

        FreeAndNil(List);
        FreeAndNil(SI);
      end;
    finally FreeAndNil(Scene) end;
  end;

begin
  CheckIterator('castle-data:/demo-models-copy/deranged_house_final.x3dv');
  CheckIterator('castle-data:/demo-models-copy/anchor_test.x3dv');
  CheckIterator('castle-data:/switches_and_transforms_2.x3dv');
  CheckIterator('castle-data:/key_sensor_2.x3dv');
  CheckIterator('castle-data:/extrusion_empty_spine.x3dv');
  CheckIterator('castle-data:/extrusion_empty_spine_concave.x3dv');
  CheckIterator('castle-data:/extrusion_empty_spine_smooth.x3dv');

  // This once failed to be read with FPC 3.1.1
  CheckIterator('castle-data:/spine/escape_from_the_universe_boss/boss.json');

  // This once failed to be read, as the Spine has DefaultSkin = nil
  CheckIterator('castle-data:/spine/empty_spine.json');

  // This once failed with access violation because TClippingAttachment.BuildNodes didn't assign Material
  CheckIterator('castle-data:/spine/clip_region/skeleton.json');
end;

procedure TTestSceneCore.TestFind;
var
  Scene: TCastleSceneCore;
begin
  Scene := TCastleSceneCore.Create(nil);
  try
    try
      Scene.Node('Left');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Field('Left', 'translation');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Event('Left', 'addChildren');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    Scene.URL := 'castle-data:/switches_and_transforms_2.x3dv';
    Scene.Node('Left');
    Scene.Field('Left', 'translation');
    Scene.Event('Left', 'addChildren');

    try
      Scene.Node('Blah');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Field('Blah', 'translation');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Event('Blah', 'addChildren');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Field('Left', 'blah');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;

    try
      Scene.Event('Left', 'blah');
      Fail('Should fail with EX3DNotFound');
    except on EX3DNotFound do ; end;
  finally FreeAndNil(Scene) end;
end;

function TTestSceneCore.SearchingForDescriptionCallback(Node: TX3DNode): Pointer;
begin
  if (Node is TAbstractViewpointNode) and
     (TAbstractViewpointNode(Node).SmartDescription = SearchingForDescription) then
    Result := Node else
    Result := nil;
end;

procedure TTestSceneCore.TestViewpointBillboardTricky;
{ Unfortunately, it cannot reproduce bug #38 for some reason...
  Probably it misses something that triggers it interactively. }
var
  Scene: TCastleScene;

  function FindViewpointByDescription(const Description: string): TAbstractViewpointNode;
  begin
    SearchingForDescription := Description;
    AssertTrue(Scene.RootNode <> nil);
    Result := TAbstractViewpointNode(
      Scene.RootNode.SearchNodes({$ifdef FPC}@{$endif}SearchingForDescriptionCallback, true));
    AssertTrue(Result <> nil);
  end;

var
  Viewpoint: TAbstractViewpointNode;
  Viewport: TCastleViewport;
  FakeRemoveMe: TRemoveType;
begin
  Viewport := TCastleSceneManager.Create(nil);
  try
    Viewport.FullSize := true;
    Viewport.AutoCamera := true;
    Scene := TCastleScene.Create(nil);
    try
      Scene.URL := 'castle-data:/city_from_bugreport_38.x3dv';
      Scene.ProcessEvents := true;
      Scene.PreciseCollisions := true;
      Viewport.Items.Add(Scene);
      Viewport.Items.MainScene := Scene;

      Scene.InternalCameraChanged;
      FakeRemoveMe := rtNone;
      Scene.Update(1, FakeRemoveMe);

      Viewpoint := FindViewpointByDescription('City plan');
      Viewpoint.Bound := true;

      Scene.InternalCameraChanged;
      FakeRemoveMe := rtNone;
      Scene.Update(1, FakeRemoveMe);
    finally FreeAndNil(Scene) end;
  finally FreeAndNil(Viewport) end;
end;

procedure TTestSceneCore.TestManifold;
var
  Scene: TCastleSceneCore;
  ManifoldEdges, BorderEdges: Cardinal;
begin
  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.URL := 'castle-data:/shadow_volumes_tests/should_be_manifold.x3d';
    Scene.EdgesCount(ManifoldEdges, BorderEdges);
    AssertEquals(0, BorderEdges);
    AssertEquals(210, ManifoldEdges);
  finally FreeAndNil(Scene) end;
end;

type
  ENodeMultipleTimes = class(Exception);

procedure TTestSceneCore.NodeMultipleTimesWarning(const Category, S: string);
begin
  if Pos('is already part of another TCastleScene instance', S) <> 0 then
    raise ENodeMultipleTimes.Create('We want this warning, good: ' + S)
  else
    raise Exception.Create('Some invalid warning: ' + S);
end;

procedure TTestSceneCore.TestMultipleScenesOneNodeIncorrect;

{ Deliberately do something incorrect: place the same TX3DRootNode
  in multiple TCastleScene instances.
  Check that we make a warning about it. }

var
  Node: TX3DRootNode;
  Scene1, Scene2: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}NodeMultipleTimesWarning);
  try
    try
      Node := nil;
      Scene1 := nil;
      Scene2 := nil;
      try
        Node := LoadNode('castle-data:/game/scene.x3d');
        Scene1 := TCastleScene.Create(nil);
        Scene2 := TCastleScene.Create(nil);
        Scene1.Load(Node, false);
        Scene2.Load(Node, false);
        raise Exception.Create('We should not get here, expected ENodeMultipleTimes on the way');
      finally
        FreeAndNil(Scene1);
        FreeAndNil(Scene2);
        // Note: you must free Node after freeing Scene1,2
        FreeAndNil(Node);
      end;
    except
      on ENodeMultipleTimes do ; { good, silence this for the sake of test }
    end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}NodeMultipleTimesWarning);
  end;
end;

procedure TTestSceneCore.TestMultipleScenesOneNodeCorrect;
var
  Node: TX3DRootNode;
  Scene1, Scene2, SceneTemplate: TCastleScene;
begin
  { When using InternalNodesReadOnly (no longer available),
    it is allowed to have the same TX3DRootNode reused: }
  (*

  Node := nil;
  Scene1 := nil;
  Scene2 := nil;
  try
    Node := LoadNode('castle-data:/game/scene.x3d');
    Scene1 := TCastleScene.Create(nil);
    Scene1.InternalNodesReadOnly := true;
    Scene2 := TCastleScene.Create(nil);
    Scene2.InternalNodesReadOnly := true;
    Scene1.Load(Node, false);
    Scene2.Load(Node, false);
  finally
    FreeAndNil(Scene1);
    FreeAndNil(Scene2);
    // Note: you must free Node after freeing Scene1,2
    FreeAndNil(Node);
  end;
  *)

  // Using DeepCopy you can also share node, without InternalNodesReadOnly

  Node := nil;
  Scene1 := nil;
  Scene2 := nil;
  try
    Node := LoadNode('castle-data:/game/scene.x3d');
    Scene1 := TCastleScene.Create(nil);
    Scene2 := TCastleScene.Create(nil);
    Scene1.Load(Node.DeepCopy as TX3DRootNode, true);
    Scene2.Load(Node.DeepCopy as TX3DRootNode, true);
  finally
    FreeAndNil(Node);
    FreeAndNil(Scene1);
    FreeAndNil(Scene2);
  end;

  // Using Clone you can also share node, without InternalNodesReadOnly

  SceneTemplate := nil;
  Scene1 := nil;
  Scene2 := nil;
  try
    SceneTemplate := TCastleScene.Create(nil);
    SceneTemplate.Load('castle-data:/game/scene.x3d');
    Scene1 := SceneTemplate.Clone(nil);
    Scene2 := SceneTemplate.Clone(nil);
  finally
    FreeAndNil(SceneTemplate);
    FreeAndNil(Scene1);
    FreeAndNil(Scene2);
  end;
end;

procedure TTestSceneCore.TestSpineUtf8Names;
var
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      Scene.Load('castle-data:/spine/uhholy_chapter_one_end/skeleton.json');
      Scene.Load('castle-data:/spine/unholy_transition/phone.json');
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestAnimsInSwitch;
var
  Scene: TCastleScene;
  Anims: TStrings;
begin
  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/animation_under_switch/both.x3dv');
    Anims := Scene.AnimationsList;
    AssertTrue(Anims.IndexOf('Attack') <> -1);
    AssertTrue(Anims.IndexOf('Damaged') <> -1);
    AssertTrue(Anims.IndexOf('flying') <> -1);
    AssertTrue(Anims.IndexOf('flying_left') <> -1);
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestLoadGzipped;

  procedure TestSphere(const Url: String);
  var
    Scene: TCastleScene;
    Shape: TShapeNode;
    Sphere: TSphereNode;
  begin
    Scene := TCastleScene.Create(nil);
    try
      Scene.Load(Url);
      AssertEquals(1, Scene.RootNode.FdChildren.Count);
      AssertTrue(Scene.RootNode.FdChildren[0] is TShapeNode);
      Shape := Scene.RootNode.FdChildren[0] as TShapeNode;
      AssertTrue(Shape.Geometry is TSphereNode);
      Sphere := Shape.Geometry as TSphereNode;
      { Special epsilon necessary to pass test on Raspberry Pi (Linux/Arm). }
      AssertSameValue(123.456, Sphere.Radius, 0.0001);
    finally FreeAndNil(Scene) end;
  end;

begin
  TestSphere('castle-data:/gzipped_x3d/sphere.wrl');
  TestSphere('castle-data:/gzipped_x3d/sphere.wrl.gz');
  TestSphere('castle-data:/gzipped_x3d/sphere.wrz');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3d');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3d.gz');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3dv');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3dv.gz');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3dvz');
  TestSphere('castle-data:/gzipped_x3d/sphere.x3dz');
  TestSphere('castle-data:/gzipped_x3d/sphere_normal_extension_but_gzipped.wrl');
  TestSphere('castle-data:/gzipped_x3d/sphere_normal_extension_but_gzipped.x3dv');
end;

procedure TTestSceneCore.OnWarningFlag(const Category, S: string);
begin
  WarningFlag := true;
end;

procedure TTestSceneCore.LoadSceneRequireWarning(const Scene: TCastleSceneCore; const Url: String);
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningFlag);
  try
    WarningFlag := false;
    Scene.Load(Url);
    if not WarningFlag then
      Fail(Format('Expected to get some warning during loading of "%s"',
        [UriDisplay(Url)]));
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningFlag);
  end;
end;

procedure TTestSceneCore.TestStarlingAndAnchors;
var
  Scene: TCastleScene;
begin
  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml');
    AssertEquals(45, Scene.AnimationsList.Count);
    AssertEquals(-1, Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#anim-naming:strict-underscore');
    AssertEquals(45, Scene.AnimationsList.Count);
    AssertEquals(-1, Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#fps:10,anim-naming:strict-underscore');
    AssertEquals(45, Scene.AnimationsList.Count);
    AssertEquals(-1, Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#anim-naming:trailing-number');
    AssertEquals(31, Scene.AnimationsList.Count);
    AssertTrue(-1 <> Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#fps:10,anim-naming:trailing-number');
    AssertEquals(31, Scene.AnimationsList.Count);
    AssertTrue(-1 <> Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    LoadSceneRequireWarning(Scene, 'castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#invalid-anchor,fps:10,anim-naming:trailing-number');
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    LoadSceneRequireWarning(Scene, 'castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#invalid-anchor:blahblah,fps:10,anim-naming:trailing-number');
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    LoadSceneRequireWarning(Scene, 'castle-data:/sprite-sheets/starling_kenney_robot/character_robot_sheet.starling-xml#fps:10,anim-naming:invalid-name');
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestCocos;
var
  Scene: TCastleScene;
begin
{$IFNDEF CPUARMEL}
  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/cocos2d_wolf/wolf.plist');
    AssertEquals(4, Scene.AnimationsList.Count);
    AssertTrue(-1 <> Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/cocos2d_wolf/wolf.plist#fps:10');
    AssertEquals(4, Scene.AnimationsList.Count);
    AssertTrue(-1 <> Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    LoadSceneRequireWarning(Scene, 'castle-data:/sprite-sheets/cocos2d_wolf/wolf.plist#fps:10,anim-naming:trailing-number');
    AssertEquals(4, Scene.AnimationsList.Count);
    AssertTrue(-1 <> Scene.AnimationsList.IndexOf('walk'));
  finally FreeAndNil(Scene) end;
{$ENDIF CPUARMEL}
end;

procedure TTestSceneCore.TestImageAsNode;
var
  Scene: TCastleScene;
begin
  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/cocos2d_wolf/wolf.png');
    AssertEquals(0, Scene.AnimationsList.Count);
  finally FreeAndNil(Scene) end;

  Scene := TCastleScene.Create(nil);
  try
    Scene.Load('castle-data:/sprite-sheets/cocos2d_wolf/wolf.png#left:100,bottom:100,width:256,height:256');
    AssertEquals(0, Scene.AnimationsList.Count);
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestPlayStopAnim;
var
  Scene: TCastleScene;
begin
  Scene := TCastleScene.Create(nil);
  try
    Scene.ProcessEvents := true;
    Scene.Load('castle-data:/spine/escape_from_the_universe_boss/boss.json');
    Scene.Exists := false; // doesn't matter for animation playing

    Scene.PlayAnimation('flying', true);
    AssertTrue(Scene.CurrentAnimation <> nil);
    AssertEquals('flying', Scene.CurrentAnimation.X3DName);

    Scene.StopAnimation;
    AssertTrue(Scene.CurrentAnimation = nil);
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestFontStyleChanges;
var
  FontStyleNode: TFontStyleNode;
  TextNode: TTextNode;
  TextShape: TShapeNode;
  RootNode: TX3DRootNode;
  Scene: TCastleScene;
  Box: TBox3D;
begin
  FontStyleNode := TFontStyleNode.Create;

  TextNode := TTextNode.CreateWithShape(TextShape);
  TextNode.FontStyle := FontStyleNode;
  TextNode.SetString(['one line of text']);

  RootNode := TX3DRootNode.Create;
  RootNode.AddChildren(TextShape);

  Scene := TCastleScene.Create(nil);
  try
    // Scene.ProcessEvents := true; // this should work even without process events, TCastleText depends on it
    Scene.Load(RootNode, true);

    Box := Scene.BoundingBox;
    //Writeln(Box.ToString);
    AssertSameValue(1, Box.Size.Y);

    FontStyleNode.Size := 10;
    Box := Scene.BoundingBox;
    //Writeln(Box.ToString);
    AssertSameValue(10, Box.Size.Y);

    FontStyleNode.Size := 100;
    Box := Scene.BoundingBox;
    //Writeln(Box.ToString);
    AssertSameValue(100, Box.Size.Y);

  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestUnassociate;
var
  Box: TCastleBox;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Box := TCastleBox.Create(nil);
    try
      Box.Material := pmUnlit;
      Box.Material := pmPhysical;
    finally FreeAndNil(Box) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociate2;
var
  Shape: TShapeNode;
  NewMaterial: TMaterialNode;
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      Scene.Load('castle-data:/test_for_material_change.x3dv');
      Scene.PreciseCollisions := true;
      Scene.ProcessEvents := true;

      Shape := Scene.Node('MyShape') as TShapeNode;

      NewMaterial := TMaterialNode.Create;
      NewMaterial.Scene := Scene;
      NewMaterial.DiffuseColor := Vector3(0, 1, 1);
      Shape.Material := NewMaterial; // using deprecated Shape.Material, TestUnassociate3 will test undeprecated way
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociate3;
var
  Shape: TShapeNode;
  Appearance: TAppearanceNode;
  NewMaterial: TMaterialNode;
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      Scene.Load('castle-data:/test_for_material_change.x3dv');
      Scene.PreciseCollisions := true;
      Scene.ProcessEvents := true;

      Shape := Scene.Node('MyShape') as TShapeNode;

      NewMaterial := TMaterialNode.Create;
      NewMaterial.Scene := Scene;
      NewMaterial.DiffuseColor := Vector3(0, 1, 1);

      Appearance := TAppearanceNode.Create;
      Appearance.Material := NewMaterial;

      Shape.Appearance := Appearance;
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociateChangeMaterialOneLeft;
var
  RootNode: TX3DRootNode;
  Shape1, Shape2: TShapeNode;
  OldMaterial: TMaterialNode;
  NewMaterial: TUnlitMaterialNode;
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      RootNode := TX3DRootNode.Create;

      OldMaterial := TMaterialNode.Create;

      Shape1 := TShapeNode.Create;
      Shape1.Material := OldMaterial; // will also auto-create Shape.Appearance, OK
      Shape1.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      RootNode.AddChildren(Shape1);

      Shape2 := TShapeNode.Create;
      Shape2.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      Shape2.Material := OldMaterial; // will also auto-create Shape.Appearance, OK
      RootNode.AddChildren(Shape2);

      Scene.Load(RootNode, true);
      Scene.PreciseCollisions := true;
      Scene.ProcessEvents := true;

      NewMaterial := TUnlitMaterialNode.Create;

      AssertTrue(OldMaterial.Scene <> nil);
      AssertTrue(NewMaterial.Scene = nil);
      AssertEquals(2, TShapeTree.AssociatedShapesCount(OldMaterial));
      AssertEquals(0, TShapeTree.AssociatedShapesCount(NewMaterial));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(Shape1.Appearance));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(Shape2.Appearance));

      // NewMaterial.Scene := Scene; // should work with and without this
      Shape1.Material := NewMaterial; // but leave Shape2 unchanged

      AssertEquals(1, TShapeTree.AssociatedShapesCount(OldMaterial));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(NewMaterial));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(Shape1.Appearance));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(Shape2.Appearance));
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociateChangeMaterialSharedAppearance;
var
  RootNode: TX3DRootNode;
  Shape1, Shape2: TShapeNode;
  OldMaterial: TMaterialNode;
  NewMaterial: TUnlitMaterialNode;
  SharedAppearance: TAppearanceNode;
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      RootNode := TX3DRootNode.Create;

      OldMaterial := TMaterialNode.Create;
      OldMaterial.KeepExistingBegin; // otherwise it would be destroyed by assigning other node

      SharedAppearance := TAppearanceNode.Create;
      SharedAppearance.Material := OldMaterial;

      Shape1 := TShapeNode.Create;
      Shape1.Appearance := SharedAppearance;
      Shape1.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      RootNode.AddChildren(Shape1);

      Shape2 := TShapeNode.Create;
      Shape2.Appearance := SharedAppearance;
      Shape2.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      RootNode.AddChildren(Shape2);

      Scene.Load(RootNode, true);
      Scene.PreciseCollisions := true;
      Scene.ProcessEvents := true;

      NewMaterial := TUnlitMaterialNode.Create;

      AssertTrue(SharedAppearance.Scene <> nil);
      AssertTrue(OldMaterial.Scene <> nil);
      AssertTrue(NewMaterial.Scene = nil);
      AssertEquals(2, TShapeTree.AssociatedShapesCount(SharedAppearance));
      AssertEquals(2, TShapeTree.AssociatedShapesCount(OldMaterial));
      AssertEquals(0, TShapeTree.AssociatedShapesCount(NewMaterial));

      // NewMaterial.Scene := Scene; // should work with and without this
      Shape1.Material := NewMaterial; // equivalent to SharedAppearance.Material := NewMaterial;
      //SharedAppearance.Material := NewMaterial;

      AssertEquals(2, TShapeTree.AssociatedShapesCount(SharedAppearance));
      AssertEquals(0, TShapeTree.AssociatedShapesCount(OldMaterial));
      AssertEquals(2, TShapeTree.AssociatedShapesCount(NewMaterial));

      // OldMaterial now unused
      OldMaterial.KeepExistingEnd;
      FreeIfUnusedAndNil(OldMaterial);
      AssertTrue(OldMaterial = nil);
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociateChangeAppearance;
var
  RootNode: TX3DRootNode;
  Shape1, Shape2: TShapeNode;
  OldAppearance: TAppearanceNode;
  NewAppearance: TAppearanceNode;
  Scene: TCastleScene;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      RootNode := TX3DRootNode.Create;

      OldAppearance := TAppearanceNode.Create;
      OldAppearance.Material := TMaterialNode.Create;

      NewAppearance := TAppearanceNode.Create;
      NewAppearance.Material := TMaterialNode.Create;

      Shape1 := TShapeNode.Create;
      Shape1.Appearance := OldAppearance;
      Shape1.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      RootNode.AddChildren(Shape1);

      Shape2 := TShapeNode.Create;
      Shape2.Appearance := OldAppearance;
      Shape2.Geometry := TBoxNode.Create; // anything non-nil, otherwise it's ignored in Scene.Shapes tree
      RootNode.AddChildren(Shape2);

      Scene.Load(RootNode, true);
      Scene.PreciseCollisions := true;
      Scene.ProcessEvents := true;

      AssertTrue(OldAppearance.Scene <> nil);
      AssertTrue(NewAppearance.Scene = nil);
      AssertEquals(2, TShapeTree.AssociatedShapesCount(OldAppearance));
      AssertEquals(0, TShapeTree.AssociatedShapesCount(NewAppearance));
      AssertEquals(2, TShapeTree.AssociatedShapesCount(OldAppearance.Material));
      AssertEquals(0, TShapeTree.AssociatedShapesCount(NewAppearance.Material));

      Shape1.Appearance := NewAppearance; // but leave Shape2 unchanged

      AssertEquals(1, TShapeTree.AssociatedShapesCount(OldAppearance));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(NewAppearance));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(OldAppearance.Material));
      AssertEquals(1, TShapeTree.AssociatedShapesCount(NewAppearance.Material));
    finally FreeAndNil(Scene) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

type
  { Testcase from https://forum.castle-engine.io/t/warning-calling-tglsceneshape-unassociatenode/713 }
  TMyClass = class(TCastleAbstractPrimitive)
  private
    FGeometry: TIndexedTriangleSetNode;
    FCoordinate: TCoordinateNode;
    FTextureCoordinate: TTextureCoordinateNode;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TMyClass.Create(AOwner: TComponent);
begin
  inherited;
  Texture := 'castle-data:/images/no_alpha.png';
  Material := pmUnlit;
  FGeometry := TIndexedTriangleSetNode.Create;
  FCoordinate := TCoordinateNode.Create;
  FTextureCoordinate := TTextureCoordinateNode.Create;
  FGeometry.Coord := FCoordinate;
  ShapeNode.Geometry := FGeometry;
  FCoordinate.SetPoint([
    Vector3(0  ,   0, 0),
    Vector3(100,   0, 0),
    Vector3(100, 100, 0),
    Vector3(0  , 100, 0),
    Vector3(0  ,   0, 0),
    Vector3(100, 100, 0)
  ]);
  FTextureCoordinate.SetPoint([
    Vector2(0, 0),
    Vector2(1, 0),
    Vector2(1, 1),
    Vector2(0, 1),
    Vector2(0, 0),
    Vector2(1, 1)
  ]);
  FGeometry.SetIndex([0, 1, 2, 3, 4, 5]);
  FGeometry.TexCoord := FTextureCoordinate;
end;

procedure TTestSceneCore.TestUnassociateAbstractPrimitive;
var
  MyObject: TMyClass;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    MyObject := TMyClass.Create(nil);
    try
    finally FreeAndNil(MyObject) end;
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestUnassociateAbstractPrimitiveSimplified;
var
  Scene: TCastleScene;
  ShapeNode: TShapeNode;
  RootNode: TX3DRootNode;
  Geometry: TIndexedTriangleSetNode;
  TextureCoordinate: TTextureCoordinateNode;
begin
  ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
  try
    Scene := TCastleScene.Create(nil);
    try
      ShapeNode := TShapeNode.Create;
      RootNode := TX3DRootNode.Create;
      RootNode.AddChildren(ShapeNode);
      Scene.Load(RootNode, true);

      Geometry := TIndexedTriangleSetNode.Create;
      TextureCoordinate := TTextureCoordinateNode.Create;
      ShapeNode.Geometry := Geometry;
      Geometry.TexCoord := TextureCoordinate;
    finally FreeAndNil(Scene) end
  finally
    ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
  end;
end;

procedure TTestSceneCore.TestNonGenericNode;
var
  Mat: TPhysicalMaterialNode;
  Appearance: TAppearanceNode;
  Shape: TShapeNode;
  Box: TBoxNode;
  Switch: TSwitchNode;
  RootNode: TX3DRootNode;
  Scene: TCastleSceneCore;
begin
  Mat := TPhysicalMaterialNode.Create('Foo');
  Mat.BaseColor := RedRGB;

  Appearance := TAppearanceNode.Create('Foo');
  Appearance.Material := Mat;

  Box := TBoxNode.CreateWithShape(Shape);
  Box.X3DName := 'Foo';
  Box.Size := Vector3(1, 2, 3);
  Shape.X3DName := 'Foo';
  Shape.Appearance := Appearance;

  Switch := TSwitchNode.Create;
  Switch.WhichChoice := -1; // Shape is inactive, but it doesn't matter for Find
  Switch.AddChildren(Shape);

  RootNode := TX3DRootNode.Create;
  RootNode.AddChildren(Switch);

  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.Load(RootNode, true);

    AssertTrue(Scene.Node(TX3DNode, 'Foo') <> nil); // undefined which node it will be

    AssertTrue((Scene.Node(TPhysicalMaterialNode, 'Foo') as TPhysicalMaterialNode) <> nil);
    AssertVectorEquals(RedRGB, (Scene.Node(TPhysicalMaterialNode, 'Foo') as TPhysicalMaterialNode).BaseColor);

    AssertTrue((Scene.Node(TBoxNode, 'Foo') as TBoxNode) <> nil);
    AssertVectorEquals(Vector3(1, 2, 3), (Scene.Node(TBoxNode, 'Foo') as TBoxNode).Size);
  finally FreeAndNil(Scene) end;
end;

{$ifdef GENERIC_METHODS}

procedure TTestSceneCore.TestGenericNode;
var
  Mat: TPhysicalMaterialNode;
  Appearance: TAppearanceNode;
  Shape: TShapeNode;
  Box: TBoxNode;
  Switch: TSwitchNode;
  RootNode: TX3DRootNode;
  Scene: TCastleSceneCore;
begin
  Mat := TPhysicalMaterialNode.Create('Foo');
  Mat.BaseColor := RedRGB;

  Appearance := TAppearanceNode.Create('Foo');
  Appearance.Material := Mat;

  Box := TBoxNode.CreateWithShape(Shape);
  Box.X3DName := 'Foo';
  Box.Size := Vector3(1, 2, 3);
  Shape.X3DName := 'Foo';
  Shape.Appearance := Appearance;

  Switch := TSwitchNode.Create;
  Switch.WhichChoice := -1; // Shape is inactive, but it doesn't matter for Find
  Switch.AddChildren(Shape);

  RootNode := TX3DRootNode.Create;
  RootNode.AddChildren(Switch);

  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.Load(RootNode, true);

    AssertTrue(Scene.{$ifdef FPC}specialize{$endif} Node<TX3DNode>('Foo') <> nil); // undefined which node it will be

    AssertTrue(Scene.{$ifdef FPC}specialize{$endif} Node<TPhysicalMaterialNode>('Foo') <> nil);
    AssertVectorEquals(RedRGB, Scene.{$ifdef FPC}specialize{$endif} Node<TPhysicalMaterialNode>('Foo').BaseColor);

    AssertTrue(Scene.{$ifdef FPC}specialize{$endif} Node<TBoxNode>('Foo') <> nil);
    AssertVectorEquals(Vector3(1, 2, 3), Scene.{$ifdef FPC}specialize{$endif} Node<TBoxNode>('Foo').Size);
  finally FreeAndNil(Scene) end;
end;

{$endif GENERIC_METHODS}

procedure TTestSceneCore.TestNonGenericNodeAndFindNodeOptions;
var
  Mat: TPhysicalMaterialNode;
  Appearance: TAppearanceNode;
  Shape: TShapeNode;
  Box: TBoxNode;
  Switch: TSwitchNode;
  RootNode: TX3DRootNode;
  Scene: TCastleSceneCore;
begin
  Mat := TPhysicalMaterialNode.Create('M');
  Mat.BaseColor := RedRGB;

  Appearance := TAppearanceNode.Create('A');
  Appearance.Material := Mat;

  Box := TBoxNode.CreateWithShape(Shape);
  Box.X3DName := 'B';
  Box.Size := Vector3(1, 2, 3);
  Shape.X3DName := 'S';
  Shape.Appearance := Appearance;

  Switch := TSwitchNode.Create;
  Switch.WhichChoice := -1; // Shape is inactive, but it doesn't matter for Find
  Switch.AddChildren(Shape);

  RootNode := TX3DRootNode.Create;
  RootNode.AddChildren(Switch);

  Scene := TCastleSceneCore.Create(nil);
  try
    { Test with Scene.RootNode = nil }
    AssertTrue(Scene.RootNode = nil);

    try
      Scene.Node(TX3DNode, 'Foo');
      Fail('This should have raised exception');
    except on E: EX3DNotFound do { valid response }; end;

    AssertTrue(Scene.Node(TX3DNode, 'Foo', [fnNilOnMissing]) = nil);

    { Test with Scene.RootNode <> nil }
    Scene.Load(RootNode, true);
    AssertTrue(Scene.RootNode <> nil);

    try
      Scene.Node(TX3DNode, 'Foo');
      Fail('This should have raised exception');
    except on E: EX3DNotFound do { valid response }; end;

    AssertTrue(Scene.Node(TX3DNode, 'Foo', [fnNilOnMissing]) = nil);

    try
      Scene.RootNode.FindNode(TX3DNode, 'Foo');
      Fail('This should have raised exception');
    except on E: EX3DNotFound do { valid response }; end;

    AssertTrue(Scene.RootNode.FindNode(TX3DNode, 'Foo', [fnNilOnMissing]) = nil);

    { Test fnOnlyActive: box is only in active subgraph }
    try
      Scene.RootNode.FindNode(TBoxNode, 'B', [fnOnlyActive]);
      Fail('This should have raised exception');
    except on E: EX3DNotFound do { valid response }; end;
    AssertTrue(Scene.RootNode.FindNode(TBoxNode, 'B', [fnOnlyActive, fnNilOnMissing]) = nil);
    AssertTrue(Scene.RootNode.FindNode(TBoxNode, 'B', []) <> nil);
    AssertTrue(Scene.RootNode.FindNode(TBoxNode, 'B', [fnNilOnMissing]) <> nil);
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestGeometryNodesInShape;
var
  Scene: TCastleSceneCore;
begin
  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.Url := 'castle-data:/geometry_not_in_shape.x3dv';
  finally FreeAndNil(Scene) end;
end;

procedure TTestSceneCore.TestExposedTransforms;

{ See also what testcase from https://github.com/castle-engine/castle-engine/issues/600
  is doing. }

var
  Scene: TCastleSceneCore;
  T1, T2: TCastleTransform;
begin
  Scene := TCastleSceneCore.Create(nil);
  try
    Scene.Load('castle-data:/exposed_transforms_names.x3dv');

    T1 := TCastleTransform.Create(nil);
    T1.Name := 'MyBoneName_123';
    Scene.Add(T1);

    T2 := TCastleTransform.Create(nil);
    // Pascal component names must match bone names, but with invalid chars -> underscores
    T2.Name := 'MyBoneName_with_spaces';
    Scene.Add(T2);

    AssertEquals(2, Scene.Count);

    Scene.ExposeTransforms.AddStrings([
      'MyBoneName_123',
      // here we provide the bone name, not the Pascal component name, so spaces are OK
      'MyBoneName with spaces'
    ]);

    // Make sure TCastleSceneCore.ExposeTransformsChange didn't create any new components
    AssertEquals(2, Scene.Count);
  finally
    FreeAndNil(Scene);
    FreeAndNil(T1);
    FreeAndNil(T2);
  end;
end;

(* InternalNodesReadOnly no longer available,
  it was very cumbersome to implement.
  Sharing nodes is just not possible across scenes now.

procedure TTestSceneCore.TestInternalNodesReadOnly;
var
  RootNode: TX3DRootNode;
  SomeShape: TShapeNode;
  SomeGeometry: TSphereNode;
  SceneNormal, SceneReadOnly: TCastleSceneCore;
begin
  // create simple X3D nodes graph
  RootNode := TX3DRootNode.Create;
  SomeGeometry := TSphereNode.Create;
  SomeShape := TShapeNode.Create;
  SomeShape.Geometry := SomeGeometry;
  RootNode.AddChildren(SomeShape);

  AssertTrue(RootNode.Scene = nil);
  AssertTrue(SomeGeometry.Scene = nil);
  AssertTrue(SomeShape.Scene = nil);

  SceneNormal := TCastleSceneCore.Create(nil);
  try
    SceneNormal.Load(RootNode, true);
    SceneNormal.ProcessEvents := true;

    AssertTrue(RootNode.Scene = SceneNormal);
    AssertTrue(SomeGeometry.Scene = SceneNormal);
    AssertTrue(SomeShape.Scene = SceneNormal);

    SceneReadOnly := TCastleSceneCore.Create(nil);
    SceneReadOnly.InternalNodesReadOnly := true;
    SceneReadOnly.Load(RootNode, true);

    // nodes are still associated with SceneNormal, not SceneReadOnly
    AssertTrue(RootNode.Scene = SceneNormal);
    AssertTrue(SomeGeometry.Scene = SceneNormal);
    AssertTrue(SomeShape.Scene = SceneNormal);

    FreeAndNil(SceneReadOnly);

    // nodes are still associated with SceneNormal, not SceneReadOnly. not nil
    AssertTrue(RootNode.Scene = SceneNormal);
    AssertTrue(SomeGeometry.Scene = SceneNormal);
    AssertTrue(SomeShape.Scene = SceneNormal);
  finally
    FreeAndNil(SceneNormal);
  end;

  AssertTrue(RootNode.Scene = nil);
  AssertTrue(SomeGeometry.Scene = nil);
  AssertTrue(SomeShape.Scene = nil);
end;
*)

initialization
  RegisterTest(TTestSceneCore);
end.