| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleDesignComponents" -*- |
| 2 |
{ |
| 3 |
Copyright 2022-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 TCastleDesign and TCastleTransformDesign classes. } |
| 18 |
unit TestCastleDesignComponents; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester, CastleClassUtils; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleDesignComponents = class(TCastleTestCase) |
| 27 |
strict private |
| 28 |
FreeNotificationHappened: Boolean; |
| 29 |
procedure FreeNotificationFlag(const Sender: TFreeNotificationObserver); |
| 30 |
published |
| 31 |
procedure TestUiFreesAllWhenUrlsChanged; |
| 32 |
procedure TestTransformFreesAllWhenUrlsChanged; |
| 33 |
end; |
| 34 |
|
| 35 |
implementation |
| 36 |
|
| 37 |
uses CastleTransform, CastleUIControls, CastleControls, CastleVectors, CastleScene; |
| 38 |
|
| 39 |
procedure TTestCastleDesignComponents.FreeNotificationFlag(const Sender: TFreeNotificationObserver); |
| 40 |
begin |
| 41 |
FreeNotificationHappened := true; |
| 42 |
end; |
| 43 |
|
| 44 |
procedure TTestCastleDesignComponents.TestUiFreesAllWhenUrlsChanged; |
| 45 |
var |
| 46 |
Own: TComponent; |
| 47 |
UiDes: TCastleDesign; |
| 48 |
Img: TCastleImageControl; |
| 49 |
ImgObserver: TFreeNotificationObserver; |
| 50 |
begin |
| 51 |
Own := TComponent.Create(nil); |
| 52 |
try |
| 53 |
UiDes := TCastleDesign.Create(Own); |
| 54 |
UiDes.Url := 'castle-data:/designs/test_free_children_of_ui.castle-user-interface'; |
| 55 |
AssertTrue(Own.FindComponent('ImageControl1') = nil); |
| 56 |
|
| 57 |
Img := UiDes.DesignedComponent('ImageControl1') as TCastleImageControl; |
| 58 |
AssertTrue(Img <> nil); |
| 59 |
// test deprecated methods that provide backward compat |
| 60 |
AssertTrue(Img = UiDes.FindComponent('ImageControl1')); |
| 61 |
AssertTrue(Img = UiDes.FindRequiredComponent('ImageControl1')); |
| 62 |
|
| 63 |
AssertVectorEquals(Vector4(1, 1, 1, 1), Img.Color); |
| 64 |
|
| 65 |
ImgObserver := TFreeNotificationObserver.Create(Own); |
| 66 |
ImgObserver.Observed := Img; |
| 67 |
ImgObserver.OnFreeNotification := {$ifdef FPC}@{$endif} FreeNotificationFlag; |
| 68 |
FreeNotificationHappened := false; |
| 69 |
|
| 70 |
UiDes.Url := 'castle-data:/designs/test_free_children_of_ui_2.castle-user-interface'; |
| 71 |
|
| 72 |
{ Changing Url should have freed all the previous loaded contents. } |
| 73 |
AssertTrue(FreeNotificationHappened); |
| 74 |
AssertTrue(ImgObserver.Observed = nil); |
| 75 |
|
| 76 |
{ new design has ImageControl1 with different color } |
| 77 |
Img := UiDes.DesignedComponent('ImageControl1') as TCastleImageControl; |
| 78 |
AssertTrue(Img <> nil); |
| 79 |
// test deprecated methods that provide backward compat |
| 80 |
AssertTrue(Img = UiDes.FindComponent('ImageControl1')); |
| 81 |
AssertTrue(Img = UiDes.FindRequiredComponent('ImageControl1')); |
| 82 |
|
| 83 |
AssertVectorEquals(Vector4(0, 0, 1, 1), Img.Color); |
| 84 |
finally FreeAndNil(Own) end; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TTestCastleDesignComponents.TestTransformFreesAllWhenUrlsChanged; |
| 88 |
var |
| 89 |
Own: TComponent; |
| 90 |
TrDes: TCastleTransformDesign; |
| 91 |
Cone: TCastleCone; |
| 92 |
ConeObserver: TFreeNotificationObserver; |
| 93 |
begin |
| 94 |
Own := TComponent.Create(nil); |
| 95 |
try |
| 96 |
TrDes := TCastleTransformDesign.Create(Own); |
| 97 |
TrDes.Url := 'castle-data:/designs/test_free_children_of_transform.castle-transform'; |
| 98 |
AssertTrue(Own.FindComponent('Cone1') = nil); |
| 99 |
|
| 100 |
Cone := TrDes.DesignedComponent('Cone1') as TCastleCone; |
| 101 |
AssertTrue(Cone <> nil); |
| 102 |
// test deprecated methods that provide backward compat |
| 103 |
AssertTrue(Cone = TrDes.FindComponent('Cone1')); |
| 104 |
AssertTrue(Cone = TrDes.FindRequiredComponent('Cone1')); |
| 105 |
|
| 106 |
AssertVectorEquals(Vector4(1, 1, 1, 1), Cone.Color); |
| 107 |
|
| 108 |
ConeObserver := TFreeNotificationObserver.Create(Own); |
| 109 |
ConeObserver.Observed := Cone; |
| 110 |
ConeObserver.OnFreeNotification := {$ifdef FPC}@{$endif} FreeNotificationFlag; |
| 111 |
FreeNotificationHappened := false; |
| 112 |
|
| 113 |
TrDes.Url := 'castle-data:/designs/test_free_children_of_transform_2.castle-transform'; |
| 114 |
|
| 115 |
{ Changing Url should have freed all the previous loaded contents. } |
| 116 |
AssertTrue(FreeNotificationHappened); |
| 117 |
AssertTrue(ConeObserver.Observed = nil); |
| 118 |
|
| 119 |
{ new design has Cone1 with different color } |
| 120 |
Cone := TrDes.DesignedComponent('Cone1') as TCastleCone; |
| 121 |
AssertTrue(Cone <> nil); |
| 122 |
// test deprecated methods that provide backward compat |
| 123 |
AssertTrue(Cone = TrDes.FindComponent('Cone1')); |
| 124 |
AssertTrue(Cone = TrDes.FindRequiredComponent('Cone1')); |
| 125 |
|
| 126 |
AssertVectorEquals(Vector4(0, 0, 1, 1), Cone.Color); |
| 127 |
finally FreeAndNil(Own) end; |
| 128 |
end; |
| 129 |
|
| 130 |
initialization |
| 131 |
RegisterTest(TTestCastleDesignComponents); |
| 132 |
end. |