| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleControls" -*- |
| 2 |
{ |
| 3 |
Copyright 2012-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 CastleControls unit. } |
| 18 |
unit TestCastleControls; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleControls = class(TCastleTestCase) |
| 27 |
published |
| 28 |
procedure TestFloatSliderRoundAndClamp; |
| 29 |
procedure TestAssigningImageURL; |
| 30 |
procedure TestRecursiveDesign; |
| 31 |
end; |
| 32 |
|
| 33 |
implementation |
| 34 |
|
| 35 |
uses CastleVectors, CastleControls, CastleImages, CastleStringUtils, |
| 36 |
CastleUIControls; |
| 37 |
|
| 38 |
procedure TTestCastleControls.TestFloatSliderRoundAndClamp; |
| 39 |
const |
| 40 |
Epsilon = 0.001; |
| 41 |
var |
| 42 |
F: TCastleFloatSlider; |
| 43 |
begin |
| 44 |
F := TCastleFloatSlider.Create(nil); |
| 45 |
try |
| 46 |
F.Min := 10; |
| 47 |
F.Max := 20; |
| 48 |
F.MultipleOf := 0; |
| 49 |
AssertSameValue(12, F.RoundAndClamp(12), Epsilon); |
| 50 |
AssertSameValue(11, F.RoundAndClamp(11), Epsilon); |
| 51 |
AssertSameValue(10.1, F.RoundAndClamp(10.1), Epsilon); |
| 52 |
AssertSameValue(10.9, F.RoundAndClamp(10.9), Epsilon); |
| 53 |
AssertSameValue(10, F.RoundAndClamp(10), Epsilon); |
| 54 |
AssertSameValue(20, F.RoundAndClamp(20), Epsilon); |
| 55 |
AssertSameValue(10, F.RoundAndClamp(5), Epsilon); |
| 56 |
AssertSameValue(20, F.RoundAndClamp(25), Epsilon); |
| 57 |
|
| 58 |
F.Min := 10; |
| 59 |
F.Max := 20; |
| 60 |
F.MultipleOf := 3; |
| 61 |
AssertSameValue(12, F.RoundAndClamp(12), Epsilon); |
| 62 |
AssertSameValue(12, F.RoundAndClamp(11), Epsilon); |
| 63 |
AssertSameValue(10, F.RoundAndClamp(10.1), Epsilon); |
| 64 |
AssertSameValue(12, F.RoundAndClamp(10.9), Epsilon); |
| 65 |
AssertSameValue(10, F.RoundAndClamp(10), Epsilon); |
| 66 |
AssertSameValue(20, F.RoundAndClamp(20), Epsilon); |
| 67 |
AssertSameValue(15, F.RoundAndClamp(14), Epsilon); |
| 68 |
AssertSameValue(10, F.RoundAndClamp(5), Epsilon); |
| 69 |
AssertSameValue(20, F.RoundAndClamp(25), Epsilon); |
| 70 |
|
| 71 |
F.Min := 10; |
| 72 |
F.Max := 20; |
| 73 |
F.MultipleOf := -3; |
| 74 |
AssertSameValue(12, F.RoundAndClamp(12), Epsilon); |
| 75 |
AssertSameValue(12, F.RoundAndClamp(11), Epsilon); |
| 76 |
AssertSameValue(10, F.RoundAndClamp(10.1), Epsilon); |
| 77 |
AssertSameValue(12, F.RoundAndClamp(10.9), Epsilon); |
| 78 |
AssertSameValue(10, F.RoundAndClamp(10), Epsilon); |
| 79 |
AssertSameValue(20, F.RoundAndClamp(20), Epsilon); |
| 80 |
AssertSameValue(15, F.RoundAndClamp(14), Epsilon); |
| 81 |
AssertSameValue(10, F.RoundAndClamp(5), Epsilon); |
| 82 |
AssertSameValue(20, F.RoundAndClamp(25), Epsilon); |
| 83 |
|
| 84 |
F.Min := -20; |
| 85 |
F.Max := -10; |
| 86 |
F.MultipleOf := 3; |
| 87 |
AssertSameValue(-12, F.RoundAndClamp(-12), Epsilon); |
| 88 |
AssertSameValue(-12, F.RoundAndClamp(-11), Epsilon); |
| 89 |
AssertSameValue(-10, F.RoundAndClamp(-10.1), Epsilon); |
| 90 |
AssertSameValue(-12, F.RoundAndClamp(-10.9), Epsilon); |
| 91 |
AssertSameValue(-10, F.RoundAndClamp(-10), Epsilon); |
| 92 |
AssertSameValue(-20, F.RoundAndClamp(-20), Epsilon); |
| 93 |
AssertSameValue(-15, F.RoundAndClamp(-14), Epsilon); |
| 94 |
AssertSameValue(-10, F.RoundAndClamp(-5), Epsilon); |
| 95 |
AssertSameValue(-20, F.RoundAndClamp(-25), Epsilon); |
| 96 |
finally FreeAndNil(F) end; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TTestCastleControls.TestAssigningImageURL; |
| 100 |
var |
| 101 |
C, C2: TCastleImageControl; |
| 102 |
begin |
| 103 |
C := TCastleImageControl.Create(nil); |
| 104 |
try |
| 105 |
C2 := TCastleImageControl.Create(nil); |
| 106 |
try |
| 107 |
C.URL := 'castle-data:/images/alpha.png'; |
| 108 |
C.URL := 'castle-data:/images/alpha.png'; |
| 109 |
|
| 110 |
C2.URL := 'castle-data:/images/alpha.png'; |
| 111 |
C2.URL := ''; |
| 112 |
|
| 113 |
C.Image := LoadImage('castle-data:/images/alpha.png'); |
| 114 |
C.Image := C.Image; |
| 115 |
finally FreeAndNil(C2) end; |
| 116 |
finally FreeAndNil(C) end; |
| 117 |
end; |
| 118 |
|
| 119 |
procedure TTestCastleControls.TestRecursiveDesign; |
| 120 |
var |
| 121 |
Owner: TComponent; |
| 122 |
begin |
| 123 |
try |
| 124 |
Owner := TComponent.Create(nil); |
| 125 |
try |
| 126 |
UserInterfaceLoad('castle-data:/designs/test_recursive_ui.castle-user-interface', Owner); |
| 127 |
Fail('Loading test_recursive_ui.castle-user-interface should have raised an exception'); |
| 128 |
except |
| 129 |
on E: Exception do |
| 130 |
{ We expect Exception with message "Exceeded maximum depth..." } |
| 131 |
if not IsPrefix('Exceeded maximum depth', E.Message) then |
| 132 |
raise; |
| 133 |
end; |
| 134 |
finally FreeAndNil(Owner) end; |
| 135 |
end; |
| 136 |
|
| 137 |
initialization |
| 138 |
RegisterTest(TTestCastleControls); |
| 139 |
end. |