File: testcastledesigncomponents.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 (132 lines) | stat: -rw-r--r-- 4,685 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
// -*- compile-command: "./test_single_testcase.sh TTestCastleDesignComponents" -*-
{
  Copyright 2022-2022 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 TCastleDesign and TCastleTransformDesign classes. }
unit TestCastleDesignComponents;

interface

uses
  Classes, SysUtils, CastleTester, CastleClassUtils;

type
  TTestCastleDesignComponents = class(TCastleTestCase)
  strict private
    FreeNotificationHappened: Boolean;
    procedure FreeNotificationFlag(const Sender: TFreeNotificationObserver);
  published
    procedure TestUiFreesAllWhenUrlsChanged;
    procedure TestTransformFreesAllWhenUrlsChanged;
  end;

implementation

uses CastleTransform, CastleUIControls, CastleControls, CastleVectors, CastleScene;

procedure TTestCastleDesignComponents.FreeNotificationFlag(const Sender: TFreeNotificationObserver);
begin
  FreeNotificationHappened := true;
end;

procedure TTestCastleDesignComponents.TestUiFreesAllWhenUrlsChanged;
var
  Own: TComponent;
  UiDes: TCastleDesign;
  Img: TCastleImageControl;
  ImgObserver: TFreeNotificationObserver;
begin
  Own := TComponent.Create(nil);
  try
    UiDes := TCastleDesign.Create(Own);
    UiDes.Url := 'castle-data:/designs/test_free_children_of_ui.castle-user-interface';
    AssertTrue(Own.FindComponent('ImageControl1') = nil);

    Img := UiDes.DesignedComponent('ImageControl1') as TCastleImageControl;
    AssertTrue(Img <> nil);
    // test deprecated methods that provide backward compat
    AssertTrue(Img = UiDes.FindComponent('ImageControl1'));
    AssertTrue(Img = UiDes.FindRequiredComponent('ImageControl1'));

    AssertVectorEquals(Vector4(1, 1, 1, 1), Img.Color);

    ImgObserver := TFreeNotificationObserver.Create(Own);
    ImgObserver.Observed := Img;
    ImgObserver.OnFreeNotification := {$ifdef FPC}@{$endif} FreeNotificationFlag;
    FreeNotificationHappened := false;

    UiDes.Url := 'castle-data:/designs/test_free_children_of_ui_2.castle-user-interface';

    { Changing Url should have freed all the previous loaded contents. }
    AssertTrue(FreeNotificationHappened);
    AssertTrue(ImgObserver.Observed = nil);

    { new design has ImageControl1 with different color }
    Img := UiDes.DesignedComponent('ImageControl1') as TCastleImageControl;
    AssertTrue(Img <> nil);
    // test deprecated methods that provide backward compat
    AssertTrue(Img = UiDes.FindComponent('ImageControl1'));
    AssertTrue(Img = UiDes.FindRequiredComponent('ImageControl1'));

    AssertVectorEquals(Vector4(0, 0, 1, 1), Img.Color);
  finally FreeAndNil(Own) end;
end;

procedure TTestCastleDesignComponents.TestTransformFreesAllWhenUrlsChanged;
var
  Own: TComponent;
  TrDes: TCastleTransformDesign;
  Cone: TCastleCone;
  ConeObserver: TFreeNotificationObserver;
begin
  Own := TComponent.Create(nil);
  try
    TrDes := TCastleTransformDesign.Create(Own);
    TrDes.Url := 'castle-data:/designs/test_free_children_of_transform.castle-transform';
    AssertTrue(Own.FindComponent('Cone1') = nil);

    Cone := TrDes.DesignedComponent('Cone1') as TCastleCone;
    AssertTrue(Cone <> nil);
    // test deprecated methods that provide backward compat
    AssertTrue(Cone = TrDes.FindComponent('Cone1'));
    AssertTrue(Cone = TrDes.FindRequiredComponent('Cone1'));

    AssertVectorEquals(Vector4(1, 1, 1, 1), Cone.Color);

    ConeObserver := TFreeNotificationObserver.Create(Own);
    ConeObserver.Observed := Cone;
    ConeObserver.OnFreeNotification := {$ifdef FPC}@{$endif} FreeNotificationFlag;
    FreeNotificationHappened := false;

    TrDes.Url := 'castle-data:/designs/test_free_children_of_transform_2.castle-transform';

    { Changing Url should have freed all the previous loaded contents. }
    AssertTrue(FreeNotificationHappened);
    AssertTrue(ConeObserver.Observed = nil);

    { new design has Cone1 with different color }
    Cone := TrDes.DesignedComponent('Cone1') as TCastleCone;
    AssertTrue(Cone <> nil);
    // test deprecated methods that provide backward compat
    AssertTrue(Cone = TrDes.FindComponent('Cone1'));
    AssertTrue(Cone = TrDes.FindRequiredComponent('Cone1'));

    AssertVectorEquals(Vector4(0, 0, 1, 1), Cone.Color);
  finally FreeAndNil(Own) end;
end;

initialization
  RegisterTest(TTestCastleDesignComponents);
end.